From abf6ffac57481653d368f37ac246df8ee65f518c Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 8 Nov 2026 19:18:25 +0000 Subject: [PATCH] TCB: dispatch each function's implementations in the order its file lists them A dispatcher's implementations were the artifacts of a module whose names extend the base's with `_`, with its signature and documentation (`Rust.groupsOf`), tried in the order the untrusted `Dispatch.lean` ranked their suffixes, a suffix of several parts parsed back into ranked parts (`partsOf`, `lexLe`). The emitter found the generic callers and variants whose instances made those suffixes by their files' paths. Now: * `Artifact.base` is the name of the function an artifact implements, its `Api`'s (`Api.artifact`; by default its own name), and a function's implementations are the artifacts of its base (`groupsOf`), each with the `Api`'s signature and documentation (`checkGroups`); * its dispatcher tries them in the order its registration file lists them: every implementation of a function is in one registration file (`Emit.checkSources`, from the driver's list of each module's artifacts), and the tables of functions in the order the artifacts list them (`fnTables`, no longer sorted by name); * one it would never choose (it needs every feature of one before it, or more) fails the emitter, rather than being left out (`armsOf`); * the emitter no longer finds variants and generic files (`Emit.grouped`, `genericGroups`, `instances`, `allOf`: the registration files apply generic callers to lists of variants themselves), nor ranks suffixes (`Rank`, `rankOf`, `partsOf`, `lexLe`, `checkRanks` and `Dispatch.lean` are gone), nor emits an artifact as another name of one with the same code (`alias`, `sameKey`). What the dispatchers call is unchanged but for five on x86-64, whose implementations come from two lists (a caller of two variants' functions: Ed25519's three functions, by SHA-512 and field arithmetic, and AES-GCM's two streaming ones, by CTR and GHASH): they now try them by the first list, then the second, which differs from the old order only between implementations whose features no CPU tells apart (SHA-512's extensions without ADX, VAES without VPCLMULQDQ). Every dispatcher's documentation now says it tries them in the order its registration file lists them. Part of #4548. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01TKUFHFjAR7Byf513R4zuFy --- .agents/skills/adding-a-primitive/SKILL.md | 16 +- CLAUDE.md | 12 +- .../src/dispatch/aes.rs | 6 +- .../src/dispatch/aes_cbc.rs | 4 +- .../src/dispatch/aes_ccm.rs | 4 +- .../src/dispatch/aes_cfb.rs | 8 +- .../src/dispatch/aes_cfb8.rs | 4 +- .../src/dispatch/aes_ctr.rs | 4 +- .../src/dispatch/aes_ecb.rs | 4 +- .../src/dispatch/aes_gcm_siv.rs | 4 +- .../src/dispatch/aes_ocb.rs | 6 +- .../src/dispatch/aes_ofb.rs | 4 +- .../src/dispatch/aes_siv.rs | 6 +- .../src/dispatch/aes_xts.rs | 4 +- .../src/dispatch/chacha20.rs | 40 +- .../src/dispatch/chacha20poly1305.rs | 8 +- .../src/dispatch/cmac_aes.rs | 66 +- .../src/dispatch/cmac_triple_des.rs | 40 +- .../src/dispatch/dsa.rs | 200 ++--- .../src/dispatch/ecdsa_brainpoolp256r1.rs | 44 +- .../src/dispatch/ecdsa_brainpoolp384r1.rs | 44 +- .../src/dispatch/ecdsa_brainpoolp512r1.rs | 44 +- .../src/dispatch/ecdsa_p192.rs | 44 +- .../src/dispatch/ecdsa_p224.rs | 44 +- .../src/dispatch/ecdsa_p256.rs | 44 +- .../src/dispatch/ecdsa_p384.rs | 44 +- .../src/dispatch/ecdsa_p521.rs | 44 +- .../src/dispatch/ecdsa_secp256k1.rs | 44 +- .../src/dispatch/ed25519.rs | 100 +-- .../src/dispatch/ed448.rs | 104 +-- .../src/dispatch/ffdh.rs | 206 ++--- .../src/dispatch/fn_tables.rs | 226 ++--- .../src/dispatch/gcm.rs | 298 +++---- .../src/dispatch/hmac_sha1.rs | 4 +- .../src/dispatch/hmac_sha224.rs | 4 +- .../src/dispatch/hmac_sha256.rs | 4 +- .../src/dispatch/hmac_sha384.rs | 4 +- .../src/dispatch/hmac_sha512.rs | 4 +- .../src/dispatch/hmac_sha512_224.rs | 4 +- .../src/dispatch/hmac_sha512_256.rs | 4 +- .../src/dispatch/idea.rs | 38 +- .../src/dispatch/mldsa44.rs | 252 +++--- .../src/dispatch/mldsa65.rs | 252 +++--- .../src/dispatch/mldsa87.rs | 252 +++--- .../src/dispatch/mlkem1024.rs | 96 +-- .../src/dispatch/mlkem768.rs | 130 +-- .../src/dispatch/pbkdf2_sha1.rs | 2 +- .../src/dispatch/pbkdf2_sha224.rs | 2 +- .../src/dispatch/pbkdf2_sha256.rs | 2 +- .../src/dispatch/pbkdf2_sha384.rs | 2 +- .../src/dispatch/pbkdf2_sha512.rs | 2 +- .../src/dispatch/pbkdf2_sha512_224.rs | 2 +- .../src/dispatch/pbkdf2_sha512_256.rs | 2 +- .../src/dispatch/poly1305.rs | 2 +- .../src/dispatch/rsa.rs | 80 +- .../src/dispatch/rsa_keygen.rs | 38 +- .../src/dispatch/rsa_pkcs1_enc.rs | 2 +- .../src/dispatch/scrypt.rs | 2 +- .../src/dispatch/sha1.rs | 64 +- .../src/dispatch/sha256.rs | 144 ++-- .../src/dispatch/sha3.rs | 366 ++++---- .../src/dispatch/sha512.rs | 314 +++---- .../src/dispatch/sm4_gcm.rs | 136 +-- .../src/dispatch/x25519.rs | 44 +- .../src/dispatch/x448.rs | 30 +- .../src/fn_tables/mod.rs | 180 ++-- .../src/dispatch/chacha20.rs | 32 +- .../src/dispatch/cmac_triple_des.rs | 40 +- .../src/dispatch/dsa.rs | 180 ++-- .../src/dispatch/ec_brainpoolp256r1.rs | 2 +- .../dispatch/ec_brainpoolp256r1_compressed.rs | 4 +- .../src/dispatch/ec_brainpoolp384r1.rs | 2 +- .../dispatch/ec_brainpoolp384r1_compressed.rs | 4 +- .../src/dispatch/ec_brainpoolp512r1.rs | 2 +- .../dispatch/ec_brainpoolp512r1_compressed.rs | 4 +- .../src/dispatch/ec_p224.rs | 2 +- .../src/dispatch/ec_p224_compressed.rs | 4 +- .../src/dispatch/ec_p256.rs | 2 +- .../src/dispatch/ec_p256_compressed.rs | 4 +- .../src/dispatch/ec_p384.rs | 2 +- .../src/dispatch/ec_p384_compressed.rs | 4 +- .../src/dispatch/ec_secp256k1.rs | 2 +- .../src/dispatch/ec_secp256k1_compressed.rs | 4 +- .../src/dispatch/ecdh_brainpoolp256r1.rs | 2 +- .../src/dispatch/ecdh_brainpoolp384r1.rs | 2 +- .../src/dispatch/ecdh_brainpoolp512r1.rs | 2 +- .../src/dispatch/ecdh_p224.rs | 2 +- .../src/dispatch/ecdh_p256.rs | 2 +- .../src/dispatch/ecdh_p384.rs | 2 +- .../src/dispatch/ecdh_secp256k1.rs | 2 +- .../src/dispatch/ecdsa_brainpoolp256r1.rs | 76 +- .../src/dispatch/ecdsa_brainpoolp384r1.rs | 76 +- .../src/dispatch/ecdsa_brainpoolp512r1.rs | 76 +- .../src/dispatch/ecdsa_p192.rs | 44 +- .../src/dispatch/ecdsa_p224.rs | 76 +- .../src/dispatch/ecdsa_p256.rs | 76 +- .../src/dispatch/ecdsa_p384.rs | 76 +- .../src/dispatch/ecdsa_p521.rs | 44 +- .../src/dispatch/ecdsa_secp256k1.rs | 76 +- .../src/dispatch/ed25519.rs | 40 +- .../src/dispatch/ed448.rs | 42 +- .../src/dispatch/ffdh.rs | 342 ++++---- .../src/dispatch/fn_tables.rs | 108 +-- .../src/dispatch/gcm.rs | 114 +-- .../src/dispatch/idea.rs | 38 +- .../src/dispatch/mldsa44.rs | 154 ++-- .../src/dispatch/mldsa65.rs | 154 ++-- .../src/dispatch/mldsa87.rs | 154 ++-- .../src/dispatch/mlkem1024.rs | 80 +- .../src/dispatch/mlkem768.rs | 46 +- .../src/dispatch/rsa.rs | 88 +- .../src/dispatch/rsa_keygen.rs | 40 +- .../src/dispatch/rsa_oaep.rs | 4 +- .../src/dispatch/rsa_pkcs1_enc.rs | 4 +- .../src/dispatch/rsa_pkcs1_sig.rs | 6 +- .../src/dispatch/rsa_pss.rs | 4 +- .../src/dispatch/sha1.rs | 34 +- .../src/dispatch/sha256.rs | 74 +- .../src/dispatch/sha3.rs | 200 ++--- .../src/dispatch/sha512.rs | 166 ++-- .../src/dispatch/sm4_gcm.rs | 100 +-- .../src/dispatch/x25519.rs | 44 +- .../src/dispatch/x448.rs | 30 +- .../src/fn_tables/mod.rs | 92 +- .../src/dispatch/chacha20.rs | 32 +- .../src/dispatch/cmac_triple_des.rs | 40 +- .../src/dispatch/dsa.rs | 108 +-- .../src/dispatch/ecdsa_brainpoolp256r1.rs | 44 +- .../src/dispatch/ecdsa_brainpoolp384r1.rs | 44 +- .../src/dispatch/ecdsa_brainpoolp512r1.rs | 44 +- .../src/dispatch/ecdsa_p192.rs | 44 +- .../src/dispatch/ecdsa_p224.rs | 44 +- .../src/dispatch/ecdsa_p256.rs | 44 +- .../src/dispatch/ecdsa_p384.rs | 44 +- .../src/dispatch/ecdsa_p521.rs | 44 +- .../src/dispatch/ecdsa_secp256k1.rs | 44 +- .../src/dispatch/ed25519.rs | 40 +- .../src/dispatch/ed448.rs | 42 +- .../src/dispatch/ffdh.rs | 178 ++-- .../src/dispatch/fn_tables.rs | 108 +-- .../src/dispatch/gcm.rs | 210 ++--- .../src/dispatch/idea.rs | 38 +- .../src/dispatch/mldsa44.rs | 154 ++-- .../src/dispatch/mldsa65.rs | 154 ++-- .../src/dispatch/mldsa87.rs | 154 ++-- .../src/dispatch/mlkem1024.rs | 80 +- .../src/dispatch/mlkem768.rs | 46 +- .../src/dispatch/rsa.rs | 56 +- .../src/dispatch/rsa_keygen.rs | 38 +- .../src/dispatch/rsa_pkcs1_sig.rs | 68 +- .../src/dispatch/rsa_pss.rs | 66 +- .../src/dispatch/sha1.rs | 34 +- .../src/dispatch/sha256.rs | 74 +- .../src/dispatch/sha3.rs | 200 ++--- .../src/dispatch/sha512.rs | 166 ++-- .../src/dispatch/sm4_gcm.rs | 100 +-- .../src/dispatch/x25519.rs | 34 +- .../src/dispatch/x448.rs | 30 +- .../src/fn_tables/mod.rs | 92 +- .../src/dispatch/chacha20.rs | 32 +- .../src/dispatch/cmac_triple_des.rs | 40 +- .../src/dispatch/dsa.rs | 90 +- .../src/dispatch/ecdsa_brainpoolp256r1.rs | 44 +- .../src/dispatch/ecdsa_brainpoolp384r1.rs | 44 +- .../src/dispatch/ecdsa_brainpoolp512r1.rs | 44 +- .../src/dispatch/ecdsa_p192.rs | 44 +- .../src/dispatch/ecdsa_p224.rs | 44 +- .../src/dispatch/ecdsa_p256.rs | 44 +- .../src/dispatch/ecdsa_p384.rs | 44 +- .../src/dispatch/ecdsa_p521.rs | 44 +- .../src/dispatch/ecdsa_secp256k1.rs | 44 +- .../src/dispatch/ed25519.rs | 40 +- .../src/dispatch/ed448.rs | 42 +- .../src/dispatch/fn_tables.rs | 108 +-- .../src/dispatch/gcm.rs | 210 ++--- .../src/dispatch/idea.rs | 38 +- .../src/dispatch/mldsa44.rs | 154 ++-- .../src/dispatch/mldsa65.rs | 154 ++-- .../src/dispatch/mldsa87.rs | 154 ++-- .../src/dispatch/mlkem1024.rs | 80 +- .../src/dispatch/mlkem768.rs | 46 +- .../src/dispatch/rsa.rs | 56 +- .../src/dispatch/rsa_keygen.rs | 38 +- .../src/dispatch/rsa_pkcs1_sig.rs | 68 +- .../src/dispatch/rsa_pss.rs | 66 +- .../src/dispatch/sha1.rs | 34 +- .../src/dispatch/sha256.rs | 74 +- .../src/dispatch/sha3.rs | 200 ++--- .../src/dispatch/sha512.rs | 166 ++-- .../src/dispatch/sm4_gcm.rs | 100 +-- .../src/dispatch/x25519.rs | 34 +- .../src/dispatch/x448.rs | 30 +- .../src/fn_tables/mod.rs | 92 +- .../src/dispatch/aes.rs | 6 +- .../src/dispatch/aes_cbc.rs | 4 +- .../src/dispatch/aes_ccm.rs | 4 +- .../src/dispatch/aes_cfb.rs | 8 +- .../src/dispatch/aes_cfb8.rs | 4 +- .../src/dispatch/aes_ctr.rs | 4 +- .../src/dispatch/aes_ecb.rs | 4 +- .../src/dispatch/aes_gcm_siv.rs | 4 +- .../src/dispatch/aes_ocb.rs | 6 +- .../src/dispatch/aes_ofb.rs | 4 +- .../src/dispatch/aes_siv.rs | 6 +- .../src/dispatch/aes_xts.rs | 4 +- .../src/dispatch/chacha20.rs | 40 +- .../src/dispatch/chacha20poly1305.rs | 8 +- .../src/dispatch/cmac_aes.rs | 6 +- .../src/dispatch/cmac_triple_des.rs | 40 +- .../src/dispatch/dsa.rs | 184 ++-- .../src/dispatch/ecdsa_brainpoolp256r1.rs | 48 +- .../src/dispatch/ecdsa_brainpoolp384r1.rs | 48 +- .../src/dispatch/ecdsa_brainpoolp512r1.rs | 48 +- .../src/dispatch/ecdsa_p192.rs | 48 +- .../src/dispatch/ecdsa_p224.rs | 48 +- .../src/dispatch/ecdsa_p256.rs | 48 +- .../src/dispatch/ecdsa_p384.rs | 48 +- .../src/dispatch/ecdsa_p521.rs | 48 +- .../src/dispatch/ecdsa_secp256k1.rs | 48 +- .../src/dispatch/ed25519.rs | 44 +- .../src/dispatch/ed448.rs | 46 +- .../src/dispatch/ffdh.rs | 164 ++-- .../src/dispatch/fn_tables.rs | 154 ++-- .../src/dispatch/gcm.rs | 460 +++++----- .../src/dispatch/hmac_sha1.rs | 4 +- .../src/dispatch/hmac_sha224.rs | 4 +- .../src/dispatch/hmac_sha256.rs | 4 +- .../src/dispatch/idea.rs | 38 +- .../src/dispatch/mldsa44.rs | 158 ++-- .../src/dispatch/mldsa65.rs | 158 ++-- .../src/dispatch/mldsa87.rs | 158 ++-- .../src/dispatch/mlkem1024.rs | 82 +- .../src/dispatch/mlkem768.rs | 82 +- .../src/dispatch/pbkdf2_sha1.rs | 2 +- .../src/dispatch/pbkdf2_sha224.rs | 2 +- .../src/dispatch/pbkdf2_sha256.rs | 2 +- .../src/dispatch/rsa.rs | 62 +- .../src/dispatch/rsa_keygen.rs | 38 +- .../src/dispatch/rsa_oaep.rs | 66 +- .../src/dispatch/rsa_pkcs1_enc.rs | 76 +- .../src/dispatch/rsa_pkcs1_sig.rs | 72 +- .../src/dispatch/rsa_pss.rs | 66 +- .../src/dispatch/scrypt.rs | 2 +- .../src/dispatch/sha1.rs | 42 +- .../src/dispatch/sha256.rs | 82 +- .../src/dispatch/sha3.rs | 196 ++--- .../src/dispatch/sha512.rs | 170 ++-- .../src/dispatch/sm4_gcm.rs | 206 ++--- .../src/dispatch/x25519.rs | 34 +- .../src/dispatch/x448.rs | 30 +- .../src/fn_tables/mod.rs | 132 +-- .../src/dispatch/aes.rs | 6 +- .../src/dispatch/aes_cbc.rs | 4 +- .../src/dispatch/aes_ccm.rs | 4 +- .../src/dispatch/aes_cfb.rs | 8 +- .../src/dispatch/aes_cfb8.rs | 4 +- .../src/dispatch/aes_ctr.rs | 4 +- .../src/dispatch/aes_ecb.rs | 4 +- .../src/dispatch/aes_gcm_siv.rs | 4 +- .../src/dispatch/aes_ocb.rs | 6 +- .../src/dispatch/aes_ofb.rs | 4 +- .../src/dispatch/aes_siv.rs | 6 +- .../src/dispatch/aes_xts.rs | 8 +- .../src/dispatch/argon2.rs | 2 +- .../src/dispatch/blake2b.rs | 4 +- .../src/dispatch/blake2s.rs | 4 +- .../src/dispatch/chacha20.rs | 40 +- .../src/dispatch/chacha20poly1305.rs | 8 +- .../src/dispatch/cmac_aes.rs | 76 +- .../src/dispatch/cmac_triple_des.rs | 40 +- .../src/dispatch/dsa.rs | 330 +++---- .../src/dispatch/ec_p256.rs | 2 +- .../src/dispatch/ec_p256_compressed.rs | 4 +- .../src/dispatch/ec_p256_validate.rs | 2 +- .../src/dispatch/ec_p384.rs | 2 +- .../src/dispatch/ec_p384_compressed.rs | 4 +- .../src/dispatch/ec_p384_validate.rs | 2 +- .../src/dispatch/ec_p521.rs | 2 +- .../src/dispatch/ec_p521_compressed.rs | 4 +- .../src/dispatch/ec_p521_validate.rs | 2 +- .../src/dispatch/ecdh_p256.rs | 2 +- .../src/dispatch/ecdh_p384.rs | 2 +- .../src/dispatch/ecdh_p521.rs | 2 +- .../src/dispatch/ecdsa_brainpoolp256r1.rs | 44 +- .../src/dispatch/ecdsa_brainpoolp384r1.rs | 44 +- .../src/dispatch/ecdsa_brainpoolp512r1.rs | 44 +- .../src/dispatch/ecdsa_p192.rs | 44 +- .../src/dispatch/ecdsa_p224.rs | 44 +- .../src/dispatch/ecdsa_p256.rs | 76 +- .../src/dispatch/ecdsa_p384.rs | 76 +- .../src/dispatch/ecdsa_p521.rs | 76 +- .../src/dispatch/ecdsa_secp256k1.rs | 44 +- .../src/dispatch/ed25519.rs | 176 ++-- .../src/dispatch/ed448.rs | 98 +-- .../src/dispatch/ffdh.rs | 430 +++++----- .../src/dispatch/fn_tables.rs | 310 +++---- .../src/dispatch/gcm.rs | 812 +++++++++--------- .../src/dispatch/hmac_sha1.rs | 4 +- .../src/dispatch/hmac_sha224.rs | 4 +- .../src/dispatch/hmac_sha256.rs | 4 +- .../src/dispatch/hmac_sha384.rs | 4 +- .../src/dispatch/hmac_sha512.rs | 4 +- .../src/dispatch/hmac_sha512_224.rs | 4 +- .../src/dispatch/hmac_sha512_256.rs | 4 +- .../src/dispatch/idea.rs | 38 +- .../src/dispatch/mldsa44.rs | 304 +++---- .../src/dispatch/mldsa65.rs | 304 +++---- .../src/dispatch/mldsa87.rs | 304 +++---- .../src/dispatch/mlkem1024.rs | 140 +-- .../src/dispatch/mlkem768.rs | 230 ++--- .../src/dispatch/pbkdf2_sha1.rs | 2 +- .../src/dispatch/pbkdf2_sha224.rs | 2 +- .../src/dispatch/pbkdf2_sha256.rs | 2 +- .../src/dispatch/pbkdf2_sha384.rs | 2 +- .../src/dispatch/pbkdf2_sha512.rs | 2 +- .../src/dispatch/pbkdf2_sha512_224.rs | 2 +- .../src/dispatch/pbkdf2_sha512_256.rs | 2 +- .../src/dispatch/poly1305.rs | 42 +- .../src/dispatch/rsa.rs | 128 +-- .../src/dispatch/rsa_keygen.rs | 40 +- .../src/dispatch/rsa_oaep.rs | 4 +- .../src/dispatch/rsa_pkcs1_enc.rs | 4 +- .../src/dispatch/rsa_pkcs1_sig.rs | 6 +- .../src/dispatch/rsa_pss.rs | 4 +- .../src/dispatch/scrypt.rs | 2 +- .../src/dispatch/sha1.rs | 76 +- .../src/dispatch/sha256.rs | 174 ++-- .../src/dispatch/sha3.rs | 200 ++--- .../src/dispatch/sha512.rs | 354 ++++---- .../src/dispatch/sm4_gcm.rs | 236 ++--- .../src/dispatch/triple_des.rs | 4 +- .../src/dispatch/triple_des_cbc.rs | 2 +- .../src/dispatch/triple_des_cfb.rs | 82 +- .../src/dispatch/x25519.rs | 74 +- .../src/dispatch/x448.rs | 58 +- .../src/dispatch/zeroize.rs | 2 +- .../src/fn_tables/mod.rs | 264 +++--- lean/Emit.lean | 12 +- lean/EmitOne.lean | 26 +- lean/README.md | 15 +- lean/VerifiedGarbage/Dispatch.lean | 76 -- .../Proof/Variants/AesBlocks/AArch64.lean | 3 +- .../Proof/Variants/AesBlocks/X86.lean | 3 +- .../Proof/Variants/AesBlocks/X86_64.lean | 3 +- .../Proof/Variants/AesCtr32/AArch64.lean | 3 +- .../Proof/Variants/AesCtr32/X86.lean | 3 +- .../Proof/Variants/AesCtr32/X86_64.lean | 3 +- .../Proof/Variants/AesGcm/AArch64.lean | 3 +- .../Proof/Variants/AesGcm/X86.lean | 3 +- .../Proof/Variants/AesGcm/X86_64.lean | 3 +- .../Proof/Variants/AesXts/AArch64.lean | 3 +- .../Proof/Variants/AesXts/X86_64.lean | 3 +- .../Proof/Variants/Argon2Compress/X86_64.lean | 3 +- .../Proof/Variants/Blake2b/AArch64.lean | 3 +- .../Proof/Variants/Blake2b/X86_64.lean | 3 +- .../Proof/Variants/Blake2s/X86_64.lean | 3 +- .../Proof/Variants/ChaCha20Xor/AArch64.lean | 3 +- .../Proof/Variants/ChaCha20Xor/X86.lean | 3 +- .../Proof/Variants/ChaCha20Xor/X86_64.lean | 3 +- .../Proof/Variants/CmacAesUpdate/AArch64.lean | 3 +- .../Proof/Variants/CmacAesUpdate/X86_64.lean | 3 +- .../Proof/Variants/Curve448Field/X86_64.lean | 3 +- .../Proof/Variants/DsaArithmetic/AArch64.lean | 3 +- .../Proof/Variants/DsaArithmetic/X86_64.lean | 3 +- .../Proof/Variants/Ffdh/Arm.lean | 3 +- .../Proof/Variants/FfdhExp/X86_64.lean | 3 +- .../Proof/Variants/Ghash/X86.lean | 3 +- .../Proof/Variants/Ghash/X86_64.lean | 3 +- .../Proof/Variants/Keccak/AArch64.lean | 3 +- .../Proof/Variants/KeccakX2/AArch64.lean | 3 +- .../Proof/Variants/MdHash/AArch64.lean | 3 +- .../Proof/Variants/MdHash/RISCV64.lean | 3 +- .../Proof/Variants/MdHash/X86_64.lean | 3 +- .../Proof/Variants/MillerRabin/AArch64.lean | 3 +- .../Proof/Variants/MillerRabin/Arm.lean | 3 +- .../Proof/Variants/MillerRabin/PPC64LE.lean | 3 +- .../Proof/Variants/MillerRabin/RISCV64.lean | 3 +- .../Proof/Variants/MillerRabin/X86.lean | 3 +- .../Proof/Variants/MillerRabin/X86_64.lean | 3 +- .../Proof/Variants/MlDsaArith/X86_64.lean | 3 +- .../Proof/Variants/MlKemSample4/X86_64.lean | 3 +- .../Proof/Variants/Poly1305Blocks/X86_64.lean | 3 +- .../Proof/Variants/RsaMont/Arm.lean | 3 +- .../Proof/Variants/RsaPrivateCrt/AArch64.lean | 3 +- .../Proof/Variants/RsaPrivateCrt/Arm.lean | 3 +- .../Proof/Variants/RsaPrivateCrt/X86.lean | 3 +- .../Proof/Variants/RsaPrivateCrt/X86_64.lean | 3 +- .../RsaPublicPrecomputed/AArch64.lean | 3 +- .../Variants/RsaPublicPrecomputed/X86_64.lean | 3 +- .../Proof/Variants/Sha1/X86.lean | 3 +- .../Proof/Variants/Sha256/X86.lean | 3 +- .../Proof/Variants/Sm4Gcm/AArch64.lean | 3 +- .../Proof/Variants/TripleDesEcb/X86_64.lean | 3 +- .../Proof/Variants/Zeroize/X86_64.lean | 3 +- lean/VerifiedGarbage/TCB/Emit.lean | 265 ++---- lean/VerifiedGarbage/TCB/Registration.lean | 10 +- lean/VerifiedGarbage/TCB/Rust.lean | 380 +++----- lean/VerifiedGarbageTest/Dispatch.lean | 130 ++- lean/VerifiedGarbageTest/Emit.lean | 179 +--- 399 files changed, 11511 insertions(+), 11977 deletions(-) delete mode 100644 lean/VerifiedGarbage/Dispatch.lean diff --git a/.agents/skills/adding-a-primitive/SKILL.md b/.agents/skills/adding-a-primitive/SKILL.md index a5a6c45e624..4070e398e5a 100644 --- a/.agents/skills/adding-a-primitive/SKILL.md +++ b/.agents/skills/adding-a-primitive/SKILL.md @@ -106,13 +106,15 @@ instructions in an ISA model) go in their own PR before either. new implementation is a new element of its interface's list, and its callers follow. The emitter writes each function's dispatcher, `_auto` (in - `crate::arch::dispatch::`), which - tries its implementations in the order `Dispatch.lean` ranks their - suffixes on the target (a suffix of several parts by the rank of each - part's): a variant with a suffix it does not rank yet fails the - emitter, so rank it there, best first (`VG.Rust.Rank`). Extra - features or a `vendor` it is chosen only with go in its registration - (`Registration.extra`, `Registration.vendor`). + `crate::arch::dispatch::`), which tries its implementations (the + artifacts of its `Api`, all in one registration file) in the order that + file lists them: so list them best first, and put a new variant in its + interface's list where it ranks, which orders its callers' instances + too (those of a caller of several interfaces by the first list, then the + next). One the dispatcher would never choose (it needs every feature of + one before it, or more) fails the emitter. Extra features or a `vendor` + it is chosen only with go in its registration (`Registration.extra`, + `Registration.vendor`). Registration files (`Artifacts/`) are reviewed by hand, so they only bind: no theorems, no tactic proofs beyond the defaults (not even a `(by decide)` argument), no definitions but those of its artifacts, and diff --git a/CLAUDE.md b/CLAUDE.md index 8fd026b6f2f..280dd2a8c85 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -144,11 +144,13 @@ own changes are rebuilt either way, on top of the restored build. So does each table of functions' (`_auto()`, in `crate::arch::dispatch::fn_tables`, `Rust.tableDispatcher`), which returns the best of the tables of its interface and data. - Which is best is not trusted: `lean/VerifiedGarbage/Dispatch.lean` ranks - the suffixes of each target, and the emitter fails on a suffix it does - not rank. A choice that only some algorithms' implementations need is - their registration's (`Registration.extra` features, and its `vendor` - for a choice by compilation target), never the ranking's. + Which is best is not trusted: the dispatcher tries a function's + implementations in the order its registration file lists them (a + generic caller's in the order of its callees' variants, best first in + `Proof/Variants//.lean`), and the emitter fails on one + it would never choose. A choice that only some algorithms' + implementations need is their registration's (`Registration.extra` + features, and its `vendor` for a choice by compilation target). A variant is chosen by the CPU's features; only when no feature tells apart the CPUs it is faster on may the choice also depend on the compilation target (`vg_poly1305_update_neon`, faster on Apple's cores and diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/aes.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/aes.rs index 7a49f7a491b..5165cca7655 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/aes.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/aes.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Aes.expandKeyContract`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_expand_key_aes` if the CPU supports its features (`VG_AES_EXPAND_KEY_AES_FEATURES`); or else `vg_aes_expand_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_expand_key_aes` if the CPU supports its features (`VG_AES_EXPAND_KEY_AES_FEATURES`); or else `vg_aes_expand_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -56,7 +56,7 @@ pub unsafe fn vg_aes_expand_key_auto(_dit: verified_garbage_cpu::Dit<'_>, key: * /// /// Contract: `VG.Spec.Aes.encryptBlocksContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_encrypt_blocks_aes` if the CPU supports its features (`VG_AES_ENCRYPT_BLOCKS_AES_FEATURES`); or else `vg_aes_encrypt_blocks`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_encrypt_blocks_aes` if the CPU supports its features (`VG_AES_ENCRYPT_BLOCKS_AES_FEATURES`); or else `vg_aes_encrypt_blocks`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -107,7 +107,7 @@ pub unsafe fn vg_aes_encrypt_blocks_auto(_dit: verified_garbage_cpu::Dit<'_>, sc /// /// Contract: `VG.Spec.Aes.decryptBlocksContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_decrypt_blocks_aes` if the CPU supports its features (`VG_AES_DECRYPT_BLOCKS_AES_FEATURES`); or else `vg_aes_decrypt_blocks`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_decrypt_blocks_aes` if the CPU supports its features (`VG_AES_DECRYPT_BLOCKS_AES_FEATURES`); or else `vg_aes_decrypt_blocks`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_cbc.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_cbc.rs index 57dfeb298ee..ececd2818e4 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_cbc.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_cbc.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Cbc.aesEncryptContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cbc_encrypt_aes` if the CPU supports its features (`VG_AES_CBC_ENCRYPT_AES_FEATURES`); or else `vg_aes_cbc_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cbc_encrypt_aes` if the CPU supports its features (`VG_AES_CBC_ENCRYPT_AES_FEATURES`); or else `vg_aes_cbc_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -58,7 +58,7 @@ pub unsafe fn vg_aes_cbc_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, sched /// /// Contract: `VG.Spec.Cbc.aesDecryptContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cbc_decrypt_aes_sha3` if the CPU supports its features (`VG_AES_CBC_DECRYPT_AES_SHA3_FEATURES`); `vg_aes_cbc_decrypt_aes` if the CPU supports its features (`VG_AES_CBC_DECRYPT_AES_FEATURES`); or else `vg_aes_cbc_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cbc_decrypt_aes_sha3` if the CPU supports its features (`VG_AES_CBC_DECRYPT_AES_SHA3_FEATURES`); `vg_aes_cbc_decrypt_aes` if the CPU supports its features (`VG_AES_CBC_DECRYPT_AES_FEATURES`); or else `vg_aes_cbc_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_ccm.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_ccm.rs index 2b23ccf202c..32a9c6ade33 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_ccm.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_ccm.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ccm.sealContract`. Constant time: only the pointers, `rounds`, the lengths and `tag_len` may affect timing, not the key schedule, the nonce, the associated data or the payload. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ccm_seal_aes` if the CPU supports its features (`VG_AES_CCM_SEAL_AES_FEATURES`); or else `vg_aes_ccm_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ccm_seal_aes` if the CPU supports its features (`VG_AES_CCM_SEAL_AES_FEATURES`); or else `vg_aes_ccm_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -60,7 +60,7 @@ pub unsafe fn vg_aes_ccm_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule /// /// Contract: `VG.Spec.Ccm.openContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key schedule, the nonce, the associated data, the data or the tag. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ccm_open_aes` if the CPU supports its features (`VG_AES_CCM_OPEN_AES_FEATURES`); or else `vg_aes_ccm_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ccm_open_aes` if the CPU supports its features (`VG_AES_CCM_OPEN_AES_FEATURES`); or else `vg_aes_ccm_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_cfb.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_cfb.rs index 8a23181a13a..b26f90afdf8 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_cfb.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_cfb.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Cfb.aesEncryptContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cfb128_encrypt_aes` if the CPU supports its features (`VG_AES_CFB128_ENCRYPT_AES_FEATURES`); or else `vg_aes_cfb128_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cfb128_encrypt_aes` if the CPU supports its features (`VG_AES_CFB128_ENCRYPT_AES_FEATURES`); or else `vg_aes_cfb128_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -58,7 +58,7 @@ pub unsafe fn vg_aes_cfb128_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, sc /// /// Contract: `VG.Spec.Cfb.aesDecryptContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cfb128_decrypt_aes` if the CPU supports its features (`VG_AES_CFB128_DECRYPT_AES_FEATURES`); or else `vg_aes_cfb128_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cfb128_decrypt_aes` if the CPU supports its features (`VG_AES_CFB128_DECRYPT_AES_FEATURES`); or else `vg_aes_cfb128_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -110,7 +110,7 @@ pub unsafe fn vg_aes_cfb128_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, sc /// /// Contract: `VG.Spec.Cfb.aesEncryptFinalContract`. Constant time: only the pointers, `rounds` and `len` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cfb128_encrypt_final_aes` if the CPU supports its features (`VG_AES_CFB128_ENCRYPT_FINAL_AES_FEATURES`); or else `vg_aes_cfb128_encrypt_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cfb128_encrypt_final_aes` if the CPU supports its features (`VG_AES_CFB128_ENCRYPT_FINAL_AES_FEATURES`); or else `vg_aes_cfb128_encrypt_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -162,7 +162,7 @@ pub unsafe fn vg_aes_cfb128_encrypt_final_auto(_dit: verified_garbage_cpu::Dit<' /// /// Contract: `VG.Spec.Cfb.aesDecryptFinalContract`. Constant time: only the pointers, `rounds` and `len` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cfb128_decrypt_final_aes` if the CPU supports its features (`VG_AES_CFB128_DECRYPT_FINAL_AES_FEATURES`); or else `vg_aes_cfb128_decrypt_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cfb128_decrypt_final_aes` if the CPU supports its features (`VG_AES_CFB128_DECRYPT_FINAL_AES_FEATURES`); or else `vg_aes_cfb128_decrypt_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_cfb8.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_cfb8.rs index c37d1831715..da0c320a3d8 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_cfb8.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_cfb8.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Cfb8.aesEncryptContract`. Constant time: only the pointers, `rounds` and `len` may affect timing, not the key schedule, the input block or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cfb8_encrypt_aes` if the CPU supports its features (`VG_AES_CFB8_ENCRYPT_AES_FEATURES`); or else `vg_aes_cfb8_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cfb8_encrypt_aes` if the CPU supports its features (`VG_AES_CFB8_ENCRYPT_AES_FEATURES`); or else `vg_aes_cfb8_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -58,7 +58,7 @@ pub unsafe fn vg_aes_cfb8_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, sche /// /// Contract: `VG.Spec.Cfb8.aesDecryptContract`. Constant time: only the pointers, `rounds` and `len` may affect timing, not the key schedule, the input block or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cfb8_decrypt_aes` if the CPU supports its features (`VG_AES_CFB8_DECRYPT_AES_FEATURES`); or else `vg_aes_cfb8_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cfb8_decrypt_aes` if the CPU supports its features (`VG_AES_CFB8_DECRYPT_AES_FEATURES`); or else `vg_aes_cfb8_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_ctr.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_ctr.rs index 92e5bf383d2..33bff8e1c60 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_ctr.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_ctr.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ctr.aesContract`. Constant time: only the pointers, `rounds`, `n` and the last four bytes of `*ctr` (the 32-bit counter, public in OpenSSL, BoringSSL and AWS-LC, which split the blocks where it wraps) may affect timing, not the key schedule, the rest of the counter block or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ctr_aes` if the CPU supports its features (`VG_AES_CTR_AES_FEATURES`); or else `vg_aes_ctr`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ctr_aes` if the CPU supports its features (`VG_AES_CTR_AES_FEATURES`); or else `vg_aes_ctr`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -58,7 +58,7 @@ pub unsafe fn vg_aes_ctr_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *co /// /// Contract: `VG.Spec.Ctr.aesFinalContract`. Constant time: only the pointers, `rounds`, `len` and the last four bytes of `*ctr` (the 32-bit counter, public in OpenSSL, BoringSSL and AWS-LC, which split the blocks where it wraps) may affect timing, not the key schedule, the rest of the counter block or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ctr_final_aes` if the CPU supports its features (`VG_AES_CTR_FINAL_AES_FEATURES`); or else `vg_aes_ctr_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ctr_final_aes` if the CPU supports its features (`VG_AES_CTR_FINAL_AES_FEATURES`); or else `vg_aes_ctr_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_ecb.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_ecb.rs index ac5288dc1b5..929f8214bfc 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_ecb.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_ecb.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.AesEcb.encryptContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ecb_encrypt_aes` if the CPU supports its features (`VG_AES_ECB_ENCRYPT_AES_FEATURES`); or else `vg_aes_ecb_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ecb_encrypt_aes` if the CPU supports its features (`VG_AES_ECB_ENCRYPT_AES_FEATURES`); or else `vg_aes_ecb_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -56,7 +56,7 @@ pub unsafe fn vg_aes_ecb_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, sched /// /// Contract: `VG.Spec.AesEcb.decryptContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ecb_decrypt_aes` if the CPU supports its features (`VG_AES_ECB_DECRYPT_AES_FEATURES`); or else `vg_aes_ecb_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ecb_decrypt_aes` if the CPU supports its features (`VG_AES_ECB_DECRYPT_AES_FEATURES`); or else `vg_aes_ecb_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_gcm_siv.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_gcm_siv.rs index f78e41bed16..99c891dff9f 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_gcm_siv.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_gcm_siv.rs @@ -8,7 +8,7 @@ /// /// Contract: `VG.Spec.GcmSiv.sealContract`. Constant time: only the pointers, `rounds` and the lengths may affect timing, not the key schedule, the nonce, the additional data or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_siv_seal_aes_sha3` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AES_SHA3_FEATURES`); `vg_aes_gcm_siv_seal_aes` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AES_FEATURES`); or else `vg_aes_gcm_siv_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_siv_seal_aes_sha3` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AES_SHA3_FEATURES`); `vg_aes_gcm_siv_seal_aes` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AES_FEATURES`); or else `vg_aes_gcm_siv_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -67,7 +67,7 @@ pub unsafe fn vg_aes_gcm_siv_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, sche /// /// Contract: `VG.Spec.GcmSiv.openContract`. Constant time but for the result: only the pointers, `rounds`, the lengths and whether the function returns 1 or 0 may affect timing, not the key schedule, the nonce, the additional data, the data or the tag. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_siv_open_aes_sha3` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AES_SHA3_FEATURES`); `vg_aes_gcm_siv_open_aes` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AES_FEATURES`); or else `vg_aes_gcm_siv_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_siv_open_aes_sha3` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AES_SHA3_FEATURES`); `vg_aes_gcm_siv_open_aes` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AES_FEATURES`); or else `vg_aes_gcm_siv_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_ocb.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_ocb.rs index 0f0604d6faf..b5e5e19406f 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_ocb.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_ocb.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ocb.initContract`. The key context is `VG.Spec.Ocb.KeyRepr`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ocb_init_aes` if the CPU supports its features (`VG_AES_OCB_INIT_AES_FEATURES`); or else `vg_aes_ocb_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ocb_init_aes` if the CPU supports its features (`VG_AES_OCB_INIT_AES_FEATURES`); or else `vg_aes_ocb_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -55,7 +55,7 @@ pub unsafe fn vg_aes_ocb_init_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *co /// /// Contract: `VG.Spec.Ocb.sealContract`. Constant time: only the pointers, `rounds`, the lengths and `tag_len` may affect timing, not the key context, the nonce, the associated data or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ocb_seal_aes` if the CPU supports its features (`VG_AES_OCB_SEAL_AES_FEATURES`); or else `vg_aes_ocb_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ocb_seal_aes` if the CPU supports its features (`VG_AES_OCB_SEAL_AES_FEATURES`); or else `vg_aes_ocb_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -108,7 +108,7 @@ pub unsafe fn vg_aes_ocb_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *co /// /// Contract: `VG.Spec.Ocb.openContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the associated data, the data or the tag. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ocb_open_aes` if the CPU supports its features (`VG_AES_OCB_OPEN_AES_FEATURES`); or else `vg_aes_ocb_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ocb_open_aes` if the CPU supports its features (`VG_AES_OCB_OPEN_AES_FEATURES`); or else `vg_aes_ocb_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_ofb.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_ofb.rs index 6a8c9dc2672..57023fdff58 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_ofb.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_ofb.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ofb.aesContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ofb_aes` if the CPU supports its features (`VG_AES_OFB_AES_FEATURES`); or else `vg_aes_ofb`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ofb_aes` if the CPU supports its features (`VG_AES_OFB_AES_FEATURES`); or else `vg_aes_ofb`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -58,7 +58,7 @@ pub unsafe fn vg_aes_ofb_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *co /// /// Contract: `VG.Spec.Ofb.aesFinalContract`. Constant time: only the pointers, `rounds` and `len` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ofb_final_aes` if the CPU supports its features (`VG_AES_OFB_FINAL_AES_FEATURES`); or else `vg_aes_ofb_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ofb_final_aes` if the CPU supports its features (`VG_AES_OFB_FINAL_AES_FEATURES`); or else `vg_aes_ofb_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_siv.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_siv.rs index ecb62317737..6722f6342ce 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_siv.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_siv.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Siv.initContract`. The key context is `VG.Spec.Siv.KeyRepr`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_siv_init_aes` if the CPU supports its features (`VG_AES_SIV_INIT_AES_FEATURES`); or else `vg_aes_siv_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_siv_init_aes` if the CPU supports its features (`VG_AES_SIV_INIT_AES_FEATURES`); or else `vg_aes_siv_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -55,7 +55,7 @@ pub unsafe fn vg_aes_siv_init_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *co /// /// Contract: `VG.Spec.Siv.encryptContract`. Constant time: only the pointers, `rounds`, `ads_count`, `len` and where the components are (their addresses and lengths) may affect timing, not the key context, the associated data or the plaintext. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_siv_encrypt_aes` if the CPU supports its features (`VG_AES_SIV_ENCRYPT_AES_FEATURES`); or else `vg_aes_siv_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_siv_encrypt_aes` if the CPU supports its features (`VG_AES_SIV_ENCRYPT_AES_FEATURES`); or else `vg_aes_siv_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -106,7 +106,7 @@ pub unsafe fn vg_aes_siv_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: /// /// Contract: `VG.Spec.Siv.decryptContract`. Constant time but for the result: only the pointers, `rounds`, `ads_count`, `len`, where the components are (their addresses and lengths) and whether the function returns 1 or 0 may affect timing, not the key context, the associated data, the data or `V`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_siv_decrypt_aes` if the CPU supports its features (`VG_AES_SIV_DECRYPT_AES_FEATURES`); or else `vg_aes_siv_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_siv_decrypt_aes` if the CPU supports its features (`VG_AES_SIV_DECRYPT_AES_FEATURES`); or else `vg_aes_siv_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_xts.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_xts.rs index 0f61db68b9e..4a2937db37a 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/aes_xts.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/aes_xts.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Xts.aesEncryptUnitContract`. Constant time: only the pointers, `rounds` and `len` may affect timing, not the key schedules, the tweak value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_xts_encrypt_unit_aes` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_AES_FEATURES`); or else `vg_aes_xts_encrypt_unit`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_xts_encrypt_unit_aes` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_AES_FEATURES`); or else `vg_aes_xts_encrypt_unit`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -60,7 +60,7 @@ pub unsafe fn vg_aes_xts_encrypt_unit_auto(_dit: verified_garbage_cpu::Dit<'_>, /// /// Contract: `VG.Spec.Xts.aesDecryptUnitContract`. Constant time: only the pointers, `rounds` and `len` may affect timing, not the key schedules, the tweak value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_xts_decrypt_unit_aes` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_AES_FEATURES`); or else `vg_aes_xts_decrypt_unit`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_xts_decrypt_unit_aes` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_AES_FEATURES`); or else `vg_aes_xts_decrypt_unit`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/chacha20.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/chacha20.rs index c9c52de93e1..c6f8be5e392 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/chacha20.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/chacha20.rs @@ -22,11 +22,30 @@ pub unsafe fn vg_chacha20_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: unsafe { crate::chacha20::vg_chacha20_init(state, key, nonce) } } +/// Restarts a ChaCha20 keystream with the same key: makes the streaming state `*state` represent the whole keystream of its key for the 16-byte nonce `*nonce` (as `vg_chacha20_init` does), discarding what was left of the previous one. +/// +/// Contract: `VG.Spec.ChaCha20.setNonceContract`. Constant time: only the pointers may affect timing, not the key or the nonce. +/// +/// It runs `vg_chacha20_set_nonce`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 768 bytes. +/// * `nonce` must be valid for reads of 16 bytes. +/// * `state` must not overlap `nonce` (distinct Rust objects never do). +/// * Neither `state` nor `nonce` may wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_chacha20_set_nonce_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 96], nonce: *const [u8; 16]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::chacha20::vg_chacha20_set_nonce(state, nonce) } +} + /// Applies a ChaCha20 keystream (RFC 8439 §2.4), encrypting or decrypting: if at least `len` bytes are left of the keystream that the streaming state `*state` represents, XORs the next `len` of them into the `len` bytes at `data`, keeps the rest in `*state`, and returns 1. Otherwise (the block counter would pass 2³² − 1) returns 0, and leaves the bytes at `data` and the keystream unchanged. /// /// Contract: `VG.Spec.ChaCha20.applyContract`. Constant time: only the pointers, `len` and the number of bytes of keystream left in `*state` (`VG.Spec.ChaCha20.leftAt`: 64 × (2³² − `c`) for the initial block counter `c`, less the bytes applied since) may affect timing, not the key, the nonce, the keystream or the data. The function may leak that number. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_chacha20_apply_sve2` if the CPU supports its features (`VG_CHACHA20_APPLY_SVE2_FEATURES`); `vg_chacha20_apply_neon` if detection reports some CPU feature; or else `vg_chacha20_apply`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_chacha20_apply_sve2` if the CPU supports its features (`VG_CHACHA20_APPLY_SVE2_FEATURES`); `vg_chacha20_apply_neon` if detection reports some CPU feature; or else `vg_chacha20_apply`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -74,22 +93,3 @@ pub unsafe fn vg_chacha20_apply_auto(_dit: verified_garbage_cpu::Dit<'_>, state: // either carries the caller's obligations, which are those of every implementation. unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, data, len) } } - -/// Restarts a ChaCha20 keystream with the same key: makes the streaming state `*state` represent the whole keystream of its key for the 16-byte nonce `*nonce` (as `vg_chacha20_init` does), discarding what was left of the previous one. -/// -/// Contract: `VG.Spec.ChaCha20.setNonceContract`. Constant time: only the pointers may affect timing, not the key or the nonce. -/// -/// It runs `vg_chacha20_set_nonce`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 768 bytes. -/// * `nonce` must be valid for reads of 16 bytes. -/// * `state` must not overlap `nonce` (distinct Rust objects never do). -/// * Neither `state` nor `nonce` may wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_chacha20_set_nonce_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 96], nonce: *const [u8; 16]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::chacha20::vg_chacha20_set_nonce(state, nonce) } -} diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/chacha20poly1305.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/chacha20poly1305.rs index 5cc22a84feb..b3bbb3c621d 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/chacha20poly1305.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/chacha20poly1305.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.ChaCha20Poly1305.sealContract`. Constant time: only the pointers and the lengths may affect timing, not the key, the nonce or the data. The block counter wraps around beyond 2³²-1 blocks of data (RFC 8439's `P_MAX`), which the caller must not exceed for the construction to be secure. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_chacha20_poly1305_seal_sve2` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_SVE2_FEATURES`); `vg_chacha20_poly1305_seal_neon` if detection reports some CPU feature; or else `vg_chacha20_poly1305_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_chacha20_poly1305_seal_sve2` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_SVE2_FEATURES`); `vg_chacha20_poly1305_seal_neon` if detection reports some CPU feature; or else `vg_chacha20_poly1305_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -62,7 +62,7 @@ pub unsafe fn vg_chacha20_poly1305_seal_auto(_dit: verified_garbage_cpu::Dit<'_> /// /// Contract: `VG.Spec.ChaCha20Poly1305.openZeroContract`. Constant time but for the result: only the pointers, the lengths and whether the function returns 1 or 0 may affect timing, not the key, the nonce, the additional data, the tag or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_chacha20_poly1305_open_sve2` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_SVE2_FEATURES`); `vg_chacha20_poly1305_open_neon` if detection reports some CPU feature; or else `vg_chacha20_poly1305_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_chacha20_poly1305_open_sve2` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_SVE2_FEATURES`); `vg_chacha20_poly1305_open_neon` if detection reports some CPU feature; or else `vg_chacha20_poly1305_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -118,7 +118,7 @@ pub unsafe fn vg_chacha20_poly1305_open_auto(_dit: verified_garbage_cpu::Dit<'_> /// /// Contract: `VG.Spec.ChaCha20Poly1305.openToContract`. Constant time but for the result: only the pointers, the lengths and whether the function returns 1 or 0 may affect timing, not the key, the nonce, the additional data, the tag or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_chacha20_poly1305_open_to_sve2` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_TO_SVE2_FEATURES`); `vg_chacha20_poly1305_open_to_neon` if detection reports some CPU feature; or else `vg_chacha20_poly1305_open_to`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_chacha20_poly1305_open_to_sve2` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_TO_SVE2_FEATURES`); `vg_chacha20_poly1305_open_to_neon` if detection reports some CPU feature; or else `vg_chacha20_poly1305_open_to`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -177,7 +177,7 @@ pub unsafe fn vg_chacha20_poly1305_open_to_auto(_dit: verified_garbage_cpu::Dit< /// /// Contract: `VG.Spec.ChaCha20Poly1305.sealGatherContract`. Constant time: only the pointers, the lengths, `src_count` and where the slices are (their addresses and lengths) may affect timing, not the key, the nonce, the additional data or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_chacha20_poly1305_seal_gather_sve2` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_GATHER_SVE2_FEATURES`); `vg_chacha20_poly1305_seal_gather_neon` if detection reports some CPU feature; or else `vg_chacha20_poly1305_seal_gather`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_chacha20_poly1305_seal_gather_sve2` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_GATHER_SVE2_FEATURES`); `vg_chacha20_poly1305_seal_gather_neon` if detection reports some CPU feature; or else `vg_chacha20_poly1305_seal_gather`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/cmac_aes.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/cmac_aes.rs index 5f9ccb304d5..d942d28a77a 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/cmac_aes.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/cmac_aes.rs @@ -2,45 +2,45 @@ //! The dispatchers of the verified `cmac_aes` functions for `aarch64` //! (`crate::cmac_aes`, `VG.Rust.dispatcher`). -/// Starts an AES-CMAC computation (NIST SP 800-38B, RFC 4493): makes the streaming state `*state` represent the empty message under the AES key of `key_len` bytes at `key` (AES-128, AES-192 or AES-256). The state holds the key schedule of AES (FIPS 197) with `key_len / 4 + 6` rounds, the subkeys `K1 ‖ K2` (§6.1), the chaining value and the message's last bytes, held back (`VG.Spec.Cmac.Repr`). Continue with `vg_cmac_aes_absorb` and `vg_cmac_aes_finish`, passing them `key_len / 4 + 6` as `rounds`. +/// Absorbs data into an AES-CMAC computation (NIST SP 800-38B §6.2): if the streaming state `*state` represents a message of `count` bytes under an AES key with `rounds` rounds (as `vg_cmac_aes_init` set it up), it then represents that message followed by the `len` bytes at `data`, under the same key, provided that the two together are shorter than 2⁶⁴ bytes. It chains (step 6) every block of the message but its last bytes `Mₙ*` (step 3), which it holds back in the state, since only `vg_cmac_aes_finish` knows they are the last. /// -/// Contract: `VG.Spec.Cmac.aesInitContract`. Constant time: only the pointers and `key_len` may affect timing, not the key. +/// Contract: `VG.Spec.Cmac.aesAbsorbContract`. Constant time: only the pointers, `rounds`, `count` and `len` may affect timing, not the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_cmac_aes_init_aes` if the CPU supports its features (`VG_CMAC_AES_INIT_AES_FEATURES`); or else `vg_cmac_aes_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_cmac_aes_absorb_aes` if the CPU supports its features (`VG_CMAC_AES_ABSORB_AES_FEATURES`); or else `vg_cmac_aes_absorb`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 304 bytes. -/// * `key` must be valid for reads of `key_len` bytes. -/// * `key_len` must be 16, 24 or 32. -/// * `state` must not overlap `key` (distinct Rust objects never do). -/// * Neither `state` nor `key` may overlap the 2304 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `rounds` must be 10, 12 or 14. +/// * `state` must not overlap `data` (distinct Rust objects never do). +/// * Neither `state` nor `data` may overlap the 2304 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_cmac_aes_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 38], key: *const u8, key_len: usize) { +pub unsafe fn vg_cmac_aes_absorb_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 38], rounds: usize, count: u64, data: *const u8, len: usize) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(state: *mut [u64; 38], key: *const u8, key_len: usize); + type Impl = unsafe extern "C" fn(state: *mut [u64; 38], rounds: usize, count: u64, data: *const u8, len: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::cmac_aes::VG_CMAC_AES_INIT_AES_FEATURES) } - || cpu.contains(crate::cmac_aes::VG_CMAC_AES_INIT_AES_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::cmac_aes::VG_CMAC_AES_ABSORB_AES_FEATURES) } + || cpu.contains(crate::cmac_aes::VG_CMAC_AES_ABSORB_AES_FEATURES) { - return crate::cmac_aes::vg_cmac_aes_init_aes; + return crate::cmac_aes::vg_cmac_aes_absorb_aes; } - crate::cmac_aes::vg_cmac_aes_init + crate::cmac_aes::vg_cmac_aes_absorb } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(state: *mut [u64; 38], key: *const u8, key_len: usize) { + unsafe extern "C" fn first(state: *mut [u64; 38], rounds: usize, count: u64, data: *const u8, len: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, key, key_len) } + unsafe { f(state, rounds, count, data, len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -48,48 +48,48 @@ pub unsafe fn vg_cmac_aes_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, key, key_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, rounds, count, data, len) } } -/// Absorbs data into an AES-CMAC computation (NIST SP 800-38B §6.2): if the streaming state `*state` represents a message of `count` bytes under an AES key with `rounds` rounds (as `vg_cmac_aes_init` set it up), it then represents that message followed by the `len` bytes at `data`, under the same key, provided that the two together are shorter than 2⁶⁴ bytes. It chains (step 6) every block of the message but its last bytes `Mₙ*` (step 3), which it holds back in the state, since only `vg_cmac_aes_finish` knows they are the last. +/// Starts an AES-CMAC computation (NIST SP 800-38B, RFC 4493): makes the streaming state `*state` represent the empty message under the AES key of `key_len` bytes at `key` (AES-128, AES-192 or AES-256). The state holds the key schedule of AES (FIPS 197) with `key_len / 4 + 6` rounds, the subkeys `K1 ‖ K2` (§6.1), the chaining value and the message's last bytes, held back (`VG.Spec.Cmac.Repr`). Continue with `vg_cmac_aes_absorb` and `vg_cmac_aes_finish`, passing them `key_len / 4 + 6` as `rounds`. /// -/// Contract: `VG.Spec.Cmac.aesAbsorbContract`. Constant time: only the pointers, `rounds`, `count` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Cmac.aesInitContract`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_cmac_aes_absorb_aes` if the CPU supports its features (`VG_CMAC_AES_ABSORB_AES_FEATURES`); or else `vg_cmac_aes_absorb`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_cmac_aes_init_aes` if the CPU supports its features (`VG_CMAC_AES_INIT_AES_FEATURES`); or else `vg_cmac_aes_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 304 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `rounds` must be 10, 12 or 14. -/// * `state` must not overlap `data` (distinct Rust objects never do). -/// * Neither `state` nor `data` may overlap the 2304 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `key` must be valid for reads of `key_len` bytes. +/// * `key_len` must be 16, 24 or 32. +/// * `state` must not overlap `key` (distinct Rust objects never do). +/// * Neither `state` nor `key` may overlap the 2304 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_cmac_aes_absorb_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 38], rounds: usize, count: u64, data: *const u8, len: usize) { +pub unsafe fn vg_cmac_aes_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 38], key: *const u8, key_len: usize) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(state: *mut [u64; 38], rounds: usize, count: u64, data: *const u8, len: usize); + type Impl = unsafe extern "C" fn(state: *mut [u64; 38], key: *const u8, key_len: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::cmac_aes::VG_CMAC_AES_ABSORB_AES_FEATURES) } - || cpu.contains(crate::cmac_aes::VG_CMAC_AES_ABSORB_AES_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::cmac_aes::VG_CMAC_AES_INIT_AES_FEATURES) } + || cpu.contains(crate::cmac_aes::VG_CMAC_AES_INIT_AES_FEATURES) { - return crate::cmac_aes::vg_cmac_aes_absorb_aes; + return crate::cmac_aes::vg_cmac_aes_init_aes; } - crate::cmac_aes::vg_cmac_aes_absorb + crate::cmac_aes::vg_cmac_aes_init } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(state: *mut [u64; 38], rounds: usize, count: u64, data: *const u8, len: usize) { + unsafe extern "C" fn first(state: *mut [u64; 38], key: *const u8, key_len: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, rounds, count, data, len) } + unsafe { f(state, key, key_len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -97,14 +97,14 @@ pub unsafe fn vg_cmac_aes_absorb_auto(_dit: verified_garbage_cpu::Dit<'_>, state // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, rounds, count, data, len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, key, key_len) } } /// Finishes an AES-CMAC computation (NIST SP 800-38B §6.2, with `Tlen = 128`): if the streaming state `*state` represents a message of `count` bytes, shorter than 2⁶⁴ bytes, under an AES key with `rounds` rounds (as `vg_cmac_aes_init` and `vg_cmac_aes_absorb` set it up), writes the MAC of that message under that key to `*out`: `Cₙ = CIPH_K(Cₙ₋₁ ⊕ Mₙ)`, where `Mₙ = K1 ⊕ Mₙ*` if the message's last bytes `Mₙ*` are a complete block, and `Mₙ = K2 ⊕ (Mₙ* ‖ 10ʲ)` otherwise (step 4). The caller truncates it (step 7) and compares it (§6.3). /// /// Contract: `VG.Spec.Cmac.aesFinishContract`. Constant time: only the pointers, `rounds` and `count` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_cmac_aes_finish_aes` if the CPU supports its features (`VG_CMAC_AES_FINISH_AES_FEATURES`); or else `vg_cmac_aes_finish`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_cmac_aes_finish_aes` if the CPU supports its features (`VG_CMAC_AES_FINISH_AES_FEATURES`); or else `vg_cmac_aes_finish`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/cmac_triple_des.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/cmac_triple_des.rs index 20ca51102e5..a0dc3f67e07 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/cmac_triple_des.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/cmac_triple_des.rs @@ -2,6 +2,26 @@ //! The dispatchers of the verified `cmac_triple_des` functions for `aarch64` //! (`crate::cmac_triple_des`, `VG.Rust.dispatcher`). +/// Starts a TDEA-CMAC computation (NIST SP 800-38B): makes the streaming state `*state` represent the empty message under the 16- or 24-byte TDEA key at `key` (FIPS 46-3; a 16-byte key is `K1 ‖ K2` with `K3 = K1`, parity bits are ignored). The state holds the key schedule (the three DES schedules), the subkeys `K1 ‖ K2` (§6.1), the chaining value and the message's last bytes, held back (`VG.Spec.Cmac.tdesRepr`). Continue with `vg_cmac_triple_des_absorb` and `vg_cmac_triple_des_finish`. +/// +/// Contract: `VG.Spec.Cmac.tdesStreamInitContract`. Constant time: only the pointers and `key_len` may affect timing, not the key. +/// +/// It runs `vg_cmac_triple_des_stream_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 416 bytes. +/// * `key` must be valid for reads of `key_len` bytes. +/// * `key_len` must be 16 or 24. +/// * `state` must not overlap `key` (distinct Rust objects never do). +/// * Neither `state` nor `key` may overlap the 640 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_cmac_triple_des_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 52], key: *const u8, key_len: usize) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::cmac_triple_des::vg_cmac_triple_des_stream_init(state, key, key_len) } +} + /// Absorbs data into a TDEA-CMAC computation (NIST SP 800-38B §6.2): if the streaming state `*state` represents a message of `count` bytes under a TDEA key (as `vg_cmac_triple_des_stream_init` set it up), it then represents that message followed by the `len` bytes at `data`, under the same key, provided that the two together are shorter than 2⁶⁴ bytes. It chains (step 6) every block of the message but its last bytes `Mₙ*` (step 3), which it holds back in the state, since only `vg_cmac_triple_des_finish` knows they are the last. /// /// Contract: `VG.Spec.Cmac.tdesAbsorbContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. @@ -40,23 +60,3 @@ pub unsafe fn vg_cmac_triple_des_finish_auto(_dit: verified_garbage_cpu::Dit<'_> // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::cmac_triple_des::vg_cmac_triple_des_finish(state, count, out) } } - -/// Starts a TDEA-CMAC computation (NIST SP 800-38B): makes the streaming state `*state` represent the empty message under the 16- or 24-byte TDEA key at `key` (FIPS 46-3; a 16-byte key is `K1 ‖ K2` with `K3 = K1`, parity bits are ignored). The state holds the key schedule (the three DES schedules), the subkeys `K1 ‖ K2` (§6.1), the chaining value and the message's last bytes, held back (`VG.Spec.Cmac.tdesRepr`). Continue with `vg_cmac_triple_des_absorb` and `vg_cmac_triple_des_finish`. -/// -/// Contract: `VG.Spec.Cmac.tdesStreamInitContract`. Constant time: only the pointers and `key_len` may affect timing, not the key. -/// -/// It runs `vg_cmac_triple_des_stream_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 416 bytes. -/// * `key` must be valid for reads of `key_len` bytes. -/// * `key_len` must be 16 or 24. -/// * `state` must not overlap `key` (distinct Rust objects never do). -/// * Neither `state` nor `key` may overlap the 640 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_cmac_triple_des_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 52], key: *const u8, key_len: usize) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::cmac_triple_des::vg_cmac_triple_des_stream_init(state, key, key_len) } -} diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/dsa.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/dsa.rs index 1a90feeed03..849f9d029c0 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/dsa.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/dsa.rs @@ -2,106 +2,66 @@ //! The dispatchers of the verified `dsa` functions for `aarch64` //! (`crate::dsa`, `VG.Rust.dispatcher`). -/// Derive a DSA private key from big-endian p||q||g and a supplied q_len-byte scalar x, as key generation does after sampling x: checks the domain and 0 < x < q, computes y = g^x mod p and checks the key. Returns 1 with p||q||g||y||x, or 0 and zero output. Checks algebraic consistency, not primality. x must be a fresh uniform scalar the caller sampled. Timing may depend on p, q, g and success, not otherwise on x. Contract: `VG.Spec.Dsa.deriveKeyContract`. +/// Start a search for DSA domain parameters with a modulus of `p_len` octets (128, 256 or 384: 1024, 2048 or 3072 bits, with a subgroup of 160, 256 and 256 bits). Returns 1 and writes the search's four public words to `state`: the seeds it may still reject, `p_len`, its entropy budget, and the length of the first random tape to give `vg_dsa_parameter_search`. Returns 0 and writes zeros for any other `p_len`. /// -/// It runs `vg_dsa_derive_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// Contract: `VG.Spec.Dsa.parameterInitContract`. +/// +/// It runs `vg_dsa_parameter_search_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `out` must be valid for reads and writes of `out_len` bytes. -/// * `params` must be valid for reads of `params_len` bytes. -/// * `x` must be valid for reads of `q_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). -/// * params_len must be 2*p_len + q_len; out_len must be 3*p_len + 2*q_len. -/// * scratch_len must be at least 16*p_len words. Scratch may contain secrets on return. -/// * `out` and `scratch` must not overlap each other, `params`, `x` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `params`, `x` and `scratch` may wrap around the end of the address space (no Rust object does). +/// * `state` must be valid for reads and writes of 32 bytes. +/// * `state` must not wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_dsa_derive_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, params: *const u8, params_len: usize, p_len: usize, x: *const u8, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_dsa_parameter_search_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 4], p_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::dsa::vg_dsa_derive_key(out, out_len, params, params_len, p_len, x, q_len, scratch, scratch_len) } + unsafe { crate::dsa::vg_dsa_parameter_search_init(state, p_len) } } -/// Generate a DSA private key from p||q||g and fresh independent random bytes. Uses at most 128 rejection-sampling draws, computes y=g^x mod p, and checks algebraic consistency, not primality. Returns 1 with p||q||g||y||x, 0 for invalid input/draw limit, or 2 for insufficient randomness. Failure zeros out; used counts consumed complete draws on every return. Timing may depend on public parameters, sampling decisions, consumption and success, not otherwise on x or randomness. Contract: `VG.Spec.Dsa.generateKeyContract`. +/// Randomized DSA signing of a prehashed digest, for an already validated packed private key (big-endian p||q||g||y||x; vg_dsa_validate_private_key validates it). Up to eight signing attempts, each with 128 nonce draws. Returns 1 with fixed-width r||s, 0 for retry exhaustion, or 2 for insufficient random bytes; out is zero on failure and used counts consumed complete draws on every return. Timing may depend on public p,q,g,y, sampling decisions, signing success bits and consumption, not otherwise on secrets. Contract: `VG.Spec.Dsa.randomSignPrevalidatedContract`. /// -/// It runs `vg_dsa_generate_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_dsa_sign_random_prevalidated`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `out` must be valid for reads and writes of `out_len` bytes. /// * `used` must be valid for reads and writes of 8 bytes. -/// * `params` must be valid for reads of `params_len` bytes. +/// * `key` must be valid for reads of `key_len` bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. /// * `rand` must be valid for reads of `rand_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. /// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). -/// * params_len must be 2*p_len + q_len; out_len must be 3*p_len + 2*q_len. -/// * scratch_len must be at least 64*p_len + 1024 words; destroy scratch after use. -/// * Caller must assure domain validity and supply fresh independent random bytes. -/// * `out`, `used` and `scratch` must not overlap each other, `params`, `rand` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `used`, `params`, `rand` and `scratch` may overlap the 256 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * key_len must be 3*p_len + 2*q_len; out_len must be 2*q_len. +/// * scratch_len must be at least 16*p_len + 64 words; destroy scratch after use. +/// * The exact key bytes must satisfy validatePrivateKeyBytes p_len q_len key = true. A successful vg_dsa_validate_private_key call establishes this only while the key bytes remain unchanged. Primality remains the caller's responsibility. +/// * Provide fresh independent random octets; never reuse a completed signing tape for another signature. +/// * `out`, `used` and `scratch` must not overlap each other, `key`, `digest`, `rand` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `used`, `key`, `digest`, `rand` and `scratch` may overlap the 256 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_dsa_generate_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], params: *const u8, params_len: usize, p_len: usize, q_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_dsa_sign_random_prevalidated_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], key: *const u8, key_len: usize, p_len: usize, q_len: usize, digest: *const u8, digest_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::dsa::vg_dsa_generate_key(out, out_len, used, params, params_len, p_len, q_len, rand, rand_len, scratch, scratch_len) } + unsafe { crate::dsa::vg_dsa_sign_random_prevalidated(out, out_len, used, key, key_len, p_len, q_len, digest, digest_len, rand, rand_len, scratch, scratch_len) } } -/// Search for DSA domain parameters (FIPS 186-4 A.1.1.2 and A.2.1) over the random tape `rand`, for the modulus width in `state`: each seed, its `q`, the `p` candidates from it and the generator's `h` candidates are read from `rand` in turn, with the rejected seeds counted in `state`. Returns 1 and writes `p || q || g` to `out` once parameters are found; 3 if the search is exhausted, by too many rejected seeds or by one seed needing more than the entropy budget; or 0 if the tape ran out inside a seed. Writes the octets of `rand` it consumed to `used`, and the state for the next call to `state`, whose last word is, for 0, the length of the next tape, more than the octets of `rand` after the first `used` and at most the entropy budget: keep those octets, in order, and append fresh random octets up to that length. `out` is zeros but for 1. Accepted parameters pass probable-prime tests, not primality certification. -/// -/// Contract: `VG.Spec.Dsa.parameterSearchContract`. Timing may depend on everything, `rand` included: these are public domain parameters. +/// Validate packed DSA p||q||g||y||x as signing checks it: domain consistency, including g^q mod p = 1, 1 < y < p, 0 < x < q and g^x mod p = y. Returns 1 exactly when validation succeeds, otherwise 0. Does not test primality. Timing may depend on p,q,g,y and the result, not otherwise on x. Scratch contents are unspecified on return and may hold secrets. Contract: `VG.Spec.Dsa.validatePrivateKeyContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_dsa_parameter_search_sha1_sha2_sha256_sha2` if the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_SHA1_SHA2_SHA256_SHA2_FEATURES`); or else `vg_dsa_parameter_search`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs `vg_dsa_validate_private_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `out` must be valid for reads and writes of `out_len` bytes. -/// * `used` must be valid for reads and writes of 8 bytes. -/// * `rand` must be valid for reads of `rand_len` bytes. +/// * `key` must be valid for reads of `key_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `state` must be valid for reads and writes of 32 bytes. -/// * `state` must come from `vg_dsa_parameter_search_init`, or from the last call of this function that returned 0. -/// * `out_len` must be `2 * state[1]` plus 20 if `state[1]` is 128, else plus 32. -/// * `rand_len` must be at most the entropy budget, `state[2]`. -/// * `scratch_len` must be at least `64 * state[1] + 1024` words. -/// * `rand` must hold independent uniform random octets. They are public under this contract and must never be used for private scalars or nonces. -/// * `out`, `used`, `scratch` and `state` must not overlap each other or `rand` (distinct Rust objects never do). -/// * None of `out`, `used`, `rand`, `scratch` and `state` may overlap the 768 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). +/// * key_len must be 3*p_len + 2*q_len; scratch_len must be at least 16*p_len words; destroy scratch after use. +/// * `scratch` must not overlap `key` (distinct Rust objects never do). +/// * Neither `key` nor `scratch` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_dsa_parameter_search_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32 { - /// The type of the implementations. - type Impl = unsafe extern "C" fn(out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32; - /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only - /// features the CPU has. - #[cold] - fn select() -> Impl { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_SHA2_SHA256_SHA2_FEATURES) } - || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_SHA2_SHA256_SHA2_FEATURES) - { - return crate::dsa::vg_dsa_parameter_search_sha1_sha2_sha256_sha2; - } - crate::dsa::vg_dsa_parameter_search - } - /// The first call: chooses the implementation, records it in `CHOSEN` for the calls - /// after it, and runs it. - #[cold] - unsafe extern "C" fn first(out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32 { - let f = select(); - CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); - // SAFETY: the caller's obligations, which are those of every implementation, `f` - // among them, and the CPU has the features `f` needs. - unsafe { f(out, out_len, used, rand, rand_len, scratch, scratch_len, state) } - } - /// The function to call: `first`, until it records the implementation it chose. - static CHOSEN: core::sync::atomic::AtomicPtr<()> = - core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); - // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, - // or an implementation `select` returned, which needs only features the CPU has. Calling - // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, used, rand, rand_len, scratch, scratch_len, state) } +pub unsafe fn vg_dsa_validate_private_key_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *const u8, key_len: usize, p_len: usize, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::dsa::vg_dsa_validate_private_key(key, key_len, p_len, q_len, scratch, scratch_len) } } /// Validate packed DSA p||q||g||y, including domain consistency and both subgroup memberships. Returns 1 exactly when validation succeeds, otherwise 0. Does not test primality. The key is public; scratch contents are unspecified on return. Contract: `VG.Spec.Dsa.validatePublicKeyContract`. @@ -146,64 +106,104 @@ pub unsafe fn vg_dsa_verify_prevalidated_auto(_dit: verified_garbage_cpu::Dit<'_ unsafe { crate::dsa::vg_dsa_verify_prevalidated(key, key_len, p_len, q_len, signature, signature_len, digest, digest_len, scratch, scratch_len) } } -/// Validate packed DSA p||q||g||y||x as signing checks it: domain consistency, including g^q mod p = 1, 1 < y < p, 0 < x < q and g^x mod p = y. Returns 1 exactly when validation succeeds, otherwise 0. Does not test primality. Timing may depend on p,q,g,y and the result, not otherwise on x. Scratch contents are unspecified on return and may hold secrets. Contract: `VG.Spec.Dsa.validatePrivateKeyContract`. +/// Generate a DSA private key from p||q||g and fresh independent random bytes. Uses at most 128 rejection-sampling draws, computes y=g^x mod p, and checks algebraic consistency, not primality. Returns 1 with p||q||g||y||x, 0 for invalid input/draw limit, or 2 for insufficient randomness. Failure zeros out; used counts consumed complete draws on every return. Timing may depend on public parameters, sampling decisions, consumption and success, not otherwise on x or randomness. Contract: `VG.Spec.Dsa.generateKeyContract`. /// -/// It runs `vg_dsa_validate_private_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_dsa_generate_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `key` must be valid for reads of `key_len` bytes. +/// * `out` must be valid for reads and writes of `out_len` bytes. +/// * `used` must be valid for reads and writes of 8 bytes. +/// * `params` must be valid for reads of `params_len` bytes. +/// * `rand` must be valid for reads of `rand_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. /// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). -/// * key_len must be 3*p_len + 2*q_len; scratch_len must be at least 16*p_len words; destroy scratch after use. -/// * `scratch` must not overlap `key` (distinct Rust objects never do). -/// * Neither `key` nor `scratch` may wrap around the end of the address space (no Rust object does). +/// * params_len must be 2*p_len + q_len; out_len must be 3*p_len + 2*q_len. +/// * scratch_len must be at least 64*p_len + 1024 words; destroy scratch after use. +/// * Caller must assure domain validity and supply fresh independent random bytes. +/// * `out`, `used` and `scratch` must not overlap each other, `params`, `rand` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `used`, `params`, `rand` and `scratch` may overlap the 256 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_dsa_validate_private_key_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *const u8, key_len: usize, p_len: usize, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_dsa_generate_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], params: *const u8, params_len: usize, p_len: usize, q_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::dsa::vg_dsa_validate_private_key(key, key_len, p_len, q_len, scratch, scratch_len) } + unsafe { crate::dsa::vg_dsa_generate_key(out, out_len, used, params, params_len, p_len, q_len, rand, rand_len, scratch, scratch_len) } } -/// Start a search for DSA domain parameters with a modulus of `p_len` octets (128, 256 or 384: 1024, 2048 or 3072 bits, with a subgroup of 160, 256 and 256 bits). Returns 1 and writes the search's four public words to `state`: the seeds it may still reject, `p_len`, its entropy budget, and the length of the first random tape to give `vg_dsa_parameter_search`. Returns 0 and writes zeros for any other `p_len`. -/// -/// Contract: `VG.Spec.Dsa.parameterInitContract`. +/// Derive a DSA private key from big-endian p||q||g and a supplied q_len-byte scalar x, as key generation does after sampling x: checks the domain and 0 < x < q, computes y = g^x mod p and checks the key. Returns 1 with p||q||g||y||x, or 0 and zero output. Checks algebraic consistency, not primality. x must be a fresh uniform scalar the caller sampled. Timing may depend on p, q, g and success, not otherwise on x. Contract: `VG.Spec.Dsa.deriveKeyContract`. /// -/// It runs `vg_dsa_parameter_search_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_dsa_derive_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 32 bytes. -/// * `state` must not wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of `out_len` bytes. +/// * `params` must be valid for reads of `params_len` bytes. +/// * `x` must be valid for reads of `q_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). +/// * params_len must be 2*p_len + q_len; out_len must be 3*p_len + 2*q_len. +/// * scratch_len must be at least 16*p_len words. Scratch may contain secrets on return. +/// * `out` and `scratch` must not overlap each other, `params`, `x` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `params`, `x` and `scratch` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_dsa_parameter_search_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 4], p_len: usize) -> u32 { +pub unsafe fn vg_dsa_derive_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, params: *const u8, params_len: usize, p_len: usize, x: *const u8, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::dsa::vg_dsa_parameter_search_init(state, p_len) } + unsafe { crate::dsa::vg_dsa_derive_key(out, out_len, params, params_len, p_len, x, q_len, scratch, scratch_len) } } -/// Randomized DSA signing of a prehashed digest, for an already validated packed private key (big-endian p||q||g||y||x; vg_dsa_validate_private_key validates it). Up to eight signing attempts, each with 128 nonce draws. Returns 1 with fixed-width r||s, 0 for retry exhaustion, or 2 for insufficient random bytes; out is zero on failure and used counts consumed complete draws on every return. Timing may depend on public p,q,g,y, sampling decisions, signing success bits and consumption, not otherwise on secrets. Contract: `VG.Spec.Dsa.randomSignPrevalidatedContract`. +/// Search for DSA domain parameters (FIPS 186-4 A.1.1.2 and A.2.1) over the random tape `rand`, for the modulus width in `state`: each seed, its `q`, the `p` candidates from it and the generator's `h` candidates are read from `rand` in turn, with the rejected seeds counted in `state`. Returns 1 and writes `p || q || g` to `out` once parameters are found; 3 if the search is exhausted, by too many rejected seeds or by one seed needing more than the entropy budget; or 0 if the tape ran out inside a seed. Writes the octets of `rand` it consumed to `used`, and the state for the next call to `state`, whose last word is, for 0, the length of the next tape, more than the octets of `rand` after the first `used` and at most the entropy budget: keep those octets, in order, and append fresh random octets up to that length. `out` is zeros but for 1. Accepted parameters pass probable-prime tests, not primality certification. /// -/// It runs `vg_dsa_sign_random_prevalidated`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// Contract: `VG.Spec.Dsa.parameterSearchContract`. Timing may depend on everything, `rand` included: these are public domain parameters. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_dsa_parameter_search_sha1_sha2_sha256_sha2` if the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_SHA1_SHA2_SHA256_SHA2_FEATURES`); or else `vg_dsa_parameter_search`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `out` must be valid for reads and writes of `out_len` bytes. /// * `used` must be valid for reads and writes of 8 bytes. -/// * `key` must be valid for reads of `key_len` bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. /// * `rand` must be valid for reads of `rand_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). -/// * key_len must be 3*p_len + 2*q_len; out_len must be 2*q_len. -/// * scratch_len must be at least 16*p_len + 64 words; destroy scratch after use. -/// * The exact key bytes must satisfy validatePrivateKeyBytes p_len q_len key = true. A successful vg_dsa_validate_private_key call establishes this only while the key bytes remain unchanged. Primality remains the caller's responsibility. -/// * Provide fresh independent random octets; never reuse a completed signing tape for another signature. -/// * `out`, `used` and `scratch` must not overlap each other, `key`, `digest`, `rand` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `used`, `key`, `digest`, `rand` and `scratch` may overlap the 256 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must be valid for reads and writes of 32 bytes. +/// * `state` must come from `vg_dsa_parameter_search_init`, or from the last call of this function that returned 0. +/// * `out_len` must be `2 * state[1]` plus 20 if `state[1]` is 128, else plus 32. +/// * `rand_len` must be at most the entropy budget, `state[2]`. +/// * `scratch_len` must be at least `64 * state[1] + 1024` words. +/// * `rand` must hold independent uniform random octets. They are public under this contract and must never be used for private scalars or nonces. +/// * `out`, `used`, `scratch` and `state` must not overlap each other or `rand` (distinct Rust objects never do). +/// * None of `out`, `used`, `rand`, `scratch` and `state` may overlap the 768 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_dsa_sign_random_prevalidated_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], key: *const u8, key_len: usize, p_len: usize, q_len: usize, digest: *const u8, digest_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::dsa::vg_dsa_sign_random_prevalidated(out, out_len, used, key, key_len, p_len, q_len, digest, digest_len, rand, rand_len, scratch, scratch_len) } +pub unsafe fn vg_dsa_parameter_search_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32 { + /// The type of the implementations. + type Impl = unsafe extern "C" fn(out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32; + /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only + /// features the CPU has. + #[cold] + fn select() -> Impl { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_SHA2_SHA256_SHA2_FEATURES) } + || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_SHA2_SHA256_SHA2_FEATURES) + { + return crate::dsa::vg_dsa_parameter_search_sha1_sha2_sha256_sha2; + } + crate::dsa::vg_dsa_parameter_search + } + /// The first call: chooses the implementation, records it in `CHOSEN` for the calls + /// after it, and runs it. + #[cold] + unsafe extern "C" fn first(out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32 { + let f = select(); + CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); + // SAFETY: the caller's obligations, which are those of every implementation, `f` + // among them, and the CPU has the features `f` needs. + unsafe { f(out, out_len, used, rand, rand_len, scratch, scratch_len, state) } + } + /// The function to call: `first`, until it records the implementation it chose. + static CHOSEN: core::sync::atomic::AtomicPtr<()> = + core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); + // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, + // or an implementation `select` returned, which needs only features the CPU has. Calling + // either carries the caller's obligations, which are those of every implementation. + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, used, rand, rand_len, scratch, scratch_len, state) } } diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_brainpoolp256r1.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_brainpoolp256r1.rs index 2225054af14..401cb80dd6b 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_brainpoolp256r1.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_brainpoolp256r1.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_brainpoolp256r1` functions for `aarch64` //! (`crate::ecdsa_brainpoolp256r1`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over brainpoolP256r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_brainpoolp256r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 65 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 64 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_brainpoolp256r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over brainpoolP256r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_brainpoolp256r1_sign_auto(_dit: verified_garbage_cpu::Dit // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over brainpoolP256r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_brainpoolp256r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 65 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 64 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_brainpoolp256r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_brainpoolp384r1.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_brainpoolp384r1.rs index 2f7cd57cd18..9712ea377bf 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_brainpoolp384r1.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_brainpoolp384r1.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_brainpoolp384r1` functions for `aarch64` //! (`crate::ecdsa_brainpoolp384r1`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over brainpoolP384r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_brainpoolp384r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 97 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 96 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_brainpoolp384r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over brainpoolP384r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (48 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (48 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_brainpoolp384r1_sign_auto(_dit: verified_garbage_cpu::Dit // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over brainpoolP384r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_brainpoolp384r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 97 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 96 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_brainpoolp384r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_brainpoolp512r1.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_brainpoolp512r1.rs index 5a3b1f3b925..203ab41ae18 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_brainpoolp512r1.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_brainpoolp512r1.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_brainpoolp512r1` functions for `aarch64` //! (`crate::ecdsa_brainpoolp512r1`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over brainpoolP512r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 64 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 512 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 64 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_brainpoolp512r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 129 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 128 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_brainpoolp512r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 129], digest: *const u8, digest_len: usize, sig: *const [u8; 128], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over brainpoolP512r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (64 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (64 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_brainpoolp512r1_sign_auto(_dit: verified_garbage_cpu::Dit // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over brainpoolP512r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 64 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 512 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 64 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_brainpoolp512r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 129 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 128 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_brainpoolp512r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 129], digest: *const u8, digest_len: usize, sig: *const [u8; 128], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_p192.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_p192.rs index 8768fe1dcaa..cedfb8e1b78 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_p192.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_p192.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_p192` functions for `aarch64` //! (`crate::ecdsa_p192`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over P-192 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 24 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 192 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 24 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_p192_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 49 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 48 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_p192_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 49], digest: *const u8, digest_len: usize, sig: *const [u8; 48], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_p192::vg_ecdsa_p192_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over P-192 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (24 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (24 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_p192_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_p192::vg_ecdsa_p192_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over P-192 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 24 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 192 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 24 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_p192_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 49 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 48 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_p192_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 49], digest: *const u8, digest_len: usize, sig: *const [u8; 48], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p192::vg_ecdsa_p192_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_p224.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_p224.rs index d4df7673fec..43baf1a6ab3 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_p224.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_p224.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_p224` functions for `aarch64` //! (`crate::ecdsa_p224`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over P-224 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 28 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 224 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 28 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_p224_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 57 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 56 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_p224_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 57], digest: *const u8, digest_len: usize, sig: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_p224::vg_ecdsa_p224_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over P-224 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (28 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (28 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_p224_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_p224::vg_ecdsa_p224_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over P-224 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 28 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 224 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 28 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_p224_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 57 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 56 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_p224_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 57], digest: *const u8, digest_len: usize, sig: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p224::vg_ecdsa_p224_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_p256.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_p256.rs index 039cccdcb0d..b51f8f957e2 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_p256.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_p256.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_p256` functions for `aarch64` //! (`crate::ecdsa_p256`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over P-256 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_p256_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 65 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 64 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_p256_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_p256::vg_ecdsa_p256_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over P-256 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_p256_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_p256::vg_ecdsa_p256_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over P-256 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_p256_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 65 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 64 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_p256_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p256::vg_ecdsa_p256_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_p384.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_p384.rs index 540b623624d..e0ae024bfb3 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_p384.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_p384.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_p384` functions for `aarch64` //! (`crate::ecdsa_p384`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over P-384 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_p384_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 97 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 96 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_p384_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_p384::vg_ecdsa_p384_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over P-384 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (48 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (48 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_p384_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_p384::vg_ecdsa_p384_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over P-384 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_p384_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 97 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 96 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_p384_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p384::vg_ecdsa_p384_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_p521.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_p521.rs index e6a0b8df6ea..28693f464be 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_p521.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_p521.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_p521` functions for `aarch64` //! (`crate::ecdsa_p521`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over P-521 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 66 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 521 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 66 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_p521_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 133 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 132 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_p521_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 133], digest: *const u8, digest_len: usize, sig: *const [u8; 132], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_p521::vg_ecdsa_p521_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over P-521 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (66 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (66 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_p521_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_p521::vg_ecdsa_p521_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over P-521 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 66 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 521 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 66 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_p521_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 133 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 132 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_p521_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 133], digest: *const u8, digest_len: usize, sig: *const [u8; 132], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p521::vg_ecdsa_p521_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_secp256k1.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_secp256k1.rs index 82fd3463a48..e054092ada2 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_secp256k1.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/ecdsa_secp256k1.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_secp256k1` functions for `aarch64` //! (`crate::ecdsa_secp256k1`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over secp256k1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_secp256k1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 65 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 64 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_secp256k1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over secp256k1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_secp256k1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over secp256k1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_secp256k1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 65 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 64 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_secp256k1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/ed25519.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/ed25519.rs index a1af145f22e..5556957dbcc 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/ed25519.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/ed25519.rs @@ -2,45 +2,47 @@ //! The dispatchers of the verified `ed25519` functions for `aarch64` //! (`crate::ed25519`, `VG.Rust.dispatcher`). -/// Ed25519 verification (RFC 8032 §5.1.7): returns 1 if the 64-byte signature at `signature` verifies for the 32-byte public key at `pk` and the `len` bytes at `message`, and 0 otherwise. Uses pure Ed25519, with no context or prehash. Checks canonical point encodings, S < L, and `[S]B = R + [k]A` with the challenge k = SHA-512(R || A || M) reduced modulo L, as in RFC 8032 §6. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed25519.verifyContract`. Not constant time: timing may depend on the public key, message and signature. +/// Ed25519 key-pair derivation (RFC 8032 §5.1.5): for the 32-byte private seed at `seed`, writes its 64-byte SHA-512 digest (§5.1.5, step 1) to `*expanded` and its 32-byte public key to `*out`, including pruning: the digest and public key that `vg_ed25519_sign_expanded` takes. Contract: `VG.Spec.Ed25519.keyPairContract`. Constant time: only pointers may affect timing. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ed25519_verify_sha512` if the CPU supports its features (`VG_ED25519_VERIFY_SHA512_FEATURES`); or else `vg_ed25519_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ed25519_key_pair_sha512` if the CPU supports its features (`VG_ED25519_KEY_PAIR_SHA512_FEATURES`); or else `vg_ed25519_key_pair`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `pk` must be valid for reads of 32 bytes. -/// * `message` must be valid for reads of `len` bytes. -/// * `signature` must be valid for reads of 64 bytes. +/// * `out` must be valid for reads and writes of 32 bytes. +/// * `expanded` must be valid for reads and writes of 64 bytes. +/// * `seed` must be valid for reads of 32 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The seed must originate from a cryptographically secure random generator. +/// * The 64 bytes written to `expanded` are secret: the caller must destroy them after use. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `scratch` must not overlap `pk`, `message` or `signature` (distinct Rust objects never do). -/// * None of `pk`, `message`, `signature` and `scratch` may overlap the 352 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out`, `expanded` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `out`, `expanded`, `seed` and `scratch` may overlap the 944 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ed25519_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 32], message: *const u8, len: usize, signature: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ed25519_key_pair_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], expanded: *mut [u8; 64], seed: *const [u8; 32], scratch: *mut [u64; 1024]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(pk: *const [u8; 32], message: *const u8, len: usize, signature: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32; + type Impl = unsafe extern "C" fn(out: *mut [u8; 32], expanded: *mut [u8; 64], seed: *const [u8; 32], scratch: *mut [u64; 1024]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_VERIFY_SHA512_FEATURES) } - || cpu.contains(crate::ed25519::VG_ED25519_VERIFY_SHA512_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_KEY_PAIR_SHA512_FEATURES) } + || cpu.contains(crate::ed25519::VG_ED25519_KEY_PAIR_SHA512_FEATURES) { - return crate::ed25519::vg_ed25519_verify_sha512; + return crate::ed25519::vg_ed25519_key_pair_sha512; } - crate::ed25519::vg_ed25519_verify + crate::ed25519::vg_ed25519_key_pair } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(pk: *const [u8; 32], message: *const u8, len: usize, signature: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { + unsafe extern "C" fn first(out: *mut [u8; 32], expanded: *mut [u8; 64], seed: *const [u8; 32], scratch: *mut [u64; 1024]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(pk, message, len, signature, scratch) } + unsafe { f(out, expanded, seed, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -48,50 +50,51 @@ pub unsafe fn vg_ed25519_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *c // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, message, len, signature, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, expanded, seed, scratch) } } -/// Ed25519 key-pair derivation (RFC 8032 §5.1.5): for the 32-byte private seed at `seed`, writes its 64-byte SHA-512 digest (§5.1.5, step 1) to `*expanded` and its 32-byte public key to `*out`, including pruning: the digest and public key that `vg_ed25519_sign_expanded` takes. Contract: `VG.Spec.Ed25519.keyPairContract`. Constant time: only pointers may affect timing. +/// Deterministic Ed25519 signing (RFC 8032 §5.1.6): writes the 64-byte signature to `*out`, for the `len` bytes at `message` and the 32-byte private seed whose SHA-512 digest (§5.1.6, step 1) is cached at `expanded`, using its cached public key at `pk`. Includes the remaining hashing, pruning and scalar/group operations. Uses pure Ed25519, with no context or prehash. Contract: `VG.Spec.Ed25519.signExpandedContract`. Constant time: only pointers and the message length may affect timing, not any buffer contents. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ed25519_key_pair_sha512` if the CPU supports its features (`VG_ED25519_KEY_PAIR_SHA512_FEATURES`); or else `vg_ed25519_key_pair`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ed25519_sign_expanded_sha512` if the CPU supports its features (`VG_ED25519_SIGN_EXPANDED_SHA512_FEATURES`); or else `vg_ed25519_sign_expanded`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 32 bytes. -/// * `expanded` must be valid for reads and writes of 64 bytes. -/// * `seed` must be valid for reads of 32 bytes. +/// * `out` must be valid for reads and writes of 64 bytes. +/// * `expanded` must be valid for reads of 64 bytes. +/// * `pk` must be valid for reads of 32 bytes. +/// * `message` must be valid for reads of `len` bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The 64 bytes at `expanded` must equal the SHA-512 digest (`VG.Spec.Sha512.sha512`) of a 32-byte seed, and the 32 bytes at `pk` the public key derived from that seed by RFC 8032 §5.1.5 (`VG.Spec.Ed25519.publicKey`). /// * The seed must originate from a cryptographically secure random generator. -/// * The 64 bytes written to `expanded` are secret: the caller must destroy them after use. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out`, `expanded` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `out`, `expanded`, `seed` and `scratch` may overlap the 944 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `expanded`, `pk` or `message` (distinct Rust objects never do). +/// * None of `out`, `expanded`, `pk`, `message` and `scratch` may overlap the 352 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ed25519_key_pair_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], expanded: *mut [u8; 64], seed: *const [u8; 32], scratch: *mut [u64; 1024]) { +pub unsafe fn vg_ed25519_sign_expanded_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 64], expanded: *const [u8; 64], pk: *const [u8; 32], message: *const u8, len: usize, scratch: *mut [u64; 1024]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(out: *mut [u8; 32], expanded: *mut [u8; 64], seed: *const [u8; 32], scratch: *mut [u64; 1024]); + type Impl = unsafe extern "C" fn(out: *mut [u8; 64], expanded: *const [u8; 64], pk: *const [u8; 32], message: *const u8, len: usize, scratch: *mut [u64; 1024]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_KEY_PAIR_SHA512_FEATURES) } - || cpu.contains(crate::ed25519::VG_ED25519_KEY_PAIR_SHA512_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_SHA512_FEATURES) } + || cpu.contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_SHA512_FEATURES) { - return crate::ed25519::vg_ed25519_key_pair_sha512; + return crate::ed25519::vg_ed25519_sign_expanded_sha512; } - crate::ed25519::vg_ed25519_key_pair + crate::ed25519::vg_ed25519_sign_expanded } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(out: *mut [u8; 32], expanded: *mut [u8; 64], seed: *const [u8; 32], scratch: *mut [u64; 1024]) { + unsafe extern "C" fn first(out: *mut [u8; 64], expanded: *const [u8; 64], pk: *const [u8; 32], message: *const u8, len: usize, scratch: *mut [u64; 1024]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, expanded, seed, scratch) } + unsafe { f(out, expanded, pk, message, len, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -99,51 +102,48 @@ pub unsafe fn vg_ed25519_key_pair_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, expanded, seed, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, expanded, pk, message, len, scratch) } } -/// Deterministic Ed25519 signing (RFC 8032 §5.1.6): writes the 64-byte signature to `*out`, for the `len` bytes at `message` and the 32-byte private seed whose SHA-512 digest (§5.1.6, step 1) is cached at `expanded`, using its cached public key at `pk`. Includes the remaining hashing, pruning and scalar/group operations. Uses pure Ed25519, with no context or prehash. Contract: `VG.Spec.Ed25519.signExpandedContract`. Constant time: only pointers and the message length may affect timing, not any buffer contents. +/// Ed25519 verification (RFC 8032 §5.1.7): returns 1 if the 64-byte signature at `signature` verifies for the 32-byte public key at `pk` and the `len` bytes at `message`, and 0 otherwise. Uses pure Ed25519, with no context or prehash. Checks canonical point encodings, S < L, and `[S]B = R + [k]A` with the challenge k = SHA-512(R || A || M) reduced modulo L, as in RFC 8032 §6. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed25519.verifyContract`. Not constant time: timing may depend on the public key, message and signature. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ed25519_sign_expanded_sha512` if the CPU supports its features (`VG_ED25519_SIGN_EXPANDED_SHA512_FEATURES`); or else `vg_ed25519_sign_expanded`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ed25519_verify_sha512` if the CPU supports its features (`VG_ED25519_VERIFY_SHA512_FEATURES`); or else `vg_ed25519_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 64 bytes. -/// * `expanded` must be valid for reads of 64 bytes. /// * `pk` must be valid for reads of 32 bytes. /// * `message` must be valid for reads of `len` bytes. +/// * `signature` must be valid for reads of 64 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The 64 bytes at `expanded` must equal the SHA-512 digest (`VG.Spec.Sha512.sha512`) of a 32-byte seed, and the 32 bytes at `pk` the public key derived from that seed by RFC 8032 §5.1.5 (`VG.Spec.Ed25519.publicKey`). -/// * The seed must originate from a cryptographically secure random generator. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `expanded`, `pk` or `message` (distinct Rust objects never do). -/// * None of `out`, `expanded`, `pk`, `message` and `scratch` may overlap the 352 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `scratch` must not overlap `pk`, `message` or `signature` (distinct Rust objects never do). +/// * None of `pk`, `message`, `signature` and `scratch` may overlap the 352 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ed25519_sign_expanded_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 64], expanded: *const [u8; 64], pk: *const [u8; 32], message: *const u8, len: usize, scratch: *mut [u64; 1024]) { +pub unsafe fn vg_ed25519_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 32], message: *const u8, len: usize, signature: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(out: *mut [u8; 64], expanded: *const [u8; 64], pk: *const [u8; 32], message: *const u8, len: usize, scratch: *mut [u64; 1024]); + type Impl = unsafe extern "C" fn(pk: *const [u8; 32], message: *const u8, len: usize, signature: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_SHA512_FEATURES) } - || cpu.contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_SHA512_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_VERIFY_SHA512_FEATURES) } + || cpu.contains(crate::ed25519::VG_ED25519_VERIFY_SHA512_FEATURES) { - return crate::ed25519::vg_ed25519_sign_expanded_sha512; + return crate::ed25519::vg_ed25519_verify_sha512; } - crate::ed25519::vg_ed25519_sign_expanded + crate::ed25519::vg_ed25519_verify } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(out: *mut [u8; 64], expanded: *const [u8; 64], pk: *const [u8; 32], message: *const u8, len: usize, scratch: *mut [u64; 1024]) { + unsafe extern "C" fn first(pk: *const [u8; 32], message: *const u8, len: usize, signature: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, expanded, pk, message, len, scratch) } + unsafe { f(pk, message, len, signature, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -151,5 +151,5 @@ pub unsafe fn vg_ed25519_sign_expanded_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, expanded, pk, message, len, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, message, len, signature, scratch) } } diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/ed448.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/ed448.rs index 6f57fcd057c..9b1287f5b3e 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/ed448.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/ed448.rs @@ -2,47 +2,46 @@ //! The dispatchers of the verified `ed448` functions for `aarch64` //! (`crate::ed448`, `VG.Rust.dispatcher`). -/// Ed448 verification (RFC 8032 §5.2.7): returns 1 if the 114-byte signature at `signature` verifies for the 57-byte public key at `pk`, the `ctxlen` bytes of context at `context` and the `len` bytes at `message`, and 0 otherwise (always 0 for a context longer than 255 bytes). Uses Ed448 with a context, not Ed448ph (no prehash). Checks canonical point encodings, S < L, and the cofactored equation `[4][S]B = [4]R + [4][k]A` with the challenge k = SHAKE256(dom4(0, C) || R || A || M, 114) reduced modulo L. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed448.verifyContract`. Not constant time: timing may depend on the public key, context, message and signature. +/// Ed448 public-key derivation (RFC 8032 §5.2.5): writes the 57-byte public key to `*out`, from the 57-byte private key at `seed`, including SHAKE256 and pruning. Contract: `VG.Spec.Ed448.publicKeyContract`. Constant time: only pointers may affect timing. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ed448_verify_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_ED448_VERIFY_SHA3_FEATURES`); or else `vg_ed448_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ed448_public_key_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_ED448_PUBLIC_KEY_SHA3_FEATURES`); or else `vg_ed448_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `pk` must be valid for reads of 57 bytes. -/// * `context` must be valid for reads of `ctxlen` bytes. -/// * `message` must be valid for reads of `len` bytes. -/// * `signature` must be valid for reads of 114 bytes. +/// * `out` must be valid for reads and writes of 57 bytes. +/// * `seed` must be valid for reads of 57 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * `seed` must originate from a cryptographically secure random generator. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `scratch` must not overlap `pk`, `context`, `message` or `signature` (distinct Rust objects never do). -/// * None of `pk`, `context`, `message`, `signature` and `scratch` may overlap the 352 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `out`, `seed` and `scratch` may overlap the 352 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ed448_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, signature: *const [u8; 114], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ed448_public_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 57], seed: *const [u8; 57], scratch: *mut [u64; 1024]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, signature: *const [u8; 114], scratch: *mut [u64; 1024]) -> u32; + type Impl = unsafe extern "C" fn(out: *mut [u8; 57], seed: *const [u8; 57], scratch: *mut [u64; 1024]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::ed448::VG_ED448_VERIFY_SHA3_FEATURES)) - && (const { verified_garbage_cpu::target_contains(crate::ed448::VG_ED448_VERIFY_SHA3_FEATURES) } - || cpu.contains(crate::ed448::VG_ED448_VERIFY_SHA3_FEATURES)) + if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::ed448::VG_ED448_PUBLIC_KEY_SHA3_FEATURES)) + && (const { verified_garbage_cpu::target_contains(crate::ed448::VG_ED448_PUBLIC_KEY_SHA3_FEATURES) } + || cpu.contains(crate::ed448::VG_ED448_PUBLIC_KEY_SHA3_FEATURES)) { - return crate::ed448::vg_ed448_verify_sha3; + return crate::ed448::vg_ed448_public_key_sha3; } - crate::ed448::vg_ed448_verify + crate::ed448::vg_ed448_public_key } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, signature: *const [u8; 114], scratch: *mut [u64; 1024]) -> u32 { + unsafe extern "C" fn first(out: *mut [u8; 57], seed: *const [u8; 57], scratch: *mut [u64; 1024]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(pk, context, ctxlen, message, len, signature, scratch) } + unsafe { f(out, seed, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -50,49 +49,54 @@ pub unsafe fn vg_ed448_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *con // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, context, ctxlen, message, len, signature, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, seed, scratch) } } -/// Ed448 public-key derivation (RFC 8032 §5.2.5): writes the 57-byte public key to `*out`, from the 57-byte private key at `seed`, including SHAKE256 and pruning. Contract: `VG.Spec.Ed448.publicKeyContract`. Constant time: only pointers may affect timing. +/// Ed448 signing (RFC 8032 §5.2.6): writes the 114-byte signature to `*out`, for the `len` bytes at `message`, the `ctxlen` bytes of context at `context` and the 57-byte private key at `seed`, using its cached public key at `pk`. Includes all hashing, pruning and scalar/group operations. Uses Ed448 with a context, not Ed448ph (no prehash); the context is empty for plain Ed448. Contract: `VG.Spec.Ed448.signCachedContract`. Constant time: only pointers and the context and message lengths may affect timing, not any buffer contents. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ed448_public_key_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_ED448_PUBLIC_KEY_SHA3_FEATURES`); or else `vg_ed448_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ed448_sign_cached_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_ED448_SIGN_CACHED_SHA3_FEATURES`); or else `vg_ed448_sign_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 57 bytes. +/// * `out` must be valid for reads and writes of 114 bytes. /// * `seed` must be valid for reads of 57 bytes. +/// * `pk` must be valid for reads of 57 bytes. +/// * `context` must be valid for reads of `ctxlen` bytes. +/// * `message` must be valid for reads of `len` bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The 57 bytes at `pk` must equal the public key derived from the 57 bytes at `seed` by RFC 8032 §5.2.5 (`VG.Spec.Ed448.publicKey`). +/// * `ctxlen` must be at most 255. /// * `seed` must originate from a cryptographically secure random generator. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `out`, `seed` and `scratch` may overlap the 352 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `seed`, `pk`, `context` or `message` (distinct Rust objects never do). +/// * None of `out`, `seed`, `pk`, `context`, `message` and `scratch` may overlap the 352 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ed448_public_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 57], seed: *const [u8; 57], scratch: *mut [u64; 1024]) { +pub unsafe fn vg_ed448_sign_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 114], seed: *const [u8; 57], pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, scratch: *mut [u64; 1024]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(out: *mut [u8; 57], seed: *const [u8; 57], scratch: *mut [u64; 1024]); + type Impl = unsafe extern "C" fn(out: *mut [u8; 114], seed: *const [u8; 57], pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, scratch: *mut [u64; 1024]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::ed448::VG_ED448_PUBLIC_KEY_SHA3_FEATURES)) - && (const { verified_garbage_cpu::target_contains(crate::ed448::VG_ED448_PUBLIC_KEY_SHA3_FEATURES) } - || cpu.contains(crate::ed448::VG_ED448_PUBLIC_KEY_SHA3_FEATURES)) + if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::ed448::VG_ED448_SIGN_CACHED_SHA3_FEATURES)) + && (const { verified_garbage_cpu::target_contains(crate::ed448::VG_ED448_SIGN_CACHED_SHA3_FEATURES) } + || cpu.contains(crate::ed448::VG_ED448_SIGN_CACHED_SHA3_FEATURES)) { - return crate::ed448::vg_ed448_public_key_sha3; + return crate::ed448::vg_ed448_sign_cached_sha3; } - crate::ed448::vg_ed448_public_key + crate::ed448::vg_ed448_sign_cached } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(out: *mut [u8; 57], seed: *const [u8; 57], scratch: *mut [u64; 1024]) { + unsafe extern "C" fn first(out: *mut [u8; 114], seed: *const [u8; 57], pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, scratch: *mut [u64; 1024]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, seed, scratch) } + unsafe { f(out, seed, pk, context, ctxlen, message, len, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -100,54 +104,50 @@ pub unsafe fn vg_ed448_public_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, seed, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, seed, pk, context, ctxlen, message, len, scratch) } } -/// Ed448 signing (RFC 8032 §5.2.6): writes the 114-byte signature to `*out`, for the `len` bytes at `message`, the `ctxlen` bytes of context at `context` and the 57-byte private key at `seed`, using its cached public key at `pk`. Includes all hashing, pruning and scalar/group operations. Uses Ed448 with a context, not Ed448ph (no prehash); the context is empty for plain Ed448. Contract: `VG.Spec.Ed448.signCachedContract`. Constant time: only pointers and the context and message lengths may affect timing, not any buffer contents. +/// Ed448 verification (RFC 8032 §5.2.7): returns 1 if the 114-byte signature at `signature` verifies for the 57-byte public key at `pk`, the `ctxlen` bytes of context at `context` and the `len` bytes at `message`, and 0 otherwise (always 0 for a context longer than 255 bytes). Uses Ed448 with a context, not Ed448ph (no prehash). Checks canonical point encodings, S < L, and the cofactored equation `[4][S]B = [4]R + [4][k]A` with the challenge k = SHAKE256(dom4(0, C) || R || A || M, 114) reduced modulo L. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed448.verifyContract`. Not constant time: timing may depend on the public key, context, message and signature. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ed448_sign_cached_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_ED448_SIGN_CACHED_SHA3_FEATURES`); or else `vg_ed448_sign_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ed448_verify_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_ED448_VERIFY_SHA3_FEATURES`); or else `vg_ed448_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 114 bytes. -/// * `seed` must be valid for reads of 57 bytes. /// * `pk` must be valid for reads of 57 bytes. /// * `context` must be valid for reads of `ctxlen` bytes. /// * `message` must be valid for reads of `len` bytes. +/// * `signature` must be valid for reads of 114 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The 57 bytes at `pk` must equal the public key derived from the 57 bytes at `seed` by RFC 8032 §5.2.5 (`VG.Spec.Ed448.publicKey`). -/// * `ctxlen` must be at most 255. -/// * `seed` must originate from a cryptographically secure random generator. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `seed`, `pk`, `context` or `message` (distinct Rust objects never do). -/// * None of `out`, `seed`, `pk`, `context`, `message` and `scratch` may overlap the 352 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `scratch` must not overlap `pk`, `context`, `message` or `signature` (distinct Rust objects never do). +/// * None of `pk`, `context`, `message`, `signature` and `scratch` may overlap the 352 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ed448_sign_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 114], seed: *const [u8; 57], pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, scratch: *mut [u64; 1024]) { +pub unsafe fn vg_ed448_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, signature: *const [u8; 114], scratch: *mut [u64; 1024]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(out: *mut [u8; 114], seed: *const [u8; 57], pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, scratch: *mut [u64; 1024]); + type Impl = unsafe extern "C" fn(pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, signature: *const [u8; 114], scratch: *mut [u64; 1024]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::ed448::VG_ED448_SIGN_CACHED_SHA3_FEATURES)) - && (const { verified_garbage_cpu::target_contains(crate::ed448::VG_ED448_SIGN_CACHED_SHA3_FEATURES) } - || cpu.contains(crate::ed448::VG_ED448_SIGN_CACHED_SHA3_FEATURES)) + if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::ed448::VG_ED448_VERIFY_SHA3_FEATURES)) + && (const { verified_garbage_cpu::target_contains(crate::ed448::VG_ED448_VERIFY_SHA3_FEATURES) } + || cpu.contains(crate::ed448::VG_ED448_VERIFY_SHA3_FEATURES)) { - return crate::ed448::vg_ed448_sign_cached_sha3; + return crate::ed448::vg_ed448_verify_sha3; } - crate::ed448::vg_ed448_sign_cached + crate::ed448::vg_ed448_verify } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(out: *mut [u8; 114], seed: *const [u8; 57], pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, scratch: *mut [u64; 1024]) { + unsafe extern "C" fn first(pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, signature: *const [u8; 114], scratch: *mut [u64; 1024]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, seed, pk, context, ctxlen, message, len, scratch) } + unsafe { f(pk, context, ctxlen, message, len, signature, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -155,5 +155,5 @@ pub unsafe fn vg_ed448_sign_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, out // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, seed, pk, context, ctxlen, message, len, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, context, ctxlen, message, len, signature, scratch) } } diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/ffdh.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/ffdh.rs index 159a8ebd7ca..f33db3628f0 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/ffdh.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/ffdh.rs @@ -2,34 +2,87 @@ //! The dispatchers of the verified `ffdh` functions for `aarch64` //! (`crate::ffdh`, `VG.Rust.dispatcher`). -/// Finite-field Diffie–Hellman (SP 800-56A Rev. 3 §5.7.1.1, PKCS #3 §8). With `domain` as `vg_ffdh_check_domain` takes it, the private scalar x (`x_len` bytes) and the peer's public value y (`p_len` bytes), most significant first, computes `z = y^x mod p`; returns 0 and writes zeros if z is 0, 1 or `p − 1`, and otherwise returns 1 and writes z (`p_len` bytes, most significant first) to `out`. Applies no KDF. +/// Initialize a reusable public fixed-base context for finite-field Diffie–Hellman key generation. The checked domain is p, g and optional q, each p_len bytes. Writes generationPrecompute: three public metadata words followed by RSA's modulus/R-squared values and 32 five-tooth comb entries. Returns 1. Word 0 is generationScalarBits, word 1 is its byte length and word 2 is the comb row count. The context has no pointers and is independent of the arithmetic backend. Contract: VG.Spec.Ffdh.generationPrecomputeContract. Timing may depend on the public domain, pointers and lengths. /// -/// Contract: `VG.Spec.Ffdh.exchangeContract`. Timing may depend on the pointers, the lengths, the domain, y and the value returned, not otherwise on x; the exponentiation may run over all `x_len` bytes of x, leading zeros included. +/// It runs `vg_ffdh_generate_key_precompute`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// -/// It runs `vg_ffdh_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// # Safety +/// +/// * `ctx` must be valid for reads and writes of `8 * ctx_len` bytes. +/// * `domain` must be valid for reads of `domain_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `p_len` must be in 64..=1024. +/// * `domain_len` must be `2 * p_len` or `3 * p_len`. +/// * domain must be one that vg_ffdh_check_domain accepted. +/// * ctx_len must be generationPrecomputedWords(p_len), namely 3 + 34 * ceil(p_len / 8). +/// * `scratch_len` must be at least `16 * p_len + 32`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `ctx` and `scratch` must not overlap each other or `domain` (distinct Rust objects never do). +/// * None of `ctx`, `domain` and `scratch` may wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ffdh_generate_key_precompute_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *mut u64, ctx_len: usize, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ffdh::vg_ffdh_generate_key_precompute(ctx, ctx_len, domain, domain_len, p_len, scratch, scratch_len) } +} + +/// One native finite-field Diffie–Hellman key-generation attempt using a reusable public five-tooth comb context. Takes p_len random bytes and implements the same vettedScalarOfOctets, keyStatus and output conventions as vg_ffdh_generate_key_vetted for checked parameters. The context's public maximum width fixes the schedule, including secret leading zeros. Contract: VG.Spec.Ffdh.cachedGenerateKeyContract. Timing may depend on the public domain, pointers, lengths and returned status, not otherwise on the random bytes or private scalar. +/// +/// It runs `vg_ffdh_generate_key_cached`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `out` must be valid for reads and writes of `out_len` bytes. +/// * `x` must be valid for reads and writes of `x_len` bytes. +/// * `y` must be valid for reads and writes of `y_len` bytes. /// * `domain` must be valid for reads of `domain_len` bytes. -/// * `x` must be valid for reads of `x_len` bytes. -/// * `peer` must be valid for reads of `peer_len` bytes. +/// * `rand` must be valid for reads of `rand_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `ctx` must be valid for reads of `8 * ctx_len` bytes. /// * `p_len` must be in 64..=1024. /// * `domain_len` must be `2 * p_len` or `3 * p_len`. -/// * `out_len` and `peer_len` must be `p_len`. -/// * `x_len` must be in 1..=`p_len`. -/// * For the result to be specified, `domain` must be one that `vg_ffdh_check_domain` accepted (returned 1 for); otherwise it is unspecified. -/// * For the result to be specified, `peer` must also be a value that `vg_ffdh_check_public` accepted for this domain. +/// * domain must be one that vg_ffdh_check_domain accepted. +/// * ctx_len must be generationPrecomputedWords(p_len). ctx must contain the unchanged words written by vg_ffdh_generate_key_precompute for these parameters. +/// * x_len, y_len and rand_len must be p_len. /// * `scratch_len` must be at least `16 * p_len + 32`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `domain`, `x`, `peer` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `domain`, `x`, `peer` and `scratch` may wrap around the end of the address space (no Rust object does). +/// * `x`, `y` and `scratch` must not overlap each other, `domain`, `rand`, `ctx` or the arguments on the stack (distinct Rust objects never do). +/// * None of `x`, `y`, `domain`, `rand`, `scratch` and `ctx` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ffdh_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, domain: *const u8, domain_len: usize, p_len: usize, x: *const u8, x_len: usize, peer: *const u8, peer_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_ffdh_generate_key_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, ctx: *const u64, ctx_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ffdh::vg_ffdh_exchange(out, out_len, domain, domain_len, p_len, x, x_len, peer, peer_len, scratch, scratch_len) } + unsafe { crate::ffdh::vg_ffdh_generate_key_cached(x, x_len, y, y_len, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, ctx, ctx_len) } +} + +/// Generate a finite-field Diffie–Hellman key pair using a reusable public five-tooth comb context and the existing vettedKeySearch rejection policy. Returns 1 with a key pair, 3 when a budget is exhausted, or 0 to request entropy. On 0, discard used bytes, preserve the suffix and extend it to state[3] bytes before calling again. On 0 and 3 both outputs are zero. Entropy consumption, retry budgets and statuses are unchanged. Contract: VG.Spec.Ffdh.cachedKeySearchContract. Timing may depend on the public domain, pointers, lengths, controller policy and sequence of attempt statuses, not otherwise on private scalar bytes. +/// +/// It runs `vg_ffdh_generate_key_search_cached`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `x` must be valid for reads and writes of `x_len` bytes. +/// * `y` must be valid for reads and writes of `y_len` bytes. +/// * `used` must be valid for reads and writes of 8 bytes. +/// * `domain` must be valid for reads of `domain_len` bytes. +/// * `rand` must be valid for reads of `rand_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `state` must be valid for reads and writes of 128 bytes. +/// * `ctx` must be valid for reads of `8 * ctx_len` bytes. +/// * `p_len` must be in 64..=1024. +/// * `domain_len` must be `2 * p_len` or `3 * p_len`. +/// * domain must be one that vg_ffdh_check_domain accepted. +/// * ctx_len must be generationPrecomputedWords(p_len). ctx must contain the unchanged words written by vg_ffdh_generate_key_precompute for these parameters. +/// * state must be initialized by vg_ffdh_key_generation_init for p_len, or be the state returned by this routine. +/// * x_len and y_len must be p_len. +/// * `scratch_len` must be at least `16 * p_len + 32`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `x`, `y`, `used`, `scratch` and `state` must not overlap each other, `domain`, `rand`, `ctx` or the arguments on the stack (distinct Rust objects never do). +/// * None of `x`, `y`, `used`, `domain`, `rand`, `scratch`, `state` and `ctx` may overlap the 64 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ffdh_generate_key_search_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, used: *mut [u64; 1], domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16], ctx: *const u64, ctx_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ffdh::vg_ffdh_generate_key_search_cached(x, x_len, y, y_len, used, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, state, ctx, ctx_len) } } /// Checks an imported finite-field Diffie–Hellman public value (SP 800-56A Rev. 3 §5.6.2.3.1, and §5.6.2.3.2 with q). With `domain` as `vg_ffdh_check_domain` takes it and y (`p_len` bytes, most significant first), returns 1 if `1 < y < p − 1` and, with q, `y^q mod p = 1`; otherwise 0. @@ -81,6 +134,36 @@ pub unsafe fn vg_ffdh_check_domain_auto(_dit: verified_garbage_cpu::Dit<'_>, dom unsafe { crate::ffdh::vg_ffdh_check_domain(domain, domain_len, p_len, scratch, scratch_len) } } +/// Finite-field Diffie–Hellman (SP 800-56A Rev. 3 §5.7.1.1, PKCS #3 §8). With `domain` as `vg_ffdh_check_domain` takes it, the private scalar x (`x_len` bytes) and the peer's public value y (`p_len` bytes), most significant first, computes `z = y^x mod p`; returns 0 and writes zeros if z is 0, 1 or `p − 1`, and otherwise returns 1 and writes z (`p_len` bytes, most significant first) to `out`. Applies no KDF. +/// +/// Contract: `VG.Spec.Ffdh.exchangeContract`. Timing may depend on the pointers, the lengths, the domain, y and the value returned, not otherwise on x; the exponentiation may run over all `x_len` bytes of x, leading zeros included. +/// +/// It runs `vg_ffdh_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `out` must be valid for reads and writes of `out_len` bytes. +/// * `domain` must be valid for reads of `domain_len` bytes. +/// * `x` must be valid for reads of `x_len` bytes. +/// * `peer` must be valid for reads of `peer_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `p_len` must be in 64..=1024. +/// * `domain_len` must be `2 * p_len` or `3 * p_len`. +/// * `out_len` and `peer_len` must be `p_len`. +/// * `x_len` must be in 1..=`p_len`. +/// * For the result to be specified, `domain` must be one that `vg_ffdh_check_domain` accepted (returned 1 for); otherwise it is unspecified. +/// * For the result to be specified, `peer` must also be a value that `vg_ffdh_check_public` accepted for this domain. +/// * `scratch_len` must be at least `16 * p_len + 32`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `out` and `scratch` must not overlap each other, `domain`, `x`, `peer` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `domain`, `x`, `peer` and `scratch` may wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ffdh_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, domain: *const u8, domain_len: usize, p_len: usize, x: *const u8, x_len: usize, peer: *const u8, peer_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ffdh::vg_ffdh_exchange(out, out_len, domain, domain_len, p_len, x, x_len, peer, peer_len, scratch, scratch_len) } +} + /// Checks an imported finite-field Diffie–Hellman key pair (SP 800-56A Rev. 3 §5.6.2.1.2 and §5.6.2.1.4). With `domain` as `vg_ffdh_check_domain` takes it, y (`p_len` bytes) and x (`x_len` bytes), most significant first: returns 2 if x is not in `1..q` (`1..p − 1` without q); 1 if y is a valid public value (as `vg_ffdh_check_public` checks it) and `y = g^x mod p`; 0 otherwise. /// /// Contract: `VG.Spec.Ffdh.checkPrivateContract`. Timing may depend on the pointers, the lengths, the domain, y and the value returned, not otherwise on x; the exponentiation may run over all `x_len` bytes of x, leading zeros included. @@ -137,32 +220,19 @@ pub unsafe fn vg_ffdh_derive_public_auto(_dit: verified_garbage_cpu::Dit<'_>, y: unsafe { crate::ffdh::vg_ffdh_derive_public(y, y_len, domain, domain_len, p_len, x, x_len, scratch, scratch_len) } } -/// One native finite-field Diffie–Hellman key-generation attempt using a reusable public five-tooth comb context. Takes p_len random bytes and implements the same vettedScalarOfOctets, keyStatus and output conventions as vg_ffdh_generate_key_vetted for checked parameters. The context's public maximum width fixes the schedule, including secret leading zeros. Contract: VG.Spec.Ffdh.cachedGenerateKeyContract. Timing may depend on the public domain, pointers, lengths and returned status, not otherwise on the random bytes or private scalar. +/// Initialize native finite-field Diffie–Hellman parameter generation. Validates 512..=8192 bits and generator 2 or 5. Returns 1 and initializes the controller's attempt limit, output width, entropy budget and first entropy request; returns 0 and clears those four words on invalid parameters. The remaining state words are private workspace. Contract: `VG.Spec.Ffdh.parameterInitContract`. /// -/// It runs `vg_ffdh_generate_key_cached`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ffdh_parameter_generation_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `x` must be valid for reads and writes of `x_len` bytes. -/// * `y` must be valid for reads and writes of `y_len` bytes. -/// * `domain` must be valid for reads of `domain_len` bytes. -/// * `rand` must be valid for reads of `rand_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `ctx` must be valid for reads of `8 * ctx_len` bytes. -/// * `p_len` must be in 64..=1024. -/// * `domain_len` must be `2 * p_len` or `3 * p_len`. -/// * domain must be one that vg_ffdh_check_domain accepted. -/// * ctx_len must be generationPrecomputedWords(p_len). ctx must contain the unchanged words written by vg_ffdh_generate_key_precompute for these parameters. -/// * x_len, y_len and rand_len must be p_len. -/// * `scratch_len` must be at least `16 * p_len + 32`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `x`, `y` and `scratch` must not overlap each other, `domain`, `rand`, `ctx` or the arguments on the stack (distinct Rust objects never do). -/// * None of `x`, `y`, `domain`, `rand`, `scratch` and `ctx` may wrap around the end of the address space (no Rust object does). +/// * `state` must be valid for reads and writes of 128 bytes. +/// * `state` must not wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ffdh_generate_key_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, ctx: *const u64, ctx_len: usize) -> u32 { +pub unsafe fn vg_ffdh_parameter_generation_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 16], bits: usize, generator: u32) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ffdh::vg_ffdh_generate_key_cached(x, x_len, y, y_len, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, ctx, ctx_len) } + unsafe { crate::ffdh::vg_ffdh_parameter_generation_init(state, bits, generator) } } /// Initialize native finite-field Diffie–Hellman key generation. Returns 1 for a domain width in 64..=1024 bytes, initializing the rejection budgets and first entropy request; returns 0 and clears the first four state words otherwise. The remaining state words are private workspace. Contract: `VG.Spec.Ffdh.keyInitContract`. @@ -203,73 +273,3 @@ pub unsafe fn vg_ffdh_generate_parameters_auto(_dit: verified_garbage_cpu::Dit<' // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ffdh::vg_ffdh_generate_parameters(out, out_len, used, bits, generator, rand, rand_len, scratch, scratch_len, state) } } - -/// Initialize a reusable public fixed-base context for finite-field Diffie–Hellman key generation. The checked domain is p, g and optional q, each p_len bytes. Writes generationPrecompute: three public metadata words followed by RSA's modulus/R-squared values and 32 five-tooth comb entries. Returns 1. Word 0 is generationScalarBits, word 1 is its byte length and word 2 is the comb row count. The context has no pointers and is independent of the arithmetic backend. Contract: VG.Spec.Ffdh.generationPrecomputeContract. Timing may depend on the public domain, pointers and lengths. -/// -/// It runs `vg_ffdh_generate_key_precompute`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `ctx` must be valid for reads and writes of `8 * ctx_len` bytes. -/// * `domain` must be valid for reads of `domain_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `p_len` must be in 64..=1024. -/// * `domain_len` must be `2 * p_len` or `3 * p_len`. -/// * domain must be one that vg_ffdh_check_domain accepted. -/// * ctx_len must be generationPrecomputedWords(p_len), namely 3 + 34 * ceil(p_len / 8). -/// * `scratch_len` must be at least `16 * p_len + 32`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `ctx` and `scratch` must not overlap each other or `domain` (distinct Rust objects never do). -/// * None of `ctx`, `domain` and `scratch` may wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ffdh_generate_key_precompute_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *mut u64, ctx_len: usize, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ffdh::vg_ffdh_generate_key_precompute(ctx, ctx_len, domain, domain_len, p_len, scratch, scratch_len) } -} - -/// Initialize native finite-field Diffie–Hellman parameter generation. Validates 512..=8192 bits and generator 2 or 5. Returns 1 and initializes the controller's attempt limit, output width, entropy budget and first entropy request; returns 0 and clears those four words on invalid parameters. The remaining state words are private workspace. Contract: `VG.Spec.Ffdh.parameterInitContract`. -/// -/// It runs `vg_ffdh_parameter_generation_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 128 bytes. -/// * `state` must not wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ffdh_parameter_generation_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 16], bits: usize, generator: u32) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ffdh::vg_ffdh_parameter_generation_init(state, bits, generator) } -} - -/// Generate a finite-field Diffie–Hellman key pair using a reusable public five-tooth comb context and the existing vettedKeySearch rejection policy. Returns 1 with a key pair, 3 when a budget is exhausted, or 0 to request entropy. On 0, discard used bytes, preserve the suffix and extend it to state[3] bytes before calling again. On 0 and 3 both outputs are zero. Entropy consumption, retry budgets and statuses are unchanged. Contract: VG.Spec.Ffdh.cachedKeySearchContract. Timing may depend on the public domain, pointers, lengths, controller policy and sequence of attempt statuses, not otherwise on private scalar bytes. -/// -/// It runs `vg_ffdh_generate_key_search_cached`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `x` must be valid for reads and writes of `x_len` bytes. -/// * `y` must be valid for reads and writes of `y_len` bytes. -/// * `used` must be valid for reads and writes of 8 bytes. -/// * `domain` must be valid for reads of `domain_len` bytes. -/// * `rand` must be valid for reads of `rand_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `state` must be valid for reads and writes of 128 bytes. -/// * `ctx` must be valid for reads of `8 * ctx_len` bytes. -/// * `p_len` must be in 64..=1024. -/// * `domain_len` must be `2 * p_len` or `3 * p_len`. -/// * domain must be one that vg_ffdh_check_domain accepted. -/// * ctx_len must be generationPrecomputedWords(p_len). ctx must contain the unchanged words written by vg_ffdh_generate_key_precompute for these parameters. -/// * state must be initialized by vg_ffdh_key_generation_init for p_len, or be the state returned by this routine. -/// * x_len and y_len must be p_len. -/// * `scratch_len` must be at least `16 * p_len + 32`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `x`, `y`, `used`, `scratch` and `state` must not overlap each other, `domain`, `rand`, `ctx` or the arguments on the stack (distinct Rust objects never do). -/// * None of `x`, `y`, `used`, `domain`, `rand`, `scratch`, `state` and `ctx` may overlap the 64 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ffdh_generate_key_search_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, used: *mut [u64; 1], domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16], ctx: *const u64, ctx_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ffdh::vg_ffdh_generate_key_search_cached(x, x_len, y, y_len, used, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, state, ctx, ctx_len) } -} diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/fn_tables.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/fn_tables.rs index cb559fc5d10..d6c821a0ed6 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/fn_tables.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/fn_tables.rs @@ -2,43 +2,115 @@ //! The dispatchers of the tables of functions for `aarch64` //! (`crate::fn_tables`, `VG.Rust.tableDispatcher`). -/// The table of functions `VG_HASH_MD5` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hash_md5_auto() -> &'static crate::fn_tables::Hash { - &crate::fn_tables::VG_HASH_MD5 -} - /// The table of functions `VG_HMAC_MD5` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] pub fn vg_hmac_md5_auto() -> &'static crate::fn_tables::Hmac { &crate::fn_tables::VG_HMAC_MD5 } -/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA1`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HASH_SHA1_SHA2` if the CPU supports its features (`VG_HASH_SHA1_SHA2_FEATURES`); or else `VG_HASH_SHA1`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA1`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HMAC_SHA1_SHA2` if the CPU supports its features (`VG_HMAC_SHA1_SHA2_FEATURES`); or else `VG_HMAC_SHA1`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] -pub fn vg_hash_sha1_auto() -> &'static crate::fn_tables::Hash { +pub fn vg_hmac_sha1_auto() -> &'static crate::fn_tables::Hmac { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HASH_SHA1_SHA2_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HASH_SHA1_SHA2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA1_SHA2_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA1_SHA2_FEATURES) { - return &crate::fn_tables::VG_HASH_SHA1_SHA2; + return &crate::fn_tables::VG_HMAC_SHA1_SHA2; } - &crate::fn_tables::VG_HASH_SHA1 + &crate::fn_tables::VG_HMAC_SHA1 } -/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA1`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HMAC_SHA1_SHA2` if the CPU supports its features (`VG_HMAC_SHA1_SHA2_FEATURES`); or else `VG_HMAC_SHA1`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HMAC_SHA224_SHA2` if the CPU supports its features (`VG_HMAC_SHA224_SHA2_FEATURES`); or else `VG_HMAC_SHA224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] -pub fn vg_hmac_sha1_auto() -> &'static crate::fn_tables::Hmac { +pub fn vg_hmac_sha224_auto() -> &'static crate::fn_tables::Hmac { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA1_SHA2_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA1_SHA2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA224_SHA2_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA224_SHA2_FEATURES) { - return &crate::fn_tables::VG_HMAC_SHA1_SHA2; + return &crate::fn_tables::VG_HMAC_SHA224_SHA2; } - &crate::fn_tables::VG_HMAC_SHA1 + &crate::fn_tables::VG_HMAC_SHA224 +} + +/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HMAC_SHA256_SHA2` if the CPU supports its features (`VG_HMAC_SHA256_SHA2_FEATURES`); or else `VG_HMAC_SHA256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +#[inline] +pub fn vg_hmac_sha256_auto() -> &'static crate::fn_tables::Hmac { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA256_SHA2_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA256_SHA2_FEATURES) + { + return &crate::fn_tables::VG_HMAC_SHA256_SHA2; + } + &crate::fn_tables::VG_HMAC_SHA256 +} + +/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA384`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HMAC_SHA384_SHA512` if the CPU supports its features (`VG_HMAC_SHA384_SHA512_FEATURES`); or else `VG_HMAC_SHA384`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +#[inline] +pub fn vg_hmac_sha384_auto() -> &'static crate::fn_tables::Hmac { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA384_SHA512_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA384_SHA512_FEATURES) + { + return &crate::fn_tables::VG_HMAC_SHA384_SHA512; + } + &crate::fn_tables::VG_HMAC_SHA384 +} + +/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA512`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HMAC_SHA512_SHA512` if the CPU supports its features (`VG_HMAC_SHA512_SHA512_FEATURES`); or else `VG_HMAC_SHA512`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +#[inline] +pub fn vg_hmac_sha512_auto() -> &'static crate::fn_tables::Hmac { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA512_SHA512_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA512_SHA512_FEATURES) + { + return &crate::fn_tables::VG_HMAC_SHA512_SHA512; + } + &crate::fn_tables::VG_HMAC_SHA512 +} + +/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA512_224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HMAC_SHA512_224_SHA512` if the CPU supports its features (`VG_HMAC_SHA512_224_SHA512_FEATURES`); or else `VG_HMAC_SHA512_224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +#[inline] +pub fn vg_hmac_sha512_224_auto() -> &'static crate::fn_tables::Hmac { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA512_224_SHA512_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA512_224_SHA512_FEATURES) + { + return &crate::fn_tables::VG_HMAC_SHA512_224_SHA512; + } + &crate::fn_tables::VG_HMAC_SHA512_224 +} + +/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA512_256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HMAC_SHA512_256_SHA512` if the CPU supports its features (`VG_HMAC_SHA512_256_SHA512_FEATURES`); or else `VG_HMAC_SHA512_256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +#[inline] +pub fn vg_hmac_sha512_256_auto() -> &'static crate::fn_tables::Hmac { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA512_256_SHA512_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA512_256_SHA512_FEATURES) + { + return &crate::fn_tables::VG_HMAC_SHA512_256_SHA512; + } + &crate::fn_tables::VG_HMAC_SHA512_256 +} + +/// The table of functions `VG_HASH_MD5` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hash_md5_auto() -> &'static crate::fn_tables::Hash { + &crate::fn_tables::VG_HASH_MD5 +} + +/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA1`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HASH_SHA1_SHA2` if the CPU supports its features (`VG_HASH_SHA1_SHA2_FEATURES`); or else `VG_HASH_SHA1`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +#[inline] +pub fn vg_hash_sha1_auto() -> &'static crate::fn_tables::Hash { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HASH_SHA1_SHA2_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HASH_SHA1_SHA2_FEATURES) + { + return &crate::fn_tables::VG_HASH_SHA1_SHA2; + } + &crate::fn_tables::VG_HASH_SHA1 } -/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HASH_SHA224_SHA2` if the CPU supports its features (`VG_HASH_SHA224_SHA2_FEATURES`); or else `VG_HASH_SHA224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HASH_SHA224_SHA2` if the CPU supports its features (`VG_HASH_SHA224_SHA2_FEATURES`); or else `VG_HASH_SHA224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_hash_sha224_auto() -> &'static crate::fn_tables::Hash { let cpu = verified_garbage_cpu::detected(); @@ -50,7 +122,7 @@ pub fn vg_hash_sha224_auto() -> &'static crate::fn_tables::Hash { &crate::fn_tables::VG_HASH_SHA224 } -/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HASH_SHA256_SHA2` if the CPU supports its features (`VG_HASH_SHA256_SHA2_FEATURES`); or else `VG_HASH_SHA256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HASH_SHA256_SHA2` if the CPU supports its features (`VG_HASH_SHA256_SHA2_FEATURES`); or else `VG_HASH_SHA256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_hash_sha256_auto() -> &'static crate::fn_tables::Hash { let cpu = verified_garbage_cpu::detected(); @@ -62,7 +134,7 @@ pub fn vg_hash_sha256_auto() -> &'static crate::fn_tables::Hash { &crate::fn_tables::VG_HASH_SHA256 } -/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA384`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HASH_SHA384_SHA512` if the CPU supports its features (`VG_HASH_SHA384_SHA512_FEATURES`); or else `VG_HASH_SHA384`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA384`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HASH_SHA384_SHA512` if the CPU supports its features (`VG_HASH_SHA384_SHA512_FEATURES`); or else `VG_HASH_SHA384`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_hash_sha384_auto() -> &'static crate::fn_tables::Hash { let cpu = verified_garbage_cpu::detected(); @@ -74,7 +146,7 @@ pub fn vg_hash_sha384_auto() -> &'static crate::fn_tables::Hash { &crate::fn_tables::VG_HASH_SHA384 } -/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA512`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HASH_SHA512_SHA512` if the CPU supports its features (`VG_HASH_SHA512_SHA512_FEATURES`); or else `VG_HASH_SHA512`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA512`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HASH_SHA512_SHA512` if the CPU supports its features (`VG_HASH_SHA512_SHA512_FEATURES`); or else `VG_HASH_SHA512`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_hash_sha512_auto() -> &'static crate::fn_tables::Hash { let cpu = verified_garbage_cpu::detected(); @@ -86,52 +158,28 @@ pub fn vg_hash_sha512_auto() -> &'static crate::fn_tables::Hash { &crate::fn_tables::VG_HASH_SHA512 } -/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HMAC_SHA224_SHA2` if the CPU supports its features (`VG_HMAC_SHA224_SHA2_FEATURES`); or else `VG_HMAC_SHA224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. -#[inline] -pub fn vg_hmac_sha224_auto() -> &'static crate::fn_tables::Hmac { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA224_SHA2_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA224_SHA2_FEATURES) - { - return &crate::fn_tables::VG_HMAC_SHA224_SHA2; - } - &crate::fn_tables::VG_HMAC_SHA224 -} - -/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HMAC_SHA256_SHA2` if the CPU supports its features (`VG_HMAC_SHA256_SHA2_FEATURES`); or else `VG_HMAC_SHA256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. -#[inline] -pub fn vg_hmac_sha256_auto() -> &'static crate::fn_tables::Hmac { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA256_SHA2_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA256_SHA2_FEATURES) - { - return &crate::fn_tables::VG_HMAC_SHA256_SHA2; - } - &crate::fn_tables::VG_HMAC_SHA256 -} - -/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA384`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HMAC_SHA384_SHA512` if the CPU supports its features (`VG_HMAC_SHA384_SHA512_FEATURES`); or else `VG_HMAC_SHA384`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA512_224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HASH_SHA512_224_SHA512` if the CPU supports its features (`VG_HASH_SHA512_224_SHA512_FEATURES`); or else `VG_HASH_SHA512_224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] -pub fn vg_hmac_sha384_auto() -> &'static crate::fn_tables::Hmac { +pub fn vg_hash_sha512_224_auto() -> &'static crate::fn_tables::Hash { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA384_SHA512_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA384_SHA512_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HASH_SHA512_224_SHA512_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HASH_SHA512_224_SHA512_FEATURES) { - return &crate::fn_tables::VG_HMAC_SHA384_SHA512; + return &crate::fn_tables::VG_HASH_SHA512_224_SHA512; } - &crate::fn_tables::VG_HMAC_SHA384 + &crate::fn_tables::VG_HASH_SHA512_224 } -/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA512`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HMAC_SHA512_SHA512` if the CPU supports its features (`VG_HMAC_SHA512_SHA512_FEATURES`); or else `VG_HMAC_SHA512`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA512_256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HASH_SHA512_256_SHA512` if the CPU supports its features (`VG_HASH_SHA512_256_SHA512_FEATURES`); or else `VG_HASH_SHA512_256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] -pub fn vg_hmac_sha512_auto() -> &'static crate::fn_tables::Hmac { +pub fn vg_hash_sha512_256_auto() -> &'static crate::fn_tables::Hash { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA512_SHA512_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA512_SHA512_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HASH_SHA512_256_SHA512_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HASH_SHA512_256_SHA512_FEATURES) { - return &crate::fn_tables::VG_HMAC_SHA512_SHA512; + return &crate::fn_tables::VG_HASH_SHA512_256_SHA512; } - &crate::fn_tables::VG_HMAC_SHA512 + &crate::fn_tables::VG_HASH_SHA512_256 } /// The table of functions `VG_MD_HASH_MD5` of the interface `MdHash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). @@ -140,7 +188,7 @@ pub fn vg_md_hash_md5_auto() -> &'static crate::fn_tables::MdHash { &crate::fn_tables::VG_MD_HASH_MD5 } -/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA1`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_MD_HASH_SHA1_SHA2` if the CPU supports its features (`VG_MD_HASH_SHA1_SHA2_FEATURES`); or else `VG_MD_HASH_SHA1`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA1`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_MD_HASH_SHA1_SHA2` if the CPU supports its features (`VG_MD_HASH_SHA1_SHA2_FEATURES`); or else `VG_MD_HASH_SHA1`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_md_hash_sha1_auto() -> &'static crate::fn_tables::MdHash { let cpu = verified_garbage_cpu::detected(); @@ -152,7 +200,7 @@ pub fn vg_md_hash_sha1_auto() -> &'static crate::fn_tables::MdHash { &crate::fn_tables::VG_MD_HASH_SHA1 } -/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_MD_HASH_SHA224_SHA2` if the CPU supports its features (`VG_MD_HASH_SHA224_SHA2_FEATURES`); or else `VG_MD_HASH_SHA224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_MD_HASH_SHA224_SHA2` if the CPU supports its features (`VG_MD_HASH_SHA224_SHA2_FEATURES`); or else `VG_MD_HASH_SHA224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_md_hash_sha224_auto() -> &'static crate::fn_tables::MdHash { let cpu = verified_garbage_cpu::detected(); @@ -164,7 +212,7 @@ pub fn vg_md_hash_sha224_auto() -> &'static crate::fn_tables::MdHash { &crate::fn_tables::VG_MD_HASH_SHA224 } -/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_MD_HASH_SHA256_SHA2` if the CPU supports its features (`VG_MD_HASH_SHA256_SHA2_FEATURES`); or else `VG_MD_HASH_SHA256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_MD_HASH_SHA256_SHA2` if the CPU supports its features (`VG_MD_HASH_SHA256_SHA2_FEATURES`); or else `VG_MD_HASH_SHA256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_md_hash_sha256_auto() -> &'static crate::fn_tables::MdHash { let cpu = verified_garbage_cpu::detected(); @@ -176,7 +224,7 @@ pub fn vg_md_hash_sha256_auto() -> &'static crate::fn_tables::MdHash { &crate::fn_tables::VG_MD_HASH_SHA256 } -/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA384`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_MD_HASH_SHA384_SHA512` if the CPU supports its features (`VG_MD_HASH_SHA384_SHA512_FEATURES`); or else `VG_MD_HASH_SHA384`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA384`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_MD_HASH_SHA384_SHA512` if the CPU supports its features (`VG_MD_HASH_SHA384_SHA512_FEATURES`); or else `VG_MD_HASH_SHA384`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_md_hash_sha384_auto() -> &'static crate::fn_tables::MdHash { let cpu = verified_garbage_cpu::detected(); @@ -188,7 +236,7 @@ pub fn vg_md_hash_sha384_auto() -> &'static crate::fn_tables::MdHash { &crate::fn_tables::VG_MD_HASH_SHA384 } -/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA512`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_MD_HASH_SHA512_SHA512` if the CPU supports its features (`VG_MD_HASH_SHA512_SHA512_FEATURES`); or else `VG_MD_HASH_SHA512`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA512`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_MD_HASH_SHA512_SHA512` if the CPU supports its features (`VG_MD_HASH_SHA512_SHA512_FEATURES`); or else `VG_MD_HASH_SHA512`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_md_hash_sha512_auto() -> &'static crate::fn_tables::MdHash { let cpu = verified_garbage_cpu::detected(); @@ -200,55 +248,7 @@ pub fn vg_md_hash_sha512_auto() -> &'static crate::fn_tables::MdHash { &crate::fn_tables::VG_MD_HASH_SHA512 } -/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA512_224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HASH_SHA512_224_SHA512` if the CPU supports its features (`VG_HASH_SHA512_224_SHA512_FEATURES`); or else `VG_HASH_SHA512_224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. -#[inline] -pub fn vg_hash_sha512_224_auto() -> &'static crate::fn_tables::Hash { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HASH_SHA512_224_SHA512_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HASH_SHA512_224_SHA512_FEATURES) - { - return &crate::fn_tables::VG_HASH_SHA512_224_SHA512; - } - &crate::fn_tables::VG_HASH_SHA512_224 -} - -/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA512_256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HASH_SHA512_256_SHA512` if the CPU supports its features (`VG_HASH_SHA512_256_SHA512_FEATURES`); or else `VG_HASH_SHA512_256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. -#[inline] -pub fn vg_hash_sha512_256_auto() -> &'static crate::fn_tables::Hash { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HASH_SHA512_256_SHA512_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HASH_SHA512_256_SHA512_FEATURES) - { - return &crate::fn_tables::VG_HASH_SHA512_256_SHA512; - } - &crate::fn_tables::VG_HASH_SHA512_256 -} - -/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA512_224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HMAC_SHA512_224_SHA512` if the CPU supports its features (`VG_HMAC_SHA512_224_SHA512_FEATURES`); or else `VG_HMAC_SHA512_224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. -#[inline] -pub fn vg_hmac_sha512_224_auto() -> &'static crate::fn_tables::Hmac { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA512_224_SHA512_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA512_224_SHA512_FEATURES) - { - return &crate::fn_tables::VG_HMAC_SHA512_224_SHA512; - } - &crate::fn_tables::VG_HMAC_SHA512_224 -} - -/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA512_256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HMAC_SHA512_256_SHA512` if the CPU supports its features (`VG_HMAC_SHA512_256_SHA512_FEATURES`); or else `VG_HMAC_SHA512_256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. -#[inline] -pub fn vg_hmac_sha512_256_auto() -> &'static crate::fn_tables::Hmac { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA512_256_SHA512_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA512_256_SHA512_FEATURES) - { - return &crate::fn_tables::VG_HMAC_SHA512_256_SHA512; - } - &crate::fn_tables::VG_HMAC_SHA512_256 -} - -/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA512_224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_MD_HASH_SHA512_224_SHA512` if the CPU supports its features (`VG_MD_HASH_SHA512_224_SHA512_FEATURES`); or else `VG_MD_HASH_SHA512_224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA512_224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_MD_HASH_SHA512_224_SHA512` if the CPU supports its features (`VG_MD_HASH_SHA512_224_SHA512_FEATURES`); or else `VG_MD_HASH_SHA512_224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_md_hash_sha512_224_auto() -> &'static crate::fn_tables::MdHash { let cpu = verified_garbage_cpu::detected(); @@ -260,7 +260,7 @@ pub fn vg_md_hash_sha512_224_auto() -> &'static crate::fn_tables::MdHash { &crate::fn_tables::VG_MD_HASH_SHA512_224 } -/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA512_256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_MD_HASH_SHA512_256_SHA512` if the CPU supports its features (`VG_MD_HASH_SHA512_256_SHA512_FEATURES`); or else `VG_MD_HASH_SHA512_256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA512_256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_MD_HASH_SHA512_256_SHA512` if the CPU supports its features (`VG_MD_HASH_SHA512_256_SHA512_FEATURES`); or else `VG_MD_HASH_SHA512_256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_md_hash_sha512_256_auto() -> &'static crate::fn_tables::MdHash { let cpu = verified_garbage_cpu::detected(); diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/gcm.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/gcm.rs index c22206fcbdd..5bf3589dde9 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/gcm.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/gcm.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Gcm.initContract`. The key context is `VG.Spec.Gcm.KeyRepr`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_init_aes` if the CPU supports its features (`VG_AES_GCM_INIT_AES_FEATURES`); or else `vg_aes_gcm_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_init_aes` if the CPU supports its features (`VG_AES_GCM_INIT_AES_FEATURES`); or else `vg_aes_gcm_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -57,7 +57,7 @@ pub unsafe fn vg_aes_gcm_init_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *co /// /// Contract: `VG.Spec.Gcm.sealContract`. Constant time: only the pointers, `rounds` and the lengths may affect timing, not the key context, the nonce, the additional data or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_seal_aes` if the CPU supports its features (`VG_AES_GCM_SEAL_AES_FEATURES`); or else `vg_aes_gcm_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_seal_aes` if the CPU supports its features (`VG_AES_GCM_SEAL_AES_FEATURES`); or else `vg_aes_gcm_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -111,7 +111,7 @@ pub unsafe fn vg_aes_gcm_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *co /// /// Contract: `VG.Spec.Gcm.openContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the additional data, the data or the tag. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_open_aes` if the CPU supports its features (`VG_AES_GCM_OPEN_AES_FEATURES`); or else `vg_aes_gcm_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_open_aes` if the CPU supports its features (`VG_AES_GCM_OPEN_AES_FEATURES`); or else `vg_aes_gcm_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -159,52 +159,47 @@ pub unsafe fn vg_aes_gcm_open_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *co unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, nonce, nonce_len, aad, aad_len, data, len, tag, tag_len) } } -/// AES-GCM authenticated decryption (NIST SP 800-38D §7.2, GCM-AD), out of place: with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds and the received tag the `tag_len` bytes at `tag`, returns 1 if `tag_len` is between 4 and 16 and the tag is the first `tag_len` bytes of that of the `len` bytes of ciphertext at `src` and the `aad_len` bytes of additional data at `aad`, under the IV the `nonce_len` bytes at `nonce`, having then written the plaintext to the `dst_len = len` bytes at `dst`; otherwise returns 0, having written zeros to them: `vg_aes_gcm_open` with the ciphertext read from `src` and the plaintext written to `dst`. `src` is not written, and `dst` is a buffer of its own: it overlaps no other buffer, `src` included. The tags are compared without a branch. +/// Starts an incremental AES-GCM encryption or decryption (NIST SP 800-38D §7): with the key context `*ctx` that `vg_aes_gcm_init` wrote, makes the streaming state `*state` represent the message with the IV the `nonce_len` bytes at `nonce`, and no additional data or text yet. Continue with `vg_aes_gcm_stream_aad`, then `vg_aes_gcm_stream_encrypt` or `vg_aes_gcm_stream_decrypt`, then `vg_aes_gcm_stream_finish` or `vg_aes_gcm_stream_verify`, with the same key context. /// -/// The function checks no other length: GCM requires a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of ciphertext and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// The function checks no length: GCM requires a nonce of 1 to `2^61 - 1` bytes (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.openToContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the additional data, the data or the tag. +/// Contract: `VG.Spec.Gcm.streamInitContract`. The streaming state is `J₀`, the GHASH accumulator, a partial block, the next counter block and a keystream block (`VG.Spec.Gcm.StreamRepr`). Constant time: only the pointers and `nonce_len` may affect timing, not the key context or the nonce. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_open_to_aes` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_AES_FEATURES`); or else `vg_aes_gcm_open_to`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_stream_init_aes` if the CPU supports its features (`VG_AES_GCM_STREAM_INIT_AES_FEATURES`); or else `vg_aes_gcm_stream_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. /// * `nonce` must be valid for reads of `nonce_len` bytes. -/// * `aad` must be valid for reads of `aad_len` bytes. -/// * `src` must be valid for reads of `len` bytes. -/// * `dst` must be valid for reads and writes of `dst_len` bytes. -/// * `tag` must be valid for reads of `tag_len` bytes. -/// * `rounds` must be 10, 12 or 14. -/// * `dst_len` must equal `len`. -/// * `dst` must not overlap `ctx`, `nonce`, `aad`, `src`, `tag` or the arguments on the stack (distinct Rust objects never do). -/// * None of `ctx`, `nonce`, `aad`, `src`, `dst` and `tag` may overlap the 2608 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must be valid for reads and writes of 80 bytes. +/// * `state` must not overlap `ctx` or `nonce` (distinct Rust objects never do). +/// * None of `ctx`, `nonce` and `state` may overlap the 2560 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_open_to_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const u8, len: usize, dst: *mut u8, dst_len: usize, tag: *const u8, tag_len: usize) -> u32 { +pub unsafe fn vg_aes_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const u8, len: usize, dst: *mut u8, dst_len: usize, tag: *const u8, tag_len: usize) -> u32; + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_OPEN_TO_AES_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_OPEN_TO_AES_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_INIT_AES_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_INIT_AES_FEATURES) { - return crate::gcm::vg_aes_gcm_open_to_aes; + return crate::gcm::vg_aes_gcm_stream_init_aes; } - crate::gcm::vg_aes_gcm_open_to + crate::gcm::vg_aes_gcm_stream_init } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const u8, len: usize, dst: *mut u8, dst_len: usize, tag: *const u8, tag_len: usize) -> u32 { + unsafe extern "C" fn first(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, rounds, nonce, nonce_len, aad, aad_len, src, len, dst, dst_len, tag, tag_len) } + unsafe { f(ctx, nonce, nonce_len, state) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -212,14 +207,14 @@ pub unsafe fn vg_aes_gcm_open_to_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, nonce, nonce_len, aad, aad_len, src, len, dst, dst_len, tag, tag_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, nonce, nonce_len, state) } } /// Absorbs additional data into an incremental AES-GCM encryption or decryption: with the key context `*ctx`, if the streaming state `*state` represents a message with `aad_len` bytes (modulo 2⁶⁴) of additional data and no text yet, it then represents that message with the `len` bytes at `data` appended to its additional data. All of the additional data comes before the text (§7.1 step 5): after text, the state represents no message. /// /// Contract: `VG.Spec.Gcm.streamAadContract`. Constant time: only the pointers, `aad_len` and `len` may affect timing, not the key context, the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_stream_aad_aes` if the CPU supports its features (`VG_AES_GCM_STREAM_AAD_AES_FEATURES`); or else `vg_aes_gcm_stream_aad`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_stream_aad_aes` if the CPU supports its features (`VG_AES_GCM_STREAM_AAD_AES_FEATURES`); or else `vg_aes_gcm_stream_aad`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -264,47 +259,48 @@ pub unsafe fn vg_aes_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ct unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, data, len) } } -/// Starts an incremental AES-GCM encryption or decryption (NIST SP 800-38D §7): with the key context `*ctx` that `vg_aes_gcm_init` wrote, makes the streaming state `*state` represent the message with the IV the `nonce_len` bytes at `nonce`, and no additional data or text yet. Continue with `vg_aes_gcm_stream_aad`, then `vg_aes_gcm_stream_encrypt` or `vg_aes_gcm_stream_decrypt`, then `vg_aes_gcm_stream_finish` or `vg_aes_gcm_stream_verify`, with the same key context. +/// Encrypts the next piece of the text of an incremental AES-GCM encryption (NIST SP 800-38D §7.1): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and the ciphertext of exactly `text_len` bytes of plaintext so far, encrypts the `len` bytes at `data` in place, as the continuation of that plaintext, and the state then represents the message with them appended. /// -/// The function checks no length: GCM requires a nonce of 1 to `2^61 - 1` bytes (§5.2.1.1), which the caller must check. +/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.streamInitContract`. The streaming state is `J₀`, the GHASH accumulator, a partial block, the next counter block and a keystream block (`VG.Spec.Gcm.StreamRepr`). Constant time: only the pointers and `nonce_len` may affect timing, not the key context or the nonce. +/// Contract: `VG.Spec.Gcm.streamEncryptContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_stream_init_aes` if the CPU supports its features (`VG_AES_GCM_STREAM_INIT_AES_FEATURES`); or else `vg_aes_gcm_stream_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_stream_encrypt_aes` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_AES_FEATURES`); or else `vg_aes_gcm_stream_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `nonce` must be valid for reads of `nonce_len` bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `state` must not overlap `ctx` or `nonce` (distinct Rust objects never do). -/// * None of `ctx`, `nonce` and `state` may overlap the 2560 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads and writes of `len` bytes. +/// * `rounds` must be 10, 12 or 14. +/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the 2560 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { +pub unsafe fn vg_aes_gcm_stream_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]); + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_INIT_AES_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_INIT_AES_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_AES_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_AES_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_init_aes; + return crate::gcm::vg_aes_gcm_stream_encrypt_aes; } - crate::gcm::vg_aes_gcm_stream_init + crate::gcm::vg_aes_gcm_stream_encrypt } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { + unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, nonce, nonce_len, state) } + unsafe { f(ctx, rounds, state, aad_len, text_len, data, len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -312,103 +308,51 @@ pub unsafe fn vg_aes_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, c // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, nonce, nonce_len, state) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, state, aad_len, text_len, data, len) } } -/// AES-GCM authenticated encryption (NIST SP 800-38D §7.1, GCM-AE, with a 128-bit tag), out of place, of a plaintext in pieces: with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, encrypts the concatenation of the `src_count` slices that `src` lists (each an address and a length, in bytes), under the IV the `nonce_len` bytes at `nonce`, writes the ciphertext to the `len` bytes at `dst`, and writes the tag of the ciphertext and the `aad_len` bytes of additional data at `aad` to `*tag`: `vg_aes_gcm_seal` with the plaintext gathered from `src`. A shorter tag is the first bytes of this one. +/// Decrypts the next piece of the text of an incremental AES-GCM decryption (NIST SP 800-38D §7.2): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext so far, decrypts the `len` bytes of ciphertext at `data` in place, as the continuation of that ciphertext, and the state then represents the message with them appended. The plaintext is not authenticated until `vg_aes_gcm_stream_verify` has returned 1. /// -/// The function checks no length. GCM is secure only for a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of data and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must ensure; and a nonce must never be used twice with the same key. +/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.sealGatherContract`. Constant time: only the pointers, `rounds`, the lengths, `src_count` and where the slices are (their addresses and lengths) may affect timing, not the key context, the nonce, the additional data or the data. +/// Contract: `VG.Spec.Gcm.streamDecryptContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_seal_gather_aes` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_AES_FEATURES`); or else `vg_aes_gcm_seal_gather`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_stream_decrypt_aes` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_AES_FEATURES`); or else `vg_aes_gcm_stream_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `nonce` must be valid for reads of `nonce_len` bytes. -/// * `aad` must be valid for reads of `aad_len` bytes. -/// * `src` must be valid for reads of `2 * size_of::() * src_count` bytes, and each slice it lists for reads of its length in bytes. -/// * `dst` must be valid for reads and writes of `len` bytes. -/// * `tag` must be valid for reads and writes of 16 bytes. +/// * `state` must be valid for reads and writes of 80 bytes. +/// * `data` must be valid for reads and writes of `len` bytes. /// * `rounds` must be 10, 12 or 14. -/// * The slices `src` lists must be `len` bytes long in all. -/// * `dst` and `tag` must not overlap each other, `ctx`, `nonce`, `aad`, `src`, the slices `src` lists or the arguments on the stack (distinct Rust objects never do). -/// * None of `ctx`, `nonce`, `aad`, `src`, the slices `src` lists, `dst` and `tag` may overlap the 2592 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_aes_gcm_seal_gather_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const [usize; 2], src_count: usize, dst: *mut u8, len: usize, tag: *mut [u8; 16]) { - /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const [usize; 2], src_count: usize, dst: *mut u8, len: usize, tag: *mut [u8; 16]); - /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only - /// features the CPU has. - #[cold] - fn select() -> Impl { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_SEAL_GATHER_AES_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_SEAL_GATHER_AES_FEATURES) - { - return crate::gcm::vg_aes_gcm_seal_gather_aes; - } - crate::gcm::vg_aes_gcm_seal_gather - } - /// The first call: chooses the implementation, records it in `CHOSEN` for the calls - /// after it, and runs it. - #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const [usize; 2], src_count: usize, dst: *mut u8, len: usize, tag: *mut [u8; 16]) { - let f = select(); - CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); - // SAFETY: the caller's obligations, which are those of every implementation, `f` - // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, rounds, nonce, nonce_len, aad, aad_len, src, src_count, dst, len, tag) } - } - /// The function to call: `first`, until it records the implementation it chose. - static CHOSEN: core::sync::atomic::AtomicPtr<()> = - core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); - // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, - // or an implementation `select` returned, which needs only features the CPU has. Calling - // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, nonce, nonce_len, aad, aad_len, src, src_count, dst, len, tag) } -} - -/// Writes to `*pw` the 8 prepared powers of the hash subkey `H` of the key context `*ctx` of `vg_aes_gcm_init` (its bytes 240 to 255, `VG.Spec.Gcm.ctxH`): at byte `32*k`, for `k` from 0 to 3, `preparedPower(H^(2*k+2))` then `preparedPower(H^(2*k+1))`, as two little-endian 128-bit words (`VG.Spec.Gcm.Powers8Repr`, `VG.Spec.Gcm.preparedPower`), as the `*_with_powers` functions require for that key context. It reads only `*ctx` and writes only `*pw`. The key context and the powers are secret: only the pointers may affect timing. -/// -/// Contract: `VG.Spec.Gcm.storePowersContract`. -/// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_store_powers_aes` if the CPU supports its features (`VG_AES_GCM_STORE_POWERS_AES_FEATURES`); or else `vg_aes_gcm_store_powers`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. -/// -/// # Safety -/// -/// * `ctx` must be valid for reads of 256 bytes. -/// * `pw` must be valid for reads and writes of 128 bytes. -/// * `pw` must not overlap `ctx` (distinct Rust objects never do). -/// * Neither `ctx` nor `pw` may wrap around the end of the address space (no Rust object does). +/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the 2560 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_store_powers_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], pw: *mut [u64; 16]) { +pub unsafe fn vg_aes_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], pw: *mut [u64; 16]); + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STORE_POWERS_AES_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STORE_POWERS_AES_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_AES_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_AES_FEATURES) { - return crate::gcm::vg_aes_gcm_store_powers_aes; + return crate::gcm::vg_aes_gcm_stream_decrypt_aes; } - crate::gcm::vg_aes_gcm_store_powers + crate::gcm::vg_aes_gcm_stream_decrypt } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], pw: *mut [u64; 16]) { + unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, pw) } + unsafe { f(ctx, rounds, state, aad_len, text_len, data, len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -416,14 +360,14 @@ pub unsafe fn vg_aes_gcm_store_powers_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, pw) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, state, aad_len, text_len, data, len) } } /// Finishes an incremental AES-GCM encryption or decryption (NIST SP 800-38D §7.1 steps 5–6, §7.2 steps 6–7): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, writes its 128-bit tag to `*tag`. A shorter tag is the first bytes of this one; to check a received tag, use `vg_aes_gcm_stream_verify`. /// /// Contract: `VG.Spec.Gcm.streamFinishContract`. Constant time: only the pointers, `rounds`, `aad_len` and `text_len` may affect timing, not the key context or the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_stream_finish_aes` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_AES_FEATURES`); or else `vg_aes_gcm_stream_finish`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_stream_finish_aes` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_AES_FEATURES`); or else `vg_aes_gcm_stream_finish`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -474,7 +418,7 @@ pub unsafe fn vg_aes_gcm_stream_finish_auto(_dit: verified_garbage_cpu::Dit<'_>, /// /// Contract: `VG.Spec.Gcm.streamVerifyContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `tag_len` may affect timing, not the key context, the state or the tags. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_stream_verify_aes` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_AES_FEATURES`); or else `vg_aes_gcm_stream_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_stream_verify_aes` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_AES_FEATURES`); or else `vg_aes_gcm_stream_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -521,48 +465,52 @@ pub unsafe fn vg_aes_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, state, aad_len, text_len, tag, tag_len) } } -/// Encrypts the next piece of the text of an incremental AES-GCM encryption (NIST SP 800-38D §7.1): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and the ciphertext of exactly `text_len` bytes of plaintext so far, encrypts the `len` bytes at `data` in place, as the continuation of that plaintext, and the state then represents the message with them appended. +/// AES-GCM authenticated encryption (NIST SP 800-38D §7.1, GCM-AE, with a 128-bit tag), out of place, of a plaintext in pieces: with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, encrypts the concatenation of the `src_count` slices that `src` lists (each an address and a length, in bytes), under the IV the `nonce_len` bytes at `nonce`, writes the ciphertext to the `len` bytes at `dst`, and writes the tag of the ciphertext and the `aad_len` bytes of additional data at `aad` to `*tag`: `vg_aes_gcm_seal` with the plaintext gathered from `src`. A shorter tag is the first bytes of this one. /// -/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// The function checks no length. GCM is secure only for a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of data and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must ensure; and a nonce must never be used twice with the same key. /// -/// Contract: `VG.Spec.Gcm.streamEncryptContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. +/// Contract: `VG.Spec.Gcm.sealGatherContract`. Constant time: only the pointers, `rounds`, the lengths, `src_count` and where the slices are (their addresses and lengths) may affect timing, not the key context, the nonce, the additional data or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_stream_encrypt_aes` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_AES_FEATURES`); or else `vg_aes_gcm_stream_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_seal_gather_aes` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_AES_FEATURES`); or else `vg_aes_gcm_seal_gather`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `state` must be valid for reads and writes of 80 bytes. -/// * `data` must be valid for reads and writes of `len` bytes. +/// * `nonce` must be valid for reads of `nonce_len` bytes. +/// * `aad` must be valid for reads of `aad_len` bytes. +/// * `src` must be valid for reads of `2 * size_of::() * src_count` bytes, and each slice it lists for reads of its length in bytes. +/// * `dst` must be valid for reads and writes of `len` bytes. +/// * `tag` must be valid for reads and writes of 16 bytes. /// * `rounds` must be 10, 12 or 14. -/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the 2560 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * The slices `src` lists must be `len` bytes long in all. +/// * `dst` and `tag` must not overlap each other, `ctx`, `nonce`, `aad`, `src`, the slices `src` lists or the arguments on the stack (distinct Rust objects never do). +/// * None of `ctx`, `nonce`, `aad`, `src`, the slices `src` lists, `dst` and `tag` may overlap the 2592 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_stream_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { +pub unsafe fn vg_aes_gcm_seal_gather_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const [usize; 2], src_count: usize, dst: *mut u8, len: usize, tag: *mut [u8; 16]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const [usize; 2], src_count: usize, dst: *mut u8, len: usize, tag: *mut [u8; 16]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_AES_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_AES_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_SEAL_GATHER_AES_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_SEAL_GATHER_AES_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_encrypt_aes; + return crate::gcm::vg_aes_gcm_seal_gather_aes; } - crate::gcm::vg_aes_gcm_stream_encrypt + crate::gcm::vg_aes_gcm_seal_gather } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { + unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const [usize; 2], src_count: usize, dst: *mut u8, len: usize, tag: *mut [u8; 16]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, rounds, state, aad_len, text_len, data, len) } + unsafe { f(ctx, rounds, nonce, nonce_len, aad, aad_len, src, src_count, dst, len, tag) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -570,51 +518,55 @@ pub unsafe fn vg_aes_gcm_stream_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_> // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, state, aad_len, text_len, data, len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, nonce, nonce_len, aad, aad_len, src, src_count, dst, len, tag) } } -/// Decrypts the next piece of the text of an incremental AES-GCM decryption (NIST SP 800-38D §7.2): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext so far, decrypts the `len` bytes of ciphertext at `data` in place, as the continuation of that ciphertext, and the state then represents the message with them appended. The plaintext is not authenticated until `vg_aes_gcm_stream_verify` has returned 1. +/// AES-GCM authenticated decryption (NIST SP 800-38D §7.2, GCM-AD), out of place: with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds and the received tag the `tag_len` bytes at `tag`, returns 1 if `tag_len` is between 4 and 16 and the tag is the first `tag_len` bytes of that of the `len` bytes of ciphertext at `src` and the `aad_len` bytes of additional data at `aad`, under the IV the `nonce_len` bytes at `nonce`, having then written the plaintext to the `dst_len = len` bytes at `dst`; otherwise returns 0, having written zeros to them: `vg_aes_gcm_open` with the ciphertext read from `src` and the plaintext written to `dst`. `src` is not written, and `dst` is a buffer of its own: it overlaps no other buffer, `src` included. The tags are compared without a branch. /// -/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// The function checks no other length: GCM requires a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of ciphertext and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.streamDecryptContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. +/// Contract: `VG.Spec.Gcm.openToContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the additional data, the data or the tag. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_stream_decrypt_aes` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_AES_FEATURES`); or else `vg_aes_gcm_stream_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_open_to_aes` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_AES_FEATURES`); or else `vg_aes_gcm_open_to`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `state` must be valid for reads and writes of 80 bytes. -/// * `data` must be valid for reads and writes of `len` bytes. +/// * `nonce` must be valid for reads of `nonce_len` bytes. +/// * `aad` must be valid for reads of `aad_len` bytes. +/// * `src` must be valid for reads of `len` bytes. +/// * `dst` must be valid for reads and writes of `dst_len` bytes. +/// * `tag` must be valid for reads of `tag_len` bytes. /// * `rounds` must be 10, 12 or 14. -/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the 2560 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `dst_len` must equal `len`. +/// * `dst` must not overlap `ctx`, `nonce`, `aad`, `src`, `tag` or the arguments on the stack (distinct Rust objects never do). +/// * None of `ctx`, `nonce`, `aad`, `src`, `dst` and `tag` may overlap the 2608 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { +pub unsafe fn vg_aes_gcm_open_to_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const u8, len: usize, dst: *mut u8, dst_len: usize, tag: *const u8, tag_len: usize) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const u8, len: usize, dst: *mut u8, dst_len: usize, tag: *const u8, tag_len: usize) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_AES_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_AES_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_OPEN_TO_AES_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_OPEN_TO_AES_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_decrypt_aes; + return crate::gcm::vg_aes_gcm_open_to_aes; } - crate::gcm::vg_aes_gcm_stream_decrypt + crate::gcm::vg_aes_gcm_open_to } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { + unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const u8, len: usize, dst: *mut u8, dst_len: usize, tag: *const u8, tag_len: usize) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, rounds, state, aad_len, text_len, data, len) } + unsafe { f(ctx, rounds, nonce, nonce_len, aad, aad_len, src, len, dst, dst_len, tag, tag_len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -622,12 +574,12 @@ pub unsafe fn vg_aes_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_> // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, state, aad_len, text_len, data, len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, nonce, nonce_len, aad, aad_len, src, len, dst, dst_len, tag, tag_len) } } /// `vg_aes_gcm_seal` with the 8 prepared powers of the hash subkey of the key context at `*pw` (`VG.Spec.Gcm.Powers8Repr`), which it may read instead of computing them. Computes the same result as `vg_aes_gcm_seal`. The powers are secret, as the key context is: they may not affect timing; all other memory, aliasing and constant-time requirements are unchanged. Contract: `VG.Spec.Gcm.sealWithPowersContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_seal_with_powers_aes_sha3` if the CPU supports its features (`VG_AES_GCM_SEAL_WITH_POWERS_AES_SHA3_FEATURES`); `vg_aes_gcm_seal_with_powers_aes` if the CPU supports its features (`VG_AES_GCM_SEAL_WITH_POWERS_AES_FEATURES`); or else `vg_aes_gcm_seal_with_powers`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_seal_with_powers_aes_sha3` if the CPU supports its features (`VG_AES_GCM_SEAL_WITH_POWERS_AES_SHA3_FEATURES`); `vg_aes_gcm_seal_with_powers_aes` if the CPU supports its features (`VG_AES_GCM_SEAL_WITH_POWERS_AES_FEATURES`); or else `vg_aes_gcm_seal_with_powers`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -684,7 +636,7 @@ pub unsafe fn vg_aes_gcm_seal_with_powers_auto(_dit: verified_garbage_cpu::Dit<' /// `vg_aes_gcm_open` with the 8 prepared powers of the hash subkey of the key context at `*pw` (`VG.Spec.Gcm.Powers8Repr`), which it may read instead of computing them. Computes the same result as `vg_aes_gcm_open`. The powers are secret, as the key context is: they may not affect timing; all other memory, aliasing and constant-time requirements are unchanged. Contract: `VG.Spec.Gcm.openWithPowersContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_open_with_powers_aes_sha3` if the CPU supports its features (`VG_AES_GCM_OPEN_WITH_POWERS_AES_SHA3_FEATURES`); `vg_aes_gcm_open_with_powers_aes` if the CPU supports its features (`VG_AES_GCM_OPEN_WITH_POWERS_AES_FEATURES`); or else `vg_aes_gcm_open_with_powers`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_open_with_powers_aes_sha3` if the CPU supports its features (`VG_AES_GCM_OPEN_WITH_POWERS_AES_SHA3_FEATURES`); `vg_aes_gcm_open_with_powers_aes` if the CPU supports its features (`VG_AES_GCM_OPEN_WITH_POWERS_AES_FEATURES`); or else `vg_aes_gcm_open_with_powers`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -741,7 +693,7 @@ pub unsafe fn vg_aes_gcm_open_with_powers_auto(_dit: verified_garbage_cpu::Dit<' /// `vg_aes_gcm_open_to` with the 8 prepared powers of the hash subkey of the key context at `*pw` (`VG.Spec.Gcm.Powers8Repr`), which it may read instead of computing them. Computes the same result as `vg_aes_gcm_open_to`. The powers are secret, as the key context is: they may not affect timing; all other memory, aliasing and constant-time requirements are unchanged. Contract: `VG.Spec.Gcm.openToWithPowersContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_open_to_with_powers_aes_sha3` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_WITH_POWERS_AES_SHA3_FEATURES`); `vg_aes_gcm_open_to_with_powers_aes` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_WITH_POWERS_AES_FEATURES`); or else `vg_aes_gcm_open_to_with_powers`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_open_to_with_powers_aes_sha3` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_WITH_POWERS_AES_SHA3_FEATURES`); `vg_aes_gcm_open_to_with_powers_aes` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_WITH_POWERS_AES_FEATURES`); or else `vg_aes_gcm_open_to_with_powers`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -797,3 +749,51 @@ pub unsafe fn vg_aes_gcm_open_to_with_powers_auto(_dit: verified_garbage_cpu::Di // either carries the caller's obligations, which are those of every implementation. unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, nonce, nonce_len, aad, aad_len, src, len, dst, dst_len, tag, tag_len, pw) } } + +/// Writes to `*pw` the 8 prepared powers of the hash subkey `H` of the key context `*ctx` of `vg_aes_gcm_init` (its bytes 240 to 255, `VG.Spec.Gcm.ctxH`): at byte `32*k`, for `k` from 0 to 3, `preparedPower(H^(2*k+2))` then `preparedPower(H^(2*k+1))`, as two little-endian 128-bit words (`VG.Spec.Gcm.Powers8Repr`, `VG.Spec.Gcm.preparedPower`), as the `*_with_powers` functions require for that key context. It reads only `*ctx` and writes only `*pw`. The key context and the powers are secret: only the pointers may affect timing. +/// +/// Contract: `VG.Spec.Gcm.storePowersContract`. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_store_powers_aes` if the CPU supports its features (`VG_AES_GCM_STORE_POWERS_AES_FEATURES`); or else `vg_aes_gcm_store_powers`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// +/// # Safety +/// +/// * `ctx` must be valid for reads of 256 bytes. +/// * `pw` must be valid for reads and writes of 128 bytes. +/// * `pw` must not overlap `ctx` (distinct Rust objects never do). +/// * Neither `ctx` nor `pw` may wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_aes_gcm_store_powers_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], pw: *mut [u64; 16]) { + /// The type of the implementations. + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], pw: *mut [u64; 16]); + /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only + /// features the CPU has. + #[cold] + fn select() -> Impl { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STORE_POWERS_AES_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STORE_POWERS_AES_FEATURES) + { + return crate::gcm::vg_aes_gcm_store_powers_aes; + } + crate::gcm::vg_aes_gcm_store_powers + } + /// The first call: chooses the implementation, records it in `CHOSEN` for the calls + /// after it, and runs it. + #[cold] + unsafe extern "C" fn first(ctx: *const [u64; 32], pw: *mut [u64; 16]) { + let f = select(); + CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); + // SAFETY: the caller's obligations, which are those of every implementation, `f` + // among them, and the CPU has the features `f` needs. + unsafe { f(ctx, pw) } + } + /// The function to call: `first`, until it records the implementation it chose. + static CHOSEN: core::sync::atomic::AtomicPtr<()> = + core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); + // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, + // or an implementation `select` returned, which needs only features the CPU has. Calling + // either carries the caller's obligations, which are those of every implementation. + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, pw) } +} diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha1.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha1.rs index 54b3262b86a..3ffc51127a4 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha1.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha1.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.initAnyKeyContract` of `VG.Spec.Hmac.sha1I`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha1_init_sha2` if the CPU supports its features (`VG_HMAC_SHA1_INIT_SHA2_FEATURES`); or else `vg_hmac_sha1_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha1_init_sha2` if the CPU supports its features (`VG_HMAC_SHA1_INIT_SHA2_FEATURES`); or else `vg_hmac_sha1_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -55,7 +55,7 @@ pub unsafe fn vg_hmac_sha1_init_auto(_dit: verified_garbage_cpu::Dit<'_>, inner: /// /// Contract: `VG.Spec.Hmac.Instance.finalizeContract` of `VG.Spec.Hmac.sha1I`. Constant time: only the pointers and `count` may affect timing, not the states. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha1_finalize_sha2` if the CPU supports its features (`VG_HMAC_SHA1_FINALIZE_SHA2_FEATURES`); or else `vg_hmac_sha1_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha1_finalize_sha2` if the CPU supports its features (`VG_HMAC_SHA1_FINALIZE_SHA2_FEATURES`); or else `vg_hmac_sha1_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha224.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha224.rs index 782131362c4..6d78909b9c2 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha224.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha224.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.initAnyKeyContract` of `VG.Spec.Hmac.sha224I`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha224_init_sha2` if the CPU supports its features (`VG_HMAC_SHA224_INIT_SHA2_FEATURES`); or else `vg_hmac_sha224_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha224_init_sha2` if the CPU supports its features (`VG_HMAC_SHA224_INIT_SHA2_FEATURES`); or else `vg_hmac_sha224_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -55,7 +55,7 @@ pub unsafe fn vg_hmac_sha224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, inne /// /// Contract: `VG.Spec.Hmac.Instance.finalizeContract` of `VG.Spec.Hmac.sha224I`. Constant time: only the pointers and `count` may affect timing, not the states. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha224_finalize_sha2` if the CPU supports its features (`VG_HMAC_SHA224_FINALIZE_SHA2_FEATURES`); or else `vg_hmac_sha224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha224_finalize_sha2` if the CPU supports its features (`VG_HMAC_SHA224_FINALIZE_SHA2_FEATURES`); or else `vg_hmac_sha224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha256.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha256.rs index 3fb395ffb47..aacc7bfdfe3 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha256.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha256.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.initAnyKeyContract` of `VG.Spec.Hmac.sha256I`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha256_init_sha2` if the CPU supports its features (`VG_HMAC_SHA256_INIT_SHA2_FEATURES`); or else `vg_hmac_sha256_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha256_init_sha2` if the CPU supports its features (`VG_HMAC_SHA256_INIT_SHA2_FEATURES`); or else `vg_hmac_sha256_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -55,7 +55,7 @@ pub unsafe fn vg_hmac_sha256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, inne /// /// Contract: `VG.Spec.Hmac.Instance.finalizeContract` of `VG.Spec.Hmac.sha256I`. Constant time: only the pointers and `count` may affect timing, not the states. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha256_finalize_sha2` if the CPU supports its features (`VG_HMAC_SHA256_FINALIZE_SHA2_FEATURES`); or else `vg_hmac_sha256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha256_finalize_sha2` if the CPU supports its features (`VG_HMAC_SHA256_FINALIZE_SHA2_FEATURES`); or else `vg_hmac_sha256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha384.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha384.rs index 41ef5648304..24d6084cd77 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha384.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha384.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.initAnyKeyContract` of `VG.Spec.Hmac.sha384I`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha384_init_sha512` if the CPU supports its features (`VG_HMAC_SHA384_INIT_SHA512_FEATURES`); or else `vg_hmac_sha384_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha384_init_sha512` if the CPU supports its features (`VG_HMAC_SHA384_INIT_SHA512_FEATURES`); or else `vg_hmac_sha384_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -55,7 +55,7 @@ pub unsafe fn vg_hmac_sha384_init_auto(_dit: verified_garbage_cpu::Dit<'_>, inne /// /// Contract: `VG.Spec.Hmac.Instance.finalizeContract` of `VG.Spec.Hmac.sha384I`. Constant time: only the pointers and `count` may affect timing, not the states. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha384_finalize_sha512` if the CPU supports its features (`VG_HMAC_SHA384_FINALIZE_SHA512_FEATURES`); or else `vg_hmac_sha384_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha384_finalize_sha512` if the CPU supports its features (`VG_HMAC_SHA384_FINALIZE_SHA512_FEATURES`); or else `vg_hmac_sha384_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha512.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha512.rs index 24edf613ed1..a94e62b645f 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha512.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha512.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.initAnyKeyContract` of `VG.Spec.Hmac.sha512I`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha512_init_sha512` if the CPU supports its features (`VG_HMAC_SHA512_INIT_SHA512_FEATURES`); or else `vg_hmac_sha512_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha512_init_sha512` if the CPU supports its features (`VG_HMAC_SHA512_INIT_SHA512_FEATURES`); or else `vg_hmac_sha512_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -55,7 +55,7 @@ pub unsafe fn vg_hmac_sha512_init_auto(_dit: verified_garbage_cpu::Dit<'_>, inne /// /// Contract: `VG.Spec.Hmac.Instance.finalizeContract` of `VG.Spec.Hmac.sha512I`. Constant time: only the pointers and `count` may affect timing, not the states. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha512_finalize_sha512` if the CPU supports its features (`VG_HMAC_SHA512_FINALIZE_SHA512_FEATURES`); or else `vg_hmac_sha512_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha512_finalize_sha512` if the CPU supports its features (`VG_HMAC_SHA512_FINALIZE_SHA512_FEATURES`); or else `vg_hmac_sha512_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha512_224.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha512_224.rs index b95d8f76a70..56986ae81db 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha512_224.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha512_224.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.initAnyKeyContract` of `VG.Spec.Hmac.sha512_224I`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha512_224_init_sha512` if the CPU supports its features (`VG_HMAC_SHA512_224_INIT_SHA512_FEATURES`); or else `vg_hmac_sha512_224_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha512_224_init_sha512` if the CPU supports its features (`VG_HMAC_SHA512_224_INIT_SHA512_FEATURES`); or else `vg_hmac_sha512_224_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -55,7 +55,7 @@ pub unsafe fn vg_hmac_sha512_224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, /// /// Contract: `VG.Spec.Hmac.Instance.finalizeContract` of `VG.Spec.Hmac.sha512_224I`. Constant time: only the pointers and `count` may affect timing, not the states. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha512_224_finalize_sha512` if the CPU supports its features (`VG_HMAC_SHA512_224_FINALIZE_SHA512_FEATURES`); or else `vg_hmac_sha512_224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha512_224_finalize_sha512` if the CPU supports its features (`VG_HMAC_SHA512_224_FINALIZE_SHA512_FEATURES`); or else `vg_hmac_sha512_224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha512_256.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha512_256.rs index 8a179a1642e..a3af824a961 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha512_256.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/hmac_sha512_256.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.initAnyKeyContract` of `VG.Spec.Hmac.sha512_256I`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha512_256_init_sha512` if the CPU supports its features (`VG_HMAC_SHA512_256_INIT_SHA512_FEATURES`); or else `vg_hmac_sha512_256_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha512_256_init_sha512` if the CPU supports its features (`VG_HMAC_SHA512_256_INIT_SHA512_FEATURES`); or else `vg_hmac_sha512_256_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -55,7 +55,7 @@ pub unsafe fn vg_hmac_sha512_256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, /// /// Contract: `VG.Spec.Hmac.Instance.finalizeContract` of `VG.Spec.Hmac.sha512_256I`. Constant time: only the pointers and `count` may affect timing, not the states. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha512_256_finalize_sha512` if the CPU supports its features (`VG_HMAC_SHA512_256_FINALIZE_SHA512_FEATURES`); or else `vg_hmac_sha512_256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha512_256_finalize_sha512` if the CPU supports its features (`VG_HMAC_SHA512_256_FINALIZE_SHA512_FEATURES`); or else `vg_hmac_sha512_256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/idea.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/idea.rs index 9a65fc6a8df..b2a815f2a91 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/idea.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/idea.rs @@ -2,25 +2,6 @@ //! The dispatchers of the verified `idea` functions for `aarch64` //! (`crate::idea`, `VG.Rust.dispatcher`). -/// IDEA in ECB mode (Lai, 1992, §3.3; SP 800-38A §6.1) on `n` complete 8-byte blocks at `data`, in place, under the 52 subkeys at `schedule`: encryption under the subkeys `vg_idea_expand_key` writes, decryption under those `vg_idea_invert_key` writes. No padding is added or removed. For `n = 0`, no data is transformed. -/// -/// Contract: `VG.Spec.Idea.ecbContract`. Constant time: only pointers and `n` may affect timing, not the subkeys or data, including words or subkeys that are 0. -/// -/// It runs `vg_idea_ecb`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `schedule` must be valid for reads of 104 bytes. -/// * `data` must be valid for reads and writes of `8 * n` bytes. -/// * `data` must not overlap `schedule` (distinct Rust objects never do). -/// * Neither `schedule` nor `data` may wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_idea_ecb_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *const [u8; 104], data: *mut [u8; 8], n: usize) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::idea::vg_idea_ecb(schedule, data, n) } -} - /// IDEA encryption key schedule (Lai, 1992, §3.3): writes the 52 encryption subkeys of the 16-byte key at `key` (as eight big-endian 16-bit words, rotated left by 25 bits after every eight subkeys) to `*schedule`, as little-endian 16-bit words. /// /// Contract: `VG.Spec.Idea.expandKeyContract`. Constant time: only pointers may affect timing, not the key. @@ -58,3 +39,22 @@ pub unsafe fn vg_idea_invert_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sched // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::idea::vg_idea_invert_key(schedule, inverse) } } + +/// IDEA in ECB mode (Lai, 1992, §3.3; SP 800-38A §6.1) on `n` complete 8-byte blocks at `data`, in place, under the 52 subkeys at `schedule`: encryption under the subkeys `vg_idea_expand_key` writes, decryption under those `vg_idea_invert_key` writes. No padding is added or removed. For `n = 0`, no data is transformed. +/// +/// Contract: `VG.Spec.Idea.ecbContract`. Constant time: only pointers and `n` may affect timing, not the subkeys or data, including words or subkeys that are 0. +/// +/// It runs `vg_idea_ecb`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `schedule` must be valid for reads of 104 bytes. +/// * `data` must be valid for reads and writes of `8 * n` bytes. +/// * `data` must not overlap `schedule` (distinct Rust objects never do). +/// * Neither `schedule` nor `data` may wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_idea_ecb_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *const [u8; 104], data: *mut [u8; 8], n: usize) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::idea::vg_idea_ecb(schedule, data, n) } +} diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/mldsa44.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/mldsa44.rs index 65ad12388b5..29d7af938d4 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/mldsa44.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/mldsa44.rs @@ -2,50 +2,48 @@ //! The dispatchers of the verified `mldsa44` functions for `aarch64` //! (`crate::mldsa44`, `VG.Rust.dispatcher`). -/// ML-DSA-44 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-44 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. +/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa44_sign_sha3` if the CPU supports its features (`VG_MLDSA44_SIGN_SHA3_FEATURES`); or else `vg_mldsa44_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa44_keygen_sha3` if the CPU supports its features (`VG_MLDSA44_KEYGEN_SHA3_FEATURES`); or else `vg_mldsa44_keygen`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `sk` must be valid for reads of 2560 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `rnd` must be valid for reads of 32 bytes. -/// * `sig` must be valid for reads and writes of 2420 bytes. +/// * `seed` must be valid for reads of 32 bytes. +/// * `pk` must be valid for reads and writes of 1312 bytes. +/// * `sk` must be valid for reads and writes of 2560 bytes. /// * `scratch` must be valid for reads and writes of 77824 bytes. -/// * `sk` must have been written by `vg_mldsa44_keygen`. -/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. +/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 64 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { +pub unsafe fn vg_mldsa44_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1312], sk: *mut [u8; 2560], scratch: *mut [u64; 9728]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(sk: *const [u8; 2560], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9728]) -> u32; + type Impl = unsafe extern "C" fn(seed: *const [u8; 32], pk: *mut [u8; 1312], sk: *mut [u8; 2560], scratch: *mut [u64; 9728]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_SIGN_SHA3_FEATURES) } - || cpu.contains(crate::mldsa44::VG_MLDSA44_SIGN_SHA3_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_KEYGEN_SHA3_FEATURES) } + || cpu.contains(crate::mldsa44::VG_MLDSA44_KEYGEN_SHA3_FEATURES) { - return crate::mldsa44::vg_mldsa44_sign_sha3; + return crate::mldsa44::vg_mldsa44_keygen_sha3; } - crate::mldsa44::vg_mldsa44_sign + crate::mldsa44::vg_mldsa44_keygen } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(sk: *const [u8; 2560], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { + unsafe extern "C" fn first(seed: *const [u8; 32], pk: *mut [u8; 1312], sk: *mut [u8; 2560], scratch: *mut [u64; 9728]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(sk, mu, rnd, sig, scratch) } + unsafe { f(seed, pk, sk, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -53,14 +51,14 @@ pub unsafe fn vg_mldsa44_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *con // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, mu, rnd, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(seed, pk, sk, scratch) } } /// ML-DSA-44 public-key hash: writes `tr = H(pk, 64)` (FIPS 204 Algorithm 8, line 6), SHAKE256 of the public key `*pk` to 64 bytes, to `*tr`, the digest `vg_mldsa44_verify_message_cached` takes. /// /// Contract: `VG.Spec.MlDsa.pkTrContract`. Constant time: only the pointers may affect timing, not the key, the digest or the working space. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa44_pk_tr_sha3` if the CPU supports its features (`VG_MLDSA44_PK_TR_SHA3_FEATURES`); or else `vg_mldsa44_pk_tr`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa44_pk_tr_sha3` if the CPU supports its features (`VG_MLDSA44_PK_TR_SHA3_FEATURES`); or else `vg_mldsa44_pk_tr`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -106,99 +104,93 @@ pub unsafe fn vg_mldsa44_pk_tr_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *co unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, tr, scratch) } } -/// ML-DSA-44 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-44 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa44_verify_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. +/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa44_keygen_sha3` if the CPU supports its features (`VG_MLDSA44_KEYGEN_SHA3_FEATURES`); or else `vg_mldsa44_keygen`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs `vg_mldsa44_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `seed` must be valid for reads of 32 bytes. -/// * `pk` must be valid for reads and writes of 1312 bytes. -/// * `sk` must be valid for reads and writes of 2560 bytes. -/// * `scratch` must be valid for reads and writes of 77824 bytes. -/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. +/// * `pk` must be valid for reads of 1312 bytes. +/// * `prepared` must be valid for reads and writes of 5408 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). +/// * None of `pk`, `prepared` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1312], sk: *mut [u8; 2560], scratch: *mut [u64; 9728]) -> u32 { - /// The type of the implementations. - type Impl = unsafe extern "C" fn(seed: *const [u8; 32], pk: *mut [u8; 1312], sk: *mut [u8; 2560], scratch: *mut [u64; 9728]) -> u32; - /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only - /// features the CPU has. - #[cold] - fn select() -> Impl { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_KEYGEN_SHA3_FEATURES) } - || cpu.contains(crate::mldsa44::VG_MLDSA44_KEYGEN_SHA3_FEATURES) - { - return crate::mldsa44::vg_mldsa44_keygen_sha3; - } - crate::mldsa44::vg_mldsa44_keygen - } - /// The first call: chooses the implementation, records it in `CHOSEN` for the calls - /// after it, and runs it. - #[cold] - unsafe extern "C" fn first(seed: *const [u8; 32], pk: *mut [u8; 1312], sk: *mut [u8; 2560], scratch: *mut [u64; 9728]) -> u32 { - let f = select(); - CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); - // SAFETY: the caller's obligations, which are those of every implementation, `f` - // among them, and the CPU has the features `f` needs. - unsafe { f(seed, pk, sk, scratch) } - } - /// The function to call: `first`, until it records the implementation it chose. - static CHOSEN: core::sync::atomic::AtomicPtr<()> = - core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); - // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, - // or an implementation `select` returned, which needs only features the CPU has. Calling - // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(seed, pk, sk, scratch) } +pub unsafe fn vg_mldsa44_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], prepared: *mut [u8; 5408], scratch: *mut [u64; 128]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mldsa44::vg_mldsa44_prepare_verifying_key(pk, prepared, scratch) } } -/// ML-DSA-44 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// ML-DSA-44 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa44_sign_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa44_verify_sha3` if the CPU supports its features (`VG_MLDSA44_VERIFY_SHA3_FEATURES`); or else `vg_mldsa44_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs `vg_mldsa44_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `pk` must be valid for reads of 1312 bytes. +/// * `sk` must be valid for reads of 2560 bytes. +/// * `prepared` must be valid for reads and writes of 14848 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). +/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). +/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). +/// * None of `sk`, `prepared` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mldsa44_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], prepared: *mut [u8; 14848], scratch: *mut [u64; 128]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mldsa44::vg_mldsa44_prepare_signing_key(sk, prepared, scratch) } +} + +/// ML-DSA-44 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// +/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa44_sign_sha3` if the CPU supports its features (`VG_MLDSA44_SIGN_SHA3_FEATURES`); or else `vg_mldsa44_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// +/// # Safety +/// +/// * `sk` must be valid for reads of 2560 bytes. /// * `mu` must be valid for reads of 64 bytes. -/// * `sig` must be valid for reads of 2420 bytes. +/// * `rnd` must be valid for reads of 32 bytes. +/// * `sig` must be valid for reads and writes of 2420 bytes. /// * `scratch` must be valid for reads and writes of 77824 bytes. +/// * `sk` must have been written by `vg_mldsa44_keygen`. +/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). -/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 64 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 64 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], mu: *const [u8; 64], sig: *const [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { +pub unsafe fn vg_mldsa44_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(pk: *const [u8; 1312], mu: *const [u8; 64], sig: *const [u8; 2420], scratch: *mut [u64; 9728]) -> u32; + type Impl = unsafe extern "C" fn(sk: *const [u8; 2560], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9728]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_VERIFY_SHA3_FEATURES) } - || cpu.contains(crate::mldsa44::VG_MLDSA44_VERIFY_SHA3_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_SIGN_SHA3_FEATURES) } + || cpu.contains(crate::mldsa44::VG_MLDSA44_SIGN_SHA3_FEATURES) { - return crate::mldsa44::vg_mldsa44_verify_sha3; + return crate::mldsa44::vg_mldsa44_sign_sha3; } - crate::mldsa44::vg_mldsa44_verify + crate::mldsa44::vg_mldsa44_sign } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(pk: *const [u8; 1312], mu: *const [u8; 64], sig: *const [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { + unsafe extern "C" fn first(sk: *const [u8; 2560], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(pk, mu, sig, scratch) } + unsafe { f(sk, mu, rnd, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -206,14 +198,14 @@ pub unsafe fn vg_mldsa44_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *c // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, mu, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, mu, rnd, sig, scratch) } } /// ML-DSA-44 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. /// /// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa44_sign_message_sha3` if the CPU supports its features (`VG_MLDSA44_SIGN_MESSAGE_SHA3_FEATURES`); or else `vg_mldsa44_sign_message`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa44_sign_message_sha3` if the CPU supports its features (`VG_MLDSA44_SIGN_MESSAGE_SHA3_FEATURES`); or else `vg_mldsa44_sign_message`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -264,54 +256,11 @@ pub unsafe fn vg_mldsa44_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } -/// ML-DSA-44 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa44_sign_message_prepared`. -/// -/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. -/// -/// It runs `vg_mldsa44_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `sk` must be valid for reads of 2560 bytes. -/// * `prepared` must be valid for reads and writes of 14848 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. -/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). -/// * None of `sk`, `prepared` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mldsa44_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], prepared: *mut [u8; 14848], scratch: *mut [u64; 128]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_prepare_signing_key(sk, prepared, scratch) } -} - -/// ML-DSA-44 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa44_verify_message_prepared`. -/// -/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. -/// -/// It runs `vg_mldsa44_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 1312 bytes. -/// * `prepared` must be valid for reads and writes of 5408 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. -/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). -/// * None of `pk`, `prepared` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mldsa44_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], prepared: *mut [u8; 5408], scratch: *mut [u64; 128]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_prepare_verifying_key(pk, prepared, scratch) } -} - /// ML-DSA-44 signing with a prepared private key: `vg_mldsa44_sign_message` (with its results, timing and leakage) of the private key that `*sk` begins with. /// /// Contract: `VG.Spec.MlDsa.preparedSignMessageContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa44_sign_message_prepared_sha3` if the CPU supports its features (`VG_MLDSA44_SIGN_MESSAGE_PREPARED_SHA3_FEATURES`); or else `vg_mldsa44_sign_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa44_sign_message_prepared_sha3` if the CPU supports its features (`VG_MLDSA44_SIGN_MESSAGE_PREPARED_SHA3_FEATURES`); or else `vg_mldsa44_sign_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -363,11 +312,62 @@ pub unsafe fn vg_mldsa44_sign_message_prepared_auto(_dit: verified_garbage_cpu:: unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } +/// ML-DSA-44 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// +/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa44_verify_sha3` if the CPU supports its features (`VG_MLDSA44_VERIFY_SHA3_FEATURES`); or else `vg_mldsa44_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 1312 bytes. +/// * `mu` must be valid for reads of 64 bytes. +/// * `sig` must be valid for reads of 2420 bytes. +/// * `scratch` must be valid for reads and writes of 77824 bytes. +/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). +/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). +/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 64 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mldsa44_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], mu: *const [u8; 64], sig: *const [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { + /// The type of the implementations. + type Impl = unsafe extern "C" fn(pk: *const [u8; 1312], mu: *const [u8; 64], sig: *const [u8; 2420], scratch: *mut [u64; 9728]) -> u32; + /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only + /// features the CPU has. + #[cold] + fn select() -> Impl { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_VERIFY_SHA3_FEATURES) } + || cpu.contains(crate::mldsa44::VG_MLDSA44_VERIFY_SHA3_FEATURES) + { + return crate::mldsa44::vg_mldsa44_verify_sha3; + } + crate::mldsa44::vg_mldsa44_verify + } + /// The first call: chooses the implementation, records it in `CHOSEN` for the calls + /// after it, and runs it. + #[cold] + unsafe extern "C" fn first(pk: *const [u8; 1312], mu: *const [u8; 64], sig: *const [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { + let f = select(); + CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); + // SAFETY: the caller's obligations, which are those of every implementation, `f` + // among them, and the CPU has the features `f` needs. + unsafe { f(pk, mu, sig, scratch) } + } + /// The function to call: `first`, until it records the implementation it chose. + static CHOSEN: core::sync::atomic::AtomicPtr<()> = + core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); + // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, + // or an implementation `select` returned, which needs only features the CPU has. Calling + // either carries the caller's obligations, which are those of every implementation. + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, mu, sig, scratch) } +} + /// ML-DSA-44 message verification with a cached public-key digest. Returns 1 for a valid signature, 0 for an invalid signature or exhausted sampling bound, and 2 if the context exceeds 255 bytes. It is `ML-DSA.Verify(pk, M, σ, ctx)` (FIPS 204 Algorithm 3) with `H(pk, 64)` read from `*tr`: with the public key `*pk`, the `msg_len` bytes of the message at `msg`, the `ctx_len` bytes of the context string at `ctx` and the signature `*sig`, it formats the message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 5), computes `μ = H(tr ‖ M′, 64)`, and verifies `*sig` on `μ` with `*pk`, `ML-DSA.Verify_internal(pk, M′, σ)` (Algorithm 8). /// /// Contract: `VG.Spec.MlDsa.verifyMessageCachedContract`. Timing may depend on the public key, message, context string and signature, and on argument addresses and lengths. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa44_verify_message_cached_sha3` if the CPU supports its features (`VG_MLDSA44_VERIFY_MESSAGE_CACHED_SHA3_FEATURES`); or else `vg_mldsa44_verify_message_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa44_verify_message_cached_sha3` if the CPU supports its features (`VG_MLDSA44_VERIFY_MESSAGE_CACHED_SHA3_FEATURES`); or else `vg_mldsa44_verify_message_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -421,7 +421,7 @@ pub unsafe fn vg_mldsa44_verify_message_cached_auto(_dit: verified_garbage_cpu:: /// /// Contract: `VG.Spec.MlDsa.preparedVerifyMessageContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa44_verify_message_prepared_sha3` if the CPU supports its features (`VG_MLDSA44_VERIFY_MESSAGE_PREPARED_SHA3_FEATURES`); or else `vg_mldsa44_verify_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa44_verify_message_prepared_sha3` if the CPU supports its features (`VG_MLDSA44_VERIFY_MESSAGE_PREPARED_SHA3_FEATURES`); or else `vg_mldsa44_verify_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/mldsa65.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/mldsa65.rs index 6f5468da1f7..5047915a202 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/mldsa65.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/mldsa65.rs @@ -2,50 +2,48 @@ //! The dispatchers of the verified `mldsa65` functions for `aarch64` //! (`crate::mldsa65`, `VG.Rust.dispatcher`). -/// ML-DSA-65 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-65 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. +/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa65_sign_sha3` if the CPU supports its features (`VG_MLDSA65_SIGN_SHA3_FEATURES`); or else `vg_mldsa65_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa65_keygen_sha3` if the CPU supports its features (`VG_MLDSA65_KEYGEN_SHA3_FEATURES`); or else `vg_mldsa65_keygen`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `sk` must be valid for reads of 4032 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `rnd` must be valid for reads of 32 bytes. -/// * `sig` must be valid for reads and writes of 3309 bytes. +/// * `seed` must be valid for reads of 32 bytes. +/// * `pk` must be valid for reads and writes of 1952 bytes. +/// * `sk` must be valid for reads and writes of 4032 bytes. /// * `scratch` must be valid for reads and writes of 103424 bytes. -/// * `sk` must have been written by `vg_mldsa65_keygen`. -/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. +/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 64 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { +pub unsafe fn vg_mldsa65_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1952], sk: *mut [u8; 4032], scratch: *mut [u64; 12928]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(sk: *const [u8; 4032], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 12928]) -> u32; + type Impl = unsafe extern "C" fn(seed: *const [u8; 32], pk: *mut [u8; 1952], sk: *mut [u8; 4032], scratch: *mut [u64; 12928]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_SIGN_SHA3_FEATURES) } - || cpu.contains(crate::mldsa65::VG_MLDSA65_SIGN_SHA3_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_KEYGEN_SHA3_FEATURES) } + || cpu.contains(crate::mldsa65::VG_MLDSA65_KEYGEN_SHA3_FEATURES) { - return crate::mldsa65::vg_mldsa65_sign_sha3; + return crate::mldsa65::vg_mldsa65_keygen_sha3; } - crate::mldsa65::vg_mldsa65_sign + crate::mldsa65::vg_mldsa65_keygen } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(sk: *const [u8; 4032], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { + unsafe extern "C" fn first(seed: *const [u8; 32], pk: *mut [u8; 1952], sk: *mut [u8; 4032], scratch: *mut [u64; 12928]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(sk, mu, rnd, sig, scratch) } + unsafe { f(seed, pk, sk, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -53,14 +51,14 @@ pub unsafe fn vg_mldsa65_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *con // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, mu, rnd, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(seed, pk, sk, scratch) } } /// ML-DSA-65 public-key hash: writes `tr = H(pk, 64)` (FIPS 204 Algorithm 8, line 6), SHAKE256 of the public key `*pk` to 64 bytes, to `*tr`, the digest `vg_mldsa65_verify_message_cached` takes. /// /// Contract: `VG.Spec.MlDsa.pkTrContract`. Constant time: only the pointers may affect timing, not the key, the digest or the working space. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa65_pk_tr_sha3` if the CPU supports its features (`VG_MLDSA65_PK_TR_SHA3_FEATURES`); or else `vg_mldsa65_pk_tr`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa65_pk_tr_sha3` if the CPU supports its features (`VG_MLDSA65_PK_TR_SHA3_FEATURES`); or else `vg_mldsa65_pk_tr`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -106,99 +104,93 @@ pub unsafe fn vg_mldsa65_pk_tr_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *co unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, tr, scratch) } } -/// ML-DSA-65 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-65 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa65_verify_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. +/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa65_keygen_sha3` if the CPU supports its features (`VG_MLDSA65_KEYGEN_SHA3_FEATURES`); or else `vg_mldsa65_keygen`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs `vg_mldsa65_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `seed` must be valid for reads of 32 bytes. -/// * `pk` must be valid for reads and writes of 1952 bytes. -/// * `sk` must be valid for reads and writes of 4032 bytes. -/// * `scratch` must be valid for reads and writes of 103424 bytes. -/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. +/// * `pk` must be valid for reads of 1952 bytes. +/// * `prepared` must be valid for reads and writes of 8096 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). +/// * None of `pk`, `prepared` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1952], sk: *mut [u8; 4032], scratch: *mut [u64; 12928]) -> u32 { - /// The type of the implementations. - type Impl = unsafe extern "C" fn(seed: *const [u8; 32], pk: *mut [u8; 1952], sk: *mut [u8; 4032], scratch: *mut [u64; 12928]) -> u32; - /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only - /// features the CPU has. - #[cold] - fn select() -> Impl { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_KEYGEN_SHA3_FEATURES) } - || cpu.contains(crate::mldsa65::VG_MLDSA65_KEYGEN_SHA3_FEATURES) - { - return crate::mldsa65::vg_mldsa65_keygen_sha3; - } - crate::mldsa65::vg_mldsa65_keygen - } - /// The first call: chooses the implementation, records it in `CHOSEN` for the calls - /// after it, and runs it. - #[cold] - unsafe extern "C" fn first(seed: *const [u8; 32], pk: *mut [u8; 1952], sk: *mut [u8; 4032], scratch: *mut [u64; 12928]) -> u32 { - let f = select(); - CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); - // SAFETY: the caller's obligations, which are those of every implementation, `f` - // among them, and the CPU has the features `f` needs. - unsafe { f(seed, pk, sk, scratch) } - } - /// The function to call: `first`, until it records the implementation it chose. - static CHOSEN: core::sync::atomic::AtomicPtr<()> = - core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); - // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, - // or an implementation `select` returned, which needs only features the CPU has. Calling - // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(seed, pk, sk, scratch) } +pub unsafe fn vg_mldsa65_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], prepared: *mut [u8; 8096], scratch: *mut [u64; 128]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mldsa65::vg_mldsa65_prepare_verifying_key(pk, prepared, scratch) } } -/// ML-DSA-65 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// ML-DSA-65 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa65_sign_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa65_verify_sha3` if the CPU supports its features (`VG_MLDSA65_VERIFY_SHA3_FEATURES`); or else `vg_mldsa65_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs `vg_mldsa65_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `pk` must be valid for reads of 1952 bytes. +/// * `sk` must be valid for reads of 4032 bytes. +/// * `prepared` must be valid for reads and writes of 21440 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). +/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). +/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). +/// * None of `sk`, `prepared` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mldsa65_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], prepared: *mut [u8; 21440], scratch: *mut [u64; 128]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mldsa65::vg_mldsa65_prepare_signing_key(sk, prepared, scratch) } +} + +/// ML-DSA-65 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// +/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa65_sign_sha3` if the CPU supports its features (`VG_MLDSA65_SIGN_SHA3_FEATURES`); or else `vg_mldsa65_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// +/// # Safety +/// +/// * `sk` must be valid for reads of 4032 bytes. /// * `mu` must be valid for reads of 64 bytes. -/// * `sig` must be valid for reads of 3309 bytes. +/// * `rnd` must be valid for reads of 32 bytes. +/// * `sig` must be valid for reads and writes of 3309 bytes. /// * `scratch` must be valid for reads and writes of 103424 bytes. +/// * `sk` must have been written by `vg_mldsa65_keygen`. +/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). -/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 64 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 64 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], mu: *const [u8; 64], sig: *const [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { +pub unsafe fn vg_mldsa65_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(pk: *const [u8; 1952], mu: *const [u8; 64], sig: *const [u8; 3309], scratch: *mut [u64; 12928]) -> u32; + type Impl = unsafe extern "C" fn(sk: *const [u8; 4032], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 12928]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_VERIFY_SHA3_FEATURES) } - || cpu.contains(crate::mldsa65::VG_MLDSA65_VERIFY_SHA3_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_SIGN_SHA3_FEATURES) } + || cpu.contains(crate::mldsa65::VG_MLDSA65_SIGN_SHA3_FEATURES) { - return crate::mldsa65::vg_mldsa65_verify_sha3; + return crate::mldsa65::vg_mldsa65_sign_sha3; } - crate::mldsa65::vg_mldsa65_verify + crate::mldsa65::vg_mldsa65_sign } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(pk: *const [u8; 1952], mu: *const [u8; 64], sig: *const [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { + unsafe extern "C" fn first(sk: *const [u8; 4032], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(pk, mu, sig, scratch) } + unsafe { f(sk, mu, rnd, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -206,14 +198,14 @@ pub unsafe fn vg_mldsa65_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *c // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, mu, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, mu, rnd, sig, scratch) } } /// ML-DSA-65 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. /// /// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa65_sign_message_sha3` if the CPU supports its features (`VG_MLDSA65_SIGN_MESSAGE_SHA3_FEATURES`); or else `vg_mldsa65_sign_message`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa65_sign_message_sha3` if the CPU supports its features (`VG_MLDSA65_SIGN_MESSAGE_SHA3_FEATURES`); or else `vg_mldsa65_sign_message`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -264,54 +256,11 @@ pub unsafe fn vg_mldsa65_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } -/// ML-DSA-65 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa65_sign_message_prepared`. -/// -/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. -/// -/// It runs `vg_mldsa65_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `sk` must be valid for reads of 4032 bytes. -/// * `prepared` must be valid for reads and writes of 21440 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. -/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). -/// * None of `sk`, `prepared` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mldsa65_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], prepared: *mut [u8; 21440], scratch: *mut [u64; 128]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_prepare_signing_key(sk, prepared, scratch) } -} - -/// ML-DSA-65 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa65_verify_message_prepared`. -/// -/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. -/// -/// It runs `vg_mldsa65_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 1952 bytes. -/// * `prepared` must be valid for reads and writes of 8096 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. -/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). -/// * None of `pk`, `prepared` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mldsa65_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], prepared: *mut [u8; 8096], scratch: *mut [u64; 128]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_prepare_verifying_key(pk, prepared, scratch) } -} - /// ML-DSA-65 signing with a prepared private key: `vg_mldsa65_sign_message` (with its results, timing and leakage) of the private key that `*sk` begins with. /// /// Contract: `VG.Spec.MlDsa.preparedSignMessageContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa65_sign_message_prepared_sha3` if the CPU supports its features (`VG_MLDSA65_SIGN_MESSAGE_PREPARED_SHA3_FEATURES`); or else `vg_mldsa65_sign_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa65_sign_message_prepared_sha3` if the CPU supports its features (`VG_MLDSA65_SIGN_MESSAGE_PREPARED_SHA3_FEATURES`); or else `vg_mldsa65_sign_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -363,11 +312,62 @@ pub unsafe fn vg_mldsa65_sign_message_prepared_auto(_dit: verified_garbage_cpu:: unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } +/// ML-DSA-65 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// +/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa65_verify_sha3` if the CPU supports its features (`VG_MLDSA65_VERIFY_SHA3_FEATURES`); or else `vg_mldsa65_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 1952 bytes. +/// * `mu` must be valid for reads of 64 bytes. +/// * `sig` must be valid for reads of 3309 bytes. +/// * `scratch` must be valid for reads and writes of 103424 bytes. +/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). +/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). +/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 64 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mldsa65_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], mu: *const [u8; 64], sig: *const [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { + /// The type of the implementations. + type Impl = unsafe extern "C" fn(pk: *const [u8; 1952], mu: *const [u8; 64], sig: *const [u8; 3309], scratch: *mut [u64; 12928]) -> u32; + /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only + /// features the CPU has. + #[cold] + fn select() -> Impl { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_VERIFY_SHA3_FEATURES) } + || cpu.contains(crate::mldsa65::VG_MLDSA65_VERIFY_SHA3_FEATURES) + { + return crate::mldsa65::vg_mldsa65_verify_sha3; + } + crate::mldsa65::vg_mldsa65_verify + } + /// The first call: chooses the implementation, records it in `CHOSEN` for the calls + /// after it, and runs it. + #[cold] + unsafe extern "C" fn first(pk: *const [u8; 1952], mu: *const [u8; 64], sig: *const [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { + let f = select(); + CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); + // SAFETY: the caller's obligations, which are those of every implementation, `f` + // among them, and the CPU has the features `f` needs. + unsafe { f(pk, mu, sig, scratch) } + } + /// The function to call: `first`, until it records the implementation it chose. + static CHOSEN: core::sync::atomic::AtomicPtr<()> = + core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); + // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, + // or an implementation `select` returned, which needs only features the CPU has. Calling + // either carries the caller's obligations, which are those of every implementation. + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, mu, sig, scratch) } +} + /// ML-DSA-65 message verification with a cached public-key digest. Returns 1 for a valid signature, 0 for an invalid signature or exhausted sampling bound, and 2 if the context exceeds 255 bytes. It is `ML-DSA.Verify(pk, M, σ, ctx)` (FIPS 204 Algorithm 3) with `H(pk, 64)` read from `*tr`: with the public key `*pk`, the `msg_len` bytes of the message at `msg`, the `ctx_len` bytes of the context string at `ctx` and the signature `*sig`, it formats the message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 5), computes `μ = H(tr ‖ M′, 64)`, and verifies `*sig` on `μ` with `*pk`, `ML-DSA.Verify_internal(pk, M′, σ)` (Algorithm 8). /// /// Contract: `VG.Spec.MlDsa.verifyMessageCachedContract`. Timing may depend on the public key, message, context string and signature, and on argument addresses and lengths. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa65_verify_message_cached_sha3` if the CPU supports its features (`VG_MLDSA65_VERIFY_MESSAGE_CACHED_SHA3_FEATURES`); or else `vg_mldsa65_verify_message_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa65_verify_message_cached_sha3` if the CPU supports its features (`VG_MLDSA65_VERIFY_MESSAGE_CACHED_SHA3_FEATURES`); or else `vg_mldsa65_verify_message_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -421,7 +421,7 @@ pub unsafe fn vg_mldsa65_verify_message_cached_auto(_dit: verified_garbage_cpu:: /// /// Contract: `VG.Spec.MlDsa.preparedVerifyMessageContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa65_verify_message_prepared_sha3` if the CPU supports its features (`VG_MLDSA65_VERIFY_MESSAGE_PREPARED_SHA3_FEATURES`); or else `vg_mldsa65_verify_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa65_verify_message_prepared_sha3` if the CPU supports its features (`VG_MLDSA65_VERIFY_MESSAGE_PREPARED_SHA3_FEATURES`); or else `vg_mldsa65_verify_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/mldsa87.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/mldsa87.rs index 9238ff5ea21..15567af576b 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/mldsa87.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/mldsa87.rs @@ -2,50 +2,48 @@ //! The dispatchers of the verified `mldsa87` functions for `aarch64` //! (`crate::mldsa87`, `VG.Rust.dispatcher`). -/// ML-DSA-87 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-87 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. +/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa87_sign_sha3` if the CPU supports its features (`VG_MLDSA87_SIGN_SHA3_FEATURES`); or else `vg_mldsa87_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa87_keygen_sha3` if the CPU supports its features (`VG_MLDSA87_KEYGEN_SHA3_FEATURES`); or else `vg_mldsa87_keygen`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `sk` must be valid for reads of 4896 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `rnd` must be valid for reads of 32 bytes. -/// * `sig` must be valid for reads and writes of 4627 bytes. +/// * `seed` must be valid for reads of 32 bytes. +/// * `pk` must be valid for reads and writes of 2592 bytes. +/// * `sk` must be valid for reads and writes of 4896 bytes. /// * `scratch` must be valid for reads and writes of 144384 bytes. -/// * `sk` must have been written by `vg_mldsa87_keygen`. -/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. +/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 64 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { +pub unsafe fn vg_mldsa87_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 2592], sk: *mut [u8; 4896], scratch: *mut [u64; 18048]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(sk: *const [u8; 4896], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18048]) -> u32; + type Impl = unsafe extern "C" fn(seed: *const [u8; 32], pk: *mut [u8; 2592], sk: *mut [u8; 4896], scratch: *mut [u64; 18048]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_SIGN_SHA3_FEATURES) } - || cpu.contains(crate::mldsa87::VG_MLDSA87_SIGN_SHA3_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_KEYGEN_SHA3_FEATURES) } + || cpu.contains(crate::mldsa87::VG_MLDSA87_KEYGEN_SHA3_FEATURES) { - return crate::mldsa87::vg_mldsa87_sign_sha3; + return crate::mldsa87::vg_mldsa87_keygen_sha3; } - crate::mldsa87::vg_mldsa87_sign + crate::mldsa87::vg_mldsa87_keygen } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(sk: *const [u8; 4896], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { + unsafe extern "C" fn first(seed: *const [u8; 32], pk: *mut [u8; 2592], sk: *mut [u8; 4896], scratch: *mut [u64; 18048]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(sk, mu, rnd, sig, scratch) } + unsafe { f(seed, pk, sk, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -53,14 +51,14 @@ pub unsafe fn vg_mldsa87_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *con // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, mu, rnd, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(seed, pk, sk, scratch) } } /// ML-DSA-87 public-key hash: writes `tr = H(pk, 64)` (FIPS 204 Algorithm 8, line 6), SHAKE256 of the public key `*pk` to 64 bytes, to `*tr`, the digest `vg_mldsa87_verify_message_cached` takes. /// /// Contract: `VG.Spec.MlDsa.pkTrContract`. Constant time: only the pointers may affect timing, not the key, the digest or the working space. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa87_pk_tr_sha3` if the CPU supports its features (`VG_MLDSA87_PK_TR_SHA3_FEATURES`); or else `vg_mldsa87_pk_tr`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa87_pk_tr_sha3` if the CPU supports its features (`VG_MLDSA87_PK_TR_SHA3_FEATURES`); or else `vg_mldsa87_pk_tr`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -106,99 +104,93 @@ pub unsafe fn vg_mldsa87_pk_tr_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *co unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, tr, scratch) } } -/// ML-DSA-87 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-87 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa87_verify_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. +/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa87_keygen_sha3` if the CPU supports its features (`VG_MLDSA87_KEYGEN_SHA3_FEATURES`); or else `vg_mldsa87_keygen`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs `vg_mldsa87_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `seed` must be valid for reads of 32 bytes. -/// * `pk` must be valid for reads and writes of 2592 bytes. -/// * `sk` must be valid for reads and writes of 4896 bytes. -/// * `scratch` must be valid for reads and writes of 144384 bytes. -/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. +/// * `pk` must be valid for reads of 2592 bytes. +/// * `prepared` must be valid for reads and writes of 10784 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). +/// * None of `pk`, `prepared` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 2592], sk: *mut [u8; 4896], scratch: *mut [u64; 18048]) -> u32 { - /// The type of the implementations. - type Impl = unsafe extern "C" fn(seed: *const [u8; 32], pk: *mut [u8; 2592], sk: *mut [u8; 4896], scratch: *mut [u64; 18048]) -> u32; - /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only - /// features the CPU has. - #[cold] - fn select() -> Impl { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_KEYGEN_SHA3_FEATURES) } - || cpu.contains(crate::mldsa87::VG_MLDSA87_KEYGEN_SHA3_FEATURES) - { - return crate::mldsa87::vg_mldsa87_keygen_sha3; - } - crate::mldsa87::vg_mldsa87_keygen - } - /// The first call: chooses the implementation, records it in `CHOSEN` for the calls - /// after it, and runs it. - #[cold] - unsafe extern "C" fn first(seed: *const [u8; 32], pk: *mut [u8; 2592], sk: *mut [u8; 4896], scratch: *mut [u64; 18048]) -> u32 { - let f = select(); - CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); - // SAFETY: the caller's obligations, which are those of every implementation, `f` - // among them, and the CPU has the features `f` needs. - unsafe { f(seed, pk, sk, scratch) } - } - /// The function to call: `first`, until it records the implementation it chose. - static CHOSEN: core::sync::atomic::AtomicPtr<()> = - core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); - // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, - // or an implementation `select` returned, which needs only features the CPU has. Calling - // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(seed, pk, sk, scratch) } +pub unsafe fn vg_mldsa87_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], prepared: *mut [u8; 10784], scratch: *mut [u64; 128]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mldsa87::vg_mldsa87_prepare_verifying_key(pk, prepared, scratch) } } -/// ML-DSA-87 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// ML-DSA-87 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa87_sign_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa87_verify_sha3` if the CPU supports its features (`VG_MLDSA87_VERIFY_SHA3_FEATURES`); or else `vg_mldsa87_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs `vg_mldsa87_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `pk` must be valid for reads of 2592 bytes. +/// * `sk` must be valid for reads of 4896 bytes. +/// * `prepared` must be valid for reads and writes of 28448 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). +/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). +/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). +/// * None of `sk`, `prepared` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mldsa87_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], prepared: *mut [u8; 28448], scratch: *mut [u64; 128]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mldsa87::vg_mldsa87_prepare_signing_key(sk, prepared, scratch) } +} + +/// ML-DSA-87 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// +/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa87_sign_sha3` if the CPU supports its features (`VG_MLDSA87_SIGN_SHA3_FEATURES`); or else `vg_mldsa87_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// +/// # Safety +/// +/// * `sk` must be valid for reads of 4896 bytes. /// * `mu` must be valid for reads of 64 bytes. -/// * `sig` must be valid for reads of 4627 bytes. +/// * `rnd` must be valid for reads of 32 bytes. +/// * `sig` must be valid for reads and writes of 4627 bytes. /// * `scratch` must be valid for reads and writes of 144384 bytes. +/// * `sk` must have been written by `vg_mldsa87_keygen`. +/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). -/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 64 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 64 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], mu: *const [u8; 64], sig: *const [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { +pub unsafe fn vg_mldsa87_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(pk: *const [u8; 2592], mu: *const [u8; 64], sig: *const [u8; 4627], scratch: *mut [u64; 18048]) -> u32; + type Impl = unsafe extern "C" fn(sk: *const [u8; 4896], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18048]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_VERIFY_SHA3_FEATURES) } - || cpu.contains(crate::mldsa87::VG_MLDSA87_VERIFY_SHA3_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_SIGN_SHA3_FEATURES) } + || cpu.contains(crate::mldsa87::VG_MLDSA87_SIGN_SHA3_FEATURES) { - return crate::mldsa87::vg_mldsa87_verify_sha3; + return crate::mldsa87::vg_mldsa87_sign_sha3; } - crate::mldsa87::vg_mldsa87_verify + crate::mldsa87::vg_mldsa87_sign } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(pk: *const [u8; 2592], mu: *const [u8; 64], sig: *const [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { + unsafe extern "C" fn first(sk: *const [u8; 4896], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(pk, mu, sig, scratch) } + unsafe { f(sk, mu, rnd, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -206,14 +198,14 @@ pub unsafe fn vg_mldsa87_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *c // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, mu, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, mu, rnd, sig, scratch) } } /// ML-DSA-87 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. /// /// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa87_sign_message_sha3` if the CPU supports its features (`VG_MLDSA87_SIGN_MESSAGE_SHA3_FEATURES`); or else `vg_mldsa87_sign_message`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa87_sign_message_sha3` if the CPU supports its features (`VG_MLDSA87_SIGN_MESSAGE_SHA3_FEATURES`); or else `vg_mldsa87_sign_message`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -264,54 +256,11 @@ pub unsafe fn vg_mldsa87_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } -/// ML-DSA-87 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa87_sign_message_prepared`. -/// -/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. -/// -/// It runs `vg_mldsa87_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `sk` must be valid for reads of 4896 bytes. -/// * `prepared` must be valid for reads and writes of 28448 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. -/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). -/// * None of `sk`, `prepared` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mldsa87_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], prepared: *mut [u8; 28448], scratch: *mut [u64; 128]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_prepare_signing_key(sk, prepared, scratch) } -} - -/// ML-DSA-87 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa87_verify_message_prepared`. -/// -/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. -/// -/// It runs `vg_mldsa87_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 2592 bytes. -/// * `prepared` must be valid for reads and writes of 10784 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. -/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). -/// * None of `pk`, `prepared` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mldsa87_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], prepared: *mut [u8; 10784], scratch: *mut [u64; 128]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_prepare_verifying_key(pk, prepared, scratch) } -} - /// ML-DSA-87 signing with a prepared private key: `vg_mldsa87_sign_message` (with its results, timing and leakage) of the private key that `*sk` begins with. /// /// Contract: `VG.Spec.MlDsa.preparedSignMessageContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa87_sign_message_prepared_sha3` if the CPU supports its features (`VG_MLDSA87_SIGN_MESSAGE_PREPARED_SHA3_FEATURES`); or else `vg_mldsa87_sign_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa87_sign_message_prepared_sha3` if the CPU supports its features (`VG_MLDSA87_SIGN_MESSAGE_PREPARED_SHA3_FEATURES`); or else `vg_mldsa87_sign_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -363,11 +312,62 @@ pub unsafe fn vg_mldsa87_sign_message_prepared_auto(_dit: verified_garbage_cpu:: unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } +/// ML-DSA-87 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// +/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa87_verify_sha3` if the CPU supports its features (`VG_MLDSA87_VERIFY_SHA3_FEATURES`); or else `vg_mldsa87_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 2592 bytes. +/// * `mu` must be valid for reads of 64 bytes. +/// * `sig` must be valid for reads of 4627 bytes. +/// * `scratch` must be valid for reads and writes of 144384 bytes. +/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). +/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). +/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 64 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mldsa87_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], mu: *const [u8; 64], sig: *const [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { + /// The type of the implementations. + type Impl = unsafe extern "C" fn(pk: *const [u8; 2592], mu: *const [u8; 64], sig: *const [u8; 4627], scratch: *mut [u64; 18048]) -> u32; + /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only + /// features the CPU has. + #[cold] + fn select() -> Impl { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_VERIFY_SHA3_FEATURES) } + || cpu.contains(crate::mldsa87::VG_MLDSA87_VERIFY_SHA3_FEATURES) + { + return crate::mldsa87::vg_mldsa87_verify_sha3; + } + crate::mldsa87::vg_mldsa87_verify + } + /// The first call: chooses the implementation, records it in `CHOSEN` for the calls + /// after it, and runs it. + #[cold] + unsafe extern "C" fn first(pk: *const [u8; 2592], mu: *const [u8; 64], sig: *const [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { + let f = select(); + CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); + // SAFETY: the caller's obligations, which are those of every implementation, `f` + // among them, and the CPU has the features `f` needs. + unsafe { f(pk, mu, sig, scratch) } + } + /// The function to call: `first`, until it records the implementation it chose. + static CHOSEN: core::sync::atomic::AtomicPtr<()> = + core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); + // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, + // or an implementation `select` returned, which needs only features the CPU has. Calling + // either carries the caller's obligations, which are those of every implementation. + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, mu, sig, scratch) } +} + /// ML-DSA-87 message verification with a cached public-key digest. Returns 1 for a valid signature, 0 for an invalid signature or exhausted sampling bound, and 2 if the context exceeds 255 bytes. It is `ML-DSA.Verify(pk, M, σ, ctx)` (FIPS 204 Algorithm 3) with `H(pk, 64)` read from `*tr`: with the public key `*pk`, the `msg_len` bytes of the message at `msg`, the `ctx_len` bytes of the context string at `ctx` and the signature `*sig`, it formats the message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 5), computes `μ = H(tr ‖ M′, 64)`, and verifies `*sig` on `μ` with `*pk`, `ML-DSA.Verify_internal(pk, M′, σ)` (Algorithm 8). /// /// Contract: `VG.Spec.MlDsa.verifyMessageCachedContract`. Timing may depend on the public key, message, context string and signature, and on argument addresses and lengths. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa87_verify_message_cached_sha3` if the CPU supports its features (`VG_MLDSA87_VERIFY_MESSAGE_CACHED_SHA3_FEATURES`); or else `vg_mldsa87_verify_message_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa87_verify_message_cached_sha3` if the CPU supports its features (`VG_MLDSA87_VERIFY_MESSAGE_CACHED_SHA3_FEATURES`); or else `vg_mldsa87_verify_message_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -421,7 +421,7 @@ pub unsafe fn vg_mldsa87_verify_message_cached_auto(_dit: verified_garbage_cpu:: /// /// Contract: `VG.Spec.MlDsa.preparedVerifyMessageContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa87_verify_message_prepared_sha3` if the CPU supports its features (`VG_MLDSA87_VERIFY_MESSAGE_PREPARED_SHA3_FEATURES`); or else `vg_mldsa87_verify_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa87_verify_message_prepared_sha3` if the CPU supports its features (`VG_MLDSA87_VERIFY_MESSAGE_PREPARED_SHA3_FEATURES`); or else `vg_mldsa87_verify_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/mlkem1024.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/mlkem1024.rs index c8c33558b60..5da5f8519d5 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/mlkem1024.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/mlkem1024.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.MlKem1024.keyGenContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of the encapsulation key), but not on anything else of the seed or the keys. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mlkem1024_keygen_x2_sha3_keccak_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_MLKEM1024_KEYGEN_X2_SHA3_KECCAK_SHA3_FEATURES`); `vg_mlkem1024_keygen_x2_sha3` if the CPU supports its features (`VG_MLKEM1024_KEYGEN_X2_SHA3_FEATURES`); or else `vg_mlkem1024_keygen`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mlkem1024_keygen_x2_sha3_keccak_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_MLKEM1024_KEYGEN_X2_SHA3_KECCAK_SHA3_FEATURES`); `vg_mlkem1024_keygen_x2_sha3` if the CPU supports its features (`VG_MLKEM1024_KEYGEN_X2_SHA3_FEATURES`); or else `vg_mlkem1024_keygen`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -64,7 +64,7 @@ pub unsafe fn vg_mlkem1024_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed /// /// Contract: `VG.Spec.MlKem1024.encapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on the randomness or on the outputs. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mlkem1024_encaps_x2_sha3_keccak_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_MLKEM1024_ENCAPS_X2_SHA3_KECCAK_SHA3_FEATURES`); `vg_mlkem1024_encaps_x2_sha3` if the CPU supports its features (`VG_MLKEM1024_ENCAPS_X2_SHA3_FEATURES`); or else `vg_mlkem1024_encaps`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mlkem1024_encaps_x2_sha3_keccak_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_MLKEM1024_ENCAPS_X2_SHA3_KECCAK_SHA3_FEATURES`); `vg_mlkem1024_encaps_x2_sha3` if the CPU supports its features (`VG_MLKEM1024_ENCAPS_X2_SHA3_FEATURES`); or else `vg_mlkem1024_encaps`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -120,54 +120,58 @@ pub unsafe fn vg_mlkem1024_encaps_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ek, m, key, ct, scratch) } } -/// ML-KEM-1024 decapsulation, `ML-KEM.Decaps_internal(dk, c)` (FIPS 203 Algorithm 18): with the decapsulation key `*dk` and the ciphertext `*ct`, writes the shared secret key to `*key`, which is the implicit rejection key `J(z ‖ c)` if the ciphertext does not re-encrypt to itself. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-KEM-1024 encapsulation with given randomness and the hash of the key, `ML-KEM.Encaps_internal(ek, m)` (FIPS 203 Algorithm 17): with the encapsulation key `*ek`, its hash `H(ek)` in `*h` and the randomness `*m`, writes the shared secret key to `*key` and the ciphertext to `*ct`, as `vg_mlkem1024_encaps` does, using `*h` instead of hashing `*ek`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlKem1024.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 3072–3103 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. +/// Contract: `VG.Spec.MlKem1024.encapsHContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on its hash, on the randomness or on the outputs. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mlkem1024_decaps_x2_sha3_keccak_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_MLKEM1024_DECAPS_X2_SHA3_KECCAK_SHA3_FEATURES`); `vg_mlkem1024_decaps_x2_sha3` if the CPU supports its features (`VG_MLKEM1024_DECAPS_X2_SHA3_FEATURES`); or else `vg_mlkem1024_decaps`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mlkem1024_encaps_h_x2_sha3_keccak_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_MLKEM1024_ENCAPS_H_X2_SHA3_KECCAK_SHA3_FEATURES`); `vg_mlkem1024_encaps_h_x2_sha3` if the CPU supports its features (`VG_MLKEM1024_ENCAPS_H_X2_SHA3_FEATURES`); or else `vg_mlkem1024_encaps_h`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `dk` must be valid for reads of 3168 bytes. -/// * `ct` must be valid for reads of 1568 bytes. +/// * `ek` must be valid for reads of 1568 bytes. +/// * `h` must be valid for reads of 32 bytes. +/// * `m` must be valid for reads of 32 bytes. /// * `key` must be valid for reads and writes of 32 bytes. +/// * `ct` must be valid for reads and writes of 1568 bytes. /// * `scratch` must be valid for reads and writes of 49152 bytes. -/// * `dk` must have been written by `vg_mlkem1024_keygen` (so that it passes the checks of FIPS 203 §7.3). +/// * `ek` must have passed `vg_mlkem1024_check_ek` (FIPS 203 §7.2). +/// * `h` must hold `H(ek)`, the SHA3-256 hash of the bytes of `*ek` (FIPS 203 §4.1). +/// * `m` must be fresh random bytes from an approved RBG (FIPS 203 §3.3). /// * `scratch` is working space: on return each of its bytes is unchanged or zero. -/// * `key` and `scratch` must not overlap each other, `dk` or `ct` (distinct Rust objects never do). -/// * None of `dk`, `ct`, `key` and `scratch` may overlap the 160 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `key`, `ct` and `scratch` must not overlap each other, `ek`, `h` or `m` (distinct Rust objects never do). +/// * None of `ek`, `h`, `m`, `key`, `ct` and `scratch` may overlap the 160 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mlkem1024_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: *const [u8; 3168], ct: *const [u8; 1568], key: *mut [u8; 32], scratch: *mut [u64; 6144]) -> u32 { +pub unsafe fn vg_mlkem1024_encaps_h_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1568], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1568], scratch: *mut [u64; 6144]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(dk: *const [u8; 3168], ct: *const [u8; 1568], key: *mut [u8; 32], scratch: *mut [u64; 6144]) -> u32; + type Impl = unsafe extern "C" fn(ek: *const [u8; 1568], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1568], scratch: *mut [u64; 6144]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::mlkem1024::VG_MLKEM1024_DECAPS_X2_SHA3_KECCAK_SHA3_FEATURES)) - && (const { verified_garbage_cpu::target_contains(crate::mlkem1024::VG_MLKEM1024_DECAPS_X2_SHA3_KECCAK_SHA3_FEATURES) } - || cpu.contains(crate::mlkem1024::VG_MLKEM1024_DECAPS_X2_SHA3_KECCAK_SHA3_FEATURES)) + if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_X2_SHA3_KECCAK_SHA3_FEATURES)) + && (const { verified_garbage_cpu::target_contains(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_X2_SHA3_KECCAK_SHA3_FEATURES) } + || cpu.contains(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_X2_SHA3_KECCAK_SHA3_FEATURES)) { - return crate::mlkem1024::vg_mlkem1024_decaps_x2_sha3_keccak_sha3; + return crate::mlkem1024::vg_mlkem1024_encaps_h_x2_sha3_keccak_sha3; } - if const { verified_garbage_cpu::target_contains(crate::mlkem1024::VG_MLKEM1024_DECAPS_X2_SHA3_FEATURES) } - || cpu.contains(crate::mlkem1024::VG_MLKEM1024_DECAPS_X2_SHA3_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_X2_SHA3_FEATURES) } + || cpu.contains(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_X2_SHA3_FEATURES) { - return crate::mlkem1024::vg_mlkem1024_decaps_x2_sha3; + return crate::mlkem1024::vg_mlkem1024_encaps_h_x2_sha3; } - crate::mlkem1024::vg_mlkem1024_decaps + crate::mlkem1024::vg_mlkem1024_encaps_h } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(dk: *const [u8; 3168], ct: *const [u8; 1568], key: *mut [u8; 32], scratch: *mut [u64; 6144]) -> u32 { + unsafe extern "C" fn first(ek: *const [u8; 1568], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1568], scratch: *mut [u64; 6144]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(dk, ct, key, scratch) } + unsafe { f(ek, h, m, key, ct, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -175,61 +179,57 @@ pub unsafe fn vg_mlkem1024_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(dk, ct, key, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ek, h, m, key, ct, scratch) } } -/// ML-KEM-1024 encapsulation with given randomness and the hash of the key, `ML-KEM.Encaps_internal(ek, m)` (FIPS 203 Algorithm 17): with the encapsulation key `*ek`, its hash `H(ek)` in `*h` and the randomness `*m`, writes the shared secret key to `*key` and the ciphertext to `*ct`, as `vg_mlkem1024_encaps` does, using `*h` instead of hashing `*ek`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-KEM-1024 decapsulation, `ML-KEM.Decaps_internal(dk, c)` (FIPS 203 Algorithm 18): with the decapsulation key `*dk` and the ciphertext `*ct`, writes the shared secret key to `*key`, which is the implicit rejection key `J(z ‖ c)` if the ciphertext does not re-encrypt to itself. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlKem1024.encapsHContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on its hash, on the randomness or on the outputs. +/// Contract: `VG.Spec.MlKem1024.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 3072–3103 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mlkem1024_encaps_h_x2_sha3_keccak_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_MLKEM1024_ENCAPS_H_X2_SHA3_KECCAK_SHA3_FEATURES`); `vg_mlkem1024_encaps_h_x2_sha3` if the CPU supports its features (`VG_MLKEM1024_ENCAPS_H_X2_SHA3_FEATURES`); or else `vg_mlkem1024_encaps_h`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mlkem1024_decaps_x2_sha3_keccak_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_MLKEM1024_DECAPS_X2_SHA3_KECCAK_SHA3_FEATURES`); `vg_mlkem1024_decaps_x2_sha3` if the CPU supports its features (`VG_MLKEM1024_DECAPS_X2_SHA3_FEATURES`); or else `vg_mlkem1024_decaps`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `ek` must be valid for reads of 1568 bytes. -/// * `h` must be valid for reads of 32 bytes. -/// * `m` must be valid for reads of 32 bytes. +/// * `dk` must be valid for reads of 3168 bytes. +/// * `ct` must be valid for reads of 1568 bytes. /// * `key` must be valid for reads and writes of 32 bytes. -/// * `ct` must be valid for reads and writes of 1568 bytes. /// * `scratch` must be valid for reads and writes of 49152 bytes. -/// * `ek` must have passed `vg_mlkem1024_check_ek` (FIPS 203 §7.2). -/// * `h` must hold `H(ek)`, the SHA3-256 hash of the bytes of `*ek` (FIPS 203 §4.1). -/// * `m` must be fresh random bytes from an approved RBG (FIPS 203 §3.3). +/// * `dk` must have been written by `vg_mlkem1024_keygen` (so that it passes the checks of FIPS 203 §7.3). /// * `scratch` is working space: on return each of its bytes is unchanged or zero. -/// * `key`, `ct` and `scratch` must not overlap each other, `ek`, `h` or `m` (distinct Rust objects never do). -/// * None of `ek`, `h`, `m`, `key`, `ct` and `scratch` may overlap the 160 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `key` and `scratch` must not overlap each other, `dk` or `ct` (distinct Rust objects never do). +/// * None of `dk`, `ct`, `key` and `scratch` may overlap the 160 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mlkem1024_encaps_h_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1568], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1568], scratch: *mut [u64; 6144]) -> u32 { +pub unsafe fn vg_mlkem1024_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: *const [u8; 3168], ct: *const [u8; 1568], key: *mut [u8; 32], scratch: *mut [u64; 6144]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ek: *const [u8; 1568], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1568], scratch: *mut [u64; 6144]) -> u32; + type Impl = unsafe extern "C" fn(dk: *const [u8; 3168], ct: *const [u8; 1568], key: *mut [u8; 32], scratch: *mut [u64; 6144]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_X2_SHA3_KECCAK_SHA3_FEATURES)) - && (const { verified_garbage_cpu::target_contains(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_X2_SHA3_KECCAK_SHA3_FEATURES) } - || cpu.contains(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_X2_SHA3_KECCAK_SHA3_FEATURES)) + if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::mlkem1024::VG_MLKEM1024_DECAPS_X2_SHA3_KECCAK_SHA3_FEATURES)) + && (const { verified_garbage_cpu::target_contains(crate::mlkem1024::VG_MLKEM1024_DECAPS_X2_SHA3_KECCAK_SHA3_FEATURES) } + || cpu.contains(crate::mlkem1024::VG_MLKEM1024_DECAPS_X2_SHA3_KECCAK_SHA3_FEATURES)) { - return crate::mlkem1024::vg_mlkem1024_encaps_h_x2_sha3_keccak_sha3; + return crate::mlkem1024::vg_mlkem1024_decaps_x2_sha3_keccak_sha3; } - if const { verified_garbage_cpu::target_contains(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_X2_SHA3_FEATURES) } - || cpu.contains(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_X2_SHA3_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mlkem1024::VG_MLKEM1024_DECAPS_X2_SHA3_FEATURES) } + || cpu.contains(crate::mlkem1024::VG_MLKEM1024_DECAPS_X2_SHA3_FEATURES) { - return crate::mlkem1024::vg_mlkem1024_encaps_h_x2_sha3; + return crate::mlkem1024::vg_mlkem1024_decaps_x2_sha3; } - crate::mlkem1024::vg_mlkem1024_encaps_h + crate::mlkem1024::vg_mlkem1024_decaps } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ek: *const [u8; 1568], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1568], scratch: *mut [u64; 6144]) -> u32 { + unsafe extern "C" fn first(dk: *const [u8; 3168], ct: *const [u8; 1568], key: *mut [u8; 32], scratch: *mut [u64; 6144]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ek, h, m, key, ct, scratch) } + unsafe { f(dk, ct, key, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -237,7 +237,7 @@ pub unsafe fn vg_mlkem1024_encaps_h_auto(_dit: verified_garbage_cpu::Dit<'_>, ek // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ek, h, m, key, ct, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(dk, ct, key, scratch) } } /// The ML-KEM-1024 encapsulation key check (FIPS 203 §7.2): returns 1 if every 12-bit integer that the first 1536 bytes of `*ek` encode is less than `q` = 3329 (the modulus check), and 0 otherwise. An encapsulation key must pass it before it is given to `vg_mlkem1024_encaps`. diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/mlkem768.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/mlkem768.rs index 2419b5309c9..9f3f6353a27 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/mlkem768.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/mlkem768.rs @@ -2,11 +2,28 @@ //! The dispatchers of the verified `mlkem768` functions for `aarch64` //! (`crate::mlkem768`, `VG.Rust.dispatcher`). +/// The ML-KEM-768 encapsulation key check (FIPS 203 §7.2): returns 1 if every 12-bit integer that the first 1152 bytes of `*ek` encode is less than `q` = 3329 (the modulus check), and 0 otherwise. An encapsulation key must pass it before it is given to `vg_mlkem768_encaps`. +/// +/// Contract: `VG.Spec.MlKem.checkEkContract`. Constant time: only the pointer may affect timing. +/// +/// It runs `vg_mlkem768_check_ek`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `ek` must be valid for reads of 1184 bytes. +/// * `ek` must not wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mlkem768_check_ek_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1184]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mlkem768::vg_mlkem768_check_ek(ek) } +} + /// ML-KEM-768 key generation from a seed, `ML-KEM.KeyGen_internal(d, z)` (FIPS 203 Algorithm 16): with `d` in bytes 0–31 of `*seed` and `z` in bytes 32–63, writes the encapsulation key to `*ek` and the decapsulation key to `*dk`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// /// Contract: `VG.Spec.MlKem.keyGenContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of the encapsulation key), but not on anything else of the seed or the keys. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mlkem768_keygen_x2_sha3_keccak_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_MLKEM768_KEYGEN_X2_SHA3_KECCAK_SHA3_FEATURES`); `vg_mlkem768_keygen_x2_sha3` if the CPU supports its features (`VG_MLKEM768_KEYGEN_X2_SHA3_FEATURES`); or else `vg_mlkem768_keygen`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mlkem768_keygen_x2_sha3_keccak_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_MLKEM768_KEYGEN_X2_SHA3_KECCAK_SHA3_FEATURES`); `vg_mlkem768_keygen_x2_sha3` if the CPU supports its features (`VG_MLKEM768_KEYGEN_X2_SHA3_FEATURES`); or else `vg_mlkem768_keygen`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -64,7 +81,7 @@ pub unsafe fn vg_mlkem768_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: /// /// Contract: `VG.Spec.MlKem.encapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on the randomness or on the outputs. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mlkem768_encaps_x2_sha3_keccak_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_MLKEM768_ENCAPS_X2_SHA3_KECCAK_SHA3_FEATURES`); `vg_mlkem768_encaps_x2_sha3` if the CPU supports its features (`VG_MLKEM768_ENCAPS_X2_SHA3_FEATURES`); or else `vg_mlkem768_encaps`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mlkem768_encaps_x2_sha3_keccak_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_MLKEM768_ENCAPS_X2_SHA3_KECCAK_SHA3_FEATURES`); `vg_mlkem768_encaps_x2_sha3` if the CPU supports its features (`VG_MLKEM768_ENCAPS_X2_SHA3_FEATURES`); or else `vg_mlkem768_encaps`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -120,54 +137,58 @@ pub unsafe fn vg_mlkem768_encaps_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: * unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ek, m, key, ct, scratch) } } -/// ML-KEM-768 decapsulation, `ML-KEM.Decaps_internal(dk, c)` (FIPS 203 Algorithm 18): with the decapsulation key `*dk` and the ciphertext `*ct`, writes the shared secret key to `*key`, which is the implicit rejection key `J(z ‖ c)` if the ciphertext does not re-encrypt to itself. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-KEM-768 encapsulation with given randomness and the hash of the key, `ML-KEM.Encaps_internal(ek, m)` (FIPS 203 Algorithm 17): with the encapsulation key `*ek`, its hash `H(ek)` in `*h` and the randomness `*m`, writes the shared secret key to `*key` and the ciphertext to `*ct`, as `vg_mlkem768_encaps` does, using `*h` instead of hashing `*ek`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlKem.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 2304–2335 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. +/// Contract: `VG.Spec.MlKem.encapsHContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on its hash, on the randomness or on the outputs. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mlkem768_decaps_x2_sha3_keccak_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_MLKEM768_DECAPS_X2_SHA3_KECCAK_SHA3_FEATURES`); `vg_mlkem768_decaps_x2_sha3` if the CPU supports its features (`VG_MLKEM768_DECAPS_X2_SHA3_FEATURES`); or else `vg_mlkem768_decaps`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mlkem768_encaps_h_x2_sha3_keccak_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_MLKEM768_ENCAPS_H_X2_SHA3_KECCAK_SHA3_FEATURES`); `vg_mlkem768_encaps_h_x2_sha3` if the CPU supports its features (`VG_MLKEM768_ENCAPS_H_X2_SHA3_FEATURES`); or else `vg_mlkem768_encaps_h`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `dk` must be valid for reads of 2400 bytes. -/// * `ct` must be valid for reads of 1088 bytes. +/// * `ek` must be valid for reads of 1184 bytes. +/// * `h` must be valid for reads of 32 bytes. +/// * `m` must be valid for reads of 32 bytes. /// * `key` must be valid for reads and writes of 32 bytes. +/// * `ct` must be valid for reads and writes of 1088 bytes. /// * `scratch` must be valid for reads and writes of 32768 bytes. -/// * `dk` must have been written by `vg_mlkem768_keygen` (so that it passes the checks of FIPS 203 §7.3). +/// * `ek` must have passed `vg_mlkem768_check_ek` (FIPS 203 §7.2). +/// * `h` must hold `H(ek)`, the SHA3-256 hash of the bytes of `*ek` (FIPS 203 §4.1). +/// * `m` must be fresh random bytes from an approved RBG (FIPS 203 §3.3). /// * `scratch` is working space: on return each of its bytes is unchanged or zero. -/// * `key` and `scratch` must not overlap each other, `dk` or `ct` (distinct Rust objects never do). -/// * None of `dk`, `ct`, `key` and `scratch` may overlap the 160 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `key`, `ct` and `scratch` must not overlap each other, `ek`, `h` or `m` (distinct Rust objects never do). +/// * None of `ek`, `h`, `m`, `key`, `ct` and `scratch` may overlap the 160 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mlkem768_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: *const [u8; 2400], ct: *const [u8; 1088], key: *mut [u8; 32], scratch: *mut [u64; 4096]) -> u32 { +pub unsafe fn vg_mlkem768_encaps_h_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1184], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1088], scratch: *mut [u64; 4096]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(dk: *const [u8; 2400], ct: *const [u8; 1088], key: *mut [u8; 32], scratch: *mut [u64; 4096]) -> u32; + type Impl = unsafe extern "C" fn(ek: *const [u8; 1184], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1088], scratch: *mut [u64; 4096]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::mlkem768::VG_MLKEM768_DECAPS_X2_SHA3_KECCAK_SHA3_FEATURES)) - && (const { verified_garbage_cpu::target_contains(crate::mlkem768::VG_MLKEM768_DECAPS_X2_SHA3_KECCAK_SHA3_FEATURES) } - || cpu.contains(crate::mlkem768::VG_MLKEM768_DECAPS_X2_SHA3_KECCAK_SHA3_FEATURES)) + if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::mlkem768::VG_MLKEM768_ENCAPS_H_X2_SHA3_KECCAK_SHA3_FEATURES)) + && (const { verified_garbage_cpu::target_contains(crate::mlkem768::VG_MLKEM768_ENCAPS_H_X2_SHA3_KECCAK_SHA3_FEATURES) } + || cpu.contains(crate::mlkem768::VG_MLKEM768_ENCAPS_H_X2_SHA3_KECCAK_SHA3_FEATURES)) { - return crate::mlkem768::vg_mlkem768_decaps_x2_sha3_keccak_sha3; + return crate::mlkem768::vg_mlkem768_encaps_h_x2_sha3_keccak_sha3; } - if const { verified_garbage_cpu::target_contains(crate::mlkem768::VG_MLKEM768_DECAPS_X2_SHA3_FEATURES) } - || cpu.contains(crate::mlkem768::VG_MLKEM768_DECAPS_X2_SHA3_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mlkem768::VG_MLKEM768_ENCAPS_H_X2_SHA3_FEATURES) } + || cpu.contains(crate::mlkem768::VG_MLKEM768_ENCAPS_H_X2_SHA3_FEATURES) { - return crate::mlkem768::vg_mlkem768_decaps_x2_sha3; + return crate::mlkem768::vg_mlkem768_encaps_h_x2_sha3; } - crate::mlkem768::vg_mlkem768_decaps + crate::mlkem768::vg_mlkem768_encaps_h } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(dk: *const [u8; 2400], ct: *const [u8; 1088], key: *mut [u8; 32], scratch: *mut [u64; 4096]) -> u32 { + unsafe extern "C" fn first(ek: *const [u8; 1184], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1088], scratch: *mut [u64; 4096]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(dk, ct, key, scratch) } + unsafe { f(ek, h, m, key, ct, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -175,78 +196,57 @@ pub unsafe fn vg_mlkem768_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: * // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(dk, ct, key, scratch) } -} - -/// The ML-KEM-768 encapsulation key check (FIPS 203 §7.2): returns 1 if every 12-bit integer that the first 1152 bytes of `*ek` encode is less than `q` = 3329 (the modulus check), and 0 otherwise. An encapsulation key must pass it before it is given to `vg_mlkem768_encaps`. -/// -/// Contract: `VG.Spec.MlKem.checkEkContract`. Constant time: only the pointer may affect timing. -/// -/// It runs `vg_mlkem768_check_ek`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `ek` must be valid for reads of 1184 bytes. -/// * `ek` must not wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mlkem768_check_ek_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1184]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mlkem768::vg_mlkem768_check_ek(ek) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ek, h, m, key, ct, scratch) } } -/// ML-KEM-768 encapsulation with given randomness and the hash of the key, `ML-KEM.Encaps_internal(ek, m)` (FIPS 203 Algorithm 17): with the encapsulation key `*ek`, its hash `H(ek)` in `*h` and the randomness `*m`, writes the shared secret key to `*key` and the ciphertext to `*ct`, as `vg_mlkem768_encaps` does, using `*h` instead of hashing `*ek`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-KEM-768 decapsulation, `ML-KEM.Decaps_internal(dk, c)` (FIPS 203 Algorithm 18): with the decapsulation key `*dk` and the ciphertext `*ct`, writes the shared secret key to `*key`, which is the implicit rejection key `J(z ‖ c)` if the ciphertext does not re-encrypt to itself. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlKem.encapsHContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on its hash, on the randomness or on the outputs. +/// Contract: `VG.Spec.MlKem.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 2304–2335 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mlkem768_encaps_h_x2_sha3_keccak_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_MLKEM768_ENCAPS_H_X2_SHA3_KECCAK_SHA3_FEATURES`); `vg_mlkem768_encaps_h_x2_sha3` if the CPU supports its features (`VG_MLKEM768_ENCAPS_H_X2_SHA3_FEATURES`); or else `vg_mlkem768_encaps_h`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mlkem768_decaps_x2_sha3_keccak_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_MLKEM768_DECAPS_X2_SHA3_KECCAK_SHA3_FEATURES`); `vg_mlkem768_decaps_x2_sha3` if the CPU supports its features (`VG_MLKEM768_DECAPS_X2_SHA3_FEATURES`); or else `vg_mlkem768_decaps`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `ek` must be valid for reads of 1184 bytes. -/// * `h` must be valid for reads of 32 bytes. -/// * `m` must be valid for reads of 32 bytes. +/// * `dk` must be valid for reads of 2400 bytes. +/// * `ct` must be valid for reads of 1088 bytes. /// * `key` must be valid for reads and writes of 32 bytes. -/// * `ct` must be valid for reads and writes of 1088 bytes. /// * `scratch` must be valid for reads and writes of 32768 bytes. -/// * `ek` must have passed `vg_mlkem768_check_ek` (FIPS 203 §7.2). -/// * `h` must hold `H(ek)`, the SHA3-256 hash of the bytes of `*ek` (FIPS 203 §4.1). -/// * `m` must be fresh random bytes from an approved RBG (FIPS 203 §3.3). +/// * `dk` must have been written by `vg_mlkem768_keygen` (so that it passes the checks of FIPS 203 §7.3). /// * `scratch` is working space: on return each of its bytes is unchanged or zero. -/// * `key`, `ct` and `scratch` must not overlap each other, `ek`, `h` or `m` (distinct Rust objects never do). -/// * None of `ek`, `h`, `m`, `key`, `ct` and `scratch` may overlap the 160 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `key` and `scratch` must not overlap each other, `dk` or `ct` (distinct Rust objects never do). +/// * None of `dk`, `ct`, `key` and `scratch` may overlap the 160 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mlkem768_encaps_h_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1184], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1088], scratch: *mut [u64; 4096]) -> u32 { +pub unsafe fn vg_mlkem768_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: *const [u8; 2400], ct: *const [u8; 1088], key: *mut [u8; 32], scratch: *mut [u64; 4096]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ek: *const [u8; 1184], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1088], scratch: *mut [u64; 4096]) -> u32; + type Impl = unsafe extern "C" fn(dk: *const [u8; 2400], ct: *const [u8; 1088], key: *mut [u8; 32], scratch: *mut [u64; 4096]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::mlkem768::VG_MLKEM768_ENCAPS_H_X2_SHA3_KECCAK_SHA3_FEATURES)) - && (const { verified_garbage_cpu::target_contains(crate::mlkem768::VG_MLKEM768_ENCAPS_H_X2_SHA3_KECCAK_SHA3_FEATURES) } - || cpu.contains(crate::mlkem768::VG_MLKEM768_ENCAPS_H_X2_SHA3_KECCAK_SHA3_FEATURES)) + if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::mlkem768::VG_MLKEM768_DECAPS_X2_SHA3_KECCAK_SHA3_FEATURES)) + && (const { verified_garbage_cpu::target_contains(crate::mlkem768::VG_MLKEM768_DECAPS_X2_SHA3_KECCAK_SHA3_FEATURES) } + || cpu.contains(crate::mlkem768::VG_MLKEM768_DECAPS_X2_SHA3_KECCAK_SHA3_FEATURES)) { - return crate::mlkem768::vg_mlkem768_encaps_h_x2_sha3_keccak_sha3; + return crate::mlkem768::vg_mlkem768_decaps_x2_sha3_keccak_sha3; } - if const { verified_garbage_cpu::target_contains(crate::mlkem768::VG_MLKEM768_ENCAPS_H_X2_SHA3_FEATURES) } - || cpu.contains(crate::mlkem768::VG_MLKEM768_ENCAPS_H_X2_SHA3_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mlkem768::VG_MLKEM768_DECAPS_X2_SHA3_FEATURES) } + || cpu.contains(crate::mlkem768::VG_MLKEM768_DECAPS_X2_SHA3_FEATURES) { - return crate::mlkem768::vg_mlkem768_encaps_h_x2_sha3; + return crate::mlkem768::vg_mlkem768_decaps_x2_sha3; } - crate::mlkem768::vg_mlkem768_encaps_h + crate::mlkem768::vg_mlkem768_decaps } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ek: *const [u8; 1184], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1088], scratch: *mut [u64; 4096]) -> u32 { + unsafe extern "C" fn first(dk: *const [u8; 2400], ct: *const [u8; 1088], key: *mut [u8; 32], scratch: *mut [u64; 4096]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ek, h, m, key, ct, scratch) } + unsafe { f(dk, ct, key, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -254,5 +254,5 @@ pub unsafe fn vg_mlkem768_encaps_h_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ek, h, m, key, ct, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(dk, ct, key, scratch) } } diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha1.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha1.rs index 24d2f45b2fa..bb1fd164c3c 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha1.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha1.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.pbkdf2Contract` of `VG.Spec.Hmac.sha1I`. Constant time: only the pointers, the lengths and `c` may affect timing, not the password, the salt or the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_pbkdf2_hmac_sha1_sha2` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA1_SHA2_FEATURES`); or else `vg_pbkdf2_hmac_sha1`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_pbkdf2_hmac_sha1_sha2` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA1_SHA2_FEATURES`); or else `vg_pbkdf2_hmac_sha1`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha224.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha224.rs index 1944cec4556..750017838ec 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha224.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha224.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.pbkdf2Contract` of `VG.Spec.Hmac.sha224I`. Constant time: only the pointers, the lengths and `c` may affect timing, not the password, the salt or the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_pbkdf2_hmac_sha224_sha2` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA224_SHA2_FEATURES`); or else `vg_pbkdf2_hmac_sha224`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_pbkdf2_hmac_sha224_sha2` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA224_SHA2_FEATURES`); or else `vg_pbkdf2_hmac_sha224`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha256.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha256.rs index 78a7a96df0f..c197ef3fcee 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha256.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha256.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.pbkdf2Contract` of `VG.Spec.Hmac.sha256I`. Constant time: only the pointers, the lengths and `c` may affect timing, not the password, the salt or the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_pbkdf2_hmac_sha256_sha2` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA256_SHA2_FEATURES`); or else `vg_pbkdf2_hmac_sha256`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_pbkdf2_hmac_sha256_sha2` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA256_SHA2_FEATURES`); or else `vg_pbkdf2_hmac_sha256`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha384.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha384.rs index 6521de61434..3b00f706907 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha384.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha384.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.pbkdf2Contract` of `VG.Spec.Hmac.sha384I`. Constant time: only the pointers, the lengths and `c` may affect timing, not the password, the salt or the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_pbkdf2_hmac_sha384_sha512` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA384_SHA512_FEATURES`); or else `vg_pbkdf2_hmac_sha384`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_pbkdf2_hmac_sha384_sha512` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA384_SHA512_FEATURES`); or else `vg_pbkdf2_hmac_sha384`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha512.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha512.rs index c517e327f1c..718461560aa 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha512.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha512.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.pbkdf2Contract` of `VG.Spec.Hmac.sha512I`. Constant time: only the pointers, the lengths and `c` may affect timing, not the password, the salt or the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_pbkdf2_hmac_sha512_sha512` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA512_SHA512_FEATURES`); or else `vg_pbkdf2_hmac_sha512`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_pbkdf2_hmac_sha512_sha512` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA512_SHA512_FEATURES`); or else `vg_pbkdf2_hmac_sha512`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha512_224.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha512_224.rs index 2ddd6e496db..bec42ef9258 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha512_224.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha512_224.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.pbkdf2Contract` of `VG.Spec.Hmac.sha512_224I`. Constant time: only the pointers, the lengths and `c` may affect timing, not the password, the salt or the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_pbkdf2_hmac_sha512_224_sha512` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA512_224_SHA512_FEATURES`); or else `vg_pbkdf2_hmac_sha512_224`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_pbkdf2_hmac_sha512_224_sha512` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA512_224_SHA512_FEATURES`); or else `vg_pbkdf2_hmac_sha512_224`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha512_256.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha512_256.rs index 8c50e6765b7..c6c684612bf 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha512_256.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/pbkdf2_sha512_256.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.pbkdf2Contract` of `VG.Spec.Hmac.sha512_256I`. Constant time: only the pointers, the lengths and `c` may affect timing, not the password, the salt or the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_pbkdf2_hmac_sha512_256_sha512` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA512_256_SHA512_FEATURES`); or else `vg_pbkdf2_hmac_sha512_256`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_pbkdf2_hmac_sha512_256_sha512` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA512_256_SHA512_FEATURES`); or else `vg_pbkdf2_hmac_sha512_256`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/poly1305.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/poly1305.rs index ac544a2f367..cdeded7e357 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/poly1305.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/poly1305.rs @@ -25,7 +25,7 @@ pub unsafe fn vg_poly1305_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: /// /// Contract: `VG.Spec.Poly1305.updateContract`. The streaming state is the accumulator, the key and the message's last bytes that do not fill a block (`VG.Spec.Poly1305.Buffered`). Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_poly1305_update_neon` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and it also supports `neon`; or else `vg_poly1305_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_poly1305_update_neon` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and it also supports `neon`; or else `vg_poly1305_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/rsa.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/rsa.rs index 5d378dafe6f..f60455c879d 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/rsa.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/rsa.rs @@ -2,36 +2,28 @@ //! The dispatchers of the verified `rsa` functions for `aarch64` //! (`crate::rsa`, `VG.Rust.dispatcher`). -/// Checks an RSA private key `(n, e, d, p, q, dP, dQ, qInv)` exactly as BoringSSL's `RSA_check_key` does. With the modulus `n` (`n_len` bytes), the public exponent `e` (`e_len` bytes), the private exponent `d` (`d_len` bytes), `p`, `dp` and `qinv` (`p_len` bytes) and `q` and `dq` (`q_len` bytes), all most significant first, returns 1 if `n` is odd, from 512 to 8192 bits and its first byte not zero, `e` is odd and from 3 to `2^33 - 1`, `d < n`, `p < n`, `q < n`, `p q = n`, `d e ≡ 1 (mod p - 1)`, `d e ≡ 1 (mod q - 1)`, `dP < p - 1`, `e dP ≡ 1 (mod p - 1)`, `dQ < q - 1`, `e dQ ≡ 1 (mod q - 1)`, `qInv < p` and `q qInv ≡ 1 (mod p)`; and 0 otherwise. Like `RSA_check_key`, it does not check that `p` and `q` are prime; if they are, `vg_rsa_private_checked` never returns its internal error for a key it accepts. +/// The values of an RSA modulus that `vg_rsa_public_precomputed_checked` takes, so that they are computed once per public key rather than once per operation. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero) of `w = ⌈n_len / 8⌉` words of 64 bits, writes `n` and then `R² mod n` for `R = 2^(64 w)` to `pre` (`w` words each, least significant first) and returns 1; or writes zeros and returns 0 if `n` is not such a modulus. /// -/// Contract: `VG.Spec.Rsa.checkKeyContract`. Constant time but for the public key: timing may depend on the pointers, the lengths and the contents of `n` and `e`, not on the private key. +/// Contract: `VG.Spec.Rsa.publicPrecomputeContract`. Timing may depend on the pointers, the lengths and the contents of `n`. /// -/// It runs `vg_rsa_check_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_rsa_public_precompute`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// +/// * `pre` must be valid for reads and writes of `8 * pre_len` bytes. /// * `n` must be valid for reads of `n_len` bytes. -/// * `e` must be valid for reads of `e_len` bytes. -/// * `d` must be valid for reads of `d_len` bytes. -/// * `p` must be valid for reads of `p_len` bytes. -/// * `q` must be valid for reads of `q_len` bytes. -/// * `dp` must be valid for reads of `dp_len` bytes. -/// * `dq` must be valid for reads of `dq_len` bytes. -/// * `qinv` must be valid for reads of `qinv_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. /// * `n_len` must be in 64..=1024. -/// * `e_len` and `d_len` must be in 1..=`n_len`. -/// * `p_len` and `q_len` must be in 1..`n_len`. -/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. -/// * `scratch_len` must be at least `16 * n_len + 32`. +/// * `pre_len` must be `2 * ⌈n_len / 8⌉`. +/// * `scratch_len` must be at least `8 * ⌈n_len / 8⌉ + 48`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `scratch` must not overlap `n`, `e`, `d`, `p`, `q`, `dp`, `dq`, `qinv` or the arguments on the stack (distinct Rust objects never do). -/// * None of `n`, `e`, `d`, `p`, `q`, `dp`, `dq`, `qinv` and `scratch` may wrap around the end of the address space (no Rust object does). +/// * `pre` and `scratch` must not overlap each other or `n` (distinct Rust objects never do). +/// * None of `pre`, `n` and `scratch` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_rsa_check_key_auto(_dit: verified_garbage_cpu::Dit<'_>, n: *const u8, n_len: usize, e: *const u8, e_len: usize, d: *const u8, d_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_rsa_public_precompute_auto(_dit: verified_garbage_cpu::Dit<'_>, pre: *mut u64, pre_len: usize, n: *const u8, n_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa::vg_rsa_check_key(n, n_len, e, e_len, d, d_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, scratch, scratch_len) } + unsafe { crate::rsa::vg_rsa_public_precompute(pre, pre_len, n, n_len, scratch, scratch_len) } } /// The CRT values of an RSA private key `(p, q, d)` (SP 800-56B Rev. 2 §6.2.2's prime-factor format), which bring it to the CRT format `(p, q, dP, dQ, qInv)` that `vg_rsa_private_checked` takes: `dP = d mod (p - 1)`, `dQ = d mod (q - 1)` and `qInv = q⁻¹ mod p` (§6.2.2's CRT format). With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), `p` (`p_len` bytes), `q` (`q_len` bytes) and `d` (`d_len` bytes), all most significant first, writes `dP` to `dp` and `qInv` to `qinv` (`p_len` bytes each) and `dQ` to `dq` (`q_len` bytes), most significant first, and returns 1; or writes zeros to all three and returns 0 if `n` is not such a modulus, `p q ≠ n`, or `q` has no inverse modulo `p`. Nothing checks that `p` and `q` are prime or that `d` is the key's private exponent. @@ -65,55 +57,63 @@ pub unsafe fn vg_rsa_crt_values_auto(_dit: verified_garbage_cpu::Dit<'_>, dp: *m unsafe { crate::rsa::vg_rsa_crt_values(dp, dp_len, dq, dq_len, qinv, qinv_len, n, n_len, p, p_len, q, q_len, d, d_len, scratch, scratch_len) } } -/// The values of an RSA modulus that `vg_rsa_public_precomputed_checked` takes, so that they are computed once per public key rather than once per operation. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero) of `w = ⌈n_len / 8⌉` words of 64 bits, writes `n` and then `R² mod n` for `R = 2^(64 w)` to `pre` (`w` words each, least significant first) and returns 1; or writes zeros and returns 0 if `n` is not such a modulus. +/// The prime factors `p > q` of the modulus of an RSA private key `(n, e, d)` (SP 800-56B Rev. 2 §6.2.2's basic format), which bring it to the prime-factor format `(p, q, d)` that `vg_rsa_crt_values` takes, recovered by SP 800-56B Rev. 2 Appendix C.2's deterministic method. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes) and the private exponent `d` (`d_len` bytes), both most significant first, writes `p` and `q` (`n_len` bytes each, most significant first) and returns 1; or writes zeros to both and returns 0 if `n` is not such a modulus or the method fails. It finds the factors of a valid two-prime key whenever `K (p + q − 1) ≤ n` for `a = K φ(n)` (Appendix C.2's step 1): with the least private exponent, whenever the smaller prime exceeds `e²`, and in practice for every key whose primes are not tiny. /// -/// Contract: `VG.Spec.Rsa.publicPrecomputeContract`. Timing may depend on the pointers, the lengths and the contents of `n`. +/// Contract: `VG.Spec.Rsa.recoverPrimesDetContract`. Constant time but for the public key: timing may depend on the pointers, the lengths and the contents of `n` and `e`, not on `d`, the factors, or whether they were found. /// -/// It runs `vg_rsa_public_precompute`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_rsa_recover_primes_det`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `pre` must be valid for reads and writes of `8 * pre_len` bytes. +/// * `p` must be valid for reads and writes of `p_len` bytes. +/// * `q` must be valid for reads and writes of `q_len` bytes. /// * `n` must be valid for reads of `n_len` bytes. +/// * `e` must be valid for reads of `e_len` bytes. +/// * `d` must be valid for reads of `d_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. /// * `n_len` must be in 64..=1024. -/// * `pre_len` must be `2 * ⌈n_len / 8⌉`. -/// * `scratch_len` must be at least `8 * ⌈n_len / 8⌉ + 48`. +/// * `p_len` and `q_len` must be `n_len`. +/// * `e_len` must be in 1..=8. +/// * `d_len` must be in 1..=`n_len`. +/// * `scratch_len` must be at least `16 * n_len + 32`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `pre` and `scratch` must not overlap each other or `n` (distinct Rust objects never do). -/// * None of `pre`, `n` and `scratch` may wrap around the end of the address space (no Rust object does). +/// * `p`, `q` and `scratch` must not overlap each other, `n`, `e`, `d` or the arguments on the stack (distinct Rust objects never do). +/// * None of `p`, `q`, `n`, `e`, `d` and `scratch` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_rsa_public_precompute_auto(_dit: verified_garbage_cpu::Dit<'_>, pre: *mut u64, pre_len: usize, n: *const u8, n_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_rsa_recover_primes_det_auto(_dit: verified_garbage_cpu::Dit<'_>, p: *mut u8, p_len: usize, q: *mut u8, q_len: usize, n: *const u8, n_len: usize, e: *const u8, e_len: usize, d: *const u8, d_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa::vg_rsa_public_precompute(pre, pre_len, n, n_len, scratch, scratch_len) } + unsafe { crate::rsa::vg_rsa_recover_primes_det(p, p_len, q, q_len, n, n_len, e, e_len, d, d_len, scratch, scratch_len) } } -/// The prime factors `p > q` of the modulus of an RSA private key `(n, e, d)` (SP 800-56B Rev. 2 §6.2.2's basic format), which bring it to the prime-factor format `(p, q, d)` that `vg_rsa_crt_values` takes, recovered by SP 800-56B Rev. 2 Appendix C.2's deterministic method. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes) and the private exponent `d` (`d_len` bytes), both most significant first, writes `p` and `q` (`n_len` bytes each, most significant first) and returns 1; or writes zeros to both and returns 0 if `n` is not such a modulus or the method fails. It finds the factors of a valid two-prime key whenever `K (p + q − 1) ≤ n` for `a = K φ(n)` (Appendix C.2's step 1): with the least private exponent, whenever the smaller prime exceeds `e²`, and in practice for every key whose primes are not tiny. +/// Checks an RSA private key `(n, e, d, p, q, dP, dQ, qInv)` exactly as BoringSSL's `RSA_check_key` does. With the modulus `n` (`n_len` bytes), the public exponent `e` (`e_len` bytes), the private exponent `d` (`d_len` bytes), `p`, `dp` and `qinv` (`p_len` bytes) and `q` and `dq` (`q_len` bytes), all most significant first, returns 1 if `n` is odd, from 512 to 8192 bits and its first byte not zero, `e` is odd and from 3 to `2^33 - 1`, `d < n`, `p < n`, `q < n`, `p q = n`, `d e ≡ 1 (mod p - 1)`, `d e ≡ 1 (mod q - 1)`, `dP < p - 1`, `e dP ≡ 1 (mod p - 1)`, `dQ < q - 1`, `e dQ ≡ 1 (mod q - 1)`, `qInv < p` and `q qInv ≡ 1 (mod p)`; and 0 otherwise. Like `RSA_check_key`, it does not check that `p` and `q` are prime; if they are, `vg_rsa_private_checked` never returns its internal error for a key it accepts. /// -/// Contract: `VG.Spec.Rsa.recoverPrimesDetContract`. Constant time but for the public key: timing may depend on the pointers, the lengths and the contents of `n` and `e`, not on `d`, the factors, or whether they were found. +/// Contract: `VG.Spec.Rsa.checkKeyContract`. Constant time but for the public key: timing may depend on the pointers, the lengths and the contents of `n` and `e`, not on the private key. /// -/// It runs `vg_rsa_recover_primes_det`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_rsa_check_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `p` must be valid for reads and writes of `p_len` bytes. -/// * `q` must be valid for reads and writes of `q_len` bytes. /// * `n` must be valid for reads of `n_len` bytes. /// * `e` must be valid for reads of `e_len` bytes. /// * `d` must be valid for reads of `d_len` bytes. +/// * `p` must be valid for reads of `p_len` bytes. +/// * `q` must be valid for reads of `q_len` bytes. +/// * `dp` must be valid for reads of `dp_len` bytes. +/// * `dq` must be valid for reads of `dq_len` bytes. +/// * `qinv` must be valid for reads of `qinv_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. /// * `n_len` must be in 64..=1024. -/// * `p_len` and `q_len` must be `n_len`. -/// * `e_len` must be in 1..=8. -/// * `d_len` must be in 1..=`n_len`. +/// * `e_len` and `d_len` must be in 1..=`n_len`. +/// * `p_len` and `q_len` must be in 1..`n_len`. +/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. /// * `scratch_len` must be at least `16 * n_len + 32`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `p`, `q` and `scratch` must not overlap each other, `n`, `e`, `d` or the arguments on the stack (distinct Rust objects never do). -/// * None of `p`, `q`, `n`, `e`, `d` and `scratch` may wrap around the end of the address space (no Rust object does). +/// * `scratch` must not overlap `n`, `e`, `d`, `p`, `q`, `dp`, `dq`, `qinv` or the arguments on the stack (distinct Rust objects never do). +/// * None of `n`, `e`, `d`, `p`, `q`, `dp`, `dq`, `qinv` and `scratch` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_rsa_recover_primes_det_auto(_dit: verified_garbage_cpu::Dit<'_>, p: *mut u8, p_len: usize, q: *mut u8, q_len: usize, n: *const u8, n_len: usize, e: *const u8, e_len: usize, d: *const u8, d_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_rsa_check_key_auto(_dit: verified_garbage_cpu::Dit<'_>, n: *const u8, n_len: usize, e: *const u8, e_len: usize, d: *const u8, d_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa::vg_rsa_recover_primes_det(p, p_len, q, q_len, n, n_len, e, e_len, d, d_len, scratch, scratch_len) } + unsafe { crate::rsa::vg_rsa_check_key(n, n_len, e, e_len, d, d_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, scratch, scratch_len) } } diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/rsa_keygen.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/rsa_keygen.rs index 337e08eb18a..38f19dd3b27 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/rsa_keygen.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/rsa_keygen.rs @@ -2,6 +2,25 @@ //! The dispatchers of the verified `rsa_keygen` functions for `aarch64` //! (`crate::rsa_keygen`, `VG.Rust.dispatcher`). +/// Start generating an RSA key, as BoringSSL's `RSA_generate_key_ex` (FIPS 186-5 Appendix A.1.3), with a modulus of `bits` bits rounded down to a multiple of 128, and the public exponent `e` (`e_len` octets, most significant first, with any number of leading zeros). Returns 0 if `e` is not odd from 3 to `2^32 - 1`, or 2 if `bits` is above 8192 or, rounded down, below 512, and writes zeros to `state`. Otherwise returns 1 and writes the search's seven public words to `state`: the rejected candidates the first prime's search allows, the primes' length in octets (`nlen / 16`), the entropy budget, the length of the first random tape to give `vg_rsa_generate`, the phase (0), the generations allowed (4), and the exponent's eight octets, most significant first. +/// +/// Contract: `VG.Spec.RsaKeyGen.keyInitContract`. Timing may depend on `bits` and `e`. +/// +/// It runs `vg_rsa_generate_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 56 bytes. +/// * `e` must be valid for reads of `e_len` bytes. +/// * `state` must not overlap `e` (distinct Rust objects never do). +/// * Neither `state` nor `e` may wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_rsa_generate_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 7], bits: usize, e: *const u8, e_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::rsa_keygen::vg_rsa_generate_init(state, bits, e, e_len) } +} + /// Generate an RSA key, as BoringSSL's `RSA_generate_key_ex` (FIPS 186-5 Appendix A.1.3), over the random tape `rand`, from the state `state` that `vg_rsa_generate_init` started: the search for `p`, then for `q` not too close to `p`, each over candidates of `p_len` octets with their top two bits and bottom bit set, rejected by trial division, a common factor of `p − 1` and `e` or Miller–Rabin, up to BoringSSL's limit of rejected candidates; then the key, `d = e⁻¹ mod lcm(p − 1, q − 1)`, `dP`, `dQ` and `qInv`, with `p` the larger prime, checked as BoringSSL's `RSA_check_key` checks it, drawing both primes again if `d` is too small; a generation whose prime's search is exhausted is started again, up to four in all. Returns 1 and writes the key to `key` (`key_len` is `9 p_len`, for the primes' length `p_len = state[1]`): `n` and `d` (`2 p_len` octets each), then `p`, `q`, `dP`, `dQ` and `qInv` (`p_len` each), all most significant first; 2 if the key failed a check; 3 if every generation was exhausted; or 0 if the tape ran out inside a candidate. Writes the octets of `rand` it consumed to `used`, and the state for the next call to `state`, whose fourth word is, for 0, the length of the next tape, more than the octets of `rand` after the first `used` and at most the entropy budget: keep those octets, in order, and append fresh random octets up to that length. For 0, `key` holds the search's `p` once it is found, which the next call reads: pass it back unchanged. `key` is otherwise zeros but for 1. /// /// Contract: `VG.Spec.RsaKeyGen.keySearchContract`. Timing may depend on the pointers, the lengths, `state`, the status and consumption it returns, and, for each prime's search, its rejected candidates and the octets its accepted candidate's tests read, and each key's status. Not on the primes or the key. @@ -28,22 +47,3 @@ pub unsafe fn vg_rsa_generate_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *mu // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::rsa_keygen::vg_rsa_generate(key, key_len, used, rand, rand_len, scratch, scratch_len, state) } } - -/// Start generating an RSA key, as BoringSSL's `RSA_generate_key_ex` (FIPS 186-5 Appendix A.1.3), with a modulus of `bits` bits rounded down to a multiple of 128, and the public exponent `e` (`e_len` octets, most significant first, with any number of leading zeros). Returns 0 if `e` is not odd from 3 to `2^32 - 1`, or 2 if `bits` is above 8192 or, rounded down, below 512, and writes zeros to `state`. Otherwise returns 1 and writes the search's seven public words to `state`: the rejected candidates the first prime's search allows, the primes' length in octets (`nlen / 16`), the entropy budget, the length of the first random tape to give `vg_rsa_generate`, the phase (0), the generations allowed (4), and the exponent's eight octets, most significant first. -/// -/// Contract: `VG.Spec.RsaKeyGen.keyInitContract`. Timing may depend on `bits` and `e`. -/// -/// It runs `vg_rsa_generate_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 56 bytes. -/// * `e` must be valid for reads of `e_len` bytes. -/// * `state` must not overlap `e` (distinct Rust objects never do). -/// * Neither `state` nor `e` may wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_rsa_generate_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 7], bits: usize, e: *const u8, e_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa_keygen::vg_rsa_generate_init(state, bits, e, e_len) } -} diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/rsa_pkcs1_enc.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/rsa_pkcs1_enc.rs index 92179e3fc3e..ef1de3cd494 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/rsa_pkcs1_enc.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/rsa_pkcs1_enc.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.RsaPkcs1Enc.decryptContract`. Constant time but for the public key: timing may depend on the pointers, the lengths and the contents of `n` and `e`, not on the ciphertext, the private key, the validity of the padding, the length of the message or which message is returned. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_pkcs1_decrypt_sha256_sha2` if the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_SHA256_SHA2_FEATURES`); or else `vg_rsa_pkcs1_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_pkcs1_decrypt_sha256_sha2` if the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_SHA256_SHA2_FEATURES`); or else `vg_rsa_pkcs1_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/scrypt.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/scrypt.rs index 18185882ab0..365e645e4c6 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/scrypt.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/scrypt.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Scrypt.scryptContract`. Not constant time in the indices: timing may depend on the pointers, the lengths, `r`, `N`, `p` and the indices `j` of step 3 of each scryptROMix, which are derived from the password and the salt and so leak information about them (as in every scrypt that indexes `V` directly), but on nothing else. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_scrypt_sha2` if the CPU supports its features (`VG_SCRYPT_SHA2_FEATURES`); or else `vg_scrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_scrypt_sha2` if the CPU supports its features (`VG_SCRYPT_SHA2_FEATURES`); or else `vg_scrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/sha1.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/sha1.rs index 442f0657a68..cf16e9dff7e 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/sha1.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/sha1.rs @@ -23,7 +23,7 @@ pub unsafe fn vg_sha1_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut /// /// Contract: `VG.Spec.Sha1.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha1_update_sha2` if the CPU supports its features (`VG_SHA1_UPDATE_SHA2_FEATURES`); or else `vg_sha1_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha1_update_sha2` if the CPU supports its features (`VG_SHA1_UPDATE_SHA2_FEATURES`); or else `vg_sha1_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -67,44 +67,45 @@ pub unsafe fn vg_sha1_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *m unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, data, len) } } -/// Computes the SHA-1 digest of the `len` bytes at `data` (20 bytes, `VG.Spec.Sha1.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-1 computation with that message. +/// Finishes a SHA-1 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), writes the SHA-1 digest of that message to `*out`. /// -/// Contract: `VG.Spec.Sha1.digestContract`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha1.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha1_digest_sha2` if the CPU supports its features (`VG_SHA1_DIGEST_SHA2_FEATURES`); or else `vg_sha1_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha1_finalize_sha2` if the CPU supports its features (`VG_SHA1_FINALIZE_SHA2_FEATURES`); or else `vg_sha1_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `data` must be valid for reads of `len` bytes. +/// * `state` must be valid for reads and writes of 84 bytes. /// * `out` must be valid for reads and writes of 20 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the 272 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 176 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha1_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 20]) { +pub unsafe fn vg_sha1_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 84], count: u64, out: *mut [u8; 20]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(data: *const u8, len: usize, out: *mut [u8; 20]); + type Impl = unsafe extern "C" fn(state: *mut [u8; 84], count: u64, out: *mut [u8; 20]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha1::VG_SHA1_DIGEST_SHA2_FEATURES) } - || cpu.contains(crate::sha1::VG_SHA1_DIGEST_SHA2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha1::VG_SHA1_FINALIZE_SHA2_FEATURES) } + || cpu.contains(crate::sha1::VG_SHA1_FINALIZE_SHA2_FEATURES) { - return crate::sha1::vg_sha1_digest_sha2; + return crate::sha1::vg_sha1_finalize_sha2; } - crate::sha1::vg_sha1_digest + crate::sha1::vg_sha1_finalize } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(data: *const u8, len: usize, out: *mut [u8; 20]) { + unsafe extern "C" fn first(state: *mut [u8; 84], count: u64, out: *mut [u8; 20]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(data, len, out) } + unsafe { f(state, count, out) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -112,48 +113,47 @@ pub unsafe fn vg_sha1_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *co // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, out) } } -/// Finishes a SHA-1 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), writes the SHA-1 digest of that message to `*out`. +/// Computes the SHA-1 digest of the `len` bytes at `data` (20 bytes, `VG.Spec.Sha1.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-1 computation with that message. /// -/// Contract: `VG.Spec.Sha1.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha1.digestContract`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha1_finalize_sha2` if the CPU supports its features (`VG_SHA1_FINALIZE_SHA2_FEATURES`); or else `vg_sha1_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha1_digest_sha2` if the CPU supports its features (`VG_SHA1_DIGEST_SHA2_FEATURES`); or else `vg_sha1_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 84 bytes. +/// * `data` must be valid for reads of `len` bytes. /// * `out` must be valid for reads and writes of 20 bytes. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 176 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the 272 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha1_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 84], count: u64, out: *mut [u8; 20]) { +pub unsafe fn vg_sha1_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 20]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(state: *mut [u8; 84], count: u64, out: *mut [u8; 20]); + type Impl = unsafe extern "C" fn(data: *const u8, len: usize, out: *mut [u8; 20]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha1::VG_SHA1_FINALIZE_SHA2_FEATURES) } - || cpu.contains(crate::sha1::VG_SHA1_FINALIZE_SHA2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha1::VG_SHA1_DIGEST_SHA2_FEATURES) } + || cpu.contains(crate::sha1::VG_SHA1_DIGEST_SHA2_FEATURES) { - return crate::sha1::vg_sha1_finalize_sha2; + return crate::sha1::vg_sha1_digest_sha2; } - crate::sha1::vg_sha1_finalize + crate::sha1::vg_sha1_digest } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(state: *mut [u8; 84], count: u64, out: *mut [u8; 20]) { + unsafe extern "C" fn first(data: *const u8, len: usize, out: *mut [u8; 20]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, count, out) } + unsafe { f(data, len, out) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -161,5 +161,5 @@ pub unsafe fn vg_sha1_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, out) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } } diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/sha256.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/sha256.rs index 0e9aaa4da1d..df6b489a58a 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/sha256.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/sha256.rs @@ -36,44 +36,45 @@ pub unsafe fn vg_sha256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *m unsafe { crate::sha256::vg_sha256_init(state) } } -/// Computes the SHA-224 digest of the `len` bytes at `data` (28 bytes, `VG.Spec.Sha256.sha224`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-224 computation with that message. +/// Finishes a SHA-224 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), hashed from the initial hash value of SHA-224 (as `vg_sha224_init` starts it), writes the SHA-224 digest of that message (28 bytes, `VG.Spec.Sha256.sha224`) to `*out`. /// -/// Contract: `VG.Spec.Sha256.digestContract` for `VG.Spec.Sha256.sha224`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha256.finalize224Contract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha224_digest_sha2` if the CPU supports its features (`VG_SHA224_DIGEST_SHA2_FEATURES`); or else `vg_sha224_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha224_finalize_sha2` if the CPU supports its features (`VG_SHA224_FINALIZE_SHA2_FEATURES`); or else `vg_sha224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `data` must be valid for reads of `len` bytes. +/// * `state` must be valid for reads and writes of 96 bytes. /// * `out` must be valid for reads and writes of 28 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the 272 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 624 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha224_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 28]) { +pub unsafe fn vg_sha224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, out: *mut [u8; 28]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(data: *const u8, len: usize, out: *mut [u8; 28]); + type Impl = unsafe extern "C" fn(state: *mut [u8; 96], count: u64, out: *mut [u8; 28]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA224_DIGEST_SHA2_FEATURES) } - || cpu.contains(crate::sha256::VG_SHA224_DIGEST_SHA2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA224_FINALIZE_SHA2_FEATURES) } + || cpu.contains(crate::sha256::VG_SHA224_FINALIZE_SHA2_FEATURES) { - return crate::sha256::vg_sha224_digest_sha2; + return crate::sha256::vg_sha224_finalize_sha2; } - crate::sha256::vg_sha224_digest + crate::sha256::vg_sha224_finalize } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(data: *const u8, len: usize, out: *mut [u8; 28]) { + unsafe extern "C" fn first(state: *mut [u8; 96], count: u64, out: *mut [u8; 28]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(data, len, out) } + unsafe { f(state, count, out) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -81,47 +82,47 @@ pub unsafe fn vg_sha224_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: * // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, out) } } -/// Absorbs data into a SHA-224 or SHA-256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), it then represents that message followed by the `len` bytes at `data`. +/// Computes the SHA-224 digest of the `len` bytes at `data` (28 bytes, `VG.Spec.Sha256.sha224`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-224 computation with that message. /// -/// Contract: `VG.Spec.Sha256.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha256.digestContract` for `VG.Spec.Sha256.sha224`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha256_update_sha2` if the CPU supports its features (`VG_SHA256_UPDATE_SHA2_FEATURES`); or else `vg_sha256_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha224_digest_sha2` if the CPU supports its features (`VG_SHA224_DIGEST_SHA2_FEATURES`); or else `vg_sha224_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 96 bytes. /// * `data` must be valid for reads of `len` bytes. -/// * `state` must not overlap `data` (distinct Rust objects never do). -/// * Neither `state` nor `data` may overlap the 624 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 28 bytes. +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the 272 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, data: *const u8, len: usize) { +pub unsafe fn vg_sha224_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 28]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(state: *mut [u8; 96], count: u64, data: *const u8, len: usize); + type Impl = unsafe extern "C" fn(data: *const u8, len: usize, out: *mut [u8; 28]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA256_UPDATE_SHA2_FEATURES) } - || cpu.contains(crate::sha256::VG_SHA256_UPDATE_SHA2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA224_DIGEST_SHA2_FEATURES) } + || cpu.contains(crate::sha256::VG_SHA224_DIGEST_SHA2_FEATURES) { - return crate::sha256::vg_sha256_update_sha2; + return crate::sha256::vg_sha224_digest_sha2; } - crate::sha256::vg_sha256_update + crate::sha256::vg_sha224_digest } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(state: *mut [u8; 96], count: u64, data: *const u8, len: usize) { + unsafe extern "C" fn first(data: *const u8, len: usize, out: *mut [u8; 28]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, count, data, len) } + unsafe { f(data, len, out) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -129,47 +130,47 @@ pub unsafe fn vg_sha256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, data, len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } } -/// Computes the SHA-256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha256.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-256 computation with that message. +/// Absorbs data into a SHA-224 or SHA-256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), it then represents that message followed by the `len` bytes at `data`. /// -/// Contract: `VG.Spec.Sha256.digestContract` for `VG.Spec.Sha256.hash`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha256.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha256_digest_sha2` if the CPU supports its features (`VG_SHA256_DIGEST_SHA2_FEATURES`); or else `vg_sha256_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha256_update_sha2` if the CPU supports its features (`VG_SHA256_UPDATE_SHA2_FEATURES`); or else `vg_sha256_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// +/// * `state` must be valid for reads and writes of 96 bytes. /// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 32 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the 272 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must not overlap `data` (distinct Rust objects never do). +/// * Neither `state` nor `data` may overlap the 624 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { +pub unsafe fn vg_sha256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, data: *const u8, len: usize) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(data: *const u8, len: usize, out: *mut [u8; 32]); + type Impl = unsafe extern "C" fn(state: *mut [u8; 96], count: u64, data: *const u8, len: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA256_DIGEST_SHA2_FEATURES) } - || cpu.contains(crate::sha256::VG_SHA256_DIGEST_SHA2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA256_UPDATE_SHA2_FEATURES) } + || cpu.contains(crate::sha256::VG_SHA256_UPDATE_SHA2_FEATURES) { - return crate::sha256::vg_sha256_digest_sha2; + return crate::sha256::vg_sha256_update_sha2; } - crate::sha256::vg_sha256_digest + crate::sha256::vg_sha256_update } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(data: *const u8, len: usize, out: *mut [u8; 32]) { + unsafe extern "C" fn first(state: *mut [u8; 96], count: u64, data: *const u8, len: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(data, len, out) } + unsafe { f(state, count, data, len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -177,43 +178,43 @@ pub unsafe fn vg_sha256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: * // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, data, len) } } -/// Finishes a SHA-224 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), hashed from the initial hash value of SHA-224 (as `vg_sha224_init` starts it), writes the SHA-224 digest of that message (28 bytes, `VG.Spec.Sha256.sha224`) to `*out`. +/// Finishes a SHA-224 or SHA-256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (32 bytes) to `*out`. The SHA-256 digest is all of it; the SHA-224 digest is its first 28 bytes. /// -/// Contract: `VG.Spec.Sha256.finalize224Contract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha256.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha224_finalize_sha2` if the CPU supports its features (`VG_SHA224_FINALIZE_SHA2_FEATURES`); or else `vg_sha224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha256_finalize_sha2` if the CPU supports its features (`VG_SHA256_FINALIZE_SHA2_FEATURES`); or else `vg_sha256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 96 bytes. -/// * `out` must be valid for reads and writes of 28 bytes. +/// * `out` must be valid for reads and writes of 32 bytes. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the 624 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, out: *mut [u8; 28]) { +pub unsafe fn vg_sha256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, out: *mut [u8; 32]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(state: *mut [u8; 96], count: u64, out: *mut [u8; 28]); + type Impl = unsafe extern "C" fn(state: *mut [u8; 96], count: u64, out: *mut [u8; 32]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA224_FINALIZE_SHA2_FEATURES) } - || cpu.contains(crate::sha256::VG_SHA224_FINALIZE_SHA2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA256_FINALIZE_SHA2_FEATURES) } + || cpu.contains(crate::sha256::VG_SHA256_FINALIZE_SHA2_FEATURES) { - return crate::sha256::vg_sha224_finalize_sha2; + return crate::sha256::vg_sha256_finalize_sha2; } - crate::sha256::vg_sha224_finalize + crate::sha256::vg_sha256_finalize } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(state: *mut [u8; 96], count: u64, out: *mut [u8; 28]) { + unsafe extern "C" fn first(state: *mut [u8; 96], count: u64, out: *mut [u8; 32]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` @@ -229,45 +230,44 @@ pub unsafe fn vg_sha224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, out) } } -/// Finishes a SHA-224 or SHA-256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (32 bytes) to `*out`. The SHA-256 digest is all of it; the SHA-224 digest is its first 28 bytes. +/// Computes the SHA-256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha256.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-256 computation with that message. /// -/// Contract: `VG.Spec.Sha256.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha256.digestContract` for `VG.Spec.Sha256.hash`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha256_finalize_sha2` if the CPU supports its features (`VG_SHA256_FINALIZE_SHA2_FEATURES`); or else `vg_sha256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha256_digest_sha2` if the CPU supports its features (`VG_SHA256_DIGEST_SHA2_FEATURES`); or else `vg_sha256_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 96 bytes. +/// * `data` must be valid for reads of `len` bytes. /// * `out` must be valid for reads and writes of 32 bytes. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 624 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the 272 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, out: *mut [u8; 32]) { +pub unsafe fn vg_sha256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(state: *mut [u8; 96], count: u64, out: *mut [u8; 32]); + type Impl = unsafe extern "C" fn(data: *const u8, len: usize, out: *mut [u8; 32]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA256_FINALIZE_SHA2_FEATURES) } - || cpu.contains(crate::sha256::VG_SHA256_FINALIZE_SHA2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA256_DIGEST_SHA2_FEATURES) } + || cpu.contains(crate::sha256::VG_SHA256_DIGEST_SHA2_FEATURES) { - return crate::sha256::vg_sha256_finalize_sha2; + return crate::sha256::vg_sha256_digest_sha2; } - crate::sha256::vg_sha256_finalize + crate::sha256::vg_sha256_digest } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(state: *mut [u8; 96], count: u64, out: *mut [u8; 32]) { + unsafe extern "C" fn first(data: *const u8, len: usize, out: *mut [u8; 32]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, count, out) } + unsafe { f(data, len, out) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -275,5 +275,5 @@ pub unsafe fn vg_sha256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, out) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } } diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/sha3.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/sha3.rs index 4d9e3630b19..a7f2d1a2f87 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/sha3.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/sha3.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Sha3.updateContract` with the rate 144. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha3_224_update_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHA3_224_UPDATE_SHA3_FEATURES`); or else `vg_sha3_224_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha3_224_update_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHA3_224_UPDATE_SHA3_FEATURES`); or else `vg_sha3_224_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -54,48 +54,47 @@ pub unsafe fn vg_sha3_224_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, pos, data, len, scratch) } } -/// Absorbs data into a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, 136), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 136`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Finishes a SHA3-224 computation: if the state `*state` represents a message whose length is `pos` modulo 144 (`VG.Spec.Sha3.Repr` with SHA3-224's rate, as `vg_sha3_224_update` leaves it), writes the SHA3-224 digest of that message (28 bytes, `VG.Spec.Sha3.sha3_224`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 136. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_224`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha3_256_update_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHA3_256_UPDATE_SHA3_FEATURES`); or else `vg_sha3_256_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha3_224_finalize_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHA3_224_FINALIZE_SHA3_FEATURES`); or else `vg_sha3_224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `scratch` must be valid for reads and writes of 640 bytes. -/// * `pos` must be less than 136. -/// * The contents of `scratch` on return are unspecified. -/// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). -/// * None of `state`, `data` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 28 bytes. +/// * `pos` must be less than 144. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 688 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 28]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize; + type Impl = unsafe extern "C" fn(state: *mut [u64; 25], pos: usize, out: *mut [u8; 28]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHA3_256_UPDATE_SHA3_FEATURES)) - && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHA3_256_UPDATE_SHA3_FEATURES) } - || cpu.contains(crate::sha3::VG_SHA3_256_UPDATE_SHA3_FEATURES)) + if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHA3_224_FINALIZE_SHA3_FEATURES)) + && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHA3_224_FINALIZE_SHA3_FEATURES) } + || cpu.contains(crate::sha3::VG_SHA3_224_FINALIZE_SHA3_FEATURES)) { - return crate::sha3::vg_sha3_256_update_sha3; + return crate::sha3::vg_sha3_224_finalize_sha3; } - crate::sha3::vg_sha3_256_update + crate::sha3::vg_sha3_224_finalize } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { + unsafe extern "C" fn first(state: *mut [u64; 25], pos: usize, out: *mut [u8; 28]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, pos, data, len, scratch) } + unsafe { f(state, pos, out) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -103,27 +102,27 @@ pub unsafe fn vg_sha3_256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, pos, data, len, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, pos, out) } } -/// Absorbs data into a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, 104), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 104`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Absorbs data into a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, 136), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 136`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 104. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 136. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha3_384_update_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHA3_384_UPDATE_SHA3_FEATURES`); or else `vg_sha3_384_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha3_256_update_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHA3_256_UPDATE_SHA3_FEATURES`); or else `vg_sha3_256_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. /// * `data` must be valid for reads of `len` bytes. /// * `scratch` must be valid for reads and writes of 640 bytes. -/// * `pos` must be less than 104. +/// * `pos` must be less than 136. /// * The contents of `scratch` on return are unspecified. /// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). /// * None of `state`, `data` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_384_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { /// The type of the implementations. type Impl = unsafe extern "C" fn(state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only @@ -131,13 +130,13 @@ pub unsafe fn vg_sha3_384_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHA3_384_UPDATE_SHA3_FEATURES)) - && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHA3_384_UPDATE_SHA3_FEATURES) } - || cpu.contains(crate::sha3::VG_SHA3_384_UPDATE_SHA3_FEATURES)) + if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHA3_256_UPDATE_SHA3_FEATURES)) + && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHA3_256_UPDATE_SHA3_FEATURES) } + || cpu.contains(crate::sha3::VG_SHA3_256_UPDATE_SHA3_FEATURES)) { - return crate::sha3::vg_sha3_384_update_sha3; + return crate::sha3::vg_sha3_256_update_sha3; } - crate::sha3::vg_sha3_384_update + crate::sha3::vg_sha3_256_update } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. @@ -158,48 +157,47 @@ pub unsafe fn vg_sha3_384_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, pos, data, len, scratch) } } -/// Absorbs data into a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, 72), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 72`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Finishes a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, as `vg_sha3_256_update` leaves it), writes the SHA3-256 digest of that message (32 bytes, `VG.Spec.Sha3.sha3_256`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 72. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_256`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha3_512_update_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHA3_512_UPDATE_SHA3_FEATURES`); or else `vg_sha3_512_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha3_256_finalize_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHA3_256_FINALIZE_SHA3_FEATURES`); or else `vg_sha3_256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `scratch` must be valid for reads and writes of 640 bytes. -/// * `pos` must be less than 72. -/// * The contents of `scratch` on return are unspecified. -/// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). -/// * None of `state`, `data` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 32 bytes. +/// * `pos` must be less than 136. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 688 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 32]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize; + type Impl = unsafe extern "C" fn(state: *mut [u64; 25], pos: usize, out: *mut [u8; 32]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHA3_512_UPDATE_SHA3_FEATURES)) - && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHA3_512_UPDATE_SHA3_FEATURES) } - || cpu.contains(crate::sha3::VG_SHA3_512_UPDATE_SHA3_FEATURES)) + if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHA3_256_FINALIZE_SHA3_FEATURES)) + && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHA3_256_FINALIZE_SHA3_FEATURES) } + || cpu.contains(crate::sha3::VG_SHA3_256_FINALIZE_SHA3_FEATURES)) { - return crate::sha3::vg_sha3_512_update_sha3; + return crate::sha3::vg_sha3_256_finalize_sha3; } - crate::sha3::vg_sha3_512_update + crate::sha3::vg_sha3_256_finalize } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { + unsafe extern "C" fn first(state: *mut [u64; 25], pos: usize, out: *mut [u8; 32]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, pos, data, len, scratch) } + unsafe { f(state, pos, out) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -207,27 +205,27 @@ pub unsafe fn vg_sha3_512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, pos, data, len, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, pos, out) } } -/// Absorbs data into a SHAKE128 computation: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, 168), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 168`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Absorbs data into a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, 104), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 104`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 168. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 104. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_shake128_update_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHAKE128_UPDATE_SHA3_FEATURES`); or else `vg_shake128_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha3_384_update_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHA3_384_UPDATE_SHA3_FEATURES`); or else `vg_sha3_384_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. /// * `data` must be valid for reads of `len` bytes. /// * `scratch` must be valid for reads and writes of 640 bytes. -/// * `pos` must be less than 168. +/// * `pos` must be less than 104. /// * The contents of `scratch` on return are unspecified. /// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). /// * None of `state`, `data` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake128_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_384_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { /// The type of the implementations. type Impl = unsafe extern "C" fn(state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only @@ -235,13 +233,13 @@ pub unsafe fn vg_shake128_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHAKE128_UPDATE_SHA3_FEATURES)) - && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHAKE128_UPDATE_SHA3_FEATURES) } - || cpu.contains(crate::sha3::VG_SHAKE128_UPDATE_SHA3_FEATURES)) + if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHA3_384_UPDATE_SHA3_FEATURES)) + && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHA3_384_UPDATE_SHA3_FEATURES) } + || cpu.contains(crate::sha3::VG_SHA3_384_UPDATE_SHA3_FEATURES)) { - return crate::sha3::vg_shake128_update_sha3; + return crate::sha3::vg_sha3_384_update_sha3; } - crate::sha3::vg_shake128_update + crate::sha3::vg_sha3_384_update } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. @@ -262,46 +260,47 @@ pub unsafe fn vg_shake128_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, pos, data, len, scratch) } } -/// Squeezes SHAKE128 output: if the state `*state`, from position `pos`, continues the output of SHAKE128 of a message (`VG.Spec.Sha3.shake128`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake128_finalize` leaves and position 0. +/// Finishes a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, as `vg_sha3_384_update` leaves it), writes the SHA3-384 digest of that message (48 bytes, `VG.Spec.Sha3.sha3_384`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_384`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_shake128_squeeze_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHAKE128_SQUEEZE_SHA3_FEATURES`); or else `vg_shake128_squeeze`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha3_384_finalize_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHA3_384_FINALIZE_SHA3_FEATURES`); or else `vg_sha3_384_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of `outlen` bytes. -/// * `pos` must be at most 168. +/// * `out` must be valid for reads and writes of 48 bytes. +/// * `pos` must be less than 104. +/// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 656 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * Neither `state` nor `out` may overlap the 688 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake128_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { +pub unsafe fn vg_sha3_384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 48]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize; + type Impl = unsafe extern "C" fn(state: *mut [u64; 25], pos: usize, out: *mut [u8; 48]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHAKE128_SQUEEZE_SHA3_FEATURES)) - && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHAKE128_SQUEEZE_SHA3_FEATURES) } - || cpu.contains(crate::sha3::VG_SHAKE128_SQUEEZE_SHA3_FEATURES)) + if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHA3_384_FINALIZE_SHA3_FEATURES)) + && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHA3_384_FINALIZE_SHA3_FEATURES) } + || cpu.contains(crate::sha3::VG_SHA3_384_FINALIZE_SHA3_FEATURES)) { - return crate::sha3::vg_shake128_squeeze_sha3; + return crate::sha3::vg_sha3_384_finalize_sha3; } - crate::sha3::vg_shake128_squeeze + crate::sha3::vg_sha3_384_finalize } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { + unsafe extern "C" fn first(state: *mut [u64; 25], pos: usize, out: *mut [u8; 48]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, pos, out, outlen) } + unsafe { f(state, pos, out) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -309,49 +308,51 @@ pub unsafe fn vg_shake128_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, stat // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, pos, out, outlen) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, pos, out) } } -/// Squeezes SHAKE256 output: if the state `*state`, from position `pos`, continues the output of SHAKE256 of a message (`VG.Spec.Sha3.shake256`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake256_finalize` leaves and position 0. +/// Absorbs data into a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, 72), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 72`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 72. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_shake256_squeeze_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHAKE256_SQUEEZE_SHA3_FEATURES`); or else `vg_shake256_squeeze`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha3_512_update_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHA3_512_UPDATE_SHA3_FEATURES`); or else `vg_sha3_512_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of `outlen` bytes. -/// * `pos` must be at most 136. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 656 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `scratch` must be valid for reads and writes of 640 bytes. +/// * `pos` must be less than 72. +/// * The contents of `scratch` on return are unspecified. +/// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). +/// * None of `state`, `data` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake256_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { +pub unsafe fn vg_sha3_512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { /// The type of the implementations. - type Impl = unsafe extern "C" fn(state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize; + type Impl = unsafe extern "C" fn(state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHAKE256_SQUEEZE_SHA3_FEATURES)) - && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHAKE256_SQUEEZE_SHA3_FEATURES) } - || cpu.contains(crate::sha3::VG_SHAKE256_SQUEEZE_SHA3_FEATURES)) + if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHA3_512_UPDATE_SHA3_FEATURES)) + && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHA3_512_UPDATE_SHA3_FEATURES) } + || cpu.contains(crate::sha3::VG_SHA3_512_UPDATE_SHA3_FEATURES)) { - return crate::sha3::vg_shake256_squeeze_sha3; + return crate::sha3::vg_sha3_512_update_sha3; } - crate::sha3::vg_shake256_squeeze + crate::sha3::vg_sha3_512_update } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { + unsafe extern "C" fn first(state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, pos, out, outlen) } + unsafe { f(state, pos, data, len, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -359,45 +360,45 @@ pub unsafe fn vg_shake256_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, stat // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, pos, out, outlen) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, pos, data, len, scratch) } } -/// Finishes a SHA3-224 computation: if the state `*state` represents a message whose length is `pos` modulo 144 (`VG.Spec.Sha3.Repr` with SHA3-224's rate, as `vg_sha3_224_update` leaves it), writes the SHA3-224 digest of that message (28 bytes, `VG.Spec.Sha3.sha3_224`) to `*out`. +/// Finishes a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, as `vg_sha3_512_update` leaves it), writes the SHA3-512 digest of that message (64 bytes, `VG.Spec.Sha3.sha3_512`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_224`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_512`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha3_224_finalize_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHA3_224_FINALIZE_SHA3_FEATURES`); or else `vg_sha3_224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha3_512_finalize_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHA3_512_FINALIZE_SHA3_FEATURES`); or else `vg_sha3_512_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of 28 bytes. -/// * `pos` must be less than 144. +/// * `out` must be valid for reads and writes of 64 bytes. +/// * `pos` must be less than 72. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the 688 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 28]) { +pub unsafe fn vg_sha3_512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 64]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(state: *mut [u64; 25], pos: usize, out: *mut [u8; 28]); + type Impl = unsafe extern "C" fn(state: *mut [u64; 25], pos: usize, out: *mut [u8; 64]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHA3_224_FINALIZE_SHA3_FEATURES)) - && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHA3_224_FINALIZE_SHA3_FEATURES) } - || cpu.contains(crate::sha3::VG_SHA3_224_FINALIZE_SHA3_FEATURES)) + if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHA3_512_FINALIZE_SHA3_FEATURES)) + && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHA3_512_FINALIZE_SHA3_FEATURES) } + || cpu.contains(crate::sha3::VG_SHA3_512_FINALIZE_SHA3_FEATURES)) { - return crate::sha3::vg_sha3_224_finalize_sha3; + return crate::sha3::vg_sha3_512_finalize_sha3; } - crate::sha3::vg_sha3_224_finalize + crate::sha3::vg_sha3_512_finalize } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(state: *mut [u64; 25], pos: usize, out: *mut [u8; 28]) { + unsafe extern "C" fn first(state: *mut [u64; 25], pos: usize, out: *mut [u8; 64]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` @@ -413,47 +414,48 @@ pub unsafe fn vg_sha3_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, sta unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, pos, out) } } -/// Finishes a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, as `vg_sha3_256_update` leaves it), writes the SHA3-256 digest of that message (32 bytes, `VG.Spec.Sha3.sha3_256`) to `*out`. +/// Absorbs data into a SHAKE128 computation: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, 168), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 168`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_256`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 168. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha3_256_finalize_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHA3_256_FINALIZE_SHA3_FEATURES`); or else `vg_sha3_256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_shake128_update_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHAKE128_UPDATE_SHA3_FEATURES`); or else `vg_shake128_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of 32 bytes. -/// * `pos` must be less than 136. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 688 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `scratch` must be valid for reads and writes of 640 bytes. +/// * `pos` must be less than 168. +/// * The contents of `scratch` on return are unspecified. +/// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). +/// * None of `state`, `data` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 32]) { +pub unsafe fn vg_shake128_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { /// The type of the implementations. - type Impl = unsafe extern "C" fn(state: *mut [u64; 25], pos: usize, out: *mut [u8; 32]); + type Impl = unsafe extern "C" fn(state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHA3_256_FINALIZE_SHA3_FEATURES)) - && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHA3_256_FINALIZE_SHA3_FEATURES) } - || cpu.contains(crate::sha3::VG_SHA3_256_FINALIZE_SHA3_FEATURES)) + if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHAKE128_UPDATE_SHA3_FEATURES)) + && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHAKE128_UPDATE_SHA3_FEATURES) } + || cpu.contains(crate::sha3::VG_SHAKE128_UPDATE_SHA3_FEATURES)) { - return crate::sha3::vg_sha3_256_finalize_sha3; + return crate::sha3::vg_shake128_update_sha3; } - crate::sha3::vg_sha3_256_finalize + crate::sha3::vg_shake128_update } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(state: *mut [u64; 25], pos: usize, out: *mut [u8; 32]) { + unsafe extern "C" fn first(state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, pos, out) } + unsafe { f(state, pos, data, len, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -461,50 +463,47 @@ pub unsafe fn vg_sha3_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, sta // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, pos, out) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, pos, data, len, scratch) } } -/// Finishes a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, as `vg_sha3_384_update` leaves it), writes the SHA3-384 digest of that message (48 bytes, `VG.Spec.Sha3.sha3_384`) to `*out`. +/// Finishes absorbing a SHAKE128 message: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, as `vg_shake128_update` leaves it), it becomes a state that, squeezed by `vg_shake128_squeeze` from position 0, gives the output of SHAKE128 of that message (`VG.Spec.Sha3.shake128`) from its first byte on (`VG.Spec.Sha3.XofRepr`). /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_384`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointer and `pos` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha3_384_finalize_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHA3_384_FINALIZE_SHA3_FEATURES`); or else `vg_sha3_384_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_shake128_finalize_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHAKE128_FINALIZE_SHA3_FEATURES`); or else `vg_shake128_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of 48 bytes. -/// * `pos` must be less than 104. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 688 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `pos` must be less than 168. +/// * `state` must not overlap the 656 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 48]) { +pub unsafe fn vg_shake128_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(state: *mut [u64; 25], pos: usize, out: *mut [u8; 48]); + type Impl = unsafe extern "C" fn(state: *mut [u64; 25], pos: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHA3_384_FINALIZE_SHA3_FEATURES)) - && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHA3_384_FINALIZE_SHA3_FEATURES) } - || cpu.contains(crate::sha3::VG_SHA3_384_FINALIZE_SHA3_FEATURES)) + if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHAKE128_FINALIZE_SHA3_FEATURES)) + && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHAKE128_FINALIZE_SHA3_FEATURES) } + || cpu.contains(crate::sha3::VG_SHAKE128_FINALIZE_SHA3_FEATURES)) { - return crate::sha3::vg_sha3_384_finalize_sha3; + return crate::sha3::vg_shake128_finalize_sha3; } - crate::sha3::vg_sha3_384_finalize + crate::sha3::vg_shake128_finalize } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(state: *mut [u64; 25], pos: usize, out: *mut [u8; 48]) { + unsafe extern "C" fn first(state: *mut [u64; 25], pos: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, pos, out) } + unsafe { f(state, pos) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -512,50 +511,49 @@ pub unsafe fn vg_sha3_384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, sta // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, pos, out) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, pos) } } -/// Finishes a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, as `vg_sha3_512_update` leaves it), writes the SHA3-512 digest of that message (64 bytes, `VG.Spec.Sha3.sha3_512`) to `*out`. +/// Squeezes SHAKE128 output: if the state `*state`, from position `pos`, continues the output of SHAKE128 of a message (`VG.Spec.Sha3.shake128`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake128_finalize` leaves and position 0. /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_512`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha3_512_finalize_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHA3_512_FINALIZE_SHA3_FEATURES`); or else `vg_sha3_512_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_shake128_squeeze_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHAKE128_SQUEEZE_SHA3_FEATURES`); or else `vg_shake128_squeeze`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of 64 bytes. -/// * `pos` must be less than 72. -/// * The contents of `state` on return are unspecified. +/// * `out` must be valid for reads and writes of `outlen` bytes. +/// * `pos` must be at most 168. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 688 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * Neither `state` nor `out` may overlap the 656 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 64]) { +pub unsafe fn vg_shake128_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { /// The type of the implementations. - type Impl = unsafe extern "C" fn(state: *mut [u64; 25], pos: usize, out: *mut [u8; 64]); + type Impl = unsafe extern "C" fn(state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHA3_512_FINALIZE_SHA3_FEATURES)) - && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHA3_512_FINALIZE_SHA3_FEATURES) } - || cpu.contains(crate::sha3::VG_SHA3_512_FINALIZE_SHA3_FEATURES)) + if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHAKE128_SQUEEZE_SHA3_FEATURES)) + && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHAKE128_SQUEEZE_SHA3_FEATURES) } + || cpu.contains(crate::sha3::VG_SHAKE128_SQUEEZE_SHA3_FEATURES)) { - return crate::sha3::vg_sha3_512_finalize_sha3; + return crate::sha3::vg_shake128_squeeze_sha3; } - crate::sha3::vg_sha3_512_finalize + crate::sha3::vg_shake128_squeeze } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(state: *mut [u64; 25], pos: usize, out: *mut [u8; 64]) { + unsafe extern "C" fn first(state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, pos, out) } + unsafe { f(state, pos, out, outlen) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -563,23 +561,23 @@ pub unsafe fn vg_sha3_512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, sta // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, pos, out) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, pos, out, outlen) } } -/// Finishes absorbing a SHAKE128 message: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, as `vg_shake128_update` leaves it), it becomes a state that, squeezed by `vg_shake128_squeeze` from position 0, gives the output of SHAKE128 of that message (`VG.Spec.Sha3.shake128`) from its first byte on (`VG.Spec.Sha3.XofRepr`). +/// Finishes absorbing a SHAKE256 message: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHAKE256's rate, as `vg_sha3_256_update` leaves it), it becomes a state that, squeezed by `vg_shake256_squeeze` from position 0, gives the output of SHAKE256 of that message (`VG.Spec.Sha3.shake256`) from its first byte on (`VG.Spec.Sha3.XofRepr`). /// -/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointer and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointer and `pos` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_shake128_finalize_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHAKE128_FINALIZE_SHA3_FEATURES`); or else `vg_shake128_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_shake256_finalize_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHAKE256_FINALIZE_SHA3_FEATURES`); or else `vg_shake256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `pos` must be less than 168. +/// * `pos` must be less than 136. /// * `state` must not overlap the 656 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake128_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { +pub unsafe fn vg_shake256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { /// The type of the implementations. type Impl = unsafe extern "C" fn(state: *mut [u64; 25], pos: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only @@ -587,13 +585,13 @@ pub unsafe fn vg_shake128_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, sta #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHAKE128_FINALIZE_SHA3_FEATURES)) - && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHAKE128_FINALIZE_SHA3_FEATURES) } - || cpu.contains(crate::sha3::VG_SHAKE128_FINALIZE_SHA3_FEATURES)) + if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHAKE256_FINALIZE_SHA3_FEATURES)) + && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHAKE256_FINALIZE_SHA3_FEATURES) } + || cpu.contains(crate::sha3::VG_SHAKE256_FINALIZE_SHA3_FEATURES)) { - return crate::sha3::vg_shake128_finalize_sha3; + return crate::sha3::vg_shake256_finalize_sha3; } - crate::sha3::vg_shake128_finalize + crate::sha3::vg_shake256_finalize } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. @@ -614,44 +612,46 @@ pub unsafe fn vg_shake128_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, sta unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, pos) } } -/// Finishes absorbing a SHAKE256 message: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHAKE256's rate, as `vg_sha3_256_update` leaves it), it becomes a state that, squeezed by `vg_shake256_squeeze` from position 0, gives the output of SHAKE256 of that message (`VG.Spec.Sha3.shake256`) from its first byte on (`VG.Spec.Sha3.XofRepr`). +/// Squeezes SHAKE256 output: if the state `*state`, from position `pos`, continues the output of SHAKE256 of a message (`VG.Spec.Sha3.shake256`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake256_finalize` leaves and position 0. /// -/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointer and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_shake256_finalize_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHAKE256_FINALIZE_SHA3_FEATURES`); or else `vg_shake256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_shake256_squeeze_sha3` if the target's vendor is `apple` (or `VG_CPU_FEATURES` names every feature it needs) and the CPU supports its features (`VG_SHAKE256_SQUEEZE_SHA3_FEATURES`); or else `vg_shake256_squeeze`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `pos` must be less than 136. -/// * `state` must not overlap the 656 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of `outlen` bytes. +/// * `pos` must be at most 136. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 656 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { +pub unsafe fn vg_shake256_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { /// The type of the implementations. - type Impl = unsafe extern "C" fn(state: *mut [u64; 25], pos: usize); + type Impl = unsafe extern "C" fn(state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHAKE256_FINALIZE_SHA3_FEATURES)) - && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHAKE256_FINALIZE_SHA3_FEATURES) } - || cpu.contains(crate::sha3::VG_SHAKE256_FINALIZE_SHA3_FEATURES)) + if (cfg!(target_vendor = "apple") || verified_garbage_cpu::requested(crate::sha3::VG_SHAKE256_SQUEEZE_SHA3_FEATURES)) + && (const { verified_garbage_cpu::target_contains(crate::sha3::VG_SHAKE256_SQUEEZE_SHA3_FEATURES) } + || cpu.contains(crate::sha3::VG_SHAKE256_SQUEEZE_SHA3_FEATURES)) { - return crate::sha3::vg_shake256_finalize_sha3; + return crate::sha3::vg_shake256_squeeze_sha3; } - crate::sha3::vg_shake256_finalize + crate::sha3::vg_shake256_squeeze } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(state: *mut [u64; 25], pos: usize) { + unsafe extern "C" fn first(state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, pos) } + unsafe { f(state, pos, out, outlen) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -659,5 +659,5 @@ pub unsafe fn vg_shake256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, sta // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, pos) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, pos, out, outlen) } } diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/sha512.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/sha512.rs index d1b99a8e6d1..9a38c7e7d52 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/sha512.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/sha512.rs @@ -36,11 +36,95 @@ pub unsafe fn vg_sha512_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *m unsafe { crate::sha512::vg_sha512_init(state) } } +/// Starts a SHA-512/224 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/224 (`VG.Spec.Sha512.H0_512_224`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. +/// +/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_224`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). +/// +/// It runs `vg_sha512_224_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 192 bytes. +/// * `state` must not wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_sha512_224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::sha512::vg_sha512_224_init(state) } +} + +/// Starts a SHA-512/256 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/256 (`VG.Spec.Sha512.H0_512_256`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. +/// +/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_256`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). +/// +/// It runs `vg_sha512_256_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 192 bytes. +/// * `state` must not wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_sha512_256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::sha512::vg_sha512_256_init(state) } +} + +/// Finishes a SHA-384 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-384 (as `vg_sha384_init` starts it), writes the SHA-384 digest of that message (48 bytes, `VG.Spec.Sha512.sha384`) to `*out`. +/// +/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_384` and `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `count` may affect timing, not the state. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha384_finalize_sha512` if the CPU supports its features (`VG_SHA384_FINALIZE_SHA512_FEATURES`); or else `vg_sha384_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 192 bytes. +/// * `out` must be valid for reads and writes of 48 bytes. +/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 1392 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_sha384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 48]) { + /// The type of the implementations. + type Impl = unsafe extern "C" fn(state: *mut [u8; 192], count: u64, out: *mut [u8; 48]); + /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only + /// features the CPU has. + #[cold] + fn select() -> Impl { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA384_FINALIZE_SHA512_FEATURES) } + || cpu.contains(crate::sha512::VG_SHA384_FINALIZE_SHA512_FEATURES) + { + return crate::sha512::vg_sha384_finalize_sha512; + } + crate::sha512::vg_sha384_finalize + } + /// The first call: chooses the implementation, records it in `CHOSEN` for the calls + /// after it, and runs it. + #[cold] + unsafe extern "C" fn first(state: *mut [u8; 192], count: u64, out: *mut [u8; 48]) { + let f = select(); + CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); + // SAFETY: the caller's obligations, which are those of every implementation, `f` + // among them, and the CPU has the features `f` needs. + unsafe { f(state, count, out) } + } + /// The function to call: `first`, until it records the implementation it chose. + static CHOSEN: core::sync::atomic::AtomicPtr<()> = + core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); + // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, + // or an implementation `select` returned, which needs only features the CPU has. Calling + // either carries the caller's obligations, which are those of every implementation. + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, out) } +} + /// Computes the SHA-384 digest of the `len` bytes at `data` (48 bytes, `VG.Spec.Sha512.sha384`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-384 computation with that message. /// /// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha384_digest_sha512` if the CPU supports its features (`VG_SHA384_DIGEST_SHA512_FEATURES`); or else `vg_sha384_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha384_digest_sha512` if the CPU supports its features (`VG_SHA384_DIGEST_SHA512_FEATURES`); or else `vg_sha384_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -88,7 +172,7 @@ pub unsafe fn vg_sha384_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: * /// /// Contract: `VG.Spec.Sha512.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha512_update_sha512` if the CPU supports its features (`VG_SHA512_UPDATE_SHA512_FEATURES`); or else `vg_sha512_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha512_update_sha512` if the CPU supports its features (`VG_SHA512_UPDATE_SHA512_FEATURES`); or else `vg_sha512_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -132,44 +216,46 @@ pub unsafe fn vg_sha512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, data, len) } } -/// Computes the SHA-512 digest of the `len` bytes at `data` (64 bytes, `VG.Spec.Sha512.sha512`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512 computation with that message. +/// Finishes a SHA-384, SHA-512, SHA-512/224 or SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (64 bytes) to `*out`. The SHA-512 digest is all of it; the SHA-384, SHA-512/224 and SHA-512/256 digests are its first 48, 28 and 32 bytes. /// -/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha512.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha512_digest_sha512` if the CPU supports its features (`VG_SHA512_DIGEST_SHA512_FEATURES`); or else `vg_sha512_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha512_finalize_sha512` if the CPU supports its features (`VG_SHA512_FINALIZE_SHA512_FEATURES`); or else `vg_sha512_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `data` must be valid for reads of `len` bytes. +/// * `state` must be valid for reads and writes of 192 bytes. /// * `out` must be valid for reads and writes of 64 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the 896 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 1392 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 64]) { +pub unsafe fn vg_sha512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 64]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(data: *const u8, len: usize, out: *mut [u8; 64]); + type Impl = unsafe extern "C" fn(state: *mut [u8; 192], count: u64, out: *mut [u8; 64]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_DIGEST_SHA512_FEATURES) } - || cpu.contains(crate::sha512::VG_SHA512_DIGEST_SHA512_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_FINALIZE_SHA512_FEATURES) } + || cpu.contains(crate::sha512::VG_SHA512_FINALIZE_SHA512_FEATURES) { - return crate::sha512::vg_sha512_digest_sha512; + return crate::sha512::vg_sha512_finalize_sha512; } - crate::sha512::vg_sha512_digest + crate::sha512::vg_sha512_finalize } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(data: *const u8, len: usize, out: *mut [u8; 64]) { + unsafe extern "C" fn first(state: *mut [u8; 192], count: u64, out: *mut [u8; 64]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(data, len, out) } + unsafe { f(state, count, out) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -177,83 +263,47 @@ pub unsafe fn vg_sha512_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: * // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } -} - -/// Starts a SHA-512/224 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/224 (`VG.Spec.Sha512.H0_512_224`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. -/// -/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_224`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). -/// -/// It runs `vg_sha512_224_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 192 bytes. -/// * `state` must not wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_sha512_224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_224_init(state) } -} - -/// Starts a SHA-512/256 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/256 (`VG.Spec.Sha512.H0_512_256`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. -/// -/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_256`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). -/// -/// It runs `vg_sha512_256_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 192 bytes. -/// * `state` must not wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_sha512_256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_256_init(state) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, out) } } -/// Finishes a SHA-384 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-384 (as `vg_sha384_init` starts it), writes the SHA-384 digest of that message (48 bytes, `VG.Spec.Sha512.sha384`) to `*out`. +/// Computes the SHA-512 digest of the `len` bytes at `data` (64 bytes, `VG.Spec.Sha512.sha512`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512 computation with that message. /// -/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_384` and `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha384_finalize_sha512` if the CPU supports its features (`VG_SHA384_FINALIZE_SHA512_FEATURES`); or else `vg_sha384_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha512_digest_sha512` if the CPU supports its features (`VG_SHA512_DIGEST_SHA512_FEATURES`); or else `vg_sha512_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 48 bytes. -/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 1392 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `out` must be valid for reads and writes of 64 bytes. +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the 896 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 48]) { +pub unsafe fn vg_sha512_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 64]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(state: *mut [u8; 192], count: u64, out: *mut [u8; 48]); + type Impl = unsafe extern "C" fn(data: *const u8, len: usize, out: *mut [u8; 64]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA384_FINALIZE_SHA512_FEATURES) } - || cpu.contains(crate::sha512::VG_SHA384_FINALIZE_SHA512_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_DIGEST_SHA512_FEATURES) } + || cpu.contains(crate::sha512::VG_SHA512_DIGEST_SHA512_FEATURES) { - return crate::sha512::vg_sha384_finalize_sha512; + return crate::sha512::vg_sha512_digest_sha512; } - crate::sha512::vg_sha384_finalize + crate::sha512::vg_sha512_digest } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(state: *mut [u8; 192], count: u64, out: *mut [u8; 48]) { + unsafe extern "C" fn first(data: *const u8, len: usize, out: *mut [u8; 64]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, count, out) } + unsafe { f(data, len, out) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -261,44 +311,44 @@ pub unsafe fn vg_sha384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, out) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } } -/// Finishes a SHA-384, SHA-512, SHA-512/224 or SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (64 bytes) to `*out`. The SHA-512 digest is all of it; the SHA-384, SHA-512/224 and SHA-512/256 digests are its first 48, 28 and 32 bytes. +/// Finishes a SHA-512/224 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/224 (as `vg_sha512_224_init` starts it), writes the SHA-512/224 digest of that message (28 bytes, `VG.Spec.Sha512.sha512_224`) to `*out`. /// -/// Contract: `VG.Spec.Sha512.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_224` and `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha512_finalize_sha512` if the CPU supports its features (`VG_SHA512_FINALIZE_SHA512_FEATURES`); or else `vg_sha512_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha512_224_finalize_sha512` if the CPU supports its features (`VG_SHA512_224_FINALIZE_SHA512_FEATURES`); or else `vg_sha512_224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 64 bytes. +/// * `out` must be valid for reads and writes of 28 bytes. /// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the 1392 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 64]) { +pub unsafe fn vg_sha512_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 28]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(state: *mut [u8; 192], count: u64, out: *mut [u8; 64]); + type Impl = unsafe extern "C" fn(state: *mut [u8; 192], count: u64, out: *mut [u8; 28]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_FINALIZE_SHA512_FEATURES) } - || cpu.contains(crate::sha512::VG_SHA512_FINALIZE_SHA512_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_224_FINALIZE_SHA512_FEATURES) } + || cpu.contains(crate::sha512::VG_SHA512_224_FINALIZE_SHA512_FEATURES) { - return crate::sha512::vg_sha512_finalize_sha512; + return crate::sha512::vg_sha512_224_finalize_sha512; } - crate::sha512::vg_sha512_finalize + crate::sha512::vg_sha512_224_finalize } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(state: *mut [u8; 192], count: u64, out: *mut [u8; 64]) { + unsafe extern "C" fn first(state: *mut [u8; 192], count: u64, out: *mut [u8; 28]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` @@ -318,7 +368,7 @@ pub unsafe fn vg_sha512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state /// /// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha512_224_digest_sha512` if the CPU supports its features (`VG_SHA512_224_DIGEST_SHA512_FEATURES`); or else `vg_sha512_224_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha512_224_digest_sha512` if the CPU supports its features (`VG_SHA512_224_DIGEST_SHA512_FEATURES`); or else `vg_sha512_224_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -362,89 +412,41 @@ pub unsafe fn vg_sha512_224_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, dat unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } } -/// Computes the SHA-512/256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha512.sha512_256`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512/256 computation with that message. -/// -/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `len` may affect timing, not the data. -/// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha512_256_digest_sha512` if the CPU supports its features (`VG_SHA512_256_DIGEST_SHA512_FEATURES`); or else `vg_sha512_256_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. -/// -/// # Safety -/// -/// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 32 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the 896 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_sha512_256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { - /// The type of the implementations. - type Impl = unsafe extern "C" fn(data: *const u8, len: usize, out: *mut [u8; 32]); - /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only - /// features the CPU has. - #[cold] - fn select() -> Impl { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_256_DIGEST_SHA512_FEATURES) } - || cpu.contains(crate::sha512::VG_SHA512_256_DIGEST_SHA512_FEATURES) - { - return crate::sha512::vg_sha512_256_digest_sha512; - } - crate::sha512::vg_sha512_256_digest - } - /// The first call: chooses the implementation, records it in `CHOSEN` for the calls - /// after it, and runs it. - #[cold] - unsafe extern "C" fn first(data: *const u8, len: usize, out: *mut [u8; 32]) { - let f = select(); - CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); - // SAFETY: the caller's obligations, which are those of every implementation, `f` - // among them, and the CPU has the features `f` needs. - unsafe { f(data, len, out) } - } - /// The function to call: `first`, until it records the implementation it chose. - static CHOSEN: core::sync::atomic::AtomicPtr<()> = - core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); - // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, - // or an implementation `select` returned, which needs only features the CPU has. Calling - // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } -} - -/// Finishes a SHA-512/224 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/224 (as `vg_sha512_224_init` starts it), writes the SHA-512/224 digest of that message (28 bytes, `VG.Spec.Sha512.sha512_224`) to `*out`. +/// Finishes a SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/256 (as `vg_sha512_256_init` starts it), writes the SHA-512/256 digest of that message (32 bytes, `VG.Spec.Sha512.sha512_256`) to `*out`. /// -/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_224` and `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_256` and `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha512_224_finalize_sha512` if the CPU supports its features (`VG_SHA512_224_FINALIZE_SHA512_FEATURES`); or else `vg_sha512_224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha512_256_finalize_sha512` if the CPU supports its features (`VG_SHA512_256_FINALIZE_SHA512_FEATURES`); or else `vg_sha512_256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 28 bytes. +/// * `out` must be valid for reads and writes of 32 bytes. /// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the 1392 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 28]) { +pub unsafe fn vg_sha512_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 32]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(state: *mut [u8; 192], count: u64, out: *mut [u8; 28]); + type Impl = unsafe extern "C" fn(state: *mut [u8; 192], count: u64, out: *mut [u8; 32]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_224_FINALIZE_SHA512_FEATURES) } - || cpu.contains(crate::sha512::VG_SHA512_224_FINALIZE_SHA512_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_256_FINALIZE_SHA512_FEATURES) } + || cpu.contains(crate::sha512::VG_SHA512_256_FINALIZE_SHA512_FEATURES) { - return crate::sha512::vg_sha512_224_finalize_sha512; + return crate::sha512::vg_sha512_256_finalize_sha512; } - crate::sha512::vg_sha512_224_finalize + crate::sha512::vg_sha512_256_finalize } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(state: *mut [u8; 192], count: u64, out: *mut [u8; 28]) { + unsafe extern "C" fn first(state: *mut [u8; 192], count: u64, out: *mut [u8; 32]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` @@ -460,46 +462,44 @@ pub unsafe fn vg_sha512_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, s unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, out) } } -/// Finishes a SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/256 (as `vg_sha512_256_init` starts it), writes the SHA-512/256 digest of that message (32 bytes, `VG.Spec.Sha512.sha512_256`) to `*out`. +/// Computes the SHA-512/256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha512.sha512_256`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512/256 computation with that message. /// -/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_256` and `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha512_256_finalize_sha512` if the CPU supports its features (`VG_SHA512_256_FINALIZE_SHA512_FEATURES`); or else `vg_sha512_256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha512_256_digest_sha512` if the CPU supports its features (`VG_SHA512_256_DIGEST_SHA512_FEATURES`); or else `vg_sha512_256_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 192 bytes. +/// * `data` must be valid for reads of `len` bytes. /// * `out` must be valid for reads and writes of 32 bytes. -/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 1392 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the 896 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 32]) { +pub unsafe fn vg_sha512_256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(state: *mut [u8; 192], count: u64, out: *mut [u8; 32]); + type Impl = unsafe extern "C" fn(data: *const u8, len: usize, out: *mut [u8; 32]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_256_FINALIZE_SHA512_FEATURES) } - || cpu.contains(crate::sha512::VG_SHA512_256_FINALIZE_SHA512_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_256_DIGEST_SHA512_FEATURES) } + || cpu.contains(crate::sha512::VG_SHA512_256_DIGEST_SHA512_FEATURES) { - return crate::sha512::vg_sha512_256_finalize_sha512; + return crate::sha512::vg_sha512_256_digest_sha512; } - crate::sha512::vg_sha512_256_finalize + crate::sha512::vg_sha512_256_digest } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(state: *mut [u8; 192], count: u64, out: *mut [u8; 32]) { + unsafe extern "C" fn first(data: *const u8, len: usize, out: *mut [u8; 32]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, count, out) } + unsafe { f(data, len, out) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -507,5 +507,5 @@ pub unsafe fn vg_sha512_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, s // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, out) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } } diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/sm4_gcm.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/sm4_gcm.rs index 89e536632fc..64e5bacf528 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/sm4_gcm.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/sm4_gcm.rs @@ -27,7 +27,7 @@ pub unsafe fn vg_sm4_gcm_init_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *co /// /// Contract: `VG.Spec.Sm4.Gcm.sealContract`. Constant time: only the pointers and the lengths may affect timing, not the key context, the nonce, the additional data or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_seal_aes` if the CPU supports its features (`VG_SM4_GCM_SEAL_AES_FEATURES`); or else `vg_sm4_gcm_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_seal_aes` if the CPU supports its features (`VG_SM4_GCM_SEAL_AES_FEATURES`); or else `vg_sm4_gcm_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -80,7 +80,7 @@ pub unsafe fn vg_sm4_gcm_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *co /// /// Contract: `VG.Spec.Sm4.Gcm.openContract`. Constant time but for the result: only the pointers, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the additional data, the data or the tag. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_open_aes` if the CPU supports its features (`VG_SM4_GCM_OPEN_AES_FEATURES`); or else `vg_sm4_gcm_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_open_aes` if the CPU supports its features (`VG_SM4_GCM_OPEN_AES_FEATURES`); or else `vg_sm4_gcm_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -127,45 +127,47 @@ pub unsafe fn vg_sm4_gcm_open_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *co unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, nonce, nonce_len, aad, aad_len, data, len, tag, tag_len) } } -/// Absorbs additional data into an incremental SM4-GCM encryption or decryption: with the key context `*ctx`, if the streaming state `*state` represents a message with `aad_len` bytes (modulo 2⁶⁴) of additional data and no text yet, it then represents that message with the `len` bytes at `data` appended to its additional data. All of the additional data comes before the text (§7.1 step 5): after text, the state represents no message. +/// Encrypts the next piece of the text of an incremental SM4-GCM encryption (NIST SP 800-38D §7.1): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and the ciphertext of exactly `text_len` bytes of plaintext so far, encrypts the `len` bytes at `data` in place, as the continuation of that plaintext, and the state then represents the message with them appended. /// -/// Contract: `VG.Spec.Gcm.streamAadContract` (AES-GCM's, which reads only the hash subkey of the key context). Constant time: only the pointers, `aad_len` and `len` may affect timing, not the key context, the state or the data. +/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_stream_aad_aes` if the CPU supports its features (`VG_SM4_GCM_STREAM_AAD_AES_FEATURES`); or else `vg_sm4_gcm_stream_aad`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// Contract: `VG.Spec.Sm4.Gcm.streamEncryptContract`. Constant time: only the pointers, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_stream_encrypt_aes` if the CPU supports its features (`VG_SM4_GCM_STREAM_ENCRYPT_AES_FEATURES`); or else `vg_sm4_gcm_stream_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `state` must not overlap `ctx` or `data` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the 2560 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads and writes of `len` bytes. +/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the 4032 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sm4_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { +pub unsafe fn vg_sm4_gcm_stream_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize); + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_AAD_AES_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_AAD_AES_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_ENCRYPT_AES_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_ENCRYPT_AES_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_stream_aad_aes; + return crate::sm4_gcm::vg_sm4_gcm_stream_encrypt_aes; } - crate::sm4_gcm::vg_sm4_gcm_stream_aad + crate::sm4_gcm::vg_sm4_gcm_stream_encrypt } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { + unsafe extern "C" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, state, aad_len, data, len) } + unsafe { f(ctx, state, aad_len, text_len, data, len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -173,50 +175,50 @@ pub unsafe fn vg_sm4_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ct // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, data, len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, text_len, data, len) } } -/// Starts an incremental SM4-GCM encryption or decryption (NIST SP 800-38D §7): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, makes the streaming state `*state` represent the message with the IV the `nonce_len` bytes at `nonce`, and no additional data or text yet. Continue with `vg_sm4_gcm_stream_aad`, then `vg_sm4_gcm_stream_encrypt` or `vg_sm4_gcm_stream_decrypt`, then `vg_sm4_gcm_stream_finish` or `vg_sm4_gcm_stream_verify`, with the same key context. +/// Decrypts the next piece of the text of an incremental SM4-GCM decryption (NIST SP 800-38D §7.2): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext so far, decrypts the `len` bytes of ciphertext at `data` in place, as the continuation of that ciphertext, and the state then represents the message with them appended. The plaintext is not authenticated until `vg_sm4_gcm_stream_verify` has returned 1. /// -/// The function checks no length: GCM requires a nonce of 1 to `2^61 - 1` bytes (§5.2.1.1), which the caller must check. +/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.streamInitContract` (AES-GCM's, which reads only the hash subkey of the key context). The streaming state is `J₀`, the GHASH accumulator, a partial block, the next counter block and a keystream block (`VG.Spec.Gcm.StreamRepr`). Constant time: only the pointers and `nonce_len` may affect timing, not the key context or the nonce. +/// Contract: `VG.Spec.Sm4.Gcm.streamDecryptContract`. Constant time: only the pointers, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_stream_init_aes` if the CPU supports its features (`VG_SM4_GCM_STREAM_INIT_AES_FEATURES`); or else `vg_sm4_gcm_stream_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_stream_decrypt_aes` if the CPU supports its features (`VG_SM4_GCM_STREAM_DECRYPT_AES_FEATURES`); or else `vg_sm4_gcm_stream_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `nonce` must be valid for reads of `nonce_len` bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `state` must not overlap `ctx` or `nonce` (distinct Rust objects never do). -/// * None of `ctx`, `nonce` and `state` may overlap the 2560 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads and writes of `len` bytes. +/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the 4032 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sm4_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { +pub unsafe fn vg_sm4_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]); + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_INIT_AES_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_INIT_AES_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_DECRYPT_AES_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_DECRYPT_AES_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_stream_init_aes; + return crate::sm4_gcm::vg_sm4_gcm_stream_decrypt_aes; } - crate::sm4_gcm::vg_sm4_gcm_stream_init + crate::sm4_gcm::vg_sm4_gcm_stream_decrypt } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { + unsafe extern "C" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, nonce, nonce_len, state) } + unsafe { f(ctx, state, aad_len, text_len, data, len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -224,14 +226,14 @@ pub unsafe fn vg_sm4_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, c // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, nonce, nonce_len, state) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, text_len, data, len) } } /// Finishes an incremental SM4-GCM encryption or decryption (NIST SP 800-38D §7.1 steps 5–6, §7.2 steps 6–7): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, writes its 128-bit tag to `*tag`. A shorter tag is the first bytes of this one; to check a received tag, use `vg_sm4_gcm_stream_verify`. /// /// Contract: `VG.Spec.Sm4.Gcm.streamFinishContract`. Constant time: only the pointers, `aad_len` and `text_len` may affect timing, not the key context or the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_stream_finish_aes` if the CPU supports its features (`VG_SM4_GCM_STREAM_FINISH_AES_FEATURES`); or else `vg_sm4_gcm_stream_finish`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_stream_finish_aes` if the CPU supports its features (`VG_SM4_GCM_STREAM_FINISH_AES_FEATURES`); or else `vg_sm4_gcm_stream_finish`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -281,7 +283,7 @@ pub unsafe fn vg_sm4_gcm_stream_finish_auto(_dit: verified_garbage_cpu::Dit<'_>, /// /// Contract: `VG.Spec.Sm4.Gcm.streamVerifyContract`. Constant time: only the pointers, `aad_len`, `text_len` and `tag_len` may affect timing, not the key context, the state or the tags. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_stream_verify_aes` if the CPU supports its features (`VG_SM4_GCM_STREAM_VERIFY_AES_FEATURES`); or else `vg_sm4_gcm_stream_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_stream_verify_aes` if the CPU supports its features (`VG_SM4_GCM_STREAM_VERIFY_AES_FEATURES`); or else `vg_sm4_gcm_stream_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -327,47 +329,47 @@ pub unsafe fn vg_sm4_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, text_len, tag, tag_len) } } -/// Encrypts the next piece of the text of an incremental SM4-GCM encryption (NIST SP 800-38D §7.1): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and the ciphertext of exactly `text_len` bytes of plaintext so far, encrypts the `len` bytes at `data` in place, as the continuation of that plaintext, and the state then represents the message with them appended. +/// Starts an incremental SM4-GCM encryption or decryption (NIST SP 800-38D §7): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, makes the streaming state `*state` represent the message with the IV the `nonce_len` bytes at `nonce`, and no additional data or text yet. Continue with `vg_sm4_gcm_stream_aad`, then `vg_sm4_gcm_stream_encrypt` or `vg_sm4_gcm_stream_decrypt`, then `vg_sm4_gcm_stream_finish` or `vg_sm4_gcm_stream_verify`, with the same key context. /// -/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// The function checks no length: GCM requires a nonce of 1 to `2^61 - 1` bytes (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Sm4.Gcm.streamEncryptContract`. Constant time: only the pointers, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. +/// Contract: `VG.Spec.Gcm.streamInitContract` (AES-GCM's, which reads only the hash subkey of the key context). The streaming state is `J₀`, the GHASH accumulator, a partial block, the next counter block and a keystream block (`VG.Spec.Gcm.StreamRepr`). Constant time: only the pointers and `nonce_len` may affect timing, not the key context or the nonce. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_stream_encrypt_aes` if the CPU supports its features (`VG_SM4_GCM_STREAM_ENCRYPT_AES_FEATURES`); or else `vg_sm4_gcm_stream_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_stream_init_aes` if the CPU supports its features (`VG_SM4_GCM_STREAM_INIT_AES_FEATURES`); or else `vg_sm4_gcm_stream_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. +/// * `nonce` must be valid for reads of `nonce_len` bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `data` must be valid for reads and writes of `len` bytes. -/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the 4032 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must not overlap `ctx` or `nonce` (distinct Rust objects never do). +/// * None of `ctx`, `nonce` and `state` may overlap the 2560 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sm4_gcm_stream_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { +pub unsafe fn vg_sm4_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_ENCRYPT_AES_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_ENCRYPT_AES_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_INIT_AES_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_INIT_AES_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_stream_encrypt_aes; + return crate::sm4_gcm::vg_sm4_gcm_stream_init_aes; } - crate::sm4_gcm::vg_sm4_gcm_stream_encrypt + crate::sm4_gcm::vg_sm4_gcm_stream_init } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { + unsafe extern "C" fn first(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, state, aad_len, text_len, data, len) } + unsafe { f(ctx, nonce, nonce_len, state) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -375,50 +377,48 @@ pub unsafe fn vg_sm4_gcm_stream_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_> // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, text_len, data, len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, nonce, nonce_len, state) } } -/// Decrypts the next piece of the text of an incremental SM4-GCM decryption (NIST SP 800-38D §7.2): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext so far, decrypts the `len` bytes of ciphertext at `data` in place, as the continuation of that ciphertext, and the state then represents the message with them appended. The plaintext is not authenticated until `vg_sm4_gcm_stream_verify` has returned 1. -/// -/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// Absorbs additional data into an incremental SM4-GCM encryption or decryption: with the key context `*ctx`, if the streaming state `*state` represents a message with `aad_len` bytes (modulo 2⁶⁴) of additional data and no text yet, it then represents that message with the `len` bytes at `data` appended to its additional data. All of the additional data comes before the text (§7.1 step 5): after text, the state represents no message. /// -/// Contract: `VG.Spec.Sm4.Gcm.streamDecryptContract`. Constant time: only the pointers, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. +/// Contract: `VG.Spec.Gcm.streamAadContract` (AES-GCM's, which reads only the hash subkey of the key context). Constant time: only the pointers, `aad_len` and `len` may affect timing, not the key context, the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_stream_decrypt_aes` if the CPU supports its features (`VG_SM4_GCM_STREAM_DECRYPT_AES_FEATURES`); or else `vg_sm4_gcm_stream_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_stream_aad_aes` if the CPU supports its features (`VG_SM4_GCM_STREAM_AAD_AES_FEATURES`); or else `vg_sm4_gcm_stream_aad`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `data` must be valid for reads and writes of `len` bytes. -/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the 4032 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `state` must not overlap `ctx` or `data` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the 2560 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sm4_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { +pub unsafe fn vg_sm4_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_DECRYPT_AES_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_DECRYPT_AES_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_AAD_AES_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_AAD_AES_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_stream_decrypt_aes; + return crate::sm4_gcm::vg_sm4_gcm_stream_aad_aes; } - crate::sm4_gcm::vg_sm4_gcm_stream_decrypt + crate::sm4_gcm::vg_sm4_gcm_stream_aad } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { + unsafe extern "C" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, state, aad_len, text_len, data, len) } + unsafe { f(ctx, state, aad_len, data, len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -426,5 +426,5 @@ pub unsafe fn vg_sm4_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_> // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, text_len, data, len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, data, len) } } diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/x25519.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/x25519.rs index 2f942d37019..53e05e632fd 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/x25519.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/x25519.rs @@ -2,50 +2,50 @@ //! The dispatchers of the verified `x25519` functions for `aarch64` //! (`crate::x25519`, `VG.Rust.dispatcher`). -/// The bytes of `scratch` that `vg_x25519_base_auto` may write: it changes no byte of `scratch` at this offset or beyond (`Artifact.written`). -pub const VG_X25519_BASE_AUTO_SCRATCH_WRITTEN: usize = 1024; - -/// Computes an X25519 public key (RFC 7748 §6.1): writes `X25519(k, 9)`, the X25519 function of the 32-byte scalar `k` at `scalar` and the base point's u-coordinate 9, to `*out`. The scalar is decoded (clamped). -/// -/// Contract: `VG.Spec.X25519.x25519BaseContract`. Constant time: only the pointers may affect timing, not the scalar. +/// Computes an X25519 shared secret (RFC 7748 §6.1): writes `X25519(k, u)` to `*out`, for the 32-byte scalar `k` at `scalar` and the 32-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped) and the most significant bit of `u` masked as RFC 7748 specifies (§5). /// -/// It runs `vg_x25519_base`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// Contract: `VG.Spec.X25519.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. /// -/// It writes at most the first 1024 bytes of `scratch` (`VG_X25519_BASE_AUTO_SCRATCH_WRITTEN`): no byte of `scratch` beyond them changes. +/// It runs `vg_x25519_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `out` must be valid for reads and writes of 32 bytes. /// * `scalar` must be valid for reads of 32 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * `point` must be valid for reads of 32 bytes. +/// * `scratch` must be valid for reads and writes of 4096 bytes. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other or `scalar` (distinct Rust objects never do). -/// * None of `out`, `scalar` and `scratch` may wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `scalar` or `point` (distinct Rust objects never do). +/// * None of `out`, `scalar`, `point` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x25519_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], scratch: *mut [u64; 1024]) { +pub unsafe fn vg_x25519_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], point: *const [u8; 32], scratch: *mut [u64; 512]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::x25519::vg_x25519_base(out, scalar, scratch) } + unsafe { crate::x25519::vg_x25519_exchange(out, scalar, point, scratch) } } -/// Computes an X25519 shared secret (RFC 7748 §6.1): writes `X25519(k, u)` to `*out`, for the 32-byte scalar `k` at `scalar` and the 32-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped) and the most significant bit of `u` masked as RFC 7748 specifies (§5). +/// The bytes of `scratch` that `vg_x25519_base_auto` may write: it changes no byte of `scratch` at this offset or beyond (`Artifact.written`). +pub const VG_X25519_BASE_AUTO_SCRATCH_WRITTEN: usize = 1024; + +/// Computes an X25519 public key (RFC 7748 §6.1): writes `X25519(k, 9)`, the X25519 function of the 32-byte scalar `k` at `scalar` and the base point's u-coordinate 9, to `*out`. The scalar is decoded (clamped). /// -/// Contract: `VG.Spec.X25519.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. +/// Contract: `VG.Spec.X25519.x25519BaseContract`. Constant time: only the pointers may affect timing, not the scalar. /// -/// It runs `vg_x25519_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_x25519_base`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// It writes at most the first 1024 bytes of `scratch` (`VG_X25519_BASE_AUTO_SCRATCH_WRITTEN`): no byte of `scratch` beyond them changes. /// /// # Safety /// /// * `out` must be valid for reads and writes of 32 bytes. /// * `scalar` must be valid for reads of 32 bytes. -/// * `point` must be valid for reads of 32 bytes. -/// * `scratch` must be valid for reads and writes of 4096 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `scalar` or `point` (distinct Rust objects never do). -/// * None of `out`, `scalar`, `point` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other or `scalar` (distinct Rust objects never do). +/// * None of `out`, `scalar` and `scratch` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x25519_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], point: *const [u8; 32], scratch: *mut [u64; 512]) -> u32 { +pub unsafe fn vg_x25519_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], scratch: *mut [u64; 1024]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::x25519::vg_x25519_exchange(out, scalar, point, scratch) } + unsafe { crate::x25519::vg_x25519_base(out, scalar, scratch) } } diff --git a/crates/verified-garbage-asm-aarch64/src/dispatch/x448.rs b/crates/verified-garbage-asm-aarch64/src/dispatch/x448.rs index 41a74dfcad8..ccd5f5d0535 100644 --- a/crates/verified-garbage-asm-aarch64/src/dispatch/x448.rs +++ b/crates/verified-garbage-asm-aarch64/src/dispatch/x448.rs @@ -2,45 +2,45 @@ //! The dispatchers of the verified `x448` functions for `aarch64` //! (`crate::x448`, `VG.Rust.dispatcher`). -/// Computes an X448 public key (RFC 7748 §6.2): writes `X448(k, 5)`, the X448 function of the 56-byte scalar `k` at `scalar` and the base point's u-coordinate 5, to `*out`. The scalar is decoded (clamped). +/// Computes an X448 shared secret (RFC 7748 §6.2): writes `X448(k, u)` to `*out`, for the 56-byte scalar `k` at `scalar` and the 56-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped); all 448 bits of `u` are used and noncanonical coordinates are reduced modulo the field prime (§5). /// -/// Contract: `VG.Spec.X448.x448BaseContract`. Constant time: only the pointers may affect timing, not the scalar. +/// Contract: `VG.Spec.X448.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. /// -/// It runs `vg_x448_base`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_x448_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `out` must be valid for reads and writes of 56 bytes. /// * `scalar` must be valid for reads of 56 bytes. +/// * `point` must be valid for reads of 56 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other or `scalar` (distinct Rust objects never do). -/// * None of `out`, `scalar` and `scratch` may wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `scalar` or `point` (distinct Rust objects never do). +/// * None of `out`, `scalar`, `point` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x448_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], scratch: *mut [u64; 1024]) { +pub unsafe fn vg_x448_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], point: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::x448::vg_x448_base(out, scalar, scratch) } + unsafe { crate::x448::vg_x448_exchange(out, scalar, point, scratch) } } -/// Computes an X448 shared secret (RFC 7748 §6.2): writes `X448(k, u)` to `*out`, for the 56-byte scalar `k` at `scalar` and the 56-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped); all 448 bits of `u` are used and noncanonical coordinates are reduced modulo the field prime (§5). +/// Computes an X448 public key (RFC 7748 §6.2): writes `X448(k, 5)`, the X448 function of the 56-byte scalar `k` at `scalar` and the base point's u-coordinate 5, to `*out`. The scalar is decoded (clamped). /// -/// Contract: `VG.Spec.X448.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. +/// Contract: `VG.Spec.X448.x448BaseContract`. Constant time: only the pointers may affect timing, not the scalar. /// -/// It runs `vg_x448_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_x448_base`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `out` must be valid for reads and writes of 56 bytes. /// * `scalar` must be valid for reads of 56 bytes. -/// * `point` must be valid for reads of 56 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `scalar` or `point` (distinct Rust objects never do). -/// * None of `out`, `scalar`, `point` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other or `scalar` (distinct Rust objects never do). +/// * None of `out`, `scalar` and `scratch` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x448_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], point: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_x448_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], scratch: *mut [u64; 1024]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::x448::vg_x448_exchange(out, scalar, point, scratch) } + unsafe { crate::x448::vg_x448_base(out, scalar, scratch) } } diff --git a/crates/verified-garbage-asm-aarch64/src/fn_tables/mod.rs b/crates/verified-garbage-asm-aarch64/src/fn_tables/mod.rs index 1777314d4fc..3cfa0363e00 100644 --- a/crates/verified-garbage-asm-aarch64/src/fn_tables/mod.rs +++ b/crates/verified-garbage-asm-aarch64/src/fn_tables/mod.rs @@ -2,36 +2,36 @@ //! The tables of functions of the verified functions for `aarch64` (`Artifact.tables`). #![allow(dead_code, unused_imports)] -/// A table of functions of the interface `Hash` (`VG.FnTable`): the addresses of its 3 functions, then its 2 words of data. Only the statics of this module are of this type. +/// A table of functions of the interface `Hmac` (`VG.FnTable`): the addresses of its 3 functions, then its 3 words of data. Only the statics of this module are of this type. #[repr(C)] -pub struct Hash { +pub struct Hmac { fns: [*const (); 3], - data: [usize; 2], + data: [usize; 3], } -// SAFETY: a `Hash` is immutable, and the functions it holds the addresses of may be called from any thread. -unsafe impl Sync for Hash {} +// SAFETY: a `Hmac` is immutable, and the functions it holds the addresses of may be called from any thread. +unsafe impl Sync for Hmac {} -impl Hash { +impl Hmac { /// A pointer to the table's words, as the verified functions that take it read them. - pub const fn as_ptr(&'static self) -> *const [usize; 5] { + pub const fn as_ptr(&'static self) -> *const [usize; 6] { core::ptr::from_ref(self).cast() } } -/// A table of functions of the interface `Hmac` (`VG.FnTable`): the addresses of its 3 functions, then its 3 words of data. Only the statics of this module are of this type. +/// A table of functions of the interface `Hash` (`VG.FnTable`): the addresses of its 3 functions, then its 2 words of data. Only the statics of this module are of this type. #[repr(C)] -pub struct Hmac { +pub struct Hash { fns: [*const (); 3], - data: [usize; 3], + data: [usize; 2], } -// SAFETY: a `Hmac` is immutable, and the functions it holds the addresses of may be called from any thread. -unsafe impl Sync for Hmac {} +// SAFETY: a `Hash` is immutable, and the functions it holds the addresses of may be called from any thread. +unsafe impl Sync for Hash {} -impl Hmac { +impl Hash { /// A pointer to the table's words, as the verified functions that take it read them. - pub const fn as_ptr(&'static self) -> *const [usize; 6] { + pub const fn as_ptr(&'static self) -> *const [usize; 5] { core::ptr::from_ref(self).cast() } } @@ -54,181 +54,181 @@ impl MdHash { } #[rustfmt::skip] -mod vg_hash_md5; -pub use vg_hash_md5::VG_HASH_MD5; +mod vg_hmac_md5; +pub use vg_hmac_md5::VG_HMAC_MD5; #[rustfmt::skip] -mod vg_hash_sha1; -pub use vg_hash_sha1::VG_HASH_SHA1; +mod vg_hmac_sha1_sha2; +pub use vg_hmac_sha1_sha2::{VG_HMAC_SHA1_SHA2, VG_HMAC_SHA1_SHA2_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha1_sha2; -pub use vg_hash_sha1_sha2::{VG_HASH_SHA1_SHA2, VG_HASH_SHA1_SHA2_FEATURES}; +mod vg_hmac_sha1; +pub use vg_hmac_sha1::VG_HMAC_SHA1; #[rustfmt::skip] -mod vg_hash_sha224; -pub use vg_hash_sha224::VG_HASH_SHA224; +mod vg_hmac_sha224_sha2; +pub use vg_hmac_sha224_sha2::{VG_HMAC_SHA224_SHA2, VG_HMAC_SHA224_SHA2_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha224_sha2; -pub use vg_hash_sha224_sha2::{VG_HASH_SHA224_SHA2, VG_HASH_SHA224_SHA2_FEATURES}; +mod vg_hmac_sha224; +pub use vg_hmac_sha224::VG_HMAC_SHA224; #[rustfmt::skip] -mod vg_hash_sha256; -pub use vg_hash_sha256::VG_HASH_SHA256; +mod vg_hmac_sha256_sha2; +pub use vg_hmac_sha256_sha2::{VG_HMAC_SHA256_SHA2, VG_HMAC_SHA256_SHA2_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha256_sha2; -pub use vg_hash_sha256_sha2::{VG_HASH_SHA256_SHA2, VG_HASH_SHA256_SHA2_FEATURES}; +mod vg_hmac_sha256; +pub use vg_hmac_sha256::VG_HMAC_SHA256; #[rustfmt::skip] -mod vg_hash_sha384; -pub use vg_hash_sha384::VG_HASH_SHA384; +mod vg_hmac_sha384_sha512; +pub use vg_hmac_sha384_sha512::{VG_HMAC_SHA384_SHA512, VG_HMAC_SHA384_SHA512_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha384_sha512; -pub use vg_hash_sha384_sha512::{VG_HASH_SHA384_SHA512, VG_HASH_SHA384_SHA512_FEATURES}; +mod vg_hmac_sha384; +pub use vg_hmac_sha384::VG_HMAC_SHA384; #[rustfmt::skip] -mod vg_hash_sha512; -pub use vg_hash_sha512::VG_HASH_SHA512; +mod vg_hmac_sha512_sha512; +pub use vg_hmac_sha512_sha512::{VG_HMAC_SHA512_SHA512, VG_HMAC_SHA512_SHA512_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha512_224; -pub use vg_hash_sha512_224::VG_HASH_SHA512_224; +mod vg_hmac_sha512; +pub use vg_hmac_sha512::VG_HMAC_SHA512; #[rustfmt::skip] -mod vg_hash_sha512_224_sha512; -pub use vg_hash_sha512_224_sha512::{VG_HASH_SHA512_224_SHA512, VG_HASH_SHA512_224_SHA512_FEATURES}; +mod vg_hmac_sha512_224_sha512; +pub use vg_hmac_sha512_224_sha512::{VG_HMAC_SHA512_224_SHA512, VG_HMAC_SHA512_224_SHA512_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha512_256; -pub use vg_hash_sha512_256::VG_HASH_SHA512_256; +mod vg_hmac_sha512_224; +pub use vg_hmac_sha512_224::VG_HMAC_SHA512_224; #[rustfmt::skip] -mod vg_hash_sha512_256_sha512; -pub use vg_hash_sha512_256_sha512::{VG_HASH_SHA512_256_SHA512, VG_HASH_SHA512_256_SHA512_FEATURES}; +mod vg_hmac_sha512_256_sha512; +pub use vg_hmac_sha512_256_sha512::{VG_HMAC_SHA512_256_SHA512, VG_HMAC_SHA512_256_SHA512_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha512_sha512; -pub use vg_hash_sha512_sha512::{VG_HASH_SHA512_SHA512, VG_HASH_SHA512_SHA512_FEATURES}; +mod vg_hmac_sha512_256; +pub use vg_hmac_sha512_256::VG_HMAC_SHA512_256; #[rustfmt::skip] -mod vg_hmac_md5; -pub use vg_hmac_md5::VG_HMAC_MD5; +mod vg_hash_md5; +pub use vg_hash_md5::VG_HASH_MD5; #[rustfmt::skip] -mod vg_hmac_sha1; -pub use vg_hmac_sha1::VG_HMAC_SHA1; +mod vg_hash_sha1_sha2; +pub use vg_hash_sha1_sha2::{VG_HASH_SHA1_SHA2, VG_HASH_SHA1_SHA2_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha1_sha2; -pub use vg_hmac_sha1_sha2::{VG_HMAC_SHA1_SHA2, VG_HMAC_SHA1_SHA2_FEATURES}; +mod vg_hash_sha1; +pub use vg_hash_sha1::VG_HASH_SHA1; #[rustfmt::skip] -mod vg_hmac_sha224; -pub use vg_hmac_sha224::VG_HMAC_SHA224; +mod vg_hash_sha224_sha2; +pub use vg_hash_sha224_sha2::{VG_HASH_SHA224_SHA2, VG_HASH_SHA224_SHA2_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha224_sha2; -pub use vg_hmac_sha224_sha2::{VG_HMAC_SHA224_SHA2, VG_HMAC_SHA224_SHA2_FEATURES}; +mod vg_hash_sha224; +pub use vg_hash_sha224::VG_HASH_SHA224; #[rustfmt::skip] -mod vg_hmac_sha256; -pub use vg_hmac_sha256::VG_HMAC_SHA256; +mod vg_hash_sha256_sha2; +pub use vg_hash_sha256_sha2::{VG_HASH_SHA256_SHA2, VG_HASH_SHA256_SHA2_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha256_sha2; -pub use vg_hmac_sha256_sha2::{VG_HMAC_SHA256_SHA2, VG_HMAC_SHA256_SHA2_FEATURES}; +mod vg_hash_sha256; +pub use vg_hash_sha256::VG_HASH_SHA256; #[rustfmt::skip] -mod vg_hmac_sha384; -pub use vg_hmac_sha384::VG_HMAC_SHA384; +mod vg_hash_sha384_sha512; +pub use vg_hash_sha384_sha512::{VG_HASH_SHA384_SHA512, VG_HASH_SHA384_SHA512_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha384_sha512; -pub use vg_hmac_sha384_sha512::{VG_HMAC_SHA384_SHA512, VG_HMAC_SHA384_SHA512_FEATURES}; +mod vg_hash_sha384; +pub use vg_hash_sha384::VG_HASH_SHA384; #[rustfmt::skip] -mod vg_hmac_sha512; -pub use vg_hmac_sha512::VG_HMAC_SHA512; +mod vg_hash_sha512_sha512; +pub use vg_hash_sha512_sha512::{VG_HASH_SHA512_SHA512, VG_HASH_SHA512_SHA512_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha512_224; -pub use vg_hmac_sha512_224::VG_HMAC_SHA512_224; +mod vg_hash_sha512; +pub use vg_hash_sha512::VG_HASH_SHA512; #[rustfmt::skip] -mod vg_hmac_sha512_224_sha512; -pub use vg_hmac_sha512_224_sha512::{VG_HMAC_SHA512_224_SHA512, VG_HMAC_SHA512_224_SHA512_FEATURES}; +mod vg_hash_sha512_224_sha512; +pub use vg_hash_sha512_224_sha512::{VG_HASH_SHA512_224_SHA512, VG_HASH_SHA512_224_SHA512_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha512_256; -pub use vg_hmac_sha512_256::VG_HMAC_SHA512_256; +mod vg_hash_sha512_224; +pub use vg_hash_sha512_224::VG_HASH_SHA512_224; #[rustfmt::skip] -mod vg_hmac_sha512_256_sha512; -pub use vg_hmac_sha512_256_sha512::{VG_HMAC_SHA512_256_SHA512, VG_HMAC_SHA512_256_SHA512_FEATURES}; +mod vg_hash_sha512_256_sha512; +pub use vg_hash_sha512_256_sha512::{VG_HASH_SHA512_256_SHA512, VG_HASH_SHA512_256_SHA512_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha512_sha512; -pub use vg_hmac_sha512_sha512::{VG_HMAC_SHA512_SHA512, VG_HMAC_SHA512_SHA512_FEATURES}; +mod vg_hash_sha512_256; +pub use vg_hash_sha512_256::VG_HASH_SHA512_256; #[rustfmt::skip] mod vg_md_hash_md5; pub use vg_md_hash_md5::VG_MD_HASH_MD5; +#[rustfmt::skip] +mod vg_md_hash_sha1_sha2; +pub use vg_md_hash_sha1_sha2::{VG_MD_HASH_SHA1_SHA2, VG_MD_HASH_SHA1_SHA2_FEATURES}; + #[rustfmt::skip] mod vg_md_hash_sha1; pub use vg_md_hash_sha1::VG_MD_HASH_SHA1; #[rustfmt::skip] -mod vg_md_hash_sha1_sha2; -pub use vg_md_hash_sha1_sha2::{VG_MD_HASH_SHA1_SHA2, VG_MD_HASH_SHA1_SHA2_FEATURES}; +mod vg_md_hash_sha224_sha2; +pub use vg_md_hash_sha224_sha2::{VG_MD_HASH_SHA224_SHA2, VG_MD_HASH_SHA224_SHA2_FEATURES}; #[rustfmt::skip] mod vg_md_hash_sha224; pub use vg_md_hash_sha224::VG_MD_HASH_SHA224; #[rustfmt::skip] -mod vg_md_hash_sha224_sha2; -pub use vg_md_hash_sha224_sha2::{VG_MD_HASH_SHA224_SHA2, VG_MD_HASH_SHA224_SHA2_FEATURES}; +mod vg_md_hash_sha256_sha2; +pub use vg_md_hash_sha256_sha2::{VG_MD_HASH_SHA256_SHA2, VG_MD_HASH_SHA256_SHA2_FEATURES}; #[rustfmt::skip] mod vg_md_hash_sha256; pub use vg_md_hash_sha256::VG_MD_HASH_SHA256; #[rustfmt::skip] -mod vg_md_hash_sha256_sha2; -pub use vg_md_hash_sha256_sha2::{VG_MD_HASH_SHA256_SHA2, VG_MD_HASH_SHA256_SHA2_FEATURES}; +mod vg_md_hash_sha384_sha512; +pub use vg_md_hash_sha384_sha512::{VG_MD_HASH_SHA384_SHA512, VG_MD_HASH_SHA384_SHA512_FEATURES}; #[rustfmt::skip] mod vg_md_hash_sha384; pub use vg_md_hash_sha384::VG_MD_HASH_SHA384; #[rustfmt::skip] -mod vg_md_hash_sha384_sha512; -pub use vg_md_hash_sha384_sha512::{VG_MD_HASH_SHA384_SHA512, VG_MD_HASH_SHA384_SHA512_FEATURES}; +mod vg_md_hash_sha512_sha512; +pub use vg_md_hash_sha512_sha512::{VG_MD_HASH_SHA512_SHA512, VG_MD_HASH_SHA512_SHA512_FEATURES}; #[rustfmt::skip] mod vg_md_hash_sha512; pub use vg_md_hash_sha512::VG_MD_HASH_SHA512; -#[rustfmt::skip] -mod vg_md_hash_sha512_224; -pub use vg_md_hash_sha512_224::VG_MD_HASH_SHA512_224; - #[rustfmt::skip] mod vg_md_hash_sha512_224_sha512; pub use vg_md_hash_sha512_224_sha512::{VG_MD_HASH_SHA512_224_SHA512, VG_MD_HASH_SHA512_224_SHA512_FEATURES}; #[rustfmt::skip] -mod vg_md_hash_sha512_256; -pub use vg_md_hash_sha512_256::VG_MD_HASH_SHA512_256; +mod vg_md_hash_sha512_224; +pub use vg_md_hash_sha512_224::VG_MD_HASH_SHA512_224; #[rustfmt::skip] mod vg_md_hash_sha512_256_sha512; pub use vg_md_hash_sha512_256_sha512::{VG_MD_HASH_SHA512_256_SHA512, VG_MD_HASH_SHA512_256_SHA512_FEATURES}; #[rustfmt::skip] -mod vg_md_hash_sha512_sha512; -pub use vg_md_hash_sha512_sha512::{VG_MD_HASH_SHA512_SHA512, VG_MD_HASH_SHA512_SHA512_FEATURES}; +mod vg_md_hash_sha512_256; +pub use vg_md_hash_sha512_256::VG_MD_HASH_SHA512_256; diff --git a/crates/verified-garbage-asm-arm/src/dispatch/chacha20.rs b/crates/verified-garbage-asm-arm/src/dispatch/chacha20.rs index 0820ab226d6..ac1b29fde66 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/chacha20.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/chacha20.rs @@ -22,40 +22,40 @@ pub unsafe fn vg_chacha20_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: unsafe { crate::chacha20::vg_chacha20_init(state, key, nonce) } } -/// Applies a ChaCha20 keystream (RFC 8439 §2.4), encrypting or decrypting: if at least `len` bytes are left of the keystream that the streaming state `*state` represents, XORs the next `len` of them into the `len` bytes at `data`, keeps the rest in `*state`, and returns 1. Otherwise (the block counter would pass 2³² − 1) returns 0, and leaves the bytes at `data` and the keystream unchanged. +/// Restarts a ChaCha20 keystream with the same key: makes the streaming state `*state` represent the whole keystream of its key for the 16-byte nonce `*nonce` (as `vg_chacha20_init` does), discarding what was left of the previous one. /// -/// Contract: `VG.Spec.ChaCha20.applyContract`. Constant time: only the pointers, `len` and the number of bytes of keystream left in `*state` (`VG.Spec.ChaCha20.leftAt`: 64 × (2³² − `c`) for the initial block counter `c`, less the bytes applied since) may affect timing, not the key, the nonce, the keystream or the data. The function may leak that number. +/// Contract: `VG.Spec.ChaCha20.setNonceContract`. Constant time: only the pointers may affect timing, not the key or the nonce. /// -/// It runs `vg_chacha20_apply`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_chacha20_set_nonce`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 768 bytes. -/// * `data` must be valid for reads and writes of `len` bytes. -/// * `state` and `data` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `data` may wrap around the end of the address space (no Rust object does). +/// * `nonce` must be valid for reads of 16 bytes. +/// * `state` must not overlap `nonce` (distinct Rust objects never do). +/// * Neither `state` nor `nonce` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_chacha20_apply_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 96], data: *mut u8, len: usize) -> u32 { +pub unsafe fn vg_chacha20_set_nonce_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 96], nonce: *const [u8; 16]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::chacha20::vg_chacha20_apply(state, data, len) } + unsafe { crate::chacha20::vg_chacha20_set_nonce(state, nonce) } } -/// Restarts a ChaCha20 keystream with the same key: makes the streaming state `*state` represent the whole keystream of its key for the 16-byte nonce `*nonce` (as `vg_chacha20_init` does), discarding what was left of the previous one. +/// Applies a ChaCha20 keystream (RFC 8439 §2.4), encrypting or decrypting: if at least `len` bytes are left of the keystream that the streaming state `*state` represents, XORs the next `len` of them into the `len` bytes at `data`, keeps the rest in `*state`, and returns 1. Otherwise (the block counter would pass 2³² − 1) returns 0, and leaves the bytes at `data` and the keystream unchanged. /// -/// Contract: `VG.Spec.ChaCha20.setNonceContract`. Constant time: only the pointers may affect timing, not the key or the nonce. +/// Contract: `VG.Spec.ChaCha20.applyContract`. Constant time: only the pointers, `len` and the number of bytes of keystream left in `*state` (`VG.Spec.ChaCha20.leftAt`: 64 × (2³² − `c`) for the initial block counter `c`, less the bytes applied since) may affect timing, not the key, the nonce, the keystream or the data. The function may leak that number. /// -/// It runs `vg_chacha20_set_nonce`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_chacha20_apply`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 768 bytes. -/// * `nonce` must be valid for reads of 16 bytes. -/// * `state` must not overlap `nonce` (distinct Rust objects never do). -/// * Neither `state` nor `nonce` may wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads and writes of `len` bytes. +/// * `state` and `data` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `data` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_chacha20_set_nonce_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 96], nonce: *const [u8; 16]) { +pub unsafe fn vg_chacha20_apply_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 96], data: *mut u8, len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::chacha20::vg_chacha20_set_nonce(state, nonce) } + unsafe { crate::chacha20::vg_chacha20_apply(state, data, len) } } diff --git a/crates/verified-garbage-asm-arm/src/dispatch/cmac_triple_des.rs b/crates/verified-garbage-asm-arm/src/dispatch/cmac_triple_des.rs index 916f60b2975..80a7775b9b7 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/cmac_triple_des.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/cmac_triple_des.rs @@ -2,6 +2,26 @@ //! The dispatchers of the verified `cmac_triple_des` functions for `arm` //! (`crate::cmac_triple_des`, `VG.Rust.dispatcher`). +/// Starts a TDEA-CMAC computation (NIST SP 800-38B): makes the streaming state `*state` represent the empty message under the 16- or 24-byte TDEA key at `key` (FIPS 46-3; a 16-byte key is `K1 ‖ K2` with `K3 = K1`, parity bits are ignored). The state holds the key schedule (the three DES schedules), the subkeys `K1 ‖ K2` (§6.1), the chaining value and the message's last bytes, held back (`VG.Spec.Cmac.tdesRepr`). Continue with `vg_cmac_triple_des_absorb` and `vg_cmac_triple_des_finish`. +/// +/// Contract: `VG.Spec.Cmac.tdesStreamInitContract`. Constant time: only the pointers and `key_len` may affect timing, not the key. +/// +/// It runs `vg_cmac_triple_des_stream_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 416 bytes. +/// * `key` must be valid for reads of `key_len` bytes. +/// * `key_len` must be 16 or 24. +/// * `state` must not overlap `key` (distinct Rust objects never do). +/// * Neither `state` nor `key` may overlap the 640 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_cmac_triple_des_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 52], key: *const u8, key_len: usize) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::cmac_triple_des::vg_cmac_triple_des_stream_init(state, key, key_len) } +} + /// Absorbs data into a TDEA-CMAC computation (NIST SP 800-38B §6.2): if the streaming state `*state` represents a message of `count` bytes under a TDEA key (as `vg_cmac_triple_des_stream_init` set it up), it then represents that message followed by the `len` bytes at `data`, under the same key, provided that the two together are shorter than 2⁶⁴ bytes. It chains (step 6) every block of the message but its last bytes `Mₙ*` (step 3), which it holds back in the state, since only `vg_cmac_triple_des_finish` knows they are the last. /// /// Contract: `VG.Spec.Cmac.tdesAbsorbContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. @@ -40,23 +60,3 @@ pub unsafe fn vg_cmac_triple_des_finish_auto(_dit: verified_garbage_cpu::Dit<'_> // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::cmac_triple_des::vg_cmac_triple_des_finish(state, count, out) } } - -/// Starts a TDEA-CMAC computation (NIST SP 800-38B): makes the streaming state `*state` represent the empty message under the 16- or 24-byte TDEA key at `key` (FIPS 46-3; a 16-byte key is `K1 ‖ K2` with `K3 = K1`, parity bits are ignored). The state holds the key schedule (the three DES schedules), the subkeys `K1 ‖ K2` (§6.1), the chaining value and the message's last bytes, held back (`VG.Spec.Cmac.tdesRepr`). Continue with `vg_cmac_triple_des_absorb` and `vg_cmac_triple_des_finish`. -/// -/// Contract: `VG.Spec.Cmac.tdesStreamInitContract`. Constant time: only the pointers and `key_len` may affect timing, not the key. -/// -/// It runs `vg_cmac_triple_des_stream_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 416 bytes. -/// * `key` must be valid for reads of `key_len` bytes. -/// * `key_len` must be 16 or 24. -/// * `state` must not overlap `key` (distinct Rust objects never do). -/// * Neither `state` nor `key` may overlap the 640 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_cmac_triple_des_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 52], key: *const u8, key_len: usize) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::cmac_triple_des::vg_cmac_triple_des_stream_init(state, key, key_len) } -} diff --git a/crates/verified-garbage-asm-arm/src/dispatch/dsa.rs b/crates/verified-garbage-asm-arm/src/dispatch/dsa.rs index b50684d331a..44c9f48ae33 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/dsa.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/dsa.rs @@ -2,6 +2,51 @@ //! The dispatchers of the verified `dsa` functions for `arm` //! (`crate::dsa`, `VG.Rust.dispatcher`). +/// Randomized DSA signing of a prehashed digest, for an already validated packed private key (big-endian p||q||g||y||x; vg_dsa_validate_private_key validates it). Up to eight signing attempts, each with 128 nonce draws. Returns 1 with fixed-width r||s, 0 for retry exhaustion, or 2 for insufficient random bytes; out is zero on failure and used counts consumed complete draws on every return. Timing may depend on public p,q,g,y, sampling decisions, signing success bits and consumption, not otherwise on secrets. Contract: `VG.Spec.Dsa.randomSignPrevalidatedContract`. +/// +/// It runs `vg_dsa_sign_random_prevalidated`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `out` must be valid for reads and writes of `out_len` bytes. +/// * `used` must be valid for reads and writes of 8 bytes. +/// * `key` must be valid for reads of `key_len` bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `rand` must be valid for reads of `rand_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). +/// * key_len must be 3*p_len + 2*q_len; out_len must be 2*q_len. +/// * scratch_len must be at least 16*p_len + 64 words; destroy scratch after use. +/// * The exact key bytes must satisfy validatePrivateKeyBytes p_len q_len key = true. A successful vg_dsa_validate_private_key call establishes this only while the key bytes remain unchanged. Primality remains the caller's responsibility. +/// * Provide fresh independent random octets; never reuse a completed signing tape for another signature. +/// * `out`, `used` and `scratch` must not overlap each other, `key`, `digest`, `rand` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `used`, `key`, `digest`, `rand` and `scratch` may overlap the 144 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_dsa_sign_random_prevalidated_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], key: *const u8, key_len: usize, p_len: usize, q_len: usize, digest: *const u8, digest_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::dsa::vg_dsa_sign_random_prevalidated(out, out_len, used, key, key_len, p_len, q_len, digest, digest_len, rand, rand_len, scratch, scratch_len) } +} + +/// Validate packed DSA p||q||g||y||x as signing checks it: domain consistency, including g^q mod p = 1, 1 < y < p, 0 < x < q and g^x mod p = y. Returns 1 exactly when validation succeeds, otherwise 0. Does not test primality. Timing may depend on p,q,g,y and the result, not otherwise on x. Scratch contents are unspecified on return and may hold secrets. Contract: `VG.Spec.Dsa.validatePrivateKeyContract`. +/// +/// It runs `vg_dsa_validate_private_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `key` must be valid for reads of `key_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). +/// * key_len must be 3*p_len + 2*q_len; scratch_len must be at least 16*p_len words; destroy scratch after use. +/// * `scratch` must not overlap `key` or the arguments on the stack (distinct Rust objects never do). +/// * Neither `key` nor `scratch` may wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_dsa_validate_private_key_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *const u8, key_len: usize, p_len: usize, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::dsa::vg_dsa_validate_private_key(key, key_len, p_len, q_len, scratch, scratch_len) } +} + /// Derive a DSA private key from big-endian p||q||g and a supplied q_len-byte scalar x, as key generation does after sampling x: checks the domain and 0 < x < q, computes y = g^x mod p and checks the key. Returns 1 with p||q||g||y||x, or 0 and zero output. Checks algebraic consistency, not primality. x must be a fresh uniform scalar the caller sampled. Timing may depend on p, q, g and success, not otherwise on x. Contract: `VG.Spec.Dsa.deriveKeyContract`. /// /// It runs `vg_dsa_derive_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. @@ -48,63 +93,6 @@ pub unsafe fn vg_dsa_generate_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: unsafe { crate::dsa::vg_dsa_generate_key(out, out_len, used, params, params_len, p_len, q_len, rand, rand_len, scratch, scratch_len) } } -/// Search for DSA domain parameters (FIPS 186-4 A.1.1.2 and A.2.1) over the random tape `rand`, for the modulus width in `state`: each seed, its `q`, the `p` candidates from it and the generator's `h` candidates are read from `rand` in turn, with the rejected seeds counted in `state`. Returns 1 and writes `p || q || g` to `out` once parameters are found; 3 if the search is exhausted, by too many rejected seeds or by one seed needing more than the entropy budget; or 0 if the tape ran out inside a seed. Writes the octets of `rand` it consumed to `used`, and the state for the next call to `state`, whose last word is, for 0, the length of the next tape, more than the octets of `rand` after the first `used` and at most the entropy budget: keep those octets, in order, and append fresh random octets up to that length. `out` is zeros but for 1. Accepted parameters pass probable-prime tests, not primality certification. -/// -/// Contract: `VG.Spec.Dsa.parameterSearchContract`. Timing may depend on everything, `rand` included: these are public domain parameters. -/// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_dsa_parameter_search_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_UMAAL_FEATURES`); or else `vg_dsa_parameter_search`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. -/// -/// # Safety -/// -/// * `out` must be valid for reads and writes of `out_len` bytes. -/// * `used` must be valid for reads and writes of 8 bytes. -/// * `rand` must be valid for reads of `rand_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `state` must be valid for reads and writes of 32 bytes. -/// * `state` must come from `vg_dsa_parameter_search_init`, or from the last call of this function that returned 0. -/// * `out_len` must be `2 * state[1]` plus 20 if `state[1]` is 128, else plus 32. -/// * `rand_len` must be at most the entropy budget, `state[2]`. -/// * `scratch_len` must be at least `64 * state[1] + 1024` words. -/// * `rand` must hold independent uniform random octets. They are public under this contract and must never be used for private scalars or nonces. -/// * `out`, `used`, `scratch` and `state` must not overlap each other, `rand` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `used`, `rand`, `scratch` and `state` may overlap the 304 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_dsa_parameter_search_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32 { - /// The type of the implementations. - type Impl = unsafe extern "C" fn(out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32; - /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only - /// features the CPU has. - #[cold] - fn select() -> Impl { - let cpu = verified_garbage_cpu::detected(); - #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_UMAAL_FEATURES) } - || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_UMAAL_FEATURES) - { - return crate::dsa::vg_dsa_parameter_search_umaal; - } - crate::dsa::vg_dsa_parameter_search - } - /// The first call: chooses the implementation, records it in `CHOSEN` for the calls - /// after it, and runs it. - #[cold] - unsafe extern "C" fn first(out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32 { - let f = select(); - CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); - // SAFETY: the caller's obligations, which are those of every implementation, `f` - // among them, and the CPU has the features `f` needs. - unsafe { f(out, out_len, used, rand, rand_len, scratch, scratch_len, state) } - } - /// The function to call: `first`, until it records the implementation it chose. - static CHOSEN: core::sync::atomic::AtomicPtr<()> = - core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); - // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, - // or an implementation `select` returned, which needs only features the CPU has. Calling - // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, used, rand, rand_len, scratch, scratch_len, state) } -} - /// Validate packed DSA p||q||g||y, including domain consistency and both subgroup memberships. Returns 1 exactly when validation succeeds, otherwise 0. Does not test primality. The key is public; scratch contents are unspecified on return. Contract: `VG.Spec.Dsa.validatePublicKeyContract`. /// /// It runs `vg_dsa_validate_public_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. @@ -147,25 +135,6 @@ pub unsafe fn vg_dsa_verify_prevalidated_auto(_dit: verified_garbage_cpu::Dit<'_ unsafe { crate::dsa::vg_dsa_verify_prevalidated(key, key_len, p_len, q_len, signature, signature_len, digest, digest_len, scratch, scratch_len) } } -/// Validate packed DSA p||q||g||y||x as signing checks it: domain consistency, including g^q mod p = 1, 1 < y < p, 0 < x < q and g^x mod p = y. Returns 1 exactly when validation succeeds, otherwise 0. Does not test primality. Timing may depend on p,q,g,y and the result, not otherwise on x. Scratch contents are unspecified on return and may hold secrets. Contract: `VG.Spec.Dsa.validatePrivateKeyContract`. -/// -/// It runs `vg_dsa_validate_private_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `key` must be valid for reads of `key_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). -/// * key_len must be 3*p_len + 2*q_len; scratch_len must be at least 16*p_len words; destroy scratch after use. -/// * `scratch` must not overlap `key` or the arguments on the stack (distinct Rust objects never do). -/// * Neither `key` nor `scratch` may wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_dsa_validate_private_key_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *const u8, key_len: usize, p_len: usize, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::dsa::vg_dsa_validate_private_key(key, key_len, p_len, q_len, scratch, scratch_len) } -} - /// Start a search for DSA domain parameters with a modulus of `p_len` octets (128, 256 or 384: 1024, 2048 or 3072 bits, with a subgroup of 160, 256 and 256 bits). Returns 1 and writes the search's four public words to `state`: the seeds it may still reject, `p_len`, its entropy budget, and the length of the first random tape to give `vg_dsa_parameter_search`. Returns 0 and writes zeros for any other `p_len`. /// /// Contract: `VG.Spec.Dsa.parameterInitContract`. @@ -183,28 +152,59 @@ pub unsafe fn vg_dsa_parameter_search_init_auto(_dit: verified_garbage_cpu::Dit< unsafe { crate::dsa::vg_dsa_parameter_search_init(state, p_len) } } -/// Randomized DSA signing of a prehashed digest, for an already validated packed private key (big-endian p||q||g||y||x; vg_dsa_validate_private_key validates it). Up to eight signing attempts, each with 128 nonce draws. Returns 1 with fixed-width r||s, 0 for retry exhaustion, or 2 for insufficient random bytes; out is zero on failure and used counts consumed complete draws on every return. Timing may depend on public p,q,g,y, sampling decisions, signing success bits and consumption, not otherwise on secrets. Contract: `VG.Spec.Dsa.randomSignPrevalidatedContract`. +/// Search for DSA domain parameters (FIPS 186-4 A.1.1.2 and A.2.1) over the random tape `rand`, for the modulus width in `state`: each seed, its `q`, the `p` candidates from it and the generator's `h` candidates are read from `rand` in turn, with the rejected seeds counted in `state`. Returns 1 and writes `p || q || g` to `out` once parameters are found; 3 if the search is exhausted, by too many rejected seeds or by one seed needing more than the entropy budget; or 0 if the tape ran out inside a seed. Writes the octets of `rand` it consumed to `used`, and the state for the next call to `state`, whose last word is, for 0, the length of the next tape, more than the octets of `rand` after the first `used` and at most the entropy budget: keep those octets, in order, and append fresh random octets up to that length. `out` is zeros but for 1. Accepted parameters pass probable-prime tests, not primality certification. /// -/// It runs `vg_dsa_sign_random_prevalidated`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// Contract: `VG.Spec.Dsa.parameterSearchContract`. Timing may depend on everything, `rand` included: these are public domain parameters. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_dsa_parameter_search_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_UMAAL_FEATURES`); or else `vg_dsa_parameter_search`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `out` must be valid for reads and writes of `out_len` bytes. /// * `used` must be valid for reads and writes of 8 bytes. -/// * `key` must be valid for reads of `key_len` bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. /// * `rand` must be valid for reads of `rand_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). -/// * key_len must be 3*p_len + 2*q_len; out_len must be 2*q_len. -/// * scratch_len must be at least 16*p_len + 64 words; destroy scratch after use. -/// * The exact key bytes must satisfy validatePrivateKeyBytes p_len q_len key = true. A successful vg_dsa_validate_private_key call establishes this only while the key bytes remain unchanged. Primality remains the caller's responsibility. -/// * Provide fresh independent random octets; never reuse a completed signing tape for another signature. -/// * `out`, `used` and `scratch` must not overlap each other, `key`, `digest`, `rand` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `used`, `key`, `digest`, `rand` and `scratch` may overlap the 144 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must be valid for reads and writes of 32 bytes. +/// * `state` must come from `vg_dsa_parameter_search_init`, or from the last call of this function that returned 0. +/// * `out_len` must be `2 * state[1]` plus 20 if `state[1]` is 128, else plus 32. +/// * `rand_len` must be at most the entropy budget, `state[2]`. +/// * `scratch_len` must be at least `64 * state[1] + 1024` words. +/// * `rand` must hold independent uniform random octets. They are public under this contract and must never be used for private scalars or nonces. +/// * `out`, `used`, `scratch` and `state` must not overlap each other, `rand` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `used`, `rand`, `scratch` and `state` may overlap the 304 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_dsa_sign_random_prevalidated_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], key: *const u8, key_len: usize, p_len: usize, q_len: usize, digest: *const u8, digest_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::dsa::vg_dsa_sign_random_prevalidated(out, out_len, used, key, key_len, p_len, q_len, digest, digest_len, rand, rand_len, scratch, scratch_len) } +pub unsafe fn vg_dsa_parameter_search_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32 { + /// The type of the implementations. + type Impl = unsafe extern "C" fn(out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32; + /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only + /// features the CPU has. + #[cold] + fn select() -> Impl { + let cpu = verified_garbage_cpu::detected(); + #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] + if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_UMAAL_FEATURES) } + || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_UMAAL_FEATURES) + { + return crate::dsa::vg_dsa_parameter_search_umaal; + } + crate::dsa::vg_dsa_parameter_search + } + /// The first call: chooses the implementation, records it in `CHOSEN` for the calls + /// after it, and runs it. + #[cold] + unsafe extern "C" fn first(out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32 { + let f = select(); + CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); + // SAFETY: the caller's obligations, which are those of every implementation, `f` + // among them, and the CPU has the features `f` needs. + unsafe { f(out, out_len, used, rand, rand_len, scratch, scratch_len, state) } + } + /// The function to call: `first`, until it records the implementation it chose. + static CHOSEN: core::sync::atomic::AtomicPtr<()> = + core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); + // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, + // or an implementation `select` returned, which needs only features the CPU has. Calling + // either carries the caller's obligations, which are those of every implementation. + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, used, rand, rand_len, scratch, scratch_len, state) } } diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp256r1.rs b/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp256r1.rs index c01bcc70514..c7015533234 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp256r1.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp256r1.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.publicKeyContract`. Constant time: only the pointers may affect timing, not the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_brainpoolp256r1_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_BRAINPOOLP256R1_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_brainpoolp256r1_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_brainpoolp256r1_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_BRAINPOOLP256R1_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_brainpoolp256r1_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp256r1_compressed.rs b/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp256r1_compressed.rs index a82f69e164d..879921e91eb 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp256r1_compressed.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp256r1_compressed.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.compressPublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_brainpoolp256r1_compress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_BRAINPOOLP256R1_COMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_brainpoolp256r1_compress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_brainpoolp256r1_compress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_BRAINPOOLP256R1_COMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_brainpoolp256r1_compress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -57,7 +57,7 @@ pub unsafe fn vg_ec_brainpoolp256r1_compress_public_key_auto(_dit: verified_garb /// /// Contract: `VG.Spec.EcKey.Instance.decompressPublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_brainpoolp256r1_decompress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_BRAINPOOLP256R1_DECOMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_brainpoolp256r1_decompress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_brainpoolp256r1_decompress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_BRAINPOOLP256R1_DECOMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_brainpoolp256r1_decompress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp384r1.rs b/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp384r1.rs index 1e48582f8b4..f9c11dbecf7 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp384r1.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp384r1.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.publicKeyContract`. Constant time: only the pointers may affect timing, not the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_brainpoolp384r1_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_BRAINPOOLP384R1_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_brainpoolp384r1_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_brainpoolp384r1_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_BRAINPOOLP384R1_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_brainpoolp384r1_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp384r1_compressed.rs b/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp384r1_compressed.rs index 7c1d9d5384a..df246b3994b 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp384r1_compressed.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp384r1_compressed.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.compressPublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_brainpoolp384r1_compress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_BRAINPOOLP384R1_COMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_brainpoolp384r1_compress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_brainpoolp384r1_compress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_BRAINPOOLP384R1_COMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_brainpoolp384r1_compress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -57,7 +57,7 @@ pub unsafe fn vg_ec_brainpoolp384r1_compress_public_key_auto(_dit: verified_garb /// /// Contract: `VG.Spec.EcKey.Instance.decompressPublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_brainpoolp384r1_decompress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_BRAINPOOLP384R1_DECOMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_brainpoolp384r1_decompress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_brainpoolp384r1_decompress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_BRAINPOOLP384R1_DECOMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_brainpoolp384r1_decompress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp512r1.rs b/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp512r1.rs index 6fabc651983..9bd12eee163 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp512r1.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp512r1.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.publicKeyContract`. Constant time: only the pointers may affect timing, not the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_brainpoolp512r1_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_BRAINPOOLP512R1_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_brainpoolp512r1_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_brainpoolp512r1_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_BRAINPOOLP512R1_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_brainpoolp512r1_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp512r1_compressed.rs b/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp512r1_compressed.rs index c2d12979d41..64dd5dc3e63 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp512r1_compressed.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ec_brainpoolp512r1_compressed.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.compressPublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_brainpoolp512r1_compress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_BRAINPOOLP512R1_COMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_brainpoolp512r1_compress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_brainpoolp512r1_compress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_BRAINPOOLP512R1_COMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_brainpoolp512r1_compress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -57,7 +57,7 @@ pub unsafe fn vg_ec_brainpoolp512r1_compress_public_key_auto(_dit: verified_garb /// /// Contract: `VG.Spec.EcKey.Instance.decompressPublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_brainpoolp512r1_decompress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_BRAINPOOLP512R1_DECOMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_brainpoolp512r1_decompress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_brainpoolp512r1_decompress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_BRAINPOOLP512R1_DECOMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_brainpoolp512r1_decompress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ec_p224.rs b/crates/verified-garbage-asm-arm/src/dispatch/ec_p224.rs index 9c7ccbdf49b..828f343c9cb 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ec_p224.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ec_p224.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.publicKeyContract`. Constant time: only the pointers may affect timing, not the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_p224_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_P224_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_p224_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_p224_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_P224_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_p224_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ec_p224_compressed.rs b/crates/verified-garbage-asm-arm/src/dispatch/ec_p224_compressed.rs index 3499609c534..ef642e52ede 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ec_p224_compressed.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ec_p224_compressed.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.compressPublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_p224_compress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_P224_COMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_p224_compress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_p224_compress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_P224_COMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_p224_compress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -57,7 +57,7 @@ pub unsafe fn vg_ec_p224_compress_public_key_auto(_dit: verified_garbage_cpu::Di /// /// Contract: `VG.Spec.EcKey.Instance.decompressPublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_p224_decompress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_P224_DECOMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_p224_decompress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_p224_decompress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_P224_DECOMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_p224_decompress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ec_p256.rs b/crates/verified-garbage-asm-arm/src/dispatch/ec_p256.rs index c8cb57fa184..5d25d4be262 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ec_p256.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ec_p256.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.publicKeyContract`. Constant time: only the pointers may affect timing, not the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_p256_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_P256_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_p256_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_p256_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_P256_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_p256_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ec_p256_compressed.rs b/crates/verified-garbage-asm-arm/src/dispatch/ec_p256_compressed.rs index 640e1a4c20b..1735ad5a00b 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ec_p256_compressed.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ec_p256_compressed.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.compressPublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_p256_compress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_P256_COMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_p256_compress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_p256_compress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_P256_COMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_p256_compress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -57,7 +57,7 @@ pub unsafe fn vg_ec_p256_compress_public_key_auto(_dit: verified_garbage_cpu::Di /// /// Contract: `VG.Spec.EcKey.Instance.decompressPublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_p256_decompress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_P256_DECOMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_p256_decompress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_p256_decompress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_P256_DECOMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_p256_decompress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ec_p384.rs b/crates/verified-garbage-asm-arm/src/dispatch/ec_p384.rs index ed66937e59e..c3ae976a274 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ec_p384.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ec_p384.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.publicKeyContract`. Constant time: only the pointers may affect timing, not the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_p384_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_P384_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_p384_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_p384_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_P384_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_p384_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ec_p384_compressed.rs b/crates/verified-garbage-asm-arm/src/dispatch/ec_p384_compressed.rs index c74ff530aac..def58b63ff8 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ec_p384_compressed.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ec_p384_compressed.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.compressPublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_p384_compress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_P384_COMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_p384_compress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_p384_compress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_P384_COMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_p384_compress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -57,7 +57,7 @@ pub unsafe fn vg_ec_p384_compress_public_key_auto(_dit: verified_garbage_cpu::Di /// /// Contract: `VG.Spec.EcKey.Instance.decompressPublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_p384_decompress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_P384_DECOMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_p384_decompress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_p384_decompress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_P384_DECOMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_p384_decompress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ec_secp256k1.rs b/crates/verified-garbage-asm-arm/src/dispatch/ec_secp256k1.rs index da678051704..4bfdc910b3e 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ec_secp256k1.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ec_secp256k1.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.publicKeyContract`. Constant time: only the pointers may affect timing, not the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_secp256k1_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_SECP256K1_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_secp256k1_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_secp256k1_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_SECP256K1_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_secp256k1_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ec_secp256k1_compressed.rs b/crates/verified-garbage-asm-arm/src/dispatch/ec_secp256k1_compressed.rs index 7eedb800f6f..5b48f90dbeb 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ec_secp256k1_compressed.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ec_secp256k1_compressed.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.compressPublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_secp256k1_compress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_SECP256K1_COMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_secp256k1_compress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_secp256k1_compress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_SECP256K1_COMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_secp256k1_compress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -57,7 +57,7 @@ pub unsafe fn vg_ec_secp256k1_compress_public_key_auto(_dit: verified_garbage_cp /// /// Contract: `VG.Spec.EcKey.Instance.decompressPublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_secp256k1_decompress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_SECP256K1_DECOMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_secp256k1_decompress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_secp256k1_decompress_public_key_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_EC_SECP256K1_DECOMPRESS_PUBLIC_KEY_UMAAL_FEATURES`); or else `vg_ec_secp256k1_decompress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ecdh_brainpoolp256r1.rs b/crates/verified-garbage-asm-arm/src/dispatch/ecdh_brainpoolp256r1.rs index 9e18519ff50..b656b253386 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ecdh_brainpoolp256r1.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ecdh_brainpoolp256r1.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ecdh.Instance.exchangeContract`. Constant time but for the peer's public key: timing may depend on the pointers and the contents of `peer`, not on the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdh_brainpoolp256r1_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDH_BRAINPOOLP256R1_UMAAL_FEATURES`); or else `vg_ecdh_brainpoolp256r1`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdh_brainpoolp256r1_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDH_BRAINPOOLP256R1_UMAAL_FEATURES`); or else `vg_ecdh_brainpoolp256r1`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ecdh_brainpoolp384r1.rs b/crates/verified-garbage-asm-arm/src/dispatch/ecdh_brainpoolp384r1.rs index 221aa7ceaac..9324b24ba69 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ecdh_brainpoolp384r1.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ecdh_brainpoolp384r1.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ecdh.Instance.exchangeContract`. Constant time but for the peer's public key: timing may depend on the pointers and the contents of `peer`, not on the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdh_brainpoolp384r1_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDH_BRAINPOOLP384R1_UMAAL_FEATURES`); or else `vg_ecdh_brainpoolp384r1`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdh_brainpoolp384r1_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDH_BRAINPOOLP384R1_UMAAL_FEATURES`); or else `vg_ecdh_brainpoolp384r1`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ecdh_brainpoolp512r1.rs b/crates/verified-garbage-asm-arm/src/dispatch/ecdh_brainpoolp512r1.rs index dfec268a305..5b07a98e8be 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ecdh_brainpoolp512r1.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ecdh_brainpoolp512r1.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ecdh.Instance.exchangeContract`. Constant time but for the peer's public key: timing may depend on the pointers and the contents of `peer`, not on the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdh_brainpoolp512r1_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDH_BRAINPOOLP512R1_UMAAL_FEATURES`); or else `vg_ecdh_brainpoolp512r1`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdh_brainpoolp512r1_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDH_BRAINPOOLP512R1_UMAAL_FEATURES`); or else `vg_ecdh_brainpoolp512r1`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ecdh_p224.rs b/crates/verified-garbage-asm-arm/src/dispatch/ecdh_p224.rs index 38b01799c9f..7c6c3b5ed20 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ecdh_p224.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ecdh_p224.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ecdh.Instance.exchangeContract`. Constant time but for the peer's public key: timing may depend on the pointers and the contents of `peer`, not on the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdh_p224_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDH_P224_UMAAL_FEATURES`); or else `vg_ecdh_p224`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdh_p224_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDH_P224_UMAAL_FEATURES`); or else `vg_ecdh_p224`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ecdh_p256.rs b/crates/verified-garbage-asm-arm/src/dispatch/ecdh_p256.rs index 7272e57d005..79658e059fc 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ecdh_p256.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ecdh_p256.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ecdh.Instance.exchangeContract`. Constant time but for the peer's public key: timing may depend on the pointers and the contents of `peer`, not on the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdh_p256_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDH_P256_UMAAL_FEATURES`); or else `vg_ecdh_p256`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdh_p256_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDH_P256_UMAAL_FEATURES`); or else `vg_ecdh_p256`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ecdh_p384.rs b/crates/verified-garbage-asm-arm/src/dispatch/ecdh_p384.rs index 27c4a0a292f..219c8a757a9 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ecdh_p384.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ecdh_p384.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ecdh.Instance.exchangeContract`. Constant time but for the peer's public key: timing may depend on the pointers and the contents of `peer`, not on the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdh_p384_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDH_P384_UMAAL_FEATURES`); or else `vg_ecdh_p384`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdh_p384_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDH_P384_UMAAL_FEATURES`); or else `vg_ecdh_p384`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ecdh_secp256k1.rs b/crates/verified-garbage-asm-arm/src/dispatch/ecdh_secp256k1.rs index 063f30d2ff2..64bbf4d20aa 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ecdh_secp256k1.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ecdh_secp256k1.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ecdh.Instance.exchangeContract`. Constant time but for the peer's public key: timing may depend on the pointers and the contents of `peer`, not on the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdh_secp256k1_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDH_SECP256K1_UMAAL_FEATURES`); or else `vg_ecdh_secp256k1`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdh_secp256k1_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDH_SECP256K1_UMAAL_FEATURES`); or else `vg_ecdh_secp256k1`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_brainpoolp256r1.rs b/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_brainpoolp256r1.rs index d108cc4fe1a..1774e5ed77f 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_brainpoolp256r1.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_brainpoolp256r1.rs @@ -2,52 +2,48 @@ //! The dispatchers of the verified `ecdsa_brainpoolp256r1` functions for `arm` //! (`crate::ecdsa_brainpoolp256r1`, `VG.Rust.dispatcher`). -/// Deterministic ECDSA signature generation over brainpoolP256r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). +/// ECDSA signature verification over brainpoolP256r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. /// -/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdsa_brainpoolp256r1_sign_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_BRAINPOOLP256R1_SIGN_UMAAL_FEATURES`); or else `vg_ecdsa_brainpoolp256r1_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdsa_brainpoolp256r1_verify_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_BRAINPOOLP256R1_VERIFY_UMAAL_FEATURES`); or else `vg_ecdsa_brainpoolp256r1_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 64 bytes. -/// * `d` must be valid for reads of 32 bytes. +/// * `public` must be valid for reads of 65 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 64 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. -/// * `digest_len` must be the digest length of the hash function `hmac` names. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `d`, `digest`, `hmac` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the 536 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest`, `sig` or the arguments on the stack (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_brainpoolp256r1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { +pub unsafe fn vg_ecdsa_brainpoolp256r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32; + type Impl = unsafe extern "C" fn(public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ecdsa_brainpoolp256r1::VG_ECDSA_BRAINPOOLP256R1_SIGN_UMAAL_FEATURES) } - || cpu.contains(crate::ecdsa_brainpoolp256r1::VG_ECDSA_BRAINPOOLP256R1_SIGN_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ecdsa_brainpoolp256r1::VG_ECDSA_BRAINPOOLP256R1_VERIFY_UMAAL_FEATURES) } + || cpu.contains(crate::ecdsa_brainpoolp256r1::VG_ECDSA_BRAINPOOLP256R1_VERIFY_UMAAL_FEATURES) { - return crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_sign_umaal; + return crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_verify_umaal; } - crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_sign + crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_verify } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { + unsafe extern "C" fn first(public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, d, digest, digest_len, scratch, hmac) } + unsafe { f(public, digest, digest_len, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -55,51 +51,55 @@ pub unsafe fn vg_ecdsa_brainpoolp256r1_sign_auto(_dit: verified_garbage_cpu::Dit // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, d, digest, digest_len, scratch, hmac) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(public, digest, digest_len, sig, scratch) } } -/// ECDSA signature verification over brainpoolP256r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// Deterministic ECDSA signature generation over brainpoolP256r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). /// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdsa_brainpoolp256r1_verify_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_BRAINPOOLP256R1_VERIFY_UMAAL_FEATURES`); or else `vg_ecdsa_brainpoolp256r1_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdsa_brainpoolp256r1_sign_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_BRAINPOOLP256R1_SIGN_UMAAL_FEATURES`); or else `vg_ecdsa_brainpoolp256r1_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `public` must be valid for reads of 65 bytes. +/// * `out` must be valid for reads and writes of 64 bytes. +/// * `d` must be valid for reads of 32 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 64 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest`, `sig` or the arguments on the stack (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. +/// * `digest_len` must be the digest length of the hash function `hmac` names. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). +/// * `out` and `scratch` must not overlap each other, `d`, `digest`, `hmac` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the 536 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_brainpoolp256r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ecdsa_brainpoolp256r1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32; + type Impl = unsafe extern "C" fn(out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ecdsa_brainpoolp256r1::VG_ECDSA_BRAINPOOLP256R1_VERIFY_UMAAL_FEATURES) } - || cpu.contains(crate::ecdsa_brainpoolp256r1::VG_ECDSA_BRAINPOOLP256R1_VERIFY_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ecdsa_brainpoolp256r1::VG_ECDSA_BRAINPOOLP256R1_SIGN_UMAAL_FEATURES) } + || cpu.contains(crate::ecdsa_brainpoolp256r1::VG_ECDSA_BRAINPOOLP256R1_SIGN_UMAAL_FEATURES) { - return crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_verify_umaal; + return crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_sign_umaal; } - crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_verify + crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_sign } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { + unsafe extern "C" fn first(out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(public, digest, digest_len, sig, scratch) } + unsafe { f(out, d, digest, digest_len, scratch, hmac) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -107,5 +107,5 @@ pub unsafe fn vg_ecdsa_brainpoolp256r1_verify_auto(_dit: verified_garbage_cpu::D // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(public, digest, digest_len, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, d, digest, digest_len, scratch, hmac) } } diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_brainpoolp384r1.rs b/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_brainpoolp384r1.rs index 33ea52ae4f1..74cbc964a8e 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_brainpoolp384r1.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_brainpoolp384r1.rs @@ -2,52 +2,48 @@ //! The dispatchers of the verified `ecdsa_brainpoolp384r1` functions for `arm` //! (`crate::ecdsa_brainpoolp384r1`, `VG.Rust.dispatcher`). -/// Deterministic ECDSA signature generation over brainpoolP384r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (48 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (48 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). +/// ECDSA signature verification over brainpoolP384r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. /// -/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdsa_brainpoolp384r1_sign_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_BRAINPOOLP384R1_SIGN_UMAAL_FEATURES`); or else `vg_ecdsa_brainpoolp384r1_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdsa_brainpoolp384r1_verify_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_BRAINPOOLP384R1_VERIFY_UMAAL_FEATURES`); or else `vg_ecdsa_brainpoolp384r1_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 96 bytes. -/// * `d` must be valid for reads of 48 bytes. +/// * `public` must be valid for reads of 97 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 96 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. -/// * `digest_len` must be the digest length of the hash function `hmac` names. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `d`, `digest`, `hmac` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the 536 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest`, `sig` or the arguments on the stack (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_brainpoolp384r1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { +pub unsafe fn vg_ecdsa_brainpoolp384r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32; + type Impl = unsafe extern "C" fn(public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ecdsa_brainpoolp384r1::VG_ECDSA_BRAINPOOLP384R1_SIGN_UMAAL_FEATURES) } - || cpu.contains(crate::ecdsa_brainpoolp384r1::VG_ECDSA_BRAINPOOLP384R1_SIGN_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ecdsa_brainpoolp384r1::VG_ECDSA_BRAINPOOLP384R1_VERIFY_UMAAL_FEATURES) } + || cpu.contains(crate::ecdsa_brainpoolp384r1::VG_ECDSA_BRAINPOOLP384R1_VERIFY_UMAAL_FEATURES) { - return crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_sign_umaal; + return crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_verify_umaal; } - crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_sign + crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_verify } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { + unsafe extern "C" fn first(public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, d, digest, digest_len, scratch, hmac) } + unsafe { f(public, digest, digest_len, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -55,51 +51,55 @@ pub unsafe fn vg_ecdsa_brainpoolp384r1_sign_auto(_dit: verified_garbage_cpu::Dit // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, d, digest, digest_len, scratch, hmac) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(public, digest, digest_len, sig, scratch) } } -/// ECDSA signature verification over brainpoolP384r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// Deterministic ECDSA signature generation over brainpoolP384r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (48 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (48 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). /// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdsa_brainpoolp384r1_verify_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_BRAINPOOLP384R1_VERIFY_UMAAL_FEATURES`); or else `vg_ecdsa_brainpoolp384r1_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdsa_brainpoolp384r1_sign_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_BRAINPOOLP384R1_SIGN_UMAAL_FEATURES`); or else `vg_ecdsa_brainpoolp384r1_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `public` must be valid for reads of 97 bytes. +/// * `out` must be valid for reads and writes of 96 bytes. +/// * `d` must be valid for reads of 48 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 96 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest`, `sig` or the arguments on the stack (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. +/// * `digest_len` must be the digest length of the hash function `hmac` names. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). +/// * `out` and `scratch` must not overlap each other, `d`, `digest`, `hmac` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the 536 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_brainpoolp384r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ecdsa_brainpoolp384r1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32; + type Impl = unsafe extern "C" fn(out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ecdsa_brainpoolp384r1::VG_ECDSA_BRAINPOOLP384R1_VERIFY_UMAAL_FEATURES) } - || cpu.contains(crate::ecdsa_brainpoolp384r1::VG_ECDSA_BRAINPOOLP384R1_VERIFY_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ecdsa_brainpoolp384r1::VG_ECDSA_BRAINPOOLP384R1_SIGN_UMAAL_FEATURES) } + || cpu.contains(crate::ecdsa_brainpoolp384r1::VG_ECDSA_BRAINPOOLP384R1_SIGN_UMAAL_FEATURES) { - return crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_verify_umaal; + return crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_sign_umaal; } - crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_verify + crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_sign } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { + unsafe extern "C" fn first(out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(public, digest, digest_len, sig, scratch) } + unsafe { f(out, d, digest, digest_len, scratch, hmac) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -107,5 +107,5 @@ pub unsafe fn vg_ecdsa_brainpoolp384r1_verify_auto(_dit: verified_garbage_cpu::D // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(public, digest, digest_len, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, d, digest, digest_len, scratch, hmac) } } diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_brainpoolp512r1.rs b/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_brainpoolp512r1.rs index cbe300f8441..67c6376bb6e 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_brainpoolp512r1.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_brainpoolp512r1.rs @@ -2,52 +2,48 @@ //! The dispatchers of the verified `ecdsa_brainpoolp512r1` functions for `arm` //! (`crate::ecdsa_brainpoolp512r1`, `VG.Rust.dispatcher`). -/// Deterministic ECDSA signature generation over brainpoolP512r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (64 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (64 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). +/// ECDSA signature verification over brainpoolP512r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 64 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 512 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 64 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. /// -/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdsa_brainpoolp512r1_sign_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_BRAINPOOLP512R1_SIGN_UMAAL_FEATURES`); or else `vg_ecdsa_brainpoolp512r1_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdsa_brainpoolp512r1_verify_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_BRAINPOOLP512R1_VERIFY_UMAAL_FEATURES`); or else `vg_ecdsa_brainpoolp512r1_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 128 bytes. -/// * `d` must be valid for reads of 64 bytes. +/// * `public` must be valid for reads of 129 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 128 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. -/// * `digest_len` must be the digest length of the hash function `hmac` names. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `d`, `digest`, `hmac` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the 536 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest`, `sig` or the arguments on the stack (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_brainpoolp512r1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 128], d: *const [u8; 64], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { +pub unsafe fn vg_ecdsa_brainpoolp512r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 129], digest: *const u8, digest_len: usize, sig: *const [u8; 128], scratch: *mut [u64; 1024]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(out: *mut [u8; 128], d: *const [u8; 64], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32; + type Impl = unsafe extern "C" fn(public: *const [u8; 129], digest: *const u8, digest_len: usize, sig: *const [u8; 128], scratch: *mut [u64; 1024]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ecdsa_brainpoolp512r1::VG_ECDSA_BRAINPOOLP512R1_SIGN_UMAAL_FEATURES) } - || cpu.contains(crate::ecdsa_brainpoolp512r1::VG_ECDSA_BRAINPOOLP512R1_SIGN_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ecdsa_brainpoolp512r1::VG_ECDSA_BRAINPOOLP512R1_VERIFY_UMAAL_FEATURES) } + || cpu.contains(crate::ecdsa_brainpoolp512r1::VG_ECDSA_BRAINPOOLP512R1_VERIFY_UMAAL_FEATURES) { - return crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_sign_umaal; + return crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_verify_umaal; } - crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_sign + crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_verify } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(out: *mut [u8; 128], d: *const [u8; 64], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { + unsafe extern "C" fn first(public: *const [u8; 129], digest: *const u8, digest_len: usize, sig: *const [u8; 128], scratch: *mut [u64; 1024]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, d, digest, digest_len, scratch, hmac) } + unsafe { f(public, digest, digest_len, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -55,51 +51,55 @@ pub unsafe fn vg_ecdsa_brainpoolp512r1_sign_auto(_dit: verified_garbage_cpu::Dit // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, d, digest, digest_len, scratch, hmac) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(public, digest, digest_len, sig, scratch) } } -/// ECDSA signature verification over brainpoolP512r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 64 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 512 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 64 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// Deterministic ECDSA signature generation over brainpoolP512r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (64 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (64 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). /// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdsa_brainpoolp512r1_verify_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_BRAINPOOLP512R1_VERIFY_UMAAL_FEATURES`); or else `vg_ecdsa_brainpoolp512r1_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdsa_brainpoolp512r1_sign_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_BRAINPOOLP512R1_SIGN_UMAAL_FEATURES`); or else `vg_ecdsa_brainpoolp512r1_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `public` must be valid for reads of 129 bytes. +/// * `out` must be valid for reads and writes of 128 bytes. +/// * `d` must be valid for reads of 64 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 128 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest`, `sig` or the arguments on the stack (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. +/// * `digest_len` must be the digest length of the hash function `hmac` names. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). +/// * `out` and `scratch` must not overlap each other, `d`, `digest`, `hmac` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the 536 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_brainpoolp512r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 129], digest: *const u8, digest_len: usize, sig: *const [u8; 128], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ecdsa_brainpoolp512r1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 128], d: *const [u8; 64], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(public: *const [u8; 129], digest: *const u8, digest_len: usize, sig: *const [u8; 128], scratch: *mut [u64; 1024]) -> u32; + type Impl = unsafe extern "C" fn(out: *mut [u8; 128], d: *const [u8; 64], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ecdsa_brainpoolp512r1::VG_ECDSA_BRAINPOOLP512R1_VERIFY_UMAAL_FEATURES) } - || cpu.contains(crate::ecdsa_brainpoolp512r1::VG_ECDSA_BRAINPOOLP512R1_VERIFY_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ecdsa_brainpoolp512r1::VG_ECDSA_BRAINPOOLP512R1_SIGN_UMAAL_FEATURES) } + || cpu.contains(crate::ecdsa_brainpoolp512r1::VG_ECDSA_BRAINPOOLP512R1_SIGN_UMAAL_FEATURES) { - return crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_verify_umaal; + return crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_sign_umaal; } - crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_verify + crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_sign } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(public: *const [u8; 129], digest: *const u8, digest_len: usize, sig: *const [u8; 128], scratch: *mut [u64; 1024]) -> u32 { + unsafe extern "C" fn first(out: *mut [u8; 128], d: *const [u8; 64], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(public, digest, digest_len, sig, scratch) } + unsafe { f(out, d, digest, digest_len, scratch, hmac) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -107,5 +107,5 @@ pub unsafe fn vg_ecdsa_brainpoolp512r1_verify_auto(_dit: verified_garbage_cpu::D // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(public, digest, digest_len, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, d, digest, digest_len, scratch, hmac) } } diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_p192.rs b/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_p192.rs index 1fd6299a49c..fb927dc1f4f 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_p192.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_p192.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_p192` functions for `arm` //! (`crate::ecdsa_p192`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over P-192 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 24 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 192 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 24 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_p192_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 49 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 48 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest`, `sig` or the arguments on the stack (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_p192_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 49], digest: *const u8, digest_len: usize, sig: *const [u8; 48], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_p192::vg_ecdsa_p192_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over P-192 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (24 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (24 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_p192_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_p192::vg_ecdsa_p192_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over P-192 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 24 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 192 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 24 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_p192_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 49 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 48 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest`, `sig` or the arguments on the stack (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_p192_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 49], digest: *const u8, digest_len: usize, sig: *const [u8; 48], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p192::vg_ecdsa_p192_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_p224.rs b/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_p224.rs index 49f4e8f553b..725f88b2c83 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_p224.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_p224.rs @@ -2,52 +2,48 @@ //! The dispatchers of the verified `ecdsa_p224` functions for `arm` //! (`crate::ecdsa_p224`, `VG.Rust.dispatcher`). -/// Deterministic ECDSA signature generation over P-224 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (28 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (28 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). +/// ECDSA signature verification over P-224 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 28 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 224 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 28 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. /// -/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdsa_p224_sign_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_P224_SIGN_UMAAL_FEATURES`); or else `vg_ecdsa_p224_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdsa_p224_verify_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_P224_VERIFY_UMAAL_FEATURES`); or else `vg_ecdsa_p224_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 56 bytes. -/// * `d` must be valid for reads of 28 bytes. +/// * `public` must be valid for reads of 57 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 56 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. -/// * `digest_len` must be the digest length of the hash function `hmac` names. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `d`, `digest`, `hmac` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the 536 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest`, `sig` or the arguments on the stack (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p224_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], d: *const [u8; 28], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { +pub unsafe fn vg_ecdsa_p224_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 57], digest: *const u8, digest_len: usize, sig: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(out: *mut [u8; 56], d: *const [u8; 28], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32; + type Impl = unsafe extern "C" fn(public: *const [u8; 57], digest: *const u8, digest_len: usize, sig: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ecdsa_p224::VG_ECDSA_P224_SIGN_UMAAL_FEATURES) } - || cpu.contains(crate::ecdsa_p224::VG_ECDSA_P224_SIGN_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ecdsa_p224::VG_ECDSA_P224_VERIFY_UMAAL_FEATURES) } + || cpu.contains(crate::ecdsa_p224::VG_ECDSA_P224_VERIFY_UMAAL_FEATURES) { - return crate::ecdsa_p224::vg_ecdsa_p224_sign_umaal; + return crate::ecdsa_p224::vg_ecdsa_p224_verify_umaal; } - crate::ecdsa_p224::vg_ecdsa_p224_sign + crate::ecdsa_p224::vg_ecdsa_p224_verify } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(out: *mut [u8; 56], d: *const [u8; 28], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { + unsafe extern "C" fn first(public: *const [u8; 57], digest: *const u8, digest_len: usize, sig: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, d, digest, digest_len, scratch, hmac) } + unsafe { f(public, digest, digest_len, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -55,51 +51,55 @@ pub unsafe fn vg_ecdsa_p224_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, d, digest, digest_len, scratch, hmac) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(public, digest, digest_len, sig, scratch) } } -/// ECDSA signature verification over P-224 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 28 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 224 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 28 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// Deterministic ECDSA signature generation over P-224 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (28 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (28 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdsa_p224_verify_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_P224_VERIFY_UMAAL_FEATURES`); or else `vg_ecdsa_p224_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdsa_p224_sign_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_P224_SIGN_UMAAL_FEATURES`); or else `vg_ecdsa_p224_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `public` must be valid for reads of 57 bytes. +/// * `out` must be valid for reads and writes of 56 bytes. +/// * `d` must be valid for reads of 28 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 56 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest`, `sig` or the arguments on the stack (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. +/// * `digest_len` must be the digest length of the hash function `hmac` names. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). +/// * `out` and `scratch` must not overlap each other, `d`, `digest`, `hmac` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the 536 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p224_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 57], digest: *const u8, digest_len: usize, sig: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ecdsa_p224_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], d: *const [u8; 28], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(public: *const [u8; 57], digest: *const u8, digest_len: usize, sig: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32; + type Impl = unsafe extern "C" fn(out: *mut [u8; 56], d: *const [u8; 28], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ecdsa_p224::VG_ECDSA_P224_VERIFY_UMAAL_FEATURES) } - || cpu.contains(crate::ecdsa_p224::VG_ECDSA_P224_VERIFY_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ecdsa_p224::VG_ECDSA_P224_SIGN_UMAAL_FEATURES) } + || cpu.contains(crate::ecdsa_p224::VG_ECDSA_P224_SIGN_UMAAL_FEATURES) { - return crate::ecdsa_p224::vg_ecdsa_p224_verify_umaal; + return crate::ecdsa_p224::vg_ecdsa_p224_sign_umaal; } - crate::ecdsa_p224::vg_ecdsa_p224_verify + crate::ecdsa_p224::vg_ecdsa_p224_sign } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(public: *const [u8; 57], digest: *const u8, digest_len: usize, sig: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { + unsafe extern "C" fn first(out: *mut [u8; 56], d: *const [u8; 28], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(public, digest, digest_len, sig, scratch) } + unsafe { f(out, d, digest, digest_len, scratch, hmac) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -107,5 +107,5 @@ pub unsafe fn vg_ecdsa_p224_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pub // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(public, digest, digest_len, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, d, digest, digest_len, scratch, hmac) } } diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_p256.rs b/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_p256.rs index b8d6d2fcc10..fba326866ce 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_p256.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_p256.rs @@ -2,52 +2,48 @@ //! The dispatchers of the verified `ecdsa_p256` functions for `arm` //! (`crate::ecdsa_p256`, `VG.Rust.dispatcher`). -/// Deterministic ECDSA signature generation over P-256 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). +/// ECDSA signature verification over P-256 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. /// -/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdsa_p256_sign_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_P256_SIGN_UMAAL_FEATURES`); or else `vg_ecdsa_p256_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdsa_p256_verify_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_P256_VERIFY_UMAAL_FEATURES`); or else `vg_ecdsa_p256_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 64 bytes. -/// * `d` must be valid for reads of 32 bytes. +/// * `public` must be valid for reads of 65 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 64 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. -/// * `digest_len` must be the digest length of the hash function `hmac` names. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `d`, `digest`, `hmac` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the 536 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest`, `sig` or the arguments on the stack (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p256_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { +pub unsafe fn vg_ecdsa_p256_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32; + type Impl = unsafe extern "C" fn(public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ecdsa_p256::VG_ECDSA_P256_SIGN_UMAAL_FEATURES) } - || cpu.contains(crate::ecdsa_p256::VG_ECDSA_P256_SIGN_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ecdsa_p256::VG_ECDSA_P256_VERIFY_UMAAL_FEATURES) } + || cpu.contains(crate::ecdsa_p256::VG_ECDSA_P256_VERIFY_UMAAL_FEATURES) { - return crate::ecdsa_p256::vg_ecdsa_p256_sign_umaal; + return crate::ecdsa_p256::vg_ecdsa_p256_verify_umaal; } - crate::ecdsa_p256::vg_ecdsa_p256_sign + crate::ecdsa_p256::vg_ecdsa_p256_verify } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { + unsafe extern "C" fn first(public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, d, digest, digest_len, scratch, hmac) } + unsafe { f(public, digest, digest_len, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -55,51 +51,55 @@ pub unsafe fn vg_ecdsa_p256_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, d, digest, digest_len, scratch, hmac) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(public, digest, digest_len, sig, scratch) } } -/// ECDSA signature verification over P-256 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// Deterministic ECDSA signature generation over P-256 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdsa_p256_verify_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_P256_VERIFY_UMAAL_FEATURES`); or else `vg_ecdsa_p256_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdsa_p256_sign_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_P256_SIGN_UMAAL_FEATURES`); or else `vg_ecdsa_p256_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `public` must be valid for reads of 65 bytes. +/// * `out` must be valid for reads and writes of 64 bytes. +/// * `d` must be valid for reads of 32 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 64 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest`, `sig` or the arguments on the stack (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. +/// * `digest_len` must be the digest length of the hash function `hmac` names. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). +/// * `out` and `scratch` must not overlap each other, `d`, `digest`, `hmac` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the 536 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p256_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ecdsa_p256_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32; + type Impl = unsafe extern "C" fn(out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ecdsa_p256::VG_ECDSA_P256_VERIFY_UMAAL_FEATURES) } - || cpu.contains(crate::ecdsa_p256::VG_ECDSA_P256_VERIFY_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ecdsa_p256::VG_ECDSA_P256_SIGN_UMAAL_FEATURES) } + || cpu.contains(crate::ecdsa_p256::VG_ECDSA_P256_SIGN_UMAAL_FEATURES) { - return crate::ecdsa_p256::vg_ecdsa_p256_verify_umaal; + return crate::ecdsa_p256::vg_ecdsa_p256_sign_umaal; } - crate::ecdsa_p256::vg_ecdsa_p256_verify + crate::ecdsa_p256::vg_ecdsa_p256_sign } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { + unsafe extern "C" fn first(out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(public, digest, digest_len, sig, scratch) } + unsafe { f(out, d, digest, digest_len, scratch, hmac) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -107,5 +107,5 @@ pub unsafe fn vg_ecdsa_p256_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pub // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(public, digest, digest_len, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, d, digest, digest_len, scratch, hmac) } } diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_p384.rs b/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_p384.rs index af7ee28b610..a5f8f10d4d2 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_p384.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_p384.rs @@ -2,52 +2,48 @@ //! The dispatchers of the verified `ecdsa_p384` functions for `arm` //! (`crate::ecdsa_p384`, `VG.Rust.dispatcher`). -/// Deterministic ECDSA signature generation over P-384 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (48 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (48 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). +/// ECDSA signature verification over P-384 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. /// -/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdsa_p384_sign_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_P384_SIGN_UMAAL_FEATURES`); or else `vg_ecdsa_p384_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdsa_p384_verify_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_P384_VERIFY_UMAAL_FEATURES`); or else `vg_ecdsa_p384_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 96 bytes. -/// * `d` must be valid for reads of 48 bytes. +/// * `public` must be valid for reads of 97 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 96 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. -/// * `digest_len` must be the digest length of the hash function `hmac` names. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `d`, `digest`, `hmac` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the 536 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest`, `sig` or the arguments on the stack (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p384_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { +pub unsafe fn vg_ecdsa_p384_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32; + type Impl = unsafe extern "C" fn(public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ecdsa_p384::VG_ECDSA_P384_SIGN_UMAAL_FEATURES) } - || cpu.contains(crate::ecdsa_p384::VG_ECDSA_P384_SIGN_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ecdsa_p384::VG_ECDSA_P384_VERIFY_UMAAL_FEATURES) } + || cpu.contains(crate::ecdsa_p384::VG_ECDSA_P384_VERIFY_UMAAL_FEATURES) { - return crate::ecdsa_p384::vg_ecdsa_p384_sign_umaal; + return crate::ecdsa_p384::vg_ecdsa_p384_verify_umaal; } - crate::ecdsa_p384::vg_ecdsa_p384_sign + crate::ecdsa_p384::vg_ecdsa_p384_verify } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { + unsafe extern "C" fn first(public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, d, digest, digest_len, scratch, hmac) } + unsafe { f(public, digest, digest_len, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -55,51 +51,55 @@ pub unsafe fn vg_ecdsa_p384_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, d, digest, digest_len, scratch, hmac) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(public, digest, digest_len, sig, scratch) } } -/// ECDSA signature verification over P-384 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// Deterministic ECDSA signature generation over P-384 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (48 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (48 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdsa_p384_verify_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_P384_VERIFY_UMAAL_FEATURES`); or else `vg_ecdsa_p384_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdsa_p384_sign_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_P384_SIGN_UMAAL_FEATURES`); or else `vg_ecdsa_p384_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `public` must be valid for reads of 97 bytes. +/// * `out` must be valid for reads and writes of 96 bytes. +/// * `d` must be valid for reads of 48 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 96 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest`, `sig` or the arguments on the stack (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. +/// * `digest_len` must be the digest length of the hash function `hmac` names. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). +/// * `out` and `scratch` must not overlap each other, `d`, `digest`, `hmac` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the 536 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p384_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ecdsa_p384_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32; + type Impl = unsafe extern "C" fn(out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ecdsa_p384::VG_ECDSA_P384_VERIFY_UMAAL_FEATURES) } - || cpu.contains(crate::ecdsa_p384::VG_ECDSA_P384_VERIFY_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ecdsa_p384::VG_ECDSA_P384_SIGN_UMAAL_FEATURES) } + || cpu.contains(crate::ecdsa_p384::VG_ECDSA_P384_SIGN_UMAAL_FEATURES) { - return crate::ecdsa_p384::vg_ecdsa_p384_verify_umaal; + return crate::ecdsa_p384::vg_ecdsa_p384_sign_umaal; } - crate::ecdsa_p384::vg_ecdsa_p384_verify + crate::ecdsa_p384::vg_ecdsa_p384_sign } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { + unsafe extern "C" fn first(out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(public, digest, digest_len, sig, scratch) } + unsafe { f(out, d, digest, digest_len, scratch, hmac) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -107,5 +107,5 @@ pub unsafe fn vg_ecdsa_p384_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pub // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(public, digest, digest_len, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, d, digest, digest_len, scratch, hmac) } } diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_p521.rs b/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_p521.rs index fdd14277f71..99aa1bb91b7 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_p521.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_p521.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_p521` functions for `arm` //! (`crate::ecdsa_p521`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over P-521 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 66 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 521 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 66 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_p521_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 133 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 132 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest`, `sig` or the arguments on the stack (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_p521_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 133], digest: *const u8, digest_len: usize, sig: *const [u8; 132], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_p521::vg_ecdsa_p521_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over P-521 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (66 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (66 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_p521_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_p521::vg_ecdsa_p521_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over P-521 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 66 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 521 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 66 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_p521_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 133 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 132 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest`, `sig` or the arguments on the stack (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_p521_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 133], digest: *const u8, digest_len: usize, sig: *const [u8; 132], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p521::vg_ecdsa_p521_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_secp256k1.rs b/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_secp256k1.rs index 787b80bec1b..c0a0c51adf8 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_secp256k1.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ecdsa_secp256k1.rs @@ -2,52 +2,48 @@ //! The dispatchers of the verified `ecdsa_secp256k1` functions for `arm` //! (`crate::ecdsa_secp256k1`, `VG.Rust.dispatcher`). -/// Deterministic ECDSA signature generation over secp256k1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). +/// ECDSA signature verification over secp256k1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. /// -/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdsa_secp256k1_sign_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_SECP256K1_SIGN_UMAAL_FEATURES`); or else `vg_ecdsa_secp256k1_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdsa_secp256k1_verify_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_SECP256K1_VERIFY_UMAAL_FEATURES`); or else `vg_ecdsa_secp256k1_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 64 bytes. -/// * `d` must be valid for reads of 32 bytes. +/// * `public` must be valid for reads of 65 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 64 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. -/// * `digest_len` must be the digest length of the hash function `hmac` names. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `d`, `digest`, `hmac` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the 536 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest`, `sig` or the arguments on the stack (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_secp256k1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { +pub unsafe fn vg_ecdsa_secp256k1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32; + type Impl = unsafe extern "C" fn(public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ecdsa_secp256k1::VG_ECDSA_SECP256K1_SIGN_UMAAL_FEATURES) } - || cpu.contains(crate::ecdsa_secp256k1::VG_ECDSA_SECP256K1_SIGN_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ecdsa_secp256k1::VG_ECDSA_SECP256K1_VERIFY_UMAAL_FEATURES) } + || cpu.contains(crate::ecdsa_secp256k1::VG_ECDSA_SECP256K1_VERIFY_UMAAL_FEATURES) { - return crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_sign_umaal; + return crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_verify_umaal; } - crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_sign + crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_verify } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { + unsafe extern "C" fn first(public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, d, digest, digest_len, scratch, hmac) } + unsafe { f(public, digest, digest_len, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -55,51 +51,55 @@ pub unsafe fn vg_ecdsa_secp256k1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, d, digest, digest_len, scratch, hmac) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(public, digest, digest_len, sig, scratch) } } -/// ECDSA signature verification over secp256k1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// Deterministic ECDSA signature generation over secp256k1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdsa_secp256k1_verify_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_SECP256K1_VERIFY_UMAAL_FEATURES`); or else `vg_ecdsa_secp256k1_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdsa_secp256k1_sign_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_ECDSA_SECP256K1_SIGN_UMAAL_FEATURES`); or else `vg_ecdsa_secp256k1_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `public` must be valid for reads of 65 bytes. +/// * `out` must be valid for reads and writes of 64 bytes. +/// * `d` must be valid for reads of 32 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 64 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest`, `sig` or the arguments on the stack (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. +/// * `digest_len` must be the digest length of the hash function `hmac` names. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). +/// * `out` and `scratch` must not overlap each other, `d`, `digest`, `hmac` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the 536 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_secp256k1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ecdsa_secp256k1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32; + type Impl = unsafe extern "C" fn(out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ecdsa_secp256k1::VG_ECDSA_SECP256K1_VERIFY_UMAAL_FEATURES) } - || cpu.contains(crate::ecdsa_secp256k1::VG_ECDSA_SECP256K1_VERIFY_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ecdsa_secp256k1::VG_ECDSA_SECP256K1_SIGN_UMAAL_FEATURES) } + || cpu.contains(crate::ecdsa_secp256k1::VG_ECDSA_SECP256K1_SIGN_UMAAL_FEATURES) { - return crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_verify_umaal; + return crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_sign_umaal; } - crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_verify + crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_sign } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { + unsafe extern "C" fn first(out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(public, digest, digest_len, sig, scratch) } + unsafe { f(out, d, digest, digest_len, scratch, hmac) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -107,5 +107,5 @@ pub unsafe fn vg_ecdsa_secp256k1_verify_auto(_dit: verified_garbage_cpu::Dit<'_> // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(public, digest, digest_len, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, d, digest, digest_len, scratch, hmac) } } diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ed25519.rs b/crates/verified-garbage-asm-arm/src/dispatch/ed25519.rs index 6b209990b64..b1d29f67fe7 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ed25519.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ed25519.rs @@ -2,26 +2,6 @@ //! The dispatchers of the verified `ed25519` functions for `arm` //! (`crate::ed25519`, `VG.Rust.dispatcher`). -/// Ed25519 verification (RFC 8032 §5.1.7): returns 1 if the 64-byte signature at `signature` verifies for the 32-byte public key at `pk` and the `len` bytes at `message`, and 0 otherwise. Uses pure Ed25519, with no context or prehash. Checks canonical point encodings, S < L, and `[S]B = R + [k]A` with the challenge k = SHA-512(R || A || M) reduced modulo L, as in RFC 8032 §6. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed25519.verifyContract`. Not constant time: timing may depend on the public key, message and signature. -/// -/// It runs `vg_ed25519_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 32 bytes. -/// * `message` must be valid for reads of `len` bytes. -/// * `signature` must be valid for reads of 64 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `scratch` must not overlap `pk`, `message`, `signature` or the arguments on the stack (distinct Rust objects never do). -/// * None of `pk`, `message`, `signature` and `scratch` may overlap the 280 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ed25519_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 32], message: *const u8, len: usize, signature: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ed25519::vg_ed25519_verify(pk, message, len, signature, scratch) } -} - /// Ed25519 key-pair derivation (RFC 8032 §5.1.5): for the 32-byte private seed at `seed`, writes its 64-byte SHA-512 digest (§5.1.5, step 1) to `*expanded` and its 32-byte public key to `*out`, including pruning: the digest and public key that `vg_ed25519_sign_expanded` takes. Contract: `VG.Spec.Ed25519.keyPairContract`. Constant time: only pointers may affect timing. /// /// It runs `vg_ed25519_key_pair`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. @@ -66,3 +46,23 @@ pub unsafe fn vg_ed25519_sign_expanded_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ed25519::vg_ed25519_sign_expanded(out, expanded, pk, message, len, scratch) } } + +/// Ed25519 verification (RFC 8032 §5.1.7): returns 1 if the 64-byte signature at `signature` verifies for the 32-byte public key at `pk` and the `len` bytes at `message`, and 0 otherwise. Uses pure Ed25519, with no context or prehash. Checks canonical point encodings, S < L, and `[S]B = R + [k]A` with the challenge k = SHA-512(R || A || M) reduced modulo L, as in RFC 8032 §6. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed25519.verifyContract`. Not constant time: timing may depend on the public key, message and signature. +/// +/// It runs `vg_ed25519_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 32 bytes. +/// * `message` must be valid for reads of `len` bytes. +/// * `signature` must be valid for reads of 64 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `scratch` must not overlap `pk`, `message`, `signature` or the arguments on the stack (distinct Rust objects never do). +/// * None of `pk`, `message`, `signature` and `scratch` may overlap the 280 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ed25519_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 32], message: *const u8, len: usize, signature: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ed25519::vg_ed25519_verify(pk, message, len, signature, scratch) } +} diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ed448.rs b/crates/verified-garbage-asm-arm/src/dispatch/ed448.rs index a74ca76db2f..ef1d06292ca 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ed448.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ed448.rs @@ -2,27 +2,6 @@ //! The dispatchers of the verified `ed448` functions for `arm` //! (`crate::ed448`, `VG.Rust.dispatcher`). -/// Ed448 verification (RFC 8032 §5.2.7): returns 1 if the 114-byte signature at `signature` verifies for the 57-byte public key at `pk`, the `ctxlen` bytes of context at `context` and the `len` bytes at `message`, and 0 otherwise (always 0 for a context longer than 255 bytes). Uses Ed448 with a context, not Ed448ph (no prehash). Checks canonical point encodings, S < L, and the cofactored equation `[4][S]B = [4]R + [4][k]A` with the challenge k = SHAKE256(dom4(0, C) || R || A || M, 114) reduced modulo L. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed448.verifyContract`. Not constant time: timing may depend on the public key, context, message and signature. -/// -/// It runs `vg_ed448_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 57 bytes. -/// * `context` must be valid for reads of `ctxlen` bytes. -/// * `message` must be valid for reads of `len` bytes. -/// * `signature` must be valid for reads of 114 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `scratch` must not overlap `pk`, `context`, `message`, `signature` or the arguments on the stack (distinct Rust objects never do). -/// * None of `pk`, `context`, `message`, `signature` and `scratch` may overlap the 280 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ed448_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, signature: *const [u8; 114], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ed448::vg_ed448_verify(pk, context, ctxlen, message, len, signature, scratch) } -} - /// Ed448 public-key derivation (RFC 8032 §5.2.5): writes the 57-byte public key to `*out`, from the 57-byte private key at `seed`, including SHAKE256 and pruning. Contract: `VG.Spec.Ed448.publicKeyContract`. Constant time: only pointers may affect timing. /// /// It runs `vg_ed448_public_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. @@ -67,3 +46,24 @@ pub unsafe fn vg_ed448_sign_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, out // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ed448::vg_ed448_sign_cached(out, seed, pk, context, ctxlen, message, len, scratch) } } + +/// Ed448 verification (RFC 8032 §5.2.7): returns 1 if the 114-byte signature at `signature` verifies for the 57-byte public key at `pk`, the `ctxlen` bytes of context at `context` and the `len` bytes at `message`, and 0 otherwise (always 0 for a context longer than 255 bytes). Uses Ed448 with a context, not Ed448ph (no prehash). Checks canonical point encodings, S < L, and the cofactored equation `[4][S]B = [4]R + [4][k]A` with the challenge k = SHAKE256(dom4(0, C) || R || A || M, 114) reduced modulo L. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed448.verifyContract`. Not constant time: timing may depend on the public key, context, message and signature. +/// +/// It runs `vg_ed448_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 57 bytes. +/// * `context` must be valid for reads of `ctxlen` bytes. +/// * `message` must be valid for reads of `len` bytes. +/// * `signature` must be valid for reads of 114 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `scratch` must not overlap `pk`, `context`, `message`, `signature` or the arguments on the stack (distinct Rust objects never do). +/// * None of `pk`, `context`, `message`, `signature` and `scratch` may overlap the 280 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ed448_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, signature: *const [u8; 114], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ed448::vg_ed448_verify(pk, context, ctxlen, message, len, signature, scratch) } +} diff --git a/crates/verified-garbage-asm-arm/src/dispatch/ffdh.rs b/crates/verified-garbage-asm-arm/src/dispatch/ffdh.rs index c71b1bc2dab..6192f343c9f 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/ffdh.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/ffdh.rs @@ -2,56 +2,50 @@ //! The dispatchers of the verified `ffdh` functions for `arm` //! (`crate::ffdh`, `VG.Rust.dispatcher`). -/// Finite-field Diffie–Hellman (SP 800-56A Rev. 3 §5.7.1.1, PKCS #3 §8). With `domain` as `vg_ffdh_check_domain` takes it, the private scalar x (`x_len` bytes) and the peer's public value y (`p_len` bytes), most significant first, computes `z = y^x mod p`; returns 0 and writes zeros if z is 0, 1 or `p − 1`, and otherwise returns 1 and writes z (`p_len` bytes, most significant first) to `out`. Applies no KDF. -/// -/// Contract: `VG.Spec.Ffdh.exchangeContract`. Timing may depend on the pointers, the lengths, the domain, y and the value returned, not otherwise on x; the exponentiation may run over all `x_len` bytes of x, leading zeros included. +/// Initialize a reusable public fixed-base context for finite-field Diffie–Hellman key generation. The checked domain is p, g and optional q, each p_len bytes. Writes generationPrecompute: three public metadata words followed by RSA's modulus/R-squared values and 32 five-tooth comb entries. Returns 1. Word 0 is generationScalarBits, word 1 is its byte length and word 2 is the comb row count. The context has no pointers and is independent of the arithmetic backend. Contract: VG.Spec.Ffdh.generationPrecomputeContract. Timing may depend on the public domain, pointers and lengths. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ffdh_exchange_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_FFDH_EXCHANGE_UMAAL_FEATURES`); or else `vg_ffdh_exchange`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ffdh_generate_key_precompute_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_FFDH_GENERATE_KEY_PRECOMPUTE_UMAAL_FEATURES`); or else `vg_ffdh_generate_key_precompute`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of `out_len` bytes. +/// * `ctx` must be valid for reads and writes of `8 * ctx_len` bytes. /// * `domain` must be valid for reads of `domain_len` bytes. -/// * `x` must be valid for reads of `x_len` bytes. -/// * `peer` must be valid for reads of `peer_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. /// * `p_len` must be in 64..=1024. /// * `domain_len` must be `2 * p_len` or `3 * p_len`. -/// * `out_len` and `peer_len` must be `p_len`. -/// * `x_len` must be in 1..=`p_len`. -/// * For the result to be specified, `domain` must be one that `vg_ffdh_check_domain` accepted (returned 1 for); otherwise it is unspecified. -/// * For the result to be specified, `peer` must also be a value that `vg_ffdh_check_public` accepted for this domain. +/// * domain must be one that vg_ffdh_check_domain accepted. +/// * ctx_len must be generationPrecomputedWords(p_len), namely 3 + 34 * ceil(p_len / 8). /// * `scratch_len` must be at least `16 * p_len + 32`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `domain`, `x`, `peer` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `domain`, `x`, `peer` and `scratch` may overlap the 176 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `ctx` and `scratch` must not overlap each other, `domain` or the arguments on the stack (distinct Rust objects never do). +/// * None of `ctx`, `domain` and `scratch` may overlap the 176 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ffdh_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, domain: *const u8, domain_len: usize, p_len: usize, x: *const u8, x_len: usize, peer: *const u8, peer_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_ffdh_generate_key_precompute_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *mut u64, ctx_len: usize, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(out: *mut u8, out_len: usize, domain: *const u8, domain_len: usize, p_len: usize, x: *const u8, x_len: usize, peer: *const u8, peer_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; + type Impl = unsafe extern "C" fn(ctx: *mut u64, ctx_len: usize, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_EXCHANGE_UMAAL_FEATURES) } - || cpu.contains(crate::ffdh::VG_FFDH_EXCHANGE_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_GENERATE_KEY_PRECOMPUTE_UMAAL_FEATURES) } + || cpu.contains(crate::ffdh::VG_FFDH_GENERATE_KEY_PRECOMPUTE_UMAAL_FEATURES) { - return crate::ffdh::vg_ffdh_exchange_umaal; + return crate::ffdh::vg_ffdh_generate_key_precompute_umaal; } - crate::ffdh::vg_ffdh_exchange + crate::ffdh::vg_ffdh_generate_key_precompute } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(out: *mut u8, out_len: usize, domain: *const u8, domain_len: usize, p_len: usize, x: *const u8, x_len: usize, peer: *const u8, peer_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + unsafe extern "C" fn first(ctx: *mut u64, ctx_len: usize, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, out_len, domain, domain_len, p_len, x, x_len, peer, peer_len, scratch, scratch_len) } + unsafe { f(ctx, ctx_len, domain, domain_len, p_len, scratch, scratch_len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -59,52 +53,57 @@ pub unsafe fn vg_ffdh_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *m // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, domain, domain_len, p_len, x, x_len, peer, peer_len, scratch, scratch_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, ctx_len, domain, domain_len, p_len, scratch, scratch_len) } } -/// Checks imported finite-field Diffie–Hellman parameters (SP 800-56A Rev. 3 §5.5.2, without primality tests). `domain` is p, g and, if `domain_len` is `3 * p_len`, q, each `p_len` bytes, most significant first. Returns 1 if p has 512 to 8192 bits, its first byte is not zero and it is odd, `1 < g < p − 1`, and, with q, q is odd, `2 < q < p − 1`, q divides `p − 1` and `g^q mod p = 1`; otherwise 0. Does not test p or q for primality: the caller must establish their provenance. -/// -/// Contract: `VG.Spec.Ffdh.checkDomainContract`. Timing may depend on the pointers, the lengths and the domain. +/// One native finite-field Diffie–Hellman key-generation attempt using a reusable public five-tooth comb context. Takes p_len random bytes and implements the same vettedScalarOfOctets, keyStatus and output conventions as vg_ffdh_generate_key_vetted for checked parameters. The context's public maximum width fixes the schedule, including secret leading zeros. Contract: VG.Spec.Ffdh.cachedGenerateKeyContract. Timing may depend on the public domain, pointers, lengths and returned status, not otherwise on the random bytes or private scalar. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ffdh_check_domain_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_FFDH_CHECK_DOMAIN_UMAAL_FEATURES`); or else `vg_ffdh_check_domain`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ffdh_generate_key_cached_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_FFDH_GENERATE_KEY_CACHED_UMAAL_FEATURES`); or else `vg_ffdh_generate_key_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// +/// * `x` must be valid for reads and writes of `x_len` bytes. +/// * `y` must be valid for reads and writes of `y_len` bytes. /// * `domain` must be valid for reads of `domain_len` bytes. +/// * `rand` must be valid for reads of `rand_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `ctx` must be valid for reads of `8 * ctx_len` bytes. /// * `p_len` must be in 64..=1024. /// * `domain_len` must be `2 * p_len` or `3 * p_len`. +/// * domain must be one that vg_ffdh_check_domain accepted. +/// * ctx_len must be generationPrecomputedWords(p_len). ctx must contain the unchanged words written by vg_ffdh_generate_key_precompute for these parameters. +/// * x_len, y_len and rand_len must be p_len. /// * `scratch_len` must be at least `16 * p_len + 32`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `scratch` must not overlap `domain` or the arguments on the stack (distinct Rust objects never do). -/// * Neither `domain` nor `scratch` may overlap the 128 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `x`, `y` and `scratch` must not overlap each other, `domain`, `rand`, `ctx` or the arguments on the stack (distinct Rust objects never do). +/// * None of `x`, `y`, `domain`, `rand`, `scratch` and `ctx` may overlap the 176 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ffdh_check_domain_auto(_dit: verified_garbage_cpu::Dit<'_>, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_ffdh_generate_key_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, ctx: *const u64, ctx_len: usize) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; + type Impl = unsafe extern "C" fn(x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, ctx: *const u64, ctx_len: usize) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_CHECK_DOMAIN_UMAAL_FEATURES) } - || cpu.contains(crate::ffdh::VG_FFDH_CHECK_DOMAIN_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_GENERATE_KEY_CACHED_UMAAL_FEATURES) } + || cpu.contains(crate::ffdh::VG_FFDH_GENERATE_KEY_CACHED_UMAAL_FEATURES) { - return crate::ffdh::vg_ffdh_check_domain_umaal; + return crate::ffdh::vg_ffdh_generate_key_cached_umaal; } - crate::ffdh::vg_ffdh_check_domain + crate::ffdh::vg_ffdh_generate_key_cached } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + unsafe extern "C" fn first(x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, ctx: *const u64, ctx_len: usize) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(domain, domain_len, p_len, scratch, scratch_len) } + unsafe { f(x, x_len, y, y_len, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, ctx, ctx_len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -112,55 +111,60 @@ pub unsafe fn vg_ffdh_check_domain_auto(_dit: verified_garbage_cpu::Dit<'_>, dom // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(domain, domain_len, p_len, scratch, scratch_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(x, x_len, y, y_len, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, ctx, ctx_len) } } -/// Checks an imported finite-field Diffie–Hellman public value (SP 800-56A Rev. 3 §5.6.2.3.1, and §5.6.2.3.2 with q). With `domain` as `vg_ffdh_check_domain` takes it and y (`p_len` bytes, most significant first), returns 1 if `1 < y < p − 1` and, with q, `y^q mod p = 1`; otherwise 0. -/// -/// Contract: `VG.Spec.Ffdh.checkPublicContract`. Timing may depend on the pointers, the lengths, the domain and y. +/// Generate a finite-field Diffie–Hellman key pair using a reusable public five-tooth comb context and the existing vettedKeySearch rejection policy. Returns 1 with a key pair, 3 when a budget is exhausted, or 0 to request entropy. On 0, discard used bytes, preserve the suffix and extend it to state[3] bytes before calling again. On 0 and 3 both outputs are zero. Entropy consumption, retry budgets and statuses are unchanged. Contract: VG.Spec.Ffdh.cachedKeySearchContract. Timing may depend on the public domain, pointers, lengths, controller policy and sequence of attempt statuses, not otherwise on private scalar bytes. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ffdh_check_public_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_FFDH_CHECK_PUBLIC_UMAAL_FEATURES`); or else `vg_ffdh_check_public`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ffdh_generate_key_search_cached_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_FFDH_GENERATE_KEY_SEARCH_CACHED_UMAAL_FEATURES`); or else `vg_ffdh_generate_key_search_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// +/// * `x` must be valid for reads and writes of `x_len` bytes. +/// * `y` must be valid for reads and writes of `y_len` bytes. +/// * `used` must be valid for reads and writes of 8 bytes. /// * `domain` must be valid for reads of `domain_len` bytes. -/// * `y` must be valid for reads of `y_len` bytes. +/// * `rand` must be valid for reads of `rand_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `state` must be valid for reads and writes of 128 bytes. +/// * `ctx` must be valid for reads of `8 * ctx_len` bytes. /// * `p_len` must be in 64..=1024. /// * `domain_len` must be `2 * p_len` or `3 * p_len`. -/// * `y_len` must be `p_len`. -/// * For the result to be specified, `domain` must be one that `vg_ffdh_check_domain` accepted (returned 1 for); otherwise it is unspecified. +/// * domain must be one that vg_ffdh_check_domain accepted. +/// * ctx_len must be generationPrecomputedWords(p_len). ctx must contain the unchanged words written by vg_ffdh_generate_key_precompute for these parameters. +/// * state must be initialized by vg_ffdh_key_generation_init for p_len, or be the state returned by this routine. +/// * x_len and y_len must be p_len. /// * `scratch_len` must be at least `16 * p_len + 32`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `scratch` must not overlap `domain`, `y` or the arguments on the stack (distinct Rust objects never do). -/// * None of `domain`, `y` and `scratch` may overlap the 176 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `x`, `y`, `used`, `scratch` and `state` must not overlap each other, `domain`, `rand`, `ctx` or the arguments on the stack (distinct Rust objects never do). +/// * None of `x`, `y`, `used`, `domain`, `rand`, `scratch`, `state` and `ctx` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ffdh_check_public_auto(_dit: verified_garbage_cpu::Dit<'_>, domain: *const u8, domain_len: usize, p_len: usize, y: *const u8, y_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_ffdh_generate_key_search_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, used: *mut [u64; 1], domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16], ctx: *const u64, ctx_len: usize) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(domain: *const u8, domain_len: usize, p_len: usize, y: *const u8, y_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; + type Impl = unsafe extern "C" fn(x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, used: *mut [u64; 1], domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16], ctx: *const u64, ctx_len: usize) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_CHECK_PUBLIC_UMAAL_FEATURES) } - || cpu.contains(crate::ffdh::VG_FFDH_CHECK_PUBLIC_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_GENERATE_KEY_SEARCH_CACHED_UMAAL_FEATURES) } + || cpu.contains(crate::ffdh::VG_FFDH_GENERATE_KEY_SEARCH_CACHED_UMAAL_FEATURES) { - return crate::ffdh::vg_ffdh_check_public_umaal; + return crate::ffdh::vg_ffdh_generate_key_search_cached_umaal; } - crate::ffdh::vg_ffdh_check_public + crate::ffdh::vg_ffdh_generate_key_search_cached } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(domain: *const u8, domain_len: usize, p_len: usize, y: *const u8, y_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + unsafe extern "C" fn first(x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, used: *mut [u64; 1], domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16], ctx: *const u64, ctx_len: usize) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(domain, domain_len, p_len, y, y_len, scratch, scratch_len) } + unsafe { f(x, x_len, y, y_len, used, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, state, ctx, ctx_len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -168,14 +172,14 @@ pub unsafe fn vg_ffdh_check_public_auto(_dit: verified_garbage_cpu::Dit<'_>, dom // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(domain, domain_len, p_len, y, y_len, scratch, scratch_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(x, x_len, y, y_len, used, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, state, ctx, ctx_len) } } /// Derives the public value of an imported finite-field Diffie–Hellman private key (SP 800-56A Rev. 3 §5.6.2.1.2 for x's range, §5.6.1.1 for `y = g^x mod p`). With `domain` as `vg_ffdh_check_domain` takes it and x (`x_len` bytes, most significant first): returns 2 if x is not in `1..q` (`1..p − 1` without q); 1 if `y = g^x mod p` is a valid public value (as `vg_ffdh_check_public` checks it), writing y (`p_len` bytes, most significant first) to `y`; 3 otherwise. Writes zeros to `y` unless it returns 1. /// /// Contract: `VG.Spec.Ffdh.derivePublicContract`. Timing may depend on the pointers, the lengths, the domain and the value returned, not otherwise on x; the exponentiation may run over all `x_len` bytes of x, leading zeros included. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ffdh_derive_public_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_FFDH_DERIVE_PUBLIC_UMAAL_FEATURES`); or else `vg_ffdh_derive_public`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ffdh_derive_public_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_FFDH_DERIVE_PUBLIC_UMAAL_FEATURES`); or else `vg_ffdh_derive_public`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -229,54 +233,49 @@ pub unsafe fn vg_ffdh_derive_public_auto(_dit: verified_garbage_cpu::Dit<'_>, y: unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(y, y_len, domain, domain_len, p_len, x, x_len, scratch, scratch_len) } } -/// Checks an imported finite-field Diffie–Hellman key pair (SP 800-56A Rev. 3 §5.6.2.1.2 and §5.6.2.1.4). With `domain` as `vg_ffdh_check_domain` takes it, y (`p_len` bytes) and x (`x_len` bytes), most significant first: returns 2 if x is not in `1..q` (`1..p − 1` without q); 1 if y is a valid public value (as `vg_ffdh_check_public` checks it) and `y = g^x mod p`; 0 otherwise. +/// Checks imported finite-field Diffie–Hellman parameters (SP 800-56A Rev. 3 §5.5.2, without primality tests). `domain` is p, g and, if `domain_len` is `3 * p_len`, q, each `p_len` bytes, most significant first. Returns 1 if p has 512 to 8192 bits, its first byte is not zero and it is odd, `1 < g < p − 1`, and, with q, q is odd, `2 < q < p − 1`, q divides `p − 1` and `g^q mod p = 1`; otherwise 0. Does not test p or q for primality: the caller must establish their provenance. /// -/// Contract: `VG.Spec.Ffdh.checkPrivateContract`. Timing may depend on the pointers, the lengths, the domain, y and the value returned, not otherwise on x; the exponentiation may run over all `x_len` bytes of x, leading zeros included. +/// Contract: `VG.Spec.Ffdh.checkDomainContract`. Timing may depend on the pointers, the lengths and the domain. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ffdh_check_private_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_FFDH_CHECK_PRIVATE_UMAAL_FEATURES`); or else `vg_ffdh_check_private`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ffdh_check_domain_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_FFDH_CHECK_DOMAIN_UMAAL_FEATURES`); or else `vg_ffdh_check_domain`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `domain` must be valid for reads of `domain_len` bytes. -/// * `y` must be valid for reads of `y_len` bytes. -/// * `x` must be valid for reads of `x_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. /// * `p_len` must be in 64..=1024. /// * `domain_len` must be `2 * p_len` or `3 * p_len`. -/// * `y_len` must be `p_len`. -/// * `x_len` must be in 1..=`p_len`. -/// * For the result to be specified, `domain` must be one that `vg_ffdh_check_domain` accepted (returned 1 for); otherwise it is unspecified. /// * `scratch_len` must be at least `16 * p_len + 32`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `scratch` must not overlap `domain`, `y`, `x` or the arguments on the stack (distinct Rust objects never do). -/// * None of `domain`, `y`, `x` and `scratch` may overlap the 128 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `scratch` must not overlap `domain` or the arguments on the stack (distinct Rust objects never do). +/// * Neither `domain` nor `scratch` may overlap the 128 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ffdh_check_private_auto(_dit: verified_garbage_cpu::Dit<'_>, domain: *const u8, domain_len: usize, p_len: usize, y: *const u8, y_len: usize, x: *const u8, x_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_ffdh_check_domain_auto(_dit: verified_garbage_cpu::Dit<'_>, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(domain: *const u8, domain_len: usize, p_len: usize, y: *const u8, y_len: usize, x: *const u8, x_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; + type Impl = unsafe extern "C" fn(domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_CHECK_PRIVATE_UMAAL_FEATURES) } - || cpu.contains(crate::ffdh::VG_FFDH_CHECK_PRIVATE_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_CHECK_DOMAIN_UMAAL_FEATURES) } + || cpu.contains(crate::ffdh::VG_FFDH_CHECK_DOMAIN_UMAAL_FEATURES) { - return crate::ffdh::vg_ffdh_check_private_umaal; + return crate::ffdh::vg_ffdh_check_domain_umaal; } - crate::ffdh::vg_ffdh_check_private + crate::ffdh::vg_ffdh_check_domain } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(domain: *const u8, domain_len: usize, p_len: usize, y: *const u8, y_len: usize, x: *const u8, x_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + unsafe extern "C" fn first(domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(domain, domain_len, p_len, y, y_len, x, x_len, scratch, scratch_len) } + unsafe { f(domain, domain_len, p_len, scratch, scratch_len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -284,57 +283,57 @@ pub unsafe fn vg_ffdh_check_private_auto(_dit: verified_garbage_cpu::Dit<'_>, do // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(domain, domain_len, p_len, y, y_len, x, x_len, scratch, scratch_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(domain, domain_len, p_len, scratch, scratch_len) } } -/// One native finite-field Diffie–Hellman key-generation attempt using a reusable public five-tooth comb context. Takes p_len random bytes and implements the same vettedScalarOfOctets, keyStatus and output conventions as vg_ffdh_generate_key_vetted for checked parameters. The context's public maximum width fixes the schedule, including secret leading zeros. Contract: VG.Spec.Ffdh.cachedGenerateKeyContract. Timing may depend on the public domain, pointers, lengths and returned status, not otherwise on the random bytes or private scalar. +/// Checks an imported finite-field Diffie–Hellman key pair (SP 800-56A Rev. 3 §5.6.2.1.2 and §5.6.2.1.4). With `domain` as `vg_ffdh_check_domain` takes it, y (`p_len` bytes) and x (`x_len` bytes), most significant first: returns 2 if x is not in `1..q` (`1..p − 1` without q); 1 if y is a valid public value (as `vg_ffdh_check_public` checks it) and `y = g^x mod p`; 0 otherwise. +/// +/// Contract: `VG.Spec.Ffdh.checkPrivateContract`. Timing may depend on the pointers, the lengths, the domain, y and the value returned, not otherwise on x; the exponentiation may run over all `x_len` bytes of x, leading zeros included. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ffdh_generate_key_cached_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_FFDH_GENERATE_KEY_CACHED_UMAAL_FEATURES`); or else `vg_ffdh_generate_key_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ffdh_check_private_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_FFDH_CHECK_PRIVATE_UMAAL_FEATURES`); or else `vg_ffdh_check_private`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `x` must be valid for reads and writes of `x_len` bytes. -/// * `y` must be valid for reads and writes of `y_len` bytes. /// * `domain` must be valid for reads of `domain_len` bytes. -/// * `rand` must be valid for reads of `rand_len` bytes. +/// * `y` must be valid for reads of `y_len` bytes. +/// * `x` must be valid for reads of `x_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `ctx` must be valid for reads of `8 * ctx_len` bytes. /// * `p_len` must be in 64..=1024. /// * `domain_len` must be `2 * p_len` or `3 * p_len`. -/// * domain must be one that vg_ffdh_check_domain accepted. -/// * ctx_len must be generationPrecomputedWords(p_len). ctx must contain the unchanged words written by vg_ffdh_generate_key_precompute for these parameters. -/// * x_len, y_len and rand_len must be p_len. +/// * `y_len` must be `p_len`. +/// * `x_len` must be in 1..=`p_len`. +/// * For the result to be specified, `domain` must be one that `vg_ffdh_check_domain` accepted (returned 1 for); otherwise it is unspecified. /// * `scratch_len` must be at least `16 * p_len + 32`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `x`, `y` and `scratch` must not overlap each other, `domain`, `rand`, `ctx` or the arguments on the stack (distinct Rust objects never do). -/// * None of `x`, `y`, `domain`, `rand`, `scratch` and `ctx` may overlap the 176 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `scratch` must not overlap `domain`, `y`, `x` or the arguments on the stack (distinct Rust objects never do). +/// * None of `domain`, `y`, `x` and `scratch` may overlap the 128 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ffdh_generate_key_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, ctx: *const u64, ctx_len: usize) -> u32 { +pub unsafe fn vg_ffdh_check_private_auto(_dit: verified_garbage_cpu::Dit<'_>, domain: *const u8, domain_len: usize, p_len: usize, y: *const u8, y_len: usize, x: *const u8, x_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, ctx: *const u64, ctx_len: usize) -> u32; + type Impl = unsafe extern "C" fn(domain: *const u8, domain_len: usize, p_len: usize, y: *const u8, y_len: usize, x: *const u8, x_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_GENERATE_KEY_CACHED_UMAAL_FEATURES) } - || cpu.contains(crate::ffdh::VG_FFDH_GENERATE_KEY_CACHED_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_CHECK_PRIVATE_UMAAL_FEATURES) } + || cpu.contains(crate::ffdh::VG_FFDH_CHECK_PRIVATE_UMAAL_FEATURES) { - return crate::ffdh::vg_ffdh_generate_key_cached_umaal; + return crate::ffdh::vg_ffdh_check_private_umaal; } - crate::ffdh::vg_ffdh_generate_key_cached + crate::ffdh::vg_ffdh_check_private } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, ctx: *const u64, ctx_len: usize) -> u32 { + unsafe extern "C" fn first(domain: *const u8, domain_len: usize, p_len: usize, y: *const u8, y_len: usize, x: *const u8, x_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(x, x_len, y, y_len, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, ctx, ctx_len) } + unsafe { f(domain, domain_len, p_len, y, y_len, x, x_len, scratch, scratch_len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -342,68 +341,55 @@ pub unsafe fn vg_ffdh_generate_key_cached_auto(_dit: verified_garbage_cpu::Dit<' // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(x, x_len, y, y_len, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, ctx, ctx_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(domain, domain_len, p_len, y, y_len, x, x_len, scratch, scratch_len) } } -/// Initialize native finite-field Diffie–Hellman key generation. Returns 1 for a domain width in 64..=1024 bytes, initializing the rejection budgets and first entropy request; returns 0 and clears the first four state words otherwise. The remaining state words are private workspace. Contract: `VG.Spec.Ffdh.keyInitContract`. -/// -/// It runs `vg_ffdh_key_generation_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety +/// Checks an imported finite-field Diffie–Hellman public value (SP 800-56A Rev. 3 §5.6.2.3.1, and §5.6.2.3.2 with q). With `domain` as `vg_ffdh_check_domain` takes it and y (`p_len` bytes, most significant first), returns 1 if `1 < y < p − 1` and, with q, `y^q mod p = 1`; otherwise 0. /// -/// * `state` must be valid for reads and writes of 128 bytes. -/// * `state` must not wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ffdh_key_generation_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 16], p_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ffdh::vg_ffdh_key_generation_init(state, p_len) } -} - -/// Run native finite-field Diffie–Hellman parameter generation, with RSA's trial-division sieve. The routine owns candidate retries, attempt limits, witness selection, primality rounds and entropy budgets. Initialize with `vg_ffdh_parameter_generation_init`. Returns 1 with the probable safe prime in `out`, 3 when a bound is exhausted, or 0 only when it needs more entropy. On 0, discard `used` bytes of `rand`, preserve its remaining suffix and extend that suffix to at least `state[3]` bytes before calling again. `state[1]` is the output width and `state[2]` the maximum tape length. The output is zero for 0 and 3. Accepted p and q pass the full 64/128 independent witness counts. Contract: `VG.Spec.Ffdh.sievedParameterSearchContract`. Timing may depend on the public tape and the first four controller words. +/// Contract: `VG.Spec.Ffdh.checkPublicContract`. Timing may depend on the pointers, the lengths, the domain and y. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ffdh_generate_parameters_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_FFDH_GENERATE_PARAMETERS_UMAAL_FEATURES`); or else `vg_ffdh_generate_parameters`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ffdh_check_public_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_FFDH_CHECK_PUBLIC_UMAAL_FEATURES`); or else `vg_ffdh_check_public`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of `out_len` bytes. -/// * `used` must be valid for reads and writes of 8 bytes. -/// * `rand` must be valid for reads of `rand_len` bytes. +/// * `domain` must be valid for reads of `domain_len` bytes. +/// * `y` must be valid for reads of `y_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `state` must be valid for reads and writes of 128 bytes. -/// * `state` must be initialized by `vg_ffdh_parameter_generation_init` for the supplied `bits` and `generator`, or be the state returned by this routine. -/// * `out_len` must equal `state[1]`, and `rand_len` must not exceed `state[2]`. -/// * `scratch_len` must be at least `16 * out_len + 32`. -/// * The last twelve controller words and the scratch contents are private workspace. -/// * `out`, `used`, `scratch` and `state` must not overlap each other, `rand` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `used`, `rand`, `scratch` and `state` may overlap the 232 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `p_len` must be in 64..=1024. +/// * `domain_len` must be `2 * p_len` or `3 * p_len`. +/// * `y_len` must be `p_len`. +/// * For the result to be specified, `domain` must be one that `vg_ffdh_check_domain` accepted (returned 1 for); otherwise it is unspecified. +/// * `scratch_len` must be at least `16 * p_len + 32`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `scratch` must not overlap `domain`, `y` or the arguments on the stack (distinct Rust objects never do). +/// * None of `domain`, `y` and `scratch` may overlap the 176 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ffdh_generate_parameters_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], bits: usize, generator: u32, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16]) -> u32 { +pub unsafe fn vg_ffdh_check_public_auto(_dit: verified_garbage_cpu::Dit<'_>, domain: *const u8, domain_len: usize, p_len: usize, y: *const u8, y_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(out: *mut u8, out_len: usize, used: *mut [u64; 1], bits: usize, generator: u32, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16]) -> u32; + type Impl = unsafe extern "C" fn(domain: *const u8, domain_len: usize, p_len: usize, y: *const u8, y_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_GENERATE_PARAMETERS_UMAAL_FEATURES) } - || cpu.contains(crate::ffdh::VG_FFDH_GENERATE_PARAMETERS_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_CHECK_PUBLIC_UMAAL_FEATURES) } + || cpu.contains(crate::ffdh::VG_FFDH_CHECK_PUBLIC_UMAAL_FEATURES) { - return crate::ffdh::vg_ffdh_generate_parameters_umaal; + return crate::ffdh::vg_ffdh_check_public_umaal; } - crate::ffdh::vg_ffdh_generate_parameters + crate::ffdh::vg_ffdh_check_public } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(out: *mut u8, out_len: usize, used: *mut [u64; 1], bits: usize, generator: u32, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16]) -> u32 { + unsafe extern "C" fn first(domain: *const u8, domain_len: usize, p_len: usize, y: *const u8, y_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, out_len, used, bits, generator, rand, rand_len, scratch, scratch_len, state) } + unsafe { f(domain, domain_len, p_len, y, y_len, scratch, scratch_len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -411,53 +397,59 @@ pub unsafe fn vg_ffdh_generate_parameters_auto(_dit: verified_garbage_cpu::Dit<' // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, used, bits, generator, rand, rand_len, scratch, scratch_len, state) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(domain, domain_len, p_len, y, y_len, scratch, scratch_len) } } -/// Initialize a reusable public fixed-base context for finite-field Diffie–Hellman key generation. The checked domain is p, g and optional q, each p_len bytes. Writes generationPrecompute: three public metadata words followed by RSA's modulus/R-squared values and 32 five-tooth comb entries. Returns 1. Word 0 is generationScalarBits, word 1 is its byte length and word 2 is the comb row count. The context has no pointers and is independent of the arithmetic backend. Contract: VG.Spec.Ffdh.generationPrecomputeContract. Timing may depend on the public domain, pointers and lengths. +/// Finite-field Diffie–Hellman (SP 800-56A Rev. 3 §5.7.1.1, PKCS #3 §8). With `domain` as `vg_ffdh_check_domain` takes it, the private scalar x (`x_len` bytes) and the peer's public value y (`p_len` bytes), most significant first, computes `z = y^x mod p`; returns 0 and writes zeros if z is 0, 1 or `p − 1`, and otherwise returns 1 and writes z (`p_len` bytes, most significant first) to `out`. Applies no KDF. +/// +/// Contract: `VG.Spec.Ffdh.exchangeContract`. Timing may depend on the pointers, the lengths, the domain, y and the value returned, not otherwise on x; the exponentiation may run over all `x_len` bytes of x, leading zeros included. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ffdh_generate_key_precompute_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_FFDH_GENERATE_KEY_PRECOMPUTE_UMAAL_FEATURES`); or else `vg_ffdh_generate_key_precompute`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ffdh_exchange_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_FFDH_EXCHANGE_UMAAL_FEATURES`); or else `vg_ffdh_exchange`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `ctx` must be valid for reads and writes of `8 * ctx_len` bytes. +/// * `out` must be valid for reads and writes of `out_len` bytes. /// * `domain` must be valid for reads of `domain_len` bytes. +/// * `x` must be valid for reads of `x_len` bytes. +/// * `peer` must be valid for reads of `peer_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. /// * `p_len` must be in 64..=1024. /// * `domain_len` must be `2 * p_len` or `3 * p_len`. -/// * domain must be one that vg_ffdh_check_domain accepted. -/// * ctx_len must be generationPrecomputedWords(p_len), namely 3 + 34 * ceil(p_len / 8). +/// * `out_len` and `peer_len` must be `p_len`. +/// * `x_len` must be in 1..=`p_len`. +/// * For the result to be specified, `domain` must be one that `vg_ffdh_check_domain` accepted (returned 1 for); otherwise it is unspecified. +/// * For the result to be specified, `peer` must also be a value that `vg_ffdh_check_public` accepted for this domain. /// * `scratch_len` must be at least `16 * p_len + 32`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `ctx` and `scratch` must not overlap each other, `domain` or the arguments on the stack (distinct Rust objects never do). -/// * None of `ctx`, `domain` and `scratch` may overlap the 176 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `domain`, `x`, `peer` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `domain`, `x`, `peer` and `scratch` may overlap the 176 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ffdh_generate_key_precompute_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *mut u64, ctx_len: usize, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_ffdh_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, domain: *const u8, domain_len: usize, p_len: usize, x: *const u8, x_len: usize, peer: *const u8, peer_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *mut u64, ctx_len: usize, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; + type Impl = unsafe extern "C" fn(out: *mut u8, out_len: usize, domain: *const u8, domain_len: usize, p_len: usize, x: *const u8, x_len: usize, peer: *const u8, peer_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_GENERATE_KEY_PRECOMPUTE_UMAAL_FEATURES) } - || cpu.contains(crate::ffdh::VG_FFDH_GENERATE_KEY_PRECOMPUTE_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_EXCHANGE_UMAAL_FEATURES) } + || cpu.contains(crate::ffdh::VG_FFDH_EXCHANGE_UMAAL_FEATURES) { - return crate::ffdh::vg_ffdh_generate_key_precompute_umaal; + return crate::ffdh::vg_ffdh_exchange_umaal; } - crate::ffdh::vg_ffdh_generate_key_precompute + crate::ffdh::vg_ffdh_exchange } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *mut u64, ctx_len: usize, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + unsafe extern "C" fn first(out: *mut u8, out_len: usize, domain: *const u8, domain_len: usize, p_len: usize, x: *const u8, x_len: usize, peer: *const u8, peer_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, ctx_len, domain, domain_len, p_len, scratch, scratch_len) } + unsafe { f(out, out_len, domain, domain_len, p_len, x, x_len, peer, peer_len, scratch, scratch_len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -465,7 +457,22 @@ pub unsafe fn vg_ffdh_generate_key_precompute_auto(_dit: verified_garbage_cpu::D // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, ctx_len, domain, domain_len, p_len, scratch, scratch_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, domain, domain_len, p_len, x, x_len, peer, peer_len, scratch, scratch_len) } +} + +/// Initialize native finite-field Diffie–Hellman key generation. Returns 1 for a domain width in 64..=1024 bytes, initializing the rejection budgets and first entropy request; returns 0 and clears the first four state words otherwise. The remaining state words are private workspace. Contract: `VG.Spec.Ffdh.keyInitContract`. +/// +/// It runs `vg_ffdh_key_generation_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 128 bytes. +/// * `state` must not wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ffdh_key_generation_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 16], p_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ffdh::vg_ffdh_key_generation_init(state, p_len) } } /// Initialize native finite-field Diffie–Hellman parameter generation. Validates 512..=8192 bits and generator 2 or 5. Returns 1 and initializes the controller's attempt limit, output width, entropy budget and first entropy request; returns 0 and clears those four words on invalid parameters. The remaining state words are private workspace. Contract: `VG.Spec.Ffdh.parameterInitContract`. @@ -483,57 +490,50 @@ pub unsafe fn vg_ffdh_parameter_generation_init_auto(_dit: verified_garbage_cpu: unsafe { crate::ffdh::vg_ffdh_parameter_generation_init(state, bits, generator) } } -/// Generate a finite-field Diffie–Hellman key pair using a reusable public five-tooth comb context and the existing vettedKeySearch rejection policy. Returns 1 with a key pair, 3 when a budget is exhausted, or 0 to request entropy. On 0, discard used bytes, preserve the suffix and extend it to state[3] bytes before calling again. On 0 and 3 both outputs are zero. Entropy consumption, retry budgets and statuses are unchanged. Contract: VG.Spec.Ffdh.cachedKeySearchContract. Timing may depend on the public domain, pointers, lengths, controller policy and sequence of attempt statuses, not otherwise on private scalar bytes. +/// Run native finite-field Diffie–Hellman parameter generation, with RSA's trial-division sieve. The routine owns candidate retries, attempt limits, witness selection, primality rounds and entropy budgets. Initialize with `vg_ffdh_parameter_generation_init`. Returns 1 with the probable safe prime in `out`, 3 when a bound is exhausted, or 0 only when it needs more entropy. On 0, discard `used` bytes of `rand`, preserve its remaining suffix and extend that suffix to at least `state[3]` bytes before calling again. `state[1]` is the output width and `state[2]` the maximum tape length. The output is zero for 0 and 3. Accepted p and q pass the full 64/128 independent witness counts. Contract: `VG.Spec.Ffdh.sievedParameterSearchContract`. Timing may depend on the public tape and the first four controller words. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ffdh_generate_key_search_cached_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_FFDH_GENERATE_KEY_SEARCH_CACHED_UMAAL_FEATURES`); or else `vg_ffdh_generate_key_search_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ffdh_generate_parameters_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_FFDH_GENERATE_PARAMETERS_UMAAL_FEATURES`); or else `vg_ffdh_generate_parameters`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `x` must be valid for reads and writes of `x_len` bytes. -/// * `y` must be valid for reads and writes of `y_len` bytes. +/// * `out` must be valid for reads and writes of `out_len` bytes. /// * `used` must be valid for reads and writes of 8 bytes. -/// * `domain` must be valid for reads of `domain_len` bytes. /// * `rand` must be valid for reads of `rand_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. /// * `state` must be valid for reads and writes of 128 bytes. -/// * `ctx` must be valid for reads of `8 * ctx_len` bytes. -/// * `p_len` must be in 64..=1024. -/// * `domain_len` must be `2 * p_len` or `3 * p_len`. -/// * domain must be one that vg_ffdh_check_domain accepted. -/// * ctx_len must be generationPrecomputedWords(p_len). ctx must contain the unchanged words written by vg_ffdh_generate_key_precompute for these parameters. -/// * state must be initialized by vg_ffdh_key_generation_init for p_len, or be the state returned by this routine. -/// * x_len and y_len must be p_len. -/// * `scratch_len` must be at least `16 * p_len + 32`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `x`, `y`, `used`, `scratch` and `state` must not overlap each other, `domain`, `rand`, `ctx` or the arguments on the stack (distinct Rust objects never do). -/// * None of `x`, `y`, `used`, `domain`, `rand`, `scratch`, `state` and `ctx` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must be initialized by `vg_ffdh_parameter_generation_init` for the supplied `bits` and `generator`, or be the state returned by this routine. +/// * `out_len` must equal `state[1]`, and `rand_len` must not exceed `state[2]`. +/// * `scratch_len` must be at least `16 * out_len + 32`. +/// * The last twelve controller words and the scratch contents are private workspace. +/// * `out`, `used`, `scratch` and `state` must not overlap each other, `rand` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `used`, `rand`, `scratch` and `state` may overlap the 232 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ffdh_generate_key_search_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, used: *mut [u64; 1], domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16], ctx: *const u64, ctx_len: usize) -> u32 { +pub unsafe fn vg_ffdh_generate_parameters_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], bits: usize, generator: u32, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, used: *mut [u64; 1], domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16], ctx: *const u64, ctx_len: usize) -> u32; + type Impl = unsafe extern "C" fn(out: *mut u8, out_len: usize, used: *mut [u64; 1], bits: usize, generator: u32, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); #[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd"))] - if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_GENERATE_KEY_SEARCH_CACHED_UMAAL_FEATURES) } - || cpu.contains(crate::ffdh::VG_FFDH_GENERATE_KEY_SEARCH_CACHED_UMAAL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_GENERATE_PARAMETERS_UMAAL_FEATURES) } + || cpu.contains(crate::ffdh::VG_FFDH_GENERATE_PARAMETERS_UMAAL_FEATURES) { - return crate::ffdh::vg_ffdh_generate_key_search_cached_umaal; + return crate::ffdh::vg_ffdh_generate_parameters_umaal; } - crate::ffdh::vg_ffdh_generate_key_search_cached + crate::ffdh::vg_ffdh_generate_parameters } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, used: *mut [u64; 1], domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16], ctx: *const u64, ctx_len: usize) -> u32 { + unsafe extern "C" fn first(out: *mut u8, out_len: usize, used: *mut [u64; 1], bits: usize, generator: u32, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(x, x_len, y, y_len, used, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, state, ctx, ctx_len) } + unsafe { f(out, out_len, used, bits, generator, rand, rand_len, scratch, scratch_len, state) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -541,5 +541,5 @@ pub unsafe fn vg_ffdh_generate_key_search_cached_auto(_dit: verified_garbage_cpu // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(x, x_len, y, y_len, used, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, state, ctx, ctx_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, used, bits, generator, rand, rand_len, scratch, scratch_len, state) } } diff --git a/crates/verified-garbage-asm-arm/src/dispatch/fn_tables.rs b/crates/verified-garbage-asm-arm/src/dispatch/fn_tables.rs index 2d982f4d20a..f14d05d4f5d 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/fn_tables.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/fn_tables.rs @@ -2,30 +2,66 @@ //! The dispatchers of the tables of functions for `arm` //! (`crate::fn_tables`, `VG.Rust.tableDispatcher`). -/// The table of functions `VG_HASH_MD5` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hash_md5_auto() -> &'static crate::fn_tables::Hash { - &crate::fn_tables::VG_HASH_MD5 -} - /// The table of functions `VG_HMAC_MD5` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] pub fn vg_hmac_md5_auto() -> &'static crate::fn_tables::Hmac { &crate::fn_tables::VG_HMAC_MD5 } -/// The table of functions `VG_HASH_SHA1` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hash_sha1_auto() -> &'static crate::fn_tables::Hash { - &crate::fn_tables::VG_HASH_SHA1 -} - /// The table of functions `VG_HMAC_SHA1` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] pub fn vg_hmac_sha1_auto() -> &'static crate::fn_tables::Hmac { &crate::fn_tables::VG_HMAC_SHA1 } +/// The table of functions `VG_HMAC_SHA224` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha224_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA224 +} + +/// The table of functions `VG_HMAC_SHA256` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha256_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA256 +} + +/// The table of functions `VG_HMAC_SHA384` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha384_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA384 +} + +/// The table of functions `VG_HMAC_SHA512` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha512_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA512 +} + +/// The table of functions `VG_HMAC_SHA512_224` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha512_224_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA512_224 +} + +/// The table of functions `VG_HMAC_SHA512_256` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha512_256_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA512_256 +} + +/// The table of functions `VG_HASH_MD5` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hash_md5_auto() -> &'static crate::fn_tables::Hash { + &crate::fn_tables::VG_HASH_MD5 +} + +/// The table of functions `VG_HASH_SHA1` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hash_sha1_auto() -> &'static crate::fn_tables::Hash { + &crate::fn_tables::VG_HASH_SHA1 +} + /// The table of functions `VG_HASH_SHA224` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] pub fn vg_hash_sha224_auto() -> &'static crate::fn_tables::Hash { @@ -50,28 +86,16 @@ pub fn vg_hash_sha512_auto() -> &'static crate::fn_tables::Hash { &crate::fn_tables::VG_HASH_SHA512 } -/// The table of functions `VG_HMAC_SHA224` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hmac_sha224_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA224 -} - -/// The table of functions `VG_HMAC_SHA256` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hmac_sha256_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA256 -} - -/// The table of functions `VG_HMAC_SHA384` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +/// The table of functions `VG_HASH_SHA512_224` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] -pub fn vg_hmac_sha384_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA384 +pub fn vg_hash_sha512_224_auto() -> &'static crate::fn_tables::Hash { + &crate::fn_tables::VG_HASH_SHA512_224 } -/// The table of functions `VG_HMAC_SHA512` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +/// The table of functions `VG_HASH_SHA512_256` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] -pub fn vg_hmac_sha512_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA512 +pub fn vg_hash_sha512_256_auto() -> &'static crate::fn_tables::Hash { + &crate::fn_tables::VG_HASH_SHA512_256 } /// The table of functions `VG_MD_HASH_MD5` of the interface `MdHash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). @@ -110,30 +134,6 @@ pub fn vg_md_hash_sha512_auto() -> &'static crate::fn_tables::MdHash { &crate::fn_tables::VG_MD_HASH_SHA512 } -/// The table of functions `VG_HASH_SHA512_224` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hash_sha512_224_auto() -> &'static crate::fn_tables::Hash { - &crate::fn_tables::VG_HASH_SHA512_224 -} - -/// The table of functions `VG_HASH_SHA512_256` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hash_sha512_256_auto() -> &'static crate::fn_tables::Hash { - &crate::fn_tables::VG_HASH_SHA512_256 -} - -/// The table of functions `VG_HMAC_SHA512_224` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hmac_sha512_224_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA512_224 -} - -/// The table of functions `VG_HMAC_SHA512_256` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hmac_sha512_256_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA512_256 -} - /// The table of functions `VG_MD_HASH_SHA512_224` of the interface `MdHash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] pub fn vg_md_hash_sha512_224_auto() -> &'static crate::fn_tables::MdHash { diff --git a/crates/verified-garbage-asm-arm/src/dispatch/gcm.rs b/crates/verified-garbage-asm-arm/src/dispatch/gcm.rs index 5a0a7765df6..c3d4c3d9f41 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/gcm.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/gcm.rs @@ -72,31 +72,26 @@ pub unsafe fn vg_aes_gcm_open_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *co unsafe { crate::gcm::vg_aes_gcm_open(ctx, rounds, nonce, nonce_len, aad, aad_len, data, len, tag, tag_len) } } -/// AES-GCM authenticated decryption (NIST SP 800-38D §7.2, GCM-AD), out of place: with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds and the received tag the `tag_len` bytes at `tag`, returns 1 if `tag_len` is between 4 and 16 and the tag is the first `tag_len` bytes of that of the `len` bytes of ciphertext at `src` and the `aad_len` bytes of additional data at `aad`, under the IV the `nonce_len` bytes at `nonce`, having then written the plaintext to the `dst_len = len` bytes at `dst`; otherwise returns 0, having written zeros to them: `vg_aes_gcm_open` with the ciphertext read from `src` and the plaintext written to `dst`. `src` is not written, and `dst` is a buffer of its own: it overlaps no other buffer, `src` included. The tags are compared without a branch. +/// Starts an incremental AES-GCM encryption or decryption (NIST SP 800-38D §7): with the key context `*ctx` that `vg_aes_gcm_init` wrote, makes the streaming state `*state` represent the message with the IV the `nonce_len` bytes at `nonce`, and no additional data or text yet. Continue with `vg_aes_gcm_stream_aad`, then `vg_aes_gcm_stream_encrypt` or `vg_aes_gcm_stream_decrypt`, then `vg_aes_gcm_stream_finish` or `vg_aes_gcm_stream_verify`, with the same key context. /// -/// The function checks no other length: GCM requires a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of ciphertext and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// The function checks no length: GCM requires a nonce of 1 to `2^61 - 1` bytes (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.openToContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the additional data, the data or the tag. +/// Contract: `VG.Spec.Gcm.streamInitContract`. The streaming state is `J₀`, the GHASH accumulator, a partial block, the next counter block and a keystream block (`VG.Spec.Gcm.StreamRepr`). Constant time: only the pointers and `nonce_len` may affect timing, not the key context or the nonce. /// -/// It runs `vg_aes_gcm_open_to`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_aes_gcm_stream_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. /// * `nonce` must be valid for reads of `nonce_len` bytes. -/// * `aad` must be valid for reads of `aad_len` bytes. -/// * `src` must be valid for reads of `len` bytes. -/// * `dst` must be valid for reads and writes of `dst_len` bytes. -/// * `tag` must be valid for reads of `tag_len` bytes. -/// * `rounds` must be 10, 12 or 14. -/// * `dst_len` must equal `len`. -/// * `dst` must not overlap `ctx`, `nonce`, `aad`, `src`, `tag` or the arguments on the stack (distinct Rust objects never do). -/// * None of `ctx`, `nonce`, `aad`, `src`, `dst` and `tag` may overlap the 2620 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must be valid for reads and writes of 80 bytes. +/// * `state` must not overlap `ctx` or `nonce` (distinct Rust objects never do). +/// * None of `ctx`, `nonce` and `state` may overlap the 2584 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_open_to_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const u8, len: usize, dst: *mut u8, dst_len: usize, tag: *const u8, tag_len: usize) -> u32 { +pub unsafe fn vg_aes_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::gcm::vg_aes_gcm_open_to(ctx, rounds, nonce, nonce_len, aad, aad_len, src, len, dst, dst_len, tag, tag_len) } + unsafe { crate::gcm::vg_aes_gcm_stream_init(ctx, nonce, nonce_len, state) } } /// Absorbs additional data into an incremental AES-GCM encryption or decryption: with the key context `*ctx`, if the streaming state `*state` represents a message with `aad_len` bytes (modulo 2⁶⁴) of additional data and no text yet, it then represents that message with the `len` bytes at `data` appended to its additional data. All of the additional data comes before the text (§7.1 step 5): after text, the state represents no message. @@ -119,53 +114,50 @@ pub unsafe fn vg_aes_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ct unsafe { crate::gcm::vg_aes_gcm_stream_aad(ctx, state, aad_len, data, len) } } -/// Starts an incremental AES-GCM encryption or decryption (NIST SP 800-38D §7): with the key context `*ctx` that `vg_aes_gcm_init` wrote, makes the streaming state `*state` represent the message with the IV the `nonce_len` bytes at `nonce`, and no additional data or text yet. Continue with `vg_aes_gcm_stream_aad`, then `vg_aes_gcm_stream_encrypt` or `vg_aes_gcm_stream_decrypt`, then `vg_aes_gcm_stream_finish` or `vg_aes_gcm_stream_verify`, with the same key context. +/// Encrypts the next piece of the text of an incremental AES-GCM encryption (NIST SP 800-38D §7.1): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and the ciphertext of exactly `text_len` bytes of plaintext so far, encrypts the `len` bytes at `data` in place, as the continuation of that plaintext, and the state then represents the message with them appended. /// -/// The function checks no length: GCM requires a nonce of 1 to `2^61 - 1` bytes (§5.2.1.1), which the caller must check. +/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.streamInitContract`. The streaming state is `J₀`, the GHASH accumulator, a partial block, the next counter block and a keystream block (`VG.Spec.Gcm.StreamRepr`). Constant time: only the pointers and `nonce_len` may affect timing, not the key context or the nonce. +/// Contract: `VG.Spec.Gcm.streamEncryptContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. /// -/// It runs `vg_aes_gcm_stream_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_aes_gcm_stream_encrypt`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `nonce` must be valid for reads of `nonce_len` bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `state` must not overlap `ctx` or `nonce` (distinct Rust objects never do). -/// * None of `ctx`, `nonce` and `state` may overlap the 2584 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads and writes of `len` bytes. +/// * `rounds` must be 10, 12 or 14. +/// * `state` and `data` must not overlap each other, `ctx` or the arguments on the stack (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the 2600 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { +pub unsafe fn vg_aes_gcm_stream_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::gcm::vg_aes_gcm_stream_init(ctx, nonce, nonce_len, state) } + unsafe { crate::gcm::vg_aes_gcm_stream_encrypt(ctx, rounds, state, aad_len, text_len, data, len) } } -/// AES-GCM authenticated encryption (NIST SP 800-38D §7.1, GCM-AE, with a 128-bit tag), out of place, of a plaintext in pieces: with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, encrypts the concatenation of the `src_count` slices that `src` lists (each an address and a length, in bytes), under the IV the `nonce_len` bytes at `nonce`, writes the ciphertext to the `len` bytes at `dst`, and writes the tag of the ciphertext and the `aad_len` bytes of additional data at `aad` to `*tag`: `vg_aes_gcm_seal` with the plaintext gathered from `src`. A shorter tag is the first bytes of this one. +/// Decrypts the next piece of the text of an incremental AES-GCM decryption (NIST SP 800-38D §7.2): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext so far, decrypts the `len` bytes of ciphertext at `data` in place, as the continuation of that ciphertext, and the state then represents the message with them appended. The plaintext is not authenticated until `vg_aes_gcm_stream_verify` has returned 1. /// -/// The function checks no length. GCM is secure only for a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of data and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must ensure; and a nonce must never be used twice with the same key. +/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.sealGatherContract`. Constant time: only the pointers, `rounds`, the lengths, `src_count` and where the slices are (their addresses and lengths) may affect timing, not the key context, the nonce, the additional data or the data. +/// Contract: `VG.Spec.Gcm.streamDecryptContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. /// -/// It runs `vg_aes_gcm_seal_gather`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_aes_gcm_stream_decrypt`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `nonce` must be valid for reads of `nonce_len` bytes. -/// * `aad` must be valid for reads of `aad_len` bytes. -/// * `src` must be valid for reads of `2 * size_of::() * src_count` bytes, and each slice it lists for reads of its length in bytes. -/// * `dst` must be valid for reads and writes of `len` bytes. -/// * `tag` must be valid for reads and writes of 16 bytes. +/// * `state` must be valid for reads and writes of 80 bytes. +/// * `data` must be valid for reads and writes of `len` bytes. /// * `rounds` must be 10, 12 or 14. -/// * The slices `src` lists must be `len` bytes long in all. -/// * `dst` and `tag` must not overlap each other, `ctx`, `nonce`, `aad`, `src`, the slices `src` lists or the arguments on the stack (distinct Rust objects never do). -/// * None of `ctx`, `nonce`, `aad`, `src`, the slices `src` lists, `dst` and `tag` may overlap the 2640 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` and `data` must not overlap each other, `ctx` or the arguments on the stack (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the 2600 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_seal_gather_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const [usize; 2], src_count: usize, dst: *mut u8, len: usize, tag: *mut [u8; 16]) { +pub unsafe fn vg_aes_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::gcm::vg_aes_gcm_seal_gather(ctx, rounds, nonce, nonce_len, aad, aad_len, src, src_count, dst, len, tag) } + unsafe { crate::gcm::vg_aes_gcm_stream_decrypt(ctx, rounds, state, aad_len, text_len, data, len) } } /// Finishes an incremental AES-GCM encryption or decryption (NIST SP 800-38D §7.1 steps 5–6, §7.2 steps 6–7): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, writes its 128-bit tag to `*tag`. A shorter tag is the first bytes of this one; to check a received tag, use `vg_aes_gcm_stream_verify`. @@ -212,48 +204,56 @@ pub unsafe fn vg_aes_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, unsafe { crate::gcm::vg_aes_gcm_stream_verify(ctx, rounds, state, aad_len, text_len, tag, tag_len) } } -/// Encrypts the next piece of the text of an incremental AES-GCM encryption (NIST SP 800-38D §7.1): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and the ciphertext of exactly `text_len` bytes of plaintext so far, encrypts the `len` bytes at `data` in place, as the continuation of that plaintext, and the state then represents the message with them appended. +/// AES-GCM authenticated encryption (NIST SP 800-38D §7.1, GCM-AE, with a 128-bit tag), out of place, of a plaintext in pieces: with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, encrypts the concatenation of the `src_count` slices that `src` lists (each an address and a length, in bytes), under the IV the `nonce_len` bytes at `nonce`, writes the ciphertext to the `len` bytes at `dst`, and writes the tag of the ciphertext and the `aad_len` bytes of additional data at `aad` to `*tag`: `vg_aes_gcm_seal` with the plaintext gathered from `src`. A shorter tag is the first bytes of this one. /// -/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// The function checks no length. GCM is secure only for a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of data and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must ensure; and a nonce must never be used twice with the same key. /// -/// Contract: `VG.Spec.Gcm.streamEncryptContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. +/// Contract: `VG.Spec.Gcm.sealGatherContract`. Constant time: only the pointers, `rounds`, the lengths, `src_count` and where the slices are (their addresses and lengths) may affect timing, not the key context, the nonce, the additional data or the data. /// -/// It runs `vg_aes_gcm_stream_encrypt`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_aes_gcm_seal_gather`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `state` must be valid for reads and writes of 80 bytes. -/// * `data` must be valid for reads and writes of `len` bytes. +/// * `nonce` must be valid for reads of `nonce_len` bytes. +/// * `aad` must be valid for reads of `aad_len` bytes. +/// * `src` must be valid for reads of `2 * size_of::() * src_count` bytes, and each slice it lists for reads of its length in bytes. +/// * `dst` must be valid for reads and writes of `len` bytes. +/// * `tag` must be valid for reads and writes of 16 bytes. /// * `rounds` must be 10, 12 or 14. -/// * `state` and `data` must not overlap each other, `ctx` or the arguments on the stack (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the 2600 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * The slices `src` lists must be `len` bytes long in all. +/// * `dst` and `tag` must not overlap each other, `ctx`, `nonce`, `aad`, `src`, the slices `src` lists or the arguments on the stack (distinct Rust objects never do). +/// * None of `ctx`, `nonce`, `aad`, `src`, the slices `src` lists, `dst` and `tag` may overlap the 2640 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_stream_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { +pub unsafe fn vg_aes_gcm_seal_gather_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const [usize; 2], src_count: usize, dst: *mut u8, len: usize, tag: *mut [u8; 16]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::gcm::vg_aes_gcm_stream_encrypt(ctx, rounds, state, aad_len, text_len, data, len) } + unsafe { crate::gcm::vg_aes_gcm_seal_gather(ctx, rounds, nonce, nonce_len, aad, aad_len, src, src_count, dst, len, tag) } } -/// Decrypts the next piece of the text of an incremental AES-GCM decryption (NIST SP 800-38D §7.2): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext so far, decrypts the `len` bytes of ciphertext at `data` in place, as the continuation of that ciphertext, and the state then represents the message with them appended. The plaintext is not authenticated until `vg_aes_gcm_stream_verify` has returned 1. +/// AES-GCM authenticated decryption (NIST SP 800-38D §7.2, GCM-AD), out of place: with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds and the received tag the `tag_len` bytes at `tag`, returns 1 if `tag_len` is between 4 and 16 and the tag is the first `tag_len` bytes of that of the `len` bytes of ciphertext at `src` and the `aad_len` bytes of additional data at `aad`, under the IV the `nonce_len` bytes at `nonce`, having then written the plaintext to the `dst_len = len` bytes at `dst`; otherwise returns 0, having written zeros to them: `vg_aes_gcm_open` with the ciphertext read from `src` and the plaintext written to `dst`. `src` is not written, and `dst` is a buffer of its own: it overlaps no other buffer, `src` included. The tags are compared without a branch. /// -/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// The function checks no other length: GCM requires a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of ciphertext and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.streamDecryptContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. +/// Contract: `VG.Spec.Gcm.openToContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the additional data, the data or the tag. /// -/// It runs `vg_aes_gcm_stream_decrypt`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_aes_gcm_open_to`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `state` must be valid for reads and writes of 80 bytes. -/// * `data` must be valid for reads and writes of `len` bytes. +/// * `nonce` must be valid for reads of `nonce_len` bytes. +/// * `aad` must be valid for reads of `aad_len` bytes. +/// * `src` must be valid for reads of `len` bytes. +/// * `dst` must be valid for reads and writes of `dst_len` bytes. +/// * `tag` must be valid for reads of `tag_len` bytes. /// * `rounds` must be 10, 12 or 14. -/// * `state` and `data` must not overlap each other, `ctx` or the arguments on the stack (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the 2600 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `dst_len` must equal `len`. +/// * `dst` must not overlap `ctx`, `nonce`, `aad`, `src`, `tag` or the arguments on the stack (distinct Rust objects never do). +/// * None of `ctx`, `nonce`, `aad`, `src`, `dst` and `tag` may overlap the 2620 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { +pub unsafe fn vg_aes_gcm_open_to_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const u8, len: usize, dst: *mut u8, dst_len: usize, tag: *const u8, tag_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::gcm::vg_aes_gcm_stream_decrypt(ctx, rounds, state, aad_len, text_len, data, len) } + unsafe { crate::gcm::vg_aes_gcm_open_to(ctx, rounds, nonce, nonce_len, aad, aad_len, src, len, dst, dst_len, tag, tag_len) } } diff --git a/crates/verified-garbage-asm-arm/src/dispatch/idea.rs b/crates/verified-garbage-asm-arm/src/dispatch/idea.rs index 40e0f4e8156..67739e531c8 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/idea.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/idea.rs @@ -2,25 +2,6 @@ //! The dispatchers of the verified `idea` functions for `arm` //! (`crate::idea`, `VG.Rust.dispatcher`). -/// IDEA in ECB mode (Lai, 1992, §3.3; SP 800-38A §6.1) on `n` complete 8-byte blocks at `data`, in place, under the 52 subkeys at `schedule`: encryption under the subkeys `vg_idea_expand_key` writes, decryption under those `vg_idea_invert_key` writes. No padding is added or removed. For `n = 0`, no data is transformed. -/// -/// Contract: `VG.Spec.Idea.ecbContract`. Constant time: only pointers and `n` may affect timing, not the subkeys or data, including words or subkeys that are 0. -/// -/// It runs `vg_idea_ecb`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `schedule` must be valid for reads of 104 bytes. -/// * `data` must be valid for reads and writes of `8 * n` bytes. -/// * `data` must not overlap `schedule` (distinct Rust objects never do). -/// * Neither `schedule` nor `data` may overlap the 32 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_idea_ecb_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *const [u8; 104], data: *mut [u8; 8], n: usize) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::idea::vg_idea_ecb(schedule, data, n) } -} - /// IDEA encryption key schedule (Lai, 1992, §3.3): writes the 52 encryption subkeys of the 16-byte key at `key` (as eight big-endian 16-bit words, rotated left by 25 bits after every eight subkeys) to `*schedule`, as little-endian 16-bit words. /// /// Contract: `VG.Spec.Idea.expandKeyContract`. Constant time: only pointers may affect timing, not the key. @@ -58,3 +39,22 @@ pub unsafe fn vg_idea_invert_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sched // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::idea::vg_idea_invert_key(schedule, inverse) } } + +/// IDEA in ECB mode (Lai, 1992, §3.3; SP 800-38A §6.1) on `n` complete 8-byte blocks at `data`, in place, under the 52 subkeys at `schedule`: encryption under the subkeys `vg_idea_expand_key` writes, decryption under those `vg_idea_invert_key` writes. No padding is added or removed. For `n = 0`, no data is transformed. +/// +/// Contract: `VG.Spec.Idea.ecbContract`. Constant time: only pointers and `n` may affect timing, not the subkeys or data, including words or subkeys that are 0. +/// +/// It runs `vg_idea_ecb`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `schedule` must be valid for reads of 104 bytes. +/// * `data` must be valid for reads and writes of `8 * n` bytes. +/// * `data` must not overlap `schedule` (distinct Rust objects never do). +/// * Neither `schedule` nor `data` may overlap the 32 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_idea_ecb_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *const [u8; 104], data: *mut [u8; 8], n: usize) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::idea::vg_idea_ecb(schedule, data, n) } +} diff --git a/crates/verified-garbage-asm-arm/src/dispatch/mldsa44.rs b/crates/verified-garbage-asm-arm/src/dispatch/mldsa44.rs index 3eaa90ff66b..3ed2b65ccd8 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/mldsa44.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/mldsa44.rs @@ -2,29 +2,27 @@ //! The dispatchers of the verified `mldsa44` functions for `arm` //! (`crate::mldsa44`, `VG.Rust.dispatcher`). -/// ML-DSA-44 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-44 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. +/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. /// -/// It runs `vg_mldsa44_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa44_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `sk` must be valid for reads of 2560 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `rnd` must be valid for reads of 32 bytes. -/// * `sig` must be valid for reads and writes of 2420 bytes. +/// * `seed` must be valid for reads of 32 bytes. +/// * `pk` must be valid for reads and writes of 1312 bytes. +/// * `sk` must be valid for reads and writes of 2560 bytes. /// * `scratch` must be valid for reads and writes of 77824 bytes. -/// * `sk` must have been written by `vg_mldsa44_keygen`. -/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. +/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `mu`, `rnd` or the arguments on the stack (distinct Rust objects never do). -/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 44 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 36 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { +pub unsafe fn vg_mldsa44_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1312], sk: *mut [u8; 2560], scratch: *mut [u64; 9728]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_sign(sk, mu, rnd, sig, scratch) } + unsafe { crate::mldsa44::vg_mldsa44_keygen(seed, pk, sk, scratch) } } /// ML-DSA-44 public-key hash: writes `tr = H(pk, 64)` (FIPS 204 Algorithm 8, line 6), SHAKE256 of the public key `*pk` to 64 bytes, to `*tr`, the digest `vg_mldsa44_verify_message_cached` takes. @@ -48,118 +46,98 @@ pub unsafe fn vg_mldsa44_pk_tr_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *co unsafe { crate::mldsa44::vg_mldsa44_pk_tr(pk, tr, scratch) } } -/// ML-DSA-44 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-44 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa44_verify_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. +/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. /// -/// It runs `vg_mldsa44_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa44_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `seed` must be valid for reads of 32 bytes. -/// * `pk` must be valid for reads and writes of 1312 bytes. -/// * `sk` must be valid for reads and writes of 2560 bytes. -/// * `scratch` must be valid for reads and writes of 77824 bytes. -/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. +/// * `pk` must be valid for reads of 1312 bytes. +/// * `prepared` must be valid for reads and writes of 5408 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 36 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). +/// * None of `pk`, `prepared` and `scratch` may overlap the 44 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1312], sk: *mut [u8; 2560], scratch: *mut [u64; 9728]) -> u32 { +pub unsafe fn vg_mldsa44_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], prepared: *mut [u8; 5408], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_keygen(seed, pk, sk, scratch) } + unsafe { crate::mldsa44::vg_mldsa44_prepare_verifying_key(pk, prepared, scratch) } } -/// ML-DSA-44 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// ML-DSA-44 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa44_sign_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. /// -/// It runs `vg_mldsa44_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa44_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `pk` must be valid for reads of 1312 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `sig` must be valid for reads of 2420 bytes. -/// * `scratch` must be valid for reads and writes of 77824 bytes. +/// * `sk` must be valid for reads of 2560 bytes. +/// * `prepared` must be valid for reads and writes of 14848 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). -/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 52 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). +/// * None of `sk`, `prepared` and `scratch` may overlap the 44 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], mu: *const [u8; 64], sig: *const [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { +pub unsafe fn vg_mldsa44_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], prepared: *mut [u8; 14848], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_verify(pk, mu, sig, scratch) } + unsafe { crate::mldsa44::vg_mldsa44_prepare_signing_key(sk, prepared, scratch) } } -/// ML-DSA-44 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. +/// ML-DSA-44 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. +/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. /// -/// It runs `vg_mldsa44_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa44_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `sk` must be valid for reads of 2560 bytes. -/// * `msg` must be valid for reads of `msg_len` bytes. -/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `mu` must be valid for reads of 64 bytes. /// * `rnd` must be valid for reads of 32 bytes. /// * `sig` must be valid for reads and writes of 2420 bytes. -/// * `scratch` must be valid for reads and writes of 78848 bytes. +/// * `scratch` must be valid for reads and writes of 77824 bytes. /// * `sk` must have been written by `vg_mldsa44_keygen`. /// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx`, `rnd` or the arguments on the stack (distinct Rust objects never do). -/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the 52 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `mu`, `rnd` or the arguments on the stack (distinct Rust objects never do). +/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 44 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9856]) -> u32 { +pub unsafe fn vg_mldsa44_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { crate::mldsa44::vg_mldsa44_sign(sk, mu, rnd, sig, scratch) } } -/// ML-DSA-44 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa44_sign_message_prepared`. +/// ML-DSA-44 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. /// -/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. +/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. /// -/// It runs `vg_mldsa44_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa44_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `sk` must be valid for reads of 2560 bytes. -/// * `prepared` must be valid for reads and writes of 14848 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. -/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). -/// * None of `sk`, `prepared` and `scratch` may overlap the 44 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mldsa44_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], prepared: *mut [u8; 14848], scratch: *mut [u64; 128]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_prepare_signing_key(sk, prepared, scratch) } -} - -/// ML-DSA-44 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa44_verify_message_prepared`. -/// -/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. -/// -/// It runs `vg_mldsa44_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 1312 bytes. -/// * `prepared` must be valid for reads and writes of 5408 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `msg` must be valid for reads of `msg_len` bytes. +/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `rnd` must be valid for reads of 32 bytes. +/// * `sig` must be valid for reads and writes of 2420 bytes. +/// * `scratch` must be valid for reads and writes of 78848 bytes. +/// * `sk` must have been written by `vg_mldsa44_keygen`. +/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). -/// * None of `pk`, `prepared` and `scratch` may overlap the 44 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx`, `rnd` or the arguments on the stack (distinct Rust objects never do). +/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the 52 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], prepared: *mut [u8; 5408], scratch: *mut [u64; 128]) { +pub unsafe fn vg_mldsa44_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9856]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_prepare_verifying_key(pk, prepared, scratch) } + unsafe { crate::mldsa44::vg_mldsa44_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } /// ML-DSA-44 signing with a prepared private key: `vg_mldsa44_sign_message` (with its results, timing and leakage) of the private key that `*sk` begins with. @@ -189,6 +167,28 @@ pub unsafe fn vg_mldsa44_sign_message_prepared_auto(_dit: verified_garbage_cpu:: unsafe { crate::mldsa44::vg_mldsa44_sign_message_prepared(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } +/// ML-DSA-44 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// +/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// +/// It runs `vg_mldsa44_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 1312 bytes. +/// * `mu` must be valid for reads of 64 bytes. +/// * `sig` must be valid for reads of 2420 bytes. +/// * `scratch` must be valid for reads and writes of 77824 bytes. +/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). +/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). +/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 52 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mldsa44_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], mu: *const [u8; 64], sig: *const [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mldsa44::vg_mldsa44_verify(pk, mu, sig, scratch) } +} + /// ML-DSA-44 message verification with a cached public-key digest. Returns 1 for a valid signature, 0 for an invalid signature or exhausted sampling bound, and 2 if the context exceeds 255 bytes. It is `ML-DSA.Verify(pk, M, σ, ctx)` (FIPS 204 Algorithm 3) with `H(pk, 64)` read from `*tr`: with the public key `*pk`, the `msg_len` bytes of the message at `msg`, the `ctx_len` bytes of the context string at `ctx` and the signature `*sig`, it formats the message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 5), computes `μ = H(tr ‖ M′, 64)`, and verifies `*sig` on `μ` with `*pk`, `ML-DSA.Verify_internal(pk, M′, σ)` (Algorithm 8). /// /// Contract: `VG.Spec.MlDsa.verifyMessageCachedContract`. Timing may depend on the public key, message, context string and signature, and on argument addresses and lengths. diff --git a/crates/verified-garbage-asm-arm/src/dispatch/mldsa65.rs b/crates/verified-garbage-asm-arm/src/dispatch/mldsa65.rs index fc21305c0a0..611fefc631e 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/mldsa65.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/mldsa65.rs @@ -2,29 +2,27 @@ //! The dispatchers of the verified `mldsa65` functions for `arm` //! (`crate::mldsa65`, `VG.Rust.dispatcher`). -/// ML-DSA-65 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-65 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. +/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. /// -/// It runs `vg_mldsa65_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa65_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `sk` must be valid for reads of 4032 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `rnd` must be valid for reads of 32 bytes. -/// * `sig` must be valid for reads and writes of 3309 bytes. +/// * `seed` must be valid for reads of 32 bytes. +/// * `pk` must be valid for reads and writes of 1952 bytes. +/// * `sk` must be valid for reads and writes of 4032 bytes. /// * `scratch` must be valid for reads and writes of 103424 bytes. -/// * `sk` must have been written by `vg_mldsa65_keygen`. -/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. +/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `mu`, `rnd` or the arguments on the stack (distinct Rust objects never do). -/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 44 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 36 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { +pub unsafe fn vg_mldsa65_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1952], sk: *mut [u8; 4032], scratch: *mut [u64; 12928]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_sign(sk, mu, rnd, sig, scratch) } + unsafe { crate::mldsa65::vg_mldsa65_keygen(seed, pk, sk, scratch) } } /// ML-DSA-65 public-key hash: writes `tr = H(pk, 64)` (FIPS 204 Algorithm 8, line 6), SHAKE256 of the public key `*pk` to 64 bytes, to `*tr`, the digest `vg_mldsa65_verify_message_cached` takes. @@ -48,118 +46,98 @@ pub unsafe fn vg_mldsa65_pk_tr_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *co unsafe { crate::mldsa65::vg_mldsa65_pk_tr(pk, tr, scratch) } } -/// ML-DSA-65 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-65 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa65_verify_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. +/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. /// -/// It runs `vg_mldsa65_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa65_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `seed` must be valid for reads of 32 bytes. -/// * `pk` must be valid for reads and writes of 1952 bytes. -/// * `sk` must be valid for reads and writes of 4032 bytes. -/// * `scratch` must be valid for reads and writes of 103424 bytes. -/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. +/// * `pk` must be valid for reads of 1952 bytes. +/// * `prepared` must be valid for reads and writes of 8096 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 36 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). +/// * None of `pk`, `prepared` and `scratch` may overlap the 44 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1952], sk: *mut [u8; 4032], scratch: *mut [u64; 12928]) -> u32 { +pub unsafe fn vg_mldsa65_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], prepared: *mut [u8; 8096], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_keygen(seed, pk, sk, scratch) } + unsafe { crate::mldsa65::vg_mldsa65_prepare_verifying_key(pk, prepared, scratch) } } -/// ML-DSA-65 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// ML-DSA-65 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa65_sign_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. /// -/// It runs `vg_mldsa65_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa65_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `pk` must be valid for reads of 1952 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `sig` must be valid for reads of 3309 bytes. -/// * `scratch` must be valid for reads and writes of 103424 bytes. +/// * `sk` must be valid for reads of 4032 bytes. +/// * `prepared` must be valid for reads and writes of 21440 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). -/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 52 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). +/// * None of `sk`, `prepared` and `scratch` may overlap the 44 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], mu: *const [u8; 64], sig: *const [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { +pub unsafe fn vg_mldsa65_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], prepared: *mut [u8; 21440], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_verify(pk, mu, sig, scratch) } + unsafe { crate::mldsa65::vg_mldsa65_prepare_signing_key(sk, prepared, scratch) } } -/// ML-DSA-65 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. +/// ML-DSA-65 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. +/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. /// -/// It runs `vg_mldsa65_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa65_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `sk` must be valid for reads of 4032 bytes. -/// * `msg` must be valid for reads of `msg_len` bytes. -/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `mu` must be valid for reads of 64 bytes. /// * `rnd` must be valid for reads of 32 bytes. /// * `sig` must be valid for reads and writes of 3309 bytes. -/// * `scratch` must be valid for reads and writes of 104448 bytes. +/// * `scratch` must be valid for reads and writes of 103424 bytes. /// * `sk` must have been written by `vg_mldsa65_keygen`. /// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx`, `rnd` or the arguments on the stack (distinct Rust objects never do). -/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the 52 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `mu`, `rnd` or the arguments on the stack (distinct Rust objects never do). +/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 44 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 13056]) -> u32 { +pub unsafe fn vg_mldsa65_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { crate::mldsa65::vg_mldsa65_sign(sk, mu, rnd, sig, scratch) } } -/// ML-DSA-65 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa65_sign_message_prepared`. +/// ML-DSA-65 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. /// -/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. +/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. /// -/// It runs `vg_mldsa65_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa65_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `sk` must be valid for reads of 4032 bytes. -/// * `prepared` must be valid for reads and writes of 21440 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. -/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). -/// * None of `sk`, `prepared` and `scratch` may overlap the 44 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mldsa65_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], prepared: *mut [u8; 21440], scratch: *mut [u64; 128]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_prepare_signing_key(sk, prepared, scratch) } -} - -/// ML-DSA-65 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa65_verify_message_prepared`. -/// -/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. -/// -/// It runs `vg_mldsa65_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 1952 bytes. -/// * `prepared` must be valid for reads and writes of 8096 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `msg` must be valid for reads of `msg_len` bytes. +/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `rnd` must be valid for reads of 32 bytes. +/// * `sig` must be valid for reads and writes of 3309 bytes. +/// * `scratch` must be valid for reads and writes of 104448 bytes. +/// * `sk` must have been written by `vg_mldsa65_keygen`. +/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). -/// * None of `pk`, `prepared` and `scratch` may overlap the 44 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx`, `rnd` or the arguments on the stack (distinct Rust objects never do). +/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the 52 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], prepared: *mut [u8; 8096], scratch: *mut [u64; 128]) { +pub unsafe fn vg_mldsa65_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 13056]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_prepare_verifying_key(pk, prepared, scratch) } + unsafe { crate::mldsa65::vg_mldsa65_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } /// ML-DSA-65 signing with a prepared private key: `vg_mldsa65_sign_message` (with its results, timing and leakage) of the private key that `*sk` begins with. @@ -189,6 +167,28 @@ pub unsafe fn vg_mldsa65_sign_message_prepared_auto(_dit: verified_garbage_cpu:: unsafe { crate::mldsa65::vg_mldsa65_sign_message_prepared(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } +/// ML-DSA-65 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// +/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// +/// It runs `vg_mldsa65_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 1952 bytes. +/// * `mu` must be valid for reads of 64 bytes. +/// * `sig` must be valid for reads of 3309 bytes. +/// * `scratch` must be valid for reads and writes of 103424 bytes. +/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). +/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). +/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 52 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mldsa65_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], mu: *const [u8; 64], sig: *const [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mldsa65::vg_mldsa65_verify(pk, mu, sig, scratch) } +} + /// ML-DSA-65 message verification with a cached public-key digest. Returns 1 for a valid signature, 0 for an invalid signature or exhausted sampling bound, and 2 if the context exceeds 255 bytes. It is `ML-DSA.Verify(pk, M, σ, ctx)` (FIPS 204 Algorithm 3) with `H(pk, 64)` read from `*tr`: with the public key `*pk`, the `msg_len` bytes of the message at `msg`, the `ctx_len` bytes of the context string at `ctx` and the signature `*sig`, it formats the message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 5), computes `μ = H(tr ‖ M′, 64)`, and verifies `*sig` on `μ` with `*pk`, `ML-DSA.Verify_internal(pk, M′, σ)` (Algorithm 8). /// /// Contract: `VG.Spec.MlDsa.verifyMessageCachedContract`. Timing may depend on the public key, message, context string and signature, and on argument addresses and lengths. diff --git a/crates/verified-garbage-asm-arm/src/dispatch/mldsa87.rs b/crates/verified-garbage-asm-arm/src/dispatch/mldsa87.rs index 83fc3077833..660ca633731 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/mldsa87.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/mldsa87.rs @@ -2,29 +2,27 @@ //! The dispatchers of the verified `mldsa87` functions for `arm` //! (`crate::mldsa87`, `VG.Rust.dispatcher`). -/// ML-DSA-87 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-87 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. +/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. /// -/// It runs `vg_mldsa87_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa87_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `sk` must be valid for reads of 4896 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `rnd` must be valid for reads of 32 bytes. -/// * `sig` must be valid for reads and writes of 4627 bytes. +/// * `seed` must be valid for reads of 32 bytes. +/// * `pk` must be valid for reads and writes of 2592 bytes. +/// * `sk` must be valid for reads and writes of 4896 bytes. /// * `scratch` must be valid for reads and writes of 144384 bytes. -/// * `sk` must have been written by `vg_mldsa87_keygen`. -/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. +/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `mu`, `rnd` or the arguments on the stack (distinct Rust objects never do). -/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 44 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 36 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { +pub unsafe fn vg_mldsa87_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 2592], sk: *mut [u8; 4896], scratch: *mut [u64; 18048]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_sign(sk, mu, rnd, sig, scratch) } + unsafe { crate::mldsa87::vg_mldsa87_keygen(seed, pk, sk, scratch) } } /// ML-DSA-87 public-key hash: writes `tr = H(pk, 64)` (FIPS 204 Algorithm 8, line 6), SHAKE256 of the public key `*pk` to 64 bytes, to `*tr`, the digest `vg_mldsa87_verify_message_cached` takes. @@ -48,118 +46,98 @@ pub unsafe fn vg_mldsa87_pk_tr_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *co unsafe { crate::mldsa87::vg_mldsa87_pk_tr(pk, tr, scratch) } } -/// ML-DSA-87 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-87 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa87_verify_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. +/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. /// -/// It runs `vg_mldsa87_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa87_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `seed` must be valid for reads of 32 bytes. -/// * `pk` must be valid for reads and writes of 2592 bytes. -/// * `sk` must be valid for reads and writes of 4896 bytes. -/// * `scratch` must be valid for reads and writes of 144384 bytes. -/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. +/// * `pk` must be valid for reads of 2592 bytes. +/// * `prepared` must be valid for reads and writes of 10784 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 36 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). +/// * None of `pk`, `prepared` and `scratch` may overlap the 44 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 2592], sk: *mut [u8; 4896], scratch: *mut [u64; 18048]) -> u32 { +pub unsafe fn vg_mldsa87_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], prepared: *mut [u8; 10784], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_keygen(seed, pk, sk, scratch) } + unsafe { crate::mldsa87::vg_mldsa87_prepare_verifying_key(pk, prepared, scratch) } } -/// ML-DSA-87 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// ML-DSA-87 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa87_sign_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. /// -/// It runs `vg_mldsa87_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa87_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `pk` must be valid for reads of 2592 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `sig` must be valid for reads of 4627 bytes. -/// * `scratch` must be valid for reads and writes of 144384 bytes. +/// * `sk` must be valid for reads of 4896 bytes. +/// * `prepared` must be valid for reads and writes of 28448 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). -/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 52 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). +/// * None of `sk`, `prepared` and `scratch` may overlap the 44 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], mu: *const [u8; 64], sig: *const [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { +pub unsafe fn vg_mldsa87_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], prepared: *mut [u8; 28448], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_verify(pk, mu, sig, scratch) } + unsafe { crate::mldsa87::vg_mldsa87_prepare_signing_key(sk, prepared, scratch) } } -/// ML-DSA-87 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. +/// ML-DSA-87 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. +/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. /// -/// It runs `vg_mldsa87_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa87_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `sk` must be valid for reads of 4896 bytes. -/// * `msg` must be valid for reads of `msg_len` bytes. -/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `mu` must be valid for reads of 64 bytes. /// * `rnd` must be valid for reads of 32 bytes. /// * `sig` must be valid for reads and writes of 4627 bytes. -/// * `scratch` must be valid for reads and writes of 145408 bytes. +/// * `scratch` must be valid for reads and writes of 144384 bytes. /// * `sk` must have been written by `vg_mldsa87_keygen`. /// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx`, `rnd` or the arguments on the stack (distinct Rust objects never do). -/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the 52 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `mu`, `rnd` or the arguments on the stack (distinct Rust objects never do). +/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 44 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18176]) -> u32 { +pub unsafe fn vg_mldsa87_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { crate::mldsa87::vg_mldsa87_sign(sk, mu, rnd, sig, scratch) } } -/// ML-DSA-87 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa87_sign_message_prepared`. +/// ML-DSA-87 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. /// -/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. +/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. /// -/// It runs `vg_mldsa87_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa87_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `sk` must be valid for reads of 4896 bytes. -/// * `prepared` must be valid for reads and writes of 28448 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. -/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). -/// * None of `sk`, `prepared` and `scratch` may overlap the 44 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mldsa87_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], prepared: *mut [u8; 28448], scratch: *mut [u64; 128]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_prepare_signing_key(sk, prepared, scratch) } -} - -/// ML-DSA-87 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa87_verify_message_prepared`. -/// -/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. -/// -/// It runs `vg_mldsa87_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 2592 bytes. -/// * `prepared` must be valid for reads and writes of 10784 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `msg` must be valid for reads of `msg_len` bytes. +/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `rnd` must be valid for reads of 32 bytes. +/// * `sig` must be valid for reads and writes of 4627 bytes. +/// * `scratch` must be valid for reads and writes of 145408 bytes. +/// * `sk` must have been written by `vg_mldsa87_keygen`. +/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). -/// * None of `pk`, `prepared` and `scratch` may overlap the 44 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx`, `rnd` or the arguments on the stack (distinct Rust objects never do). +/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the 52 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], prepared: *mut [u8; 10784], scratch: *mut [u64; 128]) { +pub unsafe fn vg_mldsa87_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18176]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_prepare_verifying_key(pk, prepared, scratch) } + unsafe { crate::mldsa87::vg_mldsa87_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } /// ML-DSA-87 signing with a prepared private key: `vg_mldsa87_sign_message` (with its results, timing and leakage) of the private key that `*sk` begins with. @@ -189,6 +167,28 @@ pub unsafe fn vg_mldsa87_sign_message_prepared_auto(_dit: verified_garbage_cpu:: unsafe { crate::mldsa87::vg_mldsa87_sign_message_prepared(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } +/// ML-DSA-87 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// +/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// +/// It runs `vg_mldsa87_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 2592 bytes. +/// * `mu` must be valid for reads of 64 bytes. +/// * `sig` must be valid for reads of 4627 bytes. +/// * `scratch` must be valid for reads and writes of 144384 bytes. +/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). +/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). +/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 52 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mldsa87_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], mu: *const [u8; 64], sig: *const [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mldsa87::vg_mldsa87_verify(pk, mu, sig, scratch) } +} + /// ML-DSA-87 message verification with a cached public-key digest. Returns 1 for a valid signature, 0 for an invalid signature or exhausted sampling bound, and 2 if the context exceeds 255 bytes. It is `ML-DSA.Verify(pk, M, σ, ctx)` (FIPS 204 Algorithm 3) with `H(pk, 64)` read from `*tr`: with the public key `*pk`, the `msg_len` bytes of the message at `msg`, the `ctx_len` bytes of the context string at `ctx` and the signature `*sig`, it formats the message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 5), computes `μ = H(tr ‖ M′, 64)`, and verifies `*sig` on `μ` with `*pk`, `ML-DSA.Verify_internal(pk, M′, σ)` (Algorithm 8). /// /// Contract: `VG.Spec.MlDsa.verifyMessageCachedContract`. Timing may depend on the public key, message, context string and signature, and on argument addresses and lengths. diff --git a/crates/verified-garbage-asm-arm/src/dispatch/mlkem1024.rs b/crates/verified-garbage-asm-arm/src/dispatch/mlkem1024.rs index d9c454eab22..6a7b1193cb4 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/mlkem1024.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/mlkem1024.rs @@ -2,6 +2,23 @@ //! The dispatchers of the verified `mlkem1024` functions for `arm` //! (`crate::mlkem1024`, `VG.Rust.dispatcher`). +/// The ML-KEM-1024 encapsulation key check (FIPS 203 §7.2): returns 1 if every 12-bit integer that the first 1536 bytes of `*ek` encode is less than `q` = 3329 (the modulus check), and 0 otherwise. An encapsulation key must pass it before it is given to `vg_mlkem1024_encaps`. +/// +/// Contract: `VG.Spec.MlKem1024.checkEkContract`. Constant time: only the pointer may affect timing. +/// +/// It runs `vg_mlkem1024_check_ek`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `ek` must be valid for reads of 1568 bytes. +/// * `ek` must not wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mlkem1024_check_ek_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1568]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mlkem1024::vg_mlkem1024_check_ek(ek) } +} + /// ML-KEM-1024 key generation from a seed, `ML-KEM.KeyGen_internal(d, z)` (FIPS 203 Algorithm 16): with `d` in bytes 0–31 of `*seed` and `z` in bytes 32–63, writes the encapsulation key to `*ek` and the decapsulation key to `*dk`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// /// Contract: `VG.Spec.MlKem1024.keyGenContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of the encapsulation key), but not on anything else of the seed or the keys. @@ -50,46 +67,6 @@ pub unsafe fn vg_mlkem1024_encaps_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: unsafe { crate::mlkem1024::vg_mlkem1024_encaps(ek, m, key, ct, scratch) } } -/// ML-KEM-1024 decapsulation, `ML-KEM.Decaps_internal(dk, c)` (FIPS 203 Algorithm 18): with the decapsulation key `*dk` and the ciphertext `*ct`, writes the shared secret key to `*key`, which is the implicit rejection key `J(z ‖ c)` if the ciphertext does not re-encrypt to itself. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. -/// -/// Contract: `VG.Spec.MlKem1024.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 3072–3103 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. -/// -/// It runs `vg_mlkem1024_decaps`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `dk` must be valid for reads of 3168 bytes. -/// * `ct` must be valid for reads of 1568 bytes. -/// * `key` must be valid for reads and writes of 32 bytes. -/// * `scratch` must be valid for reads and writes of 49152 bytes. -/// * `dk` must have been written by `vg_mlkem1024_keygen` (so that it passes the checks of FIPS 203 §7.3). -/// * `scratch` is working space: on return each of its bytes is unchanged or zero. -/// * `key` and `scratch` must not overlap each other, `dk` or `ct` (distinct Rust objects never do). -/// * None of `dk`, `ct`, `key` and `scratch` may overlap the 24 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mlkem1024_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: *const [u8; 3168], ct: *const [u8; 1568], key: *mut [u8; 32], scratch: *mut [u64; 6144]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mlkem1024::vg_mlkem1024_decaps(dk, ct, key, scratch) } -} - -/// The ML-KEM-1024 encapsulation key check (FIPS 203 §7.2): returns 1 if every 12-bit integer that the first 1536 bytes of `*ek` encode is less than `q` = 3329 (the modulus check), and 0 otherwise. An encapsulation key must pass it before it is given to `vg_mlkem1024_encaps`. -/// -/// Contract: `VG.Spec.MlKem1024.checkEkContract`. Constant time: only the pointer may affect timing. -/// -/// It runs `vg_mlkem1024_check_ek`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `ek` must be valid for reads of 1568 bytes. -/// * `ek` must not wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mlkem1024_check_ek_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1568]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mlkem1024::vg_mlkem1024_check_ek(ek) } -} - /// ML-KEM-1024 encapsulation with given randomness and the hash of the key, `ML-KEM.Encaps_internal(ek, m)` (FIPS 203 Algorithm 17): with the encapsulation key `*ek`, its hash `H(ek)` in `*h` and the randomness `*m`, writes the shared secret key to `*key` and the ciphertext to `*ct`, as `vg_mlkem1024_encaps` does, using `*h` instead of hashing `*ek`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// /// Contract: `VG.Spec.MlKem1024.encapsHContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on its hash, on the randomness or on the outputs. @@ -116,3 +93,26 @@ pub unsafe fn vg_mlkem1024_encaps_h_auto(_dit: verified_garbage_cpu::Dit<'_>, ek // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::mlkem1024::vg_mlkem1024_encaps_h(ek, h, m, key, ct, scratch) } } + +/// ML-KEM-1024 decapsulation, `ML-KEM.Decaps_internal(dk, c)` (FIPS 203 Algorithm 18): with the decapsulation key `*dk` and the ciphertext `*ct`, writes the shared secret key to `*key`, which is the implicit rejection key `J(z ‖ c)` if the ciphertext does not re-encrypt to itself. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// +/// Contract: `VG.Spec.MlKem1024.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 3072–3103 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. +/// +/// It runs `vg_mlkem1024_decaps`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `dk` must be valid for reads of 3168 bytes. +/// * `ct` must be valid for reads of 1568 bytes. +/// * `key` must be valid for reads and writes of 32 bytes. +/// * `scratch` must be valid for reads and writes of 49152 bytes. +/// * `dk` must have been written by `vg_mlkem1024_keygen` (so that it passes the checks of FIPS 203 §7.3). +/// * `scratch` is working space: on return each of its bytes is unchanged or zero. +/// * `key` and `scratch` must not overlap each other, `dk` or `ct` (distinct Rust objects never do). +/// * None of `dk`, `ct`, `key` and `scratch` may overlap the 24 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mlkem1024_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: *const [u8; 3168], ct: *const [u8; 1568], key: *mut [u8; 32], scratch: *mut [u64; 6144]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mlkem1024::vg_mlkem1024_decaps(dk, ct, key, scratch) } +} diff --git a/crates/verified-garbage-asm-arm/src/dispatch/mlkem768.rs b/crates/verified-garbage-asm-arm/src/dispatch/mlkem768.rs index 433560a24dd..01ed25d2272 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/mlkem768.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/mlkem768.rs @@ -50,29 +50,6 @@ pub unsafe fn vg_mlkem768_encaps_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: * unsafe { crate::mlkem768::vg_mlkem768_encaps(ek, m, key, ct, scratch) } } -/// ML-KEM-768 decapsulation, `ML-KEM.Decaps_internal(dk, c)` (FIPS 203 Algorithm 18): with the decapsulation key `*dk` and the ciphertext `*ct`, writes the shared secret key to `*key`, which is the implicit rejection key `J(z ‖ c)` if the ciphertext does not re-encrypt to itself. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. -/// -/// Contract: `VG.Spec.MlKem.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 2304–2335 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. -/// -/// It runs `vg_mlkem768_decaps`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `dk` must be valid for reads of 2400 bytes. -/// * `ct` must be valid for reads of 1088 bytes. -/// * `key` must be valid for reads and writes of 32 bytes. -/// * `scratch` must be valid for reads and writes of 32768 bytes. -/// * `dk` must have been written by `vg_mlkem768_keygen` (so that it passes the checks of FIPS 203 §7.3). -/// * `scratch` is working space: on return each of its bytes is unchanged or zero. -/// * `key` and `scratch` must not overlap each other, `dk` or `ct` (distinct Rust objects never do). -/// * None of `dk`, `ct`, `key` and `scratch` may overlap the 24 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mlkem768_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: *const [u8; 2400], ct: *const [u8; 1088], key: *mut [u8; 32], scratch: *mut [u64; 4096]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mlkem768::vg_mlkem768_decaps(dk, ct, key, scratch) } -} - /// ML-KEM-768 encapsulation with given randomness and the hash of the key, `ML-KEM.Encaps_internal(ek, m)` (FIPS 203 Algorithm 17): with the encapsulation key `*ek`, its hash `H(ek)` in `*h` and the randomness `*m`, writes the shared secret key to `*key` and the ciphertext to `*ct`, as `vg_mlkem768_encaps` does, using `*h` instead of hashing `*ek`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// /// Contract: `VG.Spec.MlKem.encapsHContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on its hash, on the randomness or on the outputs. @@ -100,6 +77,29 @@ pub unsafe fn vg_mlkem768_encaps_h_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: unsafe { crate::mlkem768::vg_mlkem768_encaps_h(ek, h, m, key, ct, scratch) } } +/// ML-KEM-768 decapsulation, `ML-KEM.Decaps_internal(dk, c)` (FIPS 203 Algorithm 18): with the decapsulation key `*dk` and the ciphertext `*ct`, writes the shared secret key to `*key`, which is the implicit rejection key `J(z ‖ c)` if the ciphertext does not re-encrypt to itself. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// +/// Contract: `VG.Spec.MlKem.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 2304–2335 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. +/// +/// It runs `vg_mlkem768_decaps`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `dk` must be valid for reads of 2400 bytes. +/// * `ct` must be valid for reads of 1088 bytes. +/// * `key` must be valid for reads and writes of 32 bytes. +/// * `scratch` must be valid for reads and writes of 32768 bytes. +/// * `dk` must have been written by `vg_mlkem768_keygen` (so that it passes the checks of FIPS 203 §7.3). +/// * `scratch` is working space: on return each of its bytes is unchanged or zero. +/// * `key` and `scratch` must not overlap each other, `dk` or `ct` (distinct Rust objects never do). +/// * None of `dk`, `ct`, `key` and `scratch` may overlap the 24 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mlkem768_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: *const [u8; 2400], ct: *const [u8; 1088], key: *mut [u8; 32], scratch: *mut [u64; 4096]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mlkem768::vg_mlkem768_decaps(dk, ct, key, scratch) } +} + /// The ML-KEM-768 encapsulation key check (FIPS 203 §7.2): returns 1 if every 12-bit integer that the first 1152 bytes of `*ek` encode is less than `q` = 3329 (the modulus check), and 0 otherwise. An encapsulation key must pass it before it is given to `vg_mlkem768_encaps`. /// /// Contract: `VG.Spec.MlKem.checkEkContract`. Constant time: only the pointer may affect timing. diff --git a/crates/verified-garbage-asm-arm/src/dispatch/rsa.rs b/crates/verified-garbage-asm-arm/src/dispatch/rsa.rs index 513e031c92e..c028874382a 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/rsa.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/rsa.rs @@ -2,6 +2,37 @@ //! The dispatchers of the verified `rsa` functions for `arm` //! (`crate::rsa`, `VG.Rust.dispatcher`). +/// The CRT values of an RSA private key `(p, q, d)` (SP 800-56B Rev. 2 §6.2.2's prime-factor format), which bring it to the CRT format `(p, q, dP, dQ, qInv)` that `vg_rsa_private_checked` takes: `dP = d mod (p - 1)`, `dQ = d mod (q - 1)` and `qInv = q⁻¹ mod p` (§6.2.2's CRT format). With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), `p` (`p_len` bytes), `q` (`q_len` bytes) and `d` (`d_len` bytes), all most significant first, writes `dP` to `dp` and `qInv` to `qinv` (`p_len` bytes each) and `dQ` to `dq` (`q_len` bytes), most significant first, and returns 1; or writes zeros to all three and returns 0 if `n` is not such a modulus, `p q ≠ n`, or `q` has no inverse modulo `p`. Nothing checks that `p` and `q` are prime or that `d` is the key's private exponent. +/// +/// Contract: `VG.Spec.Rsa.crtValuesContract`. Constant time but for the modulus: timing may depend on the pointers, the lengths and the contents of `n`, not on the private key. +/// +/// It runs `vg_rsa_crt_values`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `dp` must be valid for reads and writes of `dp_len` bytes. +/// * `dq` must be valid for reads and writes of `dq_len` bytes. +/// * `qinv` must be valid for reads and writes of `qinv_len` bytes. +/// * `n` must be valid for reads of `n_len` bytes. +/// * `p` must be valid for reads of `p_len` bytes. +/// * `q` must be valid for reads of `q_len` bytes. +/// * `d` must be valid for reads of `d_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `n_len` must be in 64..=1024. +/// * `p_len` and `q_len` must be in 1..`n_len`. +/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. +/// * `d_len` must be in 1..=`n_len`. +/// * `scratch_len` must be at least `16 * n_len + 32`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `dp`, `dq`, `qinv` and `scratch` must not overlap each other, `n`, `p`, `q`, `d` or the arguments on the stack (distinct Rust objects never do). +/// * None of `dp`, `dq`, `qinv`, `n`, `p`, `q`, `d` and `scratch` may wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_rsa_crt_values_auto(_dit: verified_garbage_cpu::Dit<'_>, dp: *mut u8, dp_len: usize, dq: *mut u8, dq_len: usize, qinv: *mut u8, qinv_len: usize, n: *const u8, n_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, d: *const u8, d_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::rsa::vg_rsa_crt_values(dp, dp_len, dq, dq_len, qinv, qinv_len, n, n_len, p, p_len, q, q_len, d, d_len, scratch, scratch_len) } +} + /// Checks an RSA private key `(n, e, d, p, q, dP, dQ, qInv)` exactly as BoringSSL's `RSA_check_key` does. With the modulus `n` (`n_len` bytes), the public exponent `e` (`e_len` bytes), the private exponent `d` (`d_len` bytes), `p`, `dp` and `qinv` (`p_len` bytes) and `q` and `dq` (`q_len` bytes), all most significant first, returns 1 if `n` is odd, from 512 to 8192 bits and its first byte not zero, `e` is odd and from 3 to `2^33 - 1`, `d < n`, `p < n`, `q < n`, `p q = n`, `d e ≡ 1 (mod p - 1)`, `d e ≡ 1 (mod q - 1)`, `dP < p - 1`, `e dP ≡ 1 (mod p - 1)`, `dQ < q - 1`, `e dQ ≡ 1 (mod q - 1)`, `qInv < p` and `q qInv ≡ 1 (mod p)`; and 0 otherwise. Like `RSA_check_key`, it does not check that `p` and `q` are prime; if they are, `vg_rsa_private_checked` never returns its internal error for a key it accepts. /// /// Contract: `VG.Spec.Rsa.checkKeyContract`. Constant time but for the public key: timing may depend on the pointers, the lengths and the contents of `n` and `e`, not on the private key. @@ -34,42 +65,40 @@ pub unsafe fn vg_rsa_check_key_auto(_dit: verified_garbage_cpu::Dit<'_>, n: *con unsafe { crate::rsa::vg_rsa_check_key(n, n_len, e, e_len, d, d_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, scratch, scratch_len) } } -/// The CRT values of an RSA private key `(p, q, d)` (SP 800-56B Rev. 2 §6.2.2's prime-factor format), which bring it to the CRT format `(p, q, dP, dQ, qInv)` that `vg_rsa_private_checked` takes: `dP = d mod (p - 1)`, `dQ = d mod (q - 1)` and `qInv = q⁻¹ mod p` (§6.2.2's CRT format). With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), `p` (`p_len` bytes), `q` (`q_len` bytes) and `d` (`d_len` bytes), all most significant first, writes `dP` to `dp` and `qInv` to `qinv` (`p_len` bytes each) and `dQ` to `dq` (`q_len` bytes), most significant first, and returns 1; or writes zeros to all three and returns 0 if `n` is not such a modulus, `p q ≠ n`, or `q` has no inverse modulo `p`. Nothing checks that `p` and `q` are prime or that `d` is the key's private exponent. +/// The prime factors `p > q` of the modulus of an RSA private key `(n, e, d)` (SP 800-56B Rev. 2 §6.2.2's basic format), which bring it to the prime-factor format `(p, q, d)` that `vg_rsa_crt_values` takes, recovered by SP 800-56B Rev. 2 Appendix C.2's deterministic method. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes) and the private exponent `d` (`d_len` bytes), both most significant first, writes `p` and `q` (`n_len` bytes each, most significant first) and returns 1; or writes zeros to both and returns 0 if `n` is not such a modulus or the method fails. It finds the factors of a valid two-prime key whenever `K (p + q − 1) ≤ n` for `a = K φ(n)` (Appendix C.2's step 1): with the least private exponent, whenever the smaller prime exceeds `e²`, and in practice for every key whose primes are not tiny. /// -/// Contract: `VG.Spec.Rsa.crtValuesContract`. Constant time but for the modulus: timing may depend on the pointers, the lengths and the contents of `n`, not on the private key. +/// Contract: `VG.Spec.Rsa.recoverPrimesDetContract`. Constant time but for the public key: timing may depend on the pointers, the lengths and the contents of `n` and `e`, not on `d`, the factors, or whether they were found. /// -/// It runs `vg_rsa_crt_values`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_rsa_recover_primes_det`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `dp` must be valid for reads and writes of `dp_len` bytes. -/// * `dq` must be valid for reads and writes of `dq_len` bytes. -/// * `qinv` must be valid for reads and writes of `qinv_len` bytes. +/// * `p` must be valid for reads and writes of `p_len` bytes. +/// * `q` must be valid for reads and writes of `q_len` bytes. /// * `n` must be valid for reads of `n_len` bytes. -/// * `p` must be valid for reads of `p_len` bytes. -/// * `q` must be valid for reads of `q_len` bytes. +/// * `e` must be valid for reads of `e_len` bytes. /// * `d` must be valid for reads of `d_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. /// * `n_len` must be in 64..=1024. -/// * `p_len` and `q_len` must be in 1..`n_len`. -/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. +/// * `p_len` and `q_len` must be `n_len`. +/// * `e_len` must be in 1..=8. /// * `d_len` must be in 1..=`n_len`. /// * `scratch_len` must be at least `16 * n_len + 32`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `dp`, `dq`, `qinv` and `scratch` must not overlap each other, `n`, `p`, `q`, `d` or the arguments on the stack (distinct Rust objects never do). -/// * None of `dp`, `dq`, `qinv`, `n`, `p`, `q`, `d` and `scratch` may wrap around the end of the address space (no Rust object does). +/// * `p`, `q` and `scratch` must not overlap each other, `n`, `e`, `d` or the arguments on the stack (distinct Rust objects never do). +/// * None of `p`, `q`, `n`, `e`, `d` and `scratch` may overlap the 56 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_rsa_crt_values_auto(_dit: verified_garbage_cpu::Dit<'_>, dp: *mut u8, dp_len: usize, dq: *mut u8, dq_len: usize, qinv: *mut u8, qinv_len: usize, n: *const u8, n_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, d: *const u8, d_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_rsa_recover_primes_det_auto(_dit: verified_garbage_cpu::Dit<'_>, p: *mut u8, p_len: usize, q: *mut u8, q_len: usize, n: *const u8, n_len: usize, e: *const u8, e_len: usize, d: *const u8, d_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa::vg_rsa_crt_values(dp, dp_len, dq, dq_len, qinv, qinv_len, n, n_len, p, p_len, q, q_len, d, d_len, scratch, scratch_len) } + unsafe { crate::rsa::vg_rsa_recover_primes_det(p, p_len, q, q_len, n, n_len, e, e_len, d, d_len, scratch, scratch_len) } } /// The values of an RSA modulus that `vg_rsa_public_precomputed_checked` takes, so that they are computed once per public key rather than once per operation. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero) of `w = ⌈n_len / 8⌉` words of 64 bits, writes `n` and then `R² mod n` for `R = 2^(64 w)` to `pre` (`w` words each, least significant first) and returns 1; or writes zeros and returns 0 if `n` is not such a modulus. /// /// Contract: `VG.Spec.Rsa.publicPrecomputeContract`. Timing may depend on the pointers, the lengths and the contents of `n`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_public_precompute_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_PUBLIC_PRECOMPUTE_UMAAL_FEATURES`); or else `vg_rsa_public_precompute`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_public_precompute_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_PUBLIC_PRECOMPUTE_UMAAL_FEATURES`); or else `vg_rsa_public_precompute`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -118,32 +147,3 @@ pub unsafe fn vg_rsa_public_precompute_auto(_dit: verified_garbage_cpu::Dit<'_>, // either carries the caller's obligations, which are those of every implementation. unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pre, pre_len, n, n_len, scratch, scratch_len) } } - -/// The prime factors `p > q` of the modulus of an RSA private key `(n, e, d)` (SP 800-56B Rev. 2 §6.2.2's basic format), which bring it to the prime-factor format `(p, q, d)` that `vg_rsa_crt_values` takes, recovered by SP 800-56B Rev. 2 Appendix C.2's deterministic method. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes) and the private exponent `d` (`d_len` bytes), both most significant first, writes `p` and `q` (`n_len` bytes each, most significant first) and returns 1; or writes zeros to both and returns 0 if `n` is not such a modulus or the method fails. It finds the factors of a valid two-prime key whenever `K (p + q − 1) ≤ n` for `a = K φ(n)` (Appendix C.2's step 1): with the least private exponent, whenever the smaller prime exceeds `e²`, and in practice for every key whose primes are not tiny. -/// -/// Contract: `VG.Spec.Rsa.recoverPrimesDetContract`. Constant time but for the public key: timing may depend on the pointers, the lengths and the contents of `n` and `e`, not on `d`, the factors, or whether they were found. -/// -/// It runs `vg_rsa_recover_primes_det`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `p` must be valid for reads and writes of `p_len` bytes. -/// * `q` must be valid for reads and writes of `q_len` bytes. -/// * `n` must be valid for reads of `n_len` bytes. -/// * `e` must be valid for reads of `e_len` bytes. -/// * `d` must be valid for reads of `d_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `n_len` must be in 64..=1024. -/// * `p_len` and `q_len` must be `n_len`. -/// * `e_len` must be in 1..=8. -/// * `d_len` must be in 1..=`n_len`. -/// * `scratch_len` must be at least `16 * n_len + 32`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `p`, `q` and `scratch` must not overlap each other, `n`, `e`, `d` or the arguments on the stack (distinct Rust objects never do). -/// * None of `p`, `q`, `n`, `e`, `d` and `scratch` may overlap the 56 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_rsa_recover_primes_det_auto(_dit: verified_garbage_cpu::Dit<'_>, p: *mut u8, p_len: usize, q: *mut u8, q_len: usize, n: *const u8, n_len: usize, e: *const u8, e_len: usize, d: *const u8, d_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa::vg_rsa_recover_primes_det(p, p_len, q, q_len, n, n_len, e, e_len, d, d_len, scratch, scratch_len) } -} diff --git a/crates/verified-garbage-asm-arm/src/dispatch/rsa_keygen.rs b/crates/verified-garbage-asm-arm/src/dispatch/rsa_keygen.rs index b71ec20beaf..5b25f2225a1 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/rsa_keygen.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/rsa_keygen.rs @@ -2,11 +2,30 @@ //! The dispatchers of the verified `rsa_keygen` functions for `arm` //! (`crate::rsa_keygen`, `VG.Rust.dispatcher`). +/// Start generating an RSA key, as BoringSSL's `RSA_generate_key_ex` (FIPS 186-5 Appendix A.1.3), with a modulus of `bits` bits rounded down to a multiple of 128, and the public exponent `e` (`e_len` octets, most significant first, with any number of leading zeros). Returns 0 if `e` is not odd from 3 to `2^32 - 1`, or 2 if `bits` is above 8192 or, rounded down, below 512, and writes zeros to `state`. Otherwise returns 1 and writes the search's seven public words to `state`: the rejected candidates the first prime's search allows, the primes' length in octets (`nlen / 16`), the entropy budget, the length of the first random tape to give `vg_rsa_generate`, the phase (0), the generations allowed (4), and the exponent's eight octets, most significant first. +/// +/// Contract: `VG.Spec.RsaKeyGen.keyInitContract`. Timing may depend on `bits` and `e`. +/// +/// It runs `vg_rsa_generate_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 56 bytes. +/// * `e` must be valid for reads of `e_len` bytes. +/// * `state` must not overlap `e` (distinct Rust objects never do). +/// * Neither `state` nor `e` may wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_rsa_generate_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 7], bits: usize, e: *const u8, e_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::rsa_keygen::vg_rsa_generate_init(state, bits, e, e_len) } +} + /// Generate an RSA key, as BoringSSL's `RSA_generate_key_ex` (FIPS 186-5 Appendix A.1.3), over the random tape `rand`, from the state `state` that `vg_rsa_generate_init` started: the search for `p`, then for `q` not too close to `p`, each over candidates of `p_len` octets with their top two bits and bottom bit set, rejected by trial division, a common factor of `p − 1` and `e` or Miller–Rabin, up to BoringSSL's limit of rejected candidates; then the key, `d = e⁻¹ mod lcm(p − 1, q − 1)`, `dP`, `dQ` and `qInv`, with `p` the larger prime, checked as BoringSSL's `RSA_check_key` checks it, drawing both primes again if `d` is too small; a generation whose prime's search is exhausted is started again, up to four in all. Returns 1 and writes the key to `key` (`key_len` is `9 p_len`, for the primes' length `p_len = state[1]`): `n` and `d` (`2 p_len` octets each), then `p`, `q`, `dP`, `dQ` and `qInv` (`p_len` each), all most significant first; 2 if the key failed a check; 3 if every generation was exhausted; or 0 if the tape ran out inside a candidate. Writes the octets of `rand` it consumed to `used`, and the state for the next call to `state`, whose fourth word is, for 0, the length of the next tape, more than the octets of `rand` after the first `used` and at most the entropy budget: keep those octets, in order, and append fresh random octets up to that length. For 0, `key` holds the search's `p` once it is found, which the next call reads: pass it back unchanged. `key` is otherwise zeros but for 1. /// /// Contract: `VG.Spec.RsaKeyGen.keySearchContract`. Timing may depend on the pointers, the lengths, `state`, the status and consumption it returns, and, for each prime's search, its rejected candidates and the octets its accepted candidate's tests read, and each key's status. Not on the primes or the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_generate_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_GENERATE_UMAAL_FEATURES`); or else `vg_rsa_generate`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_generate_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_GENERATE_UMAAL_FEATURES`); or else `vg_rsa_generate`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -58,22 +77,3 @@ pub unsafe fn vg_rsa_generate_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *mu // either carries the caller's obligations, which are those of every implementation. unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(key, key_len, used, rand, rand_len, scratch, scratch_len, state) } } - -/// Start generating an RSA key, as BoringSSL's `RSA_generate_key_ex` (FIPS 186-5 Appendix A.1.3), with a modulus of `bits` bits rounded down to a multiple of 128, and the public exponent `e` (`e_len` octets, most significant first, with any number of leading zeros). Returns 0 if `e` is not odd from 3 to `2^32 - 1`, or 2 if `bits` is above 8192 or, rounded down, below 512, and writes zeros to `state`. Otherwise returns 1 and writes the search's seven public words to `state`: the rejected candidates the first prime's search allows, the primes' length in octets (`nlen / 16`), the entropy budget, the length of the first random tape to give `vg_rsa_generate`, the phase (0), the generations allowed (4), and the exponent's eight octets, most significant first. -/// -/// Contract: `VG.Spec.RsaKeyGen.keyInitContract`. Timing may depend on `bits` and `e`. -/// -/// It runs `vg_rsa_generate_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 56 bytes. -/// * `e` must be valid for reads of `e_len` bytes. -/// * `state` must not overlap `e` (distinct Rust objects never do). -/// * Neither `state` nor `e` may wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_rsa_generate_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 7], bits: usize, e: *const u8, e_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa_keygen::vg_rsa_generate_init(state, bits, e, e_len) } -} diff --git a/crates/verified-garbage-asm-arm/src/dispatch/rsa_oaep.rs b/crates/verified-garbage-asm-arm/src/dispatch/rsa_oaep.rs index 27270d143ee..ba49154cc1d 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/rsa_oaep.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/rsa_oaep.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.RsaOaep.decryptTablesContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, the contents of `n` and `e`, and which tables `hash` and `mgf1` are, not on the private key, the label, the ciphertext, whether its decoding is valid or the message's length. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_oaep_decrypt_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_OAEP_DECRYPT_UMAAL_FEATURES`); or else `vg_rsa_oaep_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_oaep_decrypt_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_OAEP_DECRYPT_UMAAL_FEATURES`); or else `vg_rsa_oaep_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -78,7 +78,7 @@ pub unsafe fn vg_rsa_oaep_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, out: /// /// Contract: `VG.Spec.RsaOaep.encryptTablesContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, the contents of `n`, `e` and `pre`, and which tables `hash` and `mgf1` are, not on the message, the label or the seed. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_oaep_encrypt_precomputed_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_OAEP_ENCRYPT_PRECOMPUTED_UMAAL_FEATURES`); or else `vg_rsa_oaep_encrypt_precomputed`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_oaep_encrypt_precomputed_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_OAEP_ENCRYPT_PRECOMPUTED_UMAAL_FEATURES`); or else `vg_rsa_oaep_encrypt_precomputed`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/rsa_pkcs1_enc.rs b/crates/verified-garbage-asm-arm/src/dispatch/rsa_pkcs1_enc.rs index 315a7e4adfb..e7b9a6c548d 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/rsa_pkcs1_enc.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/rsa_pkcs1_enc.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.RsaPkcs1Enc.decryptContract`. Constant time but for the public key: timing may depend on the pointers, the lengths and the contents of `n` and `e`, not on the ciphertext, the private key, the validity of the padding, the length of the message or which message is returned. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_pkcs1_decrypt_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_UMAAL_FEATURES`); or else `vg_rsa_pkcs1_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_pkcs1_decrypt_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_UMAAL_FEATURES`); or else `vg_rsa_pkcs1_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -72,7 +72,7 @@ pub unsafe fn vg_rsa_pkcs1_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, out /// /// Contract: `VG.Spec.RsaPkcs1Enc.encryptRandomContract`. Constant time but for the public key and the tape's zero bytes: timing may depend on the pointers, the lengths, the contents of `n`, `e` and `pre`, and which bytes of `rand` are zero, not on the message or the other bytes of `rand`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_pkcs1_encrypt_random_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_PKCS1_ENCRYPT_RANDOM_UMAAL_FEATURES`); or else `vg_rsa_pkcs1_encrypt_random`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_pkcs1_encrypt_random_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_PKCS1_ENCRYPT_RANDOM_UMAAL_FEATURES`); or else `vg_rsa_pkcs1_encrypt_random`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/rsa_pkcs1_sig.rs b/crates/verified-garbage-asm-arm/src/dispatch/rsa_pkcs1_sig.rs index 2bfabc89b80..7840d852147 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/rsa_pkcs1_sig.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/rsa_pkcs1_sig.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.RsaPkcs1Sig.signContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, `hash` and the contents of `n` and `e`, not on `digest` or the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_pkcs1_sign_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_PKCS1_SIGN_UMAAL_FEATURES`); or else `vg_rsa_pkcs1_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_pkcs1_sign_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_PKCS1_SIGN_UMAAL_FEATURES`); or else `vg_rsa_pkcs1_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -74,7 +74,7 @@ pub unsafe fn vg_rsa_pkcs1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: * /// /// Contract: `VG.Spec.RsaPkcs1Sig.verifyPrecomputedContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_pkcs1_verify_precomputed_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_PKCS1_VERIFY_PRECOMPUTED_UMAAL_FEATURES`); or else `vg_rsa_pkcs1_verify_precomputed`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_pkcs1_verify_precomputed_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_PKCS1_VERIFY_PRECOMPUTED_UMAAL_FEATURES`); or else `vg_rsa_pkcs1_verify_precomputed`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -137,7 +137,7 @@ pub unsafe fn vg_rsa_pkcs1_verify_precomputed_auto(_dit: verified_garbage_cpu::D /// /// Contract: `VG.Spec.RsaPkcs1Sig.recoverPrecomputedContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_pkcs1_recover_precomputed_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_PKCS1_RECOVER_PRECOMPUTED_UMAAL_FEATURES`); or else `vg_rsa_pkcs1_recover_precomputed`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_pkcs1_recover_precomputed_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_PKCS1_RECOVER_PRECOMPUTED_UMAAL_FEATURES`); or else `vg_rsa_pkcs1_recover_precomputed`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/rsa_pss.rs b/crates/verified-garbage-asm-arm/src/dispatch/rsa_pss.rs index 19ffa5ead1d..d2e8ef906dc 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/rsa_pss.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/rsa_pss.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.RsaPss.signTableContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, the contents of `n` and `e`, and which tables `hash` and `mgf1` are, not on the private key, the digest or the salt. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_pss_sign_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_PSS_SIGN_UMAAL_FEATURES`); or else `vg_rsa_pss_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_pss_sign_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_PSS_SIGN_UMAAL_FEATURES`); or else `vg_rsa_pss_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -78,7 +78,7 @@ pub unsafe fn vg_rsa_pss_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mu /// /// Contract: `VG.Spec.RsaPss.verifyPrecomputedTableContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, `salt_len`, `any_salt_len`, the contents of `n`, `e` and `pre`, and which tables `hash` and `mgf1` are, not on the digest, the signature or anything computed from them but the result. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_pss_verify_precomputed_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_PSS_VERIFY_PRECOMPUTED_UMAAL_FEATURES`); or else `vg_rsa_pss_verify_precomputed`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_pss_verify_precomputed_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_RSA_PSS_VERIFY_PRECOMPUTED_UMAAL_FEATURES`); or else `vg_rsa_pss_verify_precomputed`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-arm/src/dispatch/sha1.rs b/crates/verified-garbage-asm-arm/src/dispatch/sha1.rs index ab92c17be21..8385877f4a6 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/sha1.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/sha1.rs @@ -38,41 +38,41 @@ pub unsafe fn vg_sha1_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *m unsafe { crate::sha1::vg_sha1_update(state, count, data, len) } } -/// Computes the SHA-1 digest of the `len` bytes at `data` (20 bytes, `VG.Spec.Sha1.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-1 computation with that message. +/// Finishes a SHA-1 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), writes the SHA-1 digest of that message to `*out`. /// -/// Contract: `VG.Spec.Sha1.digestContract`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha1.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha1_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha1_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `data` must be valid for reads of `len` bytes. +/// * `state` must be valid for reads and writes of 84 bytes. /// * `out` must be valid for reads and writes of 20 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the 320 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other or the arguments on the stack (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 176 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha1_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 20]) { +pub unsafe fn vg_sha1_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 84], count: u64, out: *mut [u8; 20]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha1::vg_sha1_digest(data, len, out) } + unsafe { crate::sha1::vg_sha1_finalize(state, count, out) } } -/// Finishes a SHA-1 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), writes the SHA-1 digest of that message to `*out`. +/// Computes the SHA-1 digest of the `len` bytes at `data` (20 bytes, `VG.Spec.Sha1.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-1 computation with that message. /// -/// Contract: `VG.Spec.Sha1.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha1.digestContract`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha1_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha1_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 84 bytes. +/// * `data` must be valid for reads of `len` bytes. /// * `out` must be valid for reads and writes of 20 bytes. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other or the arguments on the stack (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 176 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the 320 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha1_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 84], count: u64, out: *mut [u8; 20]) { +pub unsafe fn vg_sha1_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 20]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha1::vg_sha1_finalize(state, count, out) } + unsafe { crate::sha1::vg_sha1_digest(data, len, out) } } diff --git a/crates/verified-garbage-asm-arm/src/dispatch/sha256.rs b/crates/verified-garbage-asm-arm/src/dispatch/sha256.rs index 9bbefead797..b42a8d696a6 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/sha256.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/sha256.rs @@ -19,21 +19,24 @@ pub unsafe fn vg_sha224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *m unsafe { crate::sha256::vg_sha224_init(state) } } -/// Starts a SHA-256 computation: makes the streaming state `*state` represent the empty message. +/// Finishes a SHA-224 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), hashed from the initial hash value of SHA-224 (as `vg_sha224_init` starts it), writes the SHA-224 digest of that message (28 bytes, `VG.Spec.Sha256.sha224`) to `*out`. /// -/// Contract: `VG.Spec.Sha256.initContract`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha256.Repr`). +/// Contract: `VG.Spec.Sha256.finalize224Contract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha256_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 96 bytes. -/// * `state` must not wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 28 bytes. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other or the arguments on the stack (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 624 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96]) { +pub unsafe fn vg_sha224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, out: *mut [u8; 28]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha256::vg_sha256_init(state) } + unsafe { crate::sha256::vg_sha224_finalize(state, count, out) } } /// Computes the SHA-224 digest of the `len` bytes at `data` (28 bytes, `VG.Spec.Sha256.sha224`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-224 computation with that message. @@ -55,80 +58,77 @@ pub unsafe fn vg_sha224_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: * unsafe { crate::sha256::vg_sha224_digest(data, len, out) } } -/// Absorbs data into a SHA-224 or SHA-256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), it then represents that message followed by the `len` bytes at `data`. +/// Starts a SHA-256 computation: makes the streaming state `*state` represent the empty message. /// -/// Contract: `VG.Spec.Sha256.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha256.initContract`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha256.Repr`). /// -/// It runs `vg_sha256_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha256_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 96 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `state` must not overlap `data` or the arguments on the stack (distinct Rust objects never do). -/// * Neither `state` nor `data` may overlap the 624 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must not wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, data: *const u8, len: usize) { +pub unsafe fn vg_sha256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha256::vg_sha256_update(state, count, data, len) } + unsafe { crate::sha256::vg_sha256_init(state) } } -/// Computes the SHA-256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha256.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-256 computation with that message. +/// Absorbs data into a SHA-224 or SHA-256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), it then represents that message followed by the `len` bytes at `data`. /// -/// Contract: `VG.Spec.Sha256.digestContract` for `VG.Spec.Sha256.hash`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha256.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_sha256_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha256_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// +/// * `state` must be valid for reads and writes of 96 bytes. /// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 32 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the 344 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must not overlap `data` or the arguments on the stack (distinct Rust objects never do). +/// * Neither `state` nor `data` may overlap the 624 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { +pub unsafe fn vg_sha256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, data: *const u8, len: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha256::vg_sha256_digest(data, len, out) } + unsafe { crate::sha256::vg_sha256_update(state, count, data, len) } } -/// Finishes a SHA-224 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), hashed from the initial hash value of SHA-224 (as `vg_sha224_init` starts it), writes the SHA-224 digest of that message (28 bytes, `VG.Spec.Sha256.sha224`) to `*out`. +/// Finishes a SHA-224 or SHA-256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (32 bytes) to `*out`. The SHA-256 digest is all of it; the SHA-224 digest is its first 28 bytes. /// -/// Contract: `VG.Spec.Sha256.finalize224Contract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha256.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 96 bytes. -/// * `out` must be valid for reads and writes of 28 bytes. +/// * `out` must be valid for reads and writes of 32 bytes. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other or the arguments on the stack (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the 624 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, out: *mut [u8; 28]) { +pub unsafe fn vg_sha256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, out: *mut [u8; 32]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha256::vg_sha224_finalize(state, count, out) } + unsafe { crate::sha256::vg_sha256_finalize(state, count, out) } } -/// Finishes a SHA-224 or SHA-256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (32 bytes) to `*out`. The SHA-256 digest is all of it; the SHA-224 digest is its first 28 bytes. +/// Computes the SHA-256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha256.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-256 computation with that message. /// -/// Contract: `VG.Spec.Sha256.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha256.digestContract` for `VG.Spec.Sha256.hash`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha256_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 96 bytes. +/// * `data` must be valid for reads of `len` bytes. /// * `out` must be valid for reads and writes of 32 bytes. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other or the arguments on the stack (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 624 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the 344 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, out: *mut [u8; 32]) { +pub unsafe fn vg_sha256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha256::vg_sha256_finalize(state, count, out) } + unsafe { crate::sha256::vg_sha256_digest(data, len, out) } } diff --git a/crates/verified-garbage-asm-arm/src/dispatch/sha3.rs b/crates/verified-garbage-asm-arm/src/dispatch/sha3.rs index be71f4d7b7f..b2ba187e403 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/sha3.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/sha3.rs @@ -24,250 +24,250 @@ pub unsafe fn vg_sha3_224_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state unsafe { crate::sha3::vg_sha3_224_update(state, pos, data, len, scratch) } } -/// Absorbs data into a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, 136), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 136`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Finishes a SHA3-224 computation: if the state `*state` represents a message whose length is `pos` modulo 144 (`VG.Spec.Sha3.Repr` with SHA3-224's rate, as `vg_sha3_224_update` leaves it), writes the SHA3-224 digest of that message (28 bytes, `VG.Spec.Sha3.sha3_224`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 136. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_224`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs `vg_sha3_256_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `scratch` must be valid for reads and writes of 640 bytes. -/// * `pos` must be less than 136. -/// * The contents of `scratch` on return are unspecified. -/// * `state` and `scratch` must not overlap each other, `data` or the arguments on the stack (distinct Rust objects never do). -/// * None of `state`, `data` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 28 bytes. +/// * `pos` must be less than 144. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 664 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 28]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_256_update(state, pos, data, len, scratch) } + unsafe { crate::sha3::vg_sha3_224_finalize(state, pos, out) } } -/// Absorbs data into a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, 104), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 104`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Absorbs data into a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, 136), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 136`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 104. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 136. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_sha3_384_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_256_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. /// * `data` must be valid for reads of `len` bytes. /// * `scratch` must be valid for reads and writes of 640 bytes. -/// * `pos` must be less than 104. +/// * `pos` must be less than 136. /// * The contents of `scratch` on return are unspecified. /// * `state` and `scratch` must not overlap each other, `data` or the arguments on the stack (distinct Rust objects never do). /// * None of `state`, `data` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_384_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_384_update(state, pos, data, len, scratch) } + unsafe { crate::sha3::vg_sha3_256_update(state, pos, data, len, scratch) } } -/// Absorbs data into a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, 72), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 72`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Finishes a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, as `vg_sha3_256_update` leaves it), writes the SHA3-256 digest of that message (32 bytes, `VG.Spec.Sha3.sha3_256`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 72. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_256`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs `vg_sha3_512_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `scratch` must be valid for reads and writes of 640 bytes. -/// * `pos` must be less than 72. -/// * The contents of `scratch` on return are unspecified. -/// * `state` and `scratch` must not overlap each other, `data` or the arguments on the stack (distinct Rust objects never do). -/// * None of `state`, `data` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 32 bytes. +/// * `pos` must be less than 136. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 664 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 32]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_512_update(state, pos, data, len, scratch) } + unsafe { crate::sha3::vg_sha3_256_finalize(state, pos, out) } } -/// Absorbs data into a SHAKE128 computation: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, 168), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 168`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Absorbs data into a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, 104), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 104`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 168. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 104. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_shake128_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_384_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. /// * `data` must be valid for reads of `len` bytes. /// * `scratch` must be valid for reads and writes of 640 bytes. -/// * `pos` must be less than 168. +/// * `pos` must be less than 104. /// * The contents of `scratch` on return are unspecified. /// * `state` and `scratch` must not overlap each other, `data` or the arguments on the stack (distinct Rust objects never do). /// * None of `state`, `data` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake128_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_384_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake128_update(state, pos, data, len, scratch) } + unsafe { crate::sha3::vg_sha3_384_update(state, pos, data, len, scratch) } } -/// Squeezes SHAKE128 output: if the state `*state`, from position `pos`, continues the output of SHAKE128 of a message (`VG.Spec.Sha3.shake128`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake128_finalize` leaves and position 0. +/// Finishes a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, as `vg_sha3_384_update` leaves it), writes the SHA3-384 digest of that message (48 bytes, `VG.Spec.Sha3.sha3_384`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_384`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs `vg_shake128_squeeze`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_384_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of `outlen` bytes. -/// * `pos` must be at most 168. +/// * `out` must be valid for reads and writes of 48 bytes. +/// * `pos` must be less than 104. +/// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 656 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * Neither `state` nor `out` may overlap the 664 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake128_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { +pub unsafe fn vg_sha3_384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 48]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake128_squeeze(state, pos, out, outlen) } + unsafe { crate::sha3::vg_sha3_384_finalize(state, pos, out) } } -/// Squeezes SHAKE256 output: if the state `*state`, from position `pos`, continues the output of SHAKE256 of a message (`VG.Spec.Sha3.shake256`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake256_finalize` leaves and position 0. +/// Absorbs data into a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, 72), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 72`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 72. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_shake256_squeeze`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_512_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of `outlen` bytes. -/// * `pos` must be at most 136. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 656 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `scratch` must be valid for reads and writes of 640 bytes. +/// * `pos` must be less than 72. +/// * The contents of `scratch` on return are unspecified. +/// * `state` and `scratch` must not overlap each other, `data` or the arguments on the stack (distinct Rust objects never do). +/// * None of `state`, `data` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake256_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { +pub unsafe fn vg_sha3_512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake256_squeeze(state, pos, out, outlen) } + unsafe { crate::sha3::vg_sha3_512_update(state, pos, data, len, scratch) } } -/// Finishes a SHA3-224 computation: if the state `*state` represents a message whose length is `pos` modulo 144 (`VG.Spec.Sha3.Repr` with SHA3-224's rate, as `vg_sha3_224_update` leaves it), writes the SHA3-224 digest of that message (28 bytes, `VG.Spec.Sha3.sha3_224`) to `*out`. +/// Finishes a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, as `vg_sha3_512_update` leaves it), writes the SHA3-512 digest of that message (64 bytes, `VG.Spec.Sha3.sha3_512`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_224`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_512`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs `vg_sha3_224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_512_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of 28 bytes. -/// * `pos` must be less than 144. +/// * `out` must be valid for reads and writes of 64 bytes. +/// * `pos` must be less than 72. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the 664 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 28]) { +pub unsafe fn vg_sha3_512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 64]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_224_finalize(state, pos, out) } + unsafe { crate::sha3::vg_sha3_512_finalize(state, pos, out) } } -/// Finishes a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, as `vg_sha3_256_update` leaves it), writes the SHA3-256 digest of that message (32 bytes, `VG.Spec.Sha3.sha3_256`) to `*out`. +/// Absorbs data into a SHAKE128 computation: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, 168), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 168`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_256`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 168. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_sha3_256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake128_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of 32 bytes. -/// * `pos` must be less than 136. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 664 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `scratch` must be valid for reads and writes of 640 bytes. +/// * `pos` must be less than 168. +/// * The contents of `scratch` on return are unspecified. +/// * `state` and `scratch` must not overlap each other, `data` or the arguments on the stack (distinct Rust objects never do). +/// * None of `state`, `data` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 32]) { +pub unsafe fn vg_shake128_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_256_finalize(state, pos, out) } + unsafe { crate::sha3::vg_shake128_update(state, pos, data, len, scratch) } } -/// Finishes a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, as `vg_sha3_384_update` leaves it), writes the SHA3-384 digest of that message (48 bytes, `VG.Spec.Sha3.sha3_384`) to `*out`. +/// Finishes absorbing a SHAKE128 message: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, as `vg_shake128_update` leaves it), it becomes a state that, squeezed by `vg_shake128_squeeze` from position 0, gives the output of SHAKE128 of that message (`VG.Spec.Sha3.shake128`) from its first byte on (`VG.Spec.Sha3.XofRepr`). /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_384`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointer and `pos` may affect timing, not the state. /// -/// It runs `vg_sha3_384_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake128_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of 48 bytes. -/// * `pos` must be less than 104. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 664 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `pos` must be less than 168. +/// * `state` must not overlap the 648 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 48]) { +pub unsafe fn vg_shake128_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_384_finalize(state, pos, out) } + unsafe { crate::sha3::vg_shake128_finalize(state, pos) } } -/// Finishes a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, as `vg_sha3_512_update` leaves it), writes the SHA3-512 digest of that message (64 bytes, `VG.Spec.Sha3.sha3_512`) to `*out`. +/// Squeezes SHAKE128 output: if the state `*state`, from position `pos`, continues the output of SHAKE128 of a message (`VG.Spec.Sha3.shake128`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake128_finalize` leaves and position 0. /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_512`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. /// -/// It runs `vg_sha3_512_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake128_squeeze`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of 64 bytes. -/// * `pos` must be less than 72. -/// * The contents of `state` on return are unspecified. +/// * `out` must be valid for reads and writes of `outlen` bytes. +/// * `pos` must be at most 168. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 664 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * Neither `state` nor `out` may overlap the 656 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 64]) { +pub unsafe fn vg_shake128_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_512_finalize(state, pos, out) } + unsafe { crate::sha3::vg_shake128_squeeze(state, pos, out, outlen) } } -/// Finishes absorbing a SHAKE128 message: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, as `vg_shake128_update` leaves it), it becomes a state that, squeezed by `vg_shake128_squeeze` from position 0, gives the output of SHAKE128 of that message (`VG.Spec.Sha3.shake128`) from its first byte on (`VG.Spec.Sha3.XofRepr`). +/// Finishes absorbing a SHAKE256 message: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHAKE256's rate, as `vg_sha3_256_update` leaves it), it becomes a state that, squeezed by `vg_shake256_squeeze` from position 0, gives the output of SHAKE256 of that message (`VG.Spec.Sha3.shake256`) from its first byte on (`VG.Spec.Sha3.XofRepr`). /// -/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointer and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointer and `pos` may affect timing, not the state. /// -/// It runs `vg_shake128_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `pos` must be less than 168. +/// * `pos` must be less than 136. /// * `state` must not overlap the 648 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake128_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { +pub unsafe fn vg_shake256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake128_finalize(state, pos) } + unsafe { crate::sha3::vg_shake256_finalize(state, pos) } } -/// Finishes absorbing a SHAKE256 message: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHAKE256's rate, as `vg_sha3_256_update` leaves it), it becomes a state that, squeezed by `vg_shake256_squeeze` from position 0, gives the output of SHAKE256 of that message (`VG.Spec.Sha3.shake256`) from its first byte on (`VG.Spec.Sha3.XofRepr`). +/// Squeezes SHAKE256 output: if the state `*state`, from position `pos`, continues the output of SHAKE256 of a message (`VG.Spec.Sha3.shake256`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake256_finalize` leaves and position 0. /// -/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointer and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. /// -/// It runs `vg_shake256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake256_squeeze`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `pos` must be less than 136. -/// * `state` must not overlap the 648 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of `outlen` bytes. +/// * `pos` must be at most 136. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 656 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { +pub unsafe fn vg_shake256_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake256_finalize(state, pos) } + unsafe { crate::sha3::vg_shake256_squeeze(state, pos, out, outlen) } } diff --git a/crates/verified-garbage-asm-arm/src/dispatch/sha512.rs b/crates/verified-garbage-asm-arm/src/dispatch/sha512.rs index 5ace8bd949e..f0bd4aa5a43 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/sha512.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/sha512.rs @@ -36,215 +36,215 @@ pub unsafe fn vg_sha512_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *m unsafe { crate::sha512::vg_sha512_init(state) } } -/// Absorbs data into a SHA-384, SHA-512, SHA-512/224 or SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), it then represents that message followed by the `len` bytes at `data`. +/// Starts a SHA-512/224 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/224 (`VG.Spec.Sha512.H0_512_224`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. /// -/// Contract: `VG.Spec.Sha512.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_224`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). /// -/// It runs `vg_sha512_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_224_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `state` must not overlap `data` or the arguments on the stack (distinct Rust objects never do). -/// * Neither `state` nor `data` may overlap the 1392 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must not wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, data: *const u8, len: usize) { +pub unsafe fn vg_sha512_224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_update(state, count, data, len) } + unsafe { crate::sha512::vg_sha512_224_init(state) } } -/// Computes the SHA-384 digest of the `len` bytes at `data` (48 bytes, `VG.Spec.Sha512.sha384`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-384 computation with that message. +/// Starts a SHA-512/256 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/256 (`VG.Spec.Sha512.H0_512_256`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. /// -/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_256`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). /// -/// It runs `vg_sha384_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_256_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 48 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the 584 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must be valid for reads and writes of 192 bytes. +/// * `state` must not wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha384_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 48]) { +pub unsafe fn vg_sha512_256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha384_digest(data, len, out) } + unsafe { crate::sha512::vg_sha512_256_init(state) } } -/// Computes the SHA-512 digest of the `len` bytes at `data` (64 bytes, `VG.Spec.Sha512.sha512`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512 computation with that message. +/// Absorbs data into a SHA-384, SHA-512, SHA-512/224 or SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), it then represents that message followed by the `len` bytes at `data`. /// -/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha512.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_sha512_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// +/// * `state` must be valid for reads and writes of 192 bytes. /// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 64 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the 584 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must not overlap `data` or the arguments on the stack (distinct Rust objects never do). +/// * Neither `state` nor `data` may overlap the 1392 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 64]) { +pub unsafe fn vg_sha512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, data: *const u8, len: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_digest(data, len, out) } + unsafe { crate::sha512::vg_sha512_update(state, count, data, len) } } -/// Starts a SHA-512/224 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/224 (`VG.Spec.Sha512.H0_512_224`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. +/// Finishes a SHA-384, SHA-512, SHA-512/224 or SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (64 bytes) to `*out`. The SHA-512 digest is all of it; the SHA-384, SHA-512/224 and SHA-512/256 digests are its first 48, 28 and 32 bytes. /// -/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_224`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). +/// Contract: `VG.Spec.Sha512.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha512_224_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `state` must not wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 64 bytes. +/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other or the arguments on the stack (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 1392 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { +pub unsafe fn vg_sha512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 64]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_224_init(state) } + unsafe { crate::sha512::vg_sha512_finalize(state, count, out) } } -/// Starts a SHA-512/256 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/256 (`VG.Spec.Sha512.H0_512_256`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. +/// Finishes a SHA-384 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-384 (as `vg_sha384_init` starts it), writes the SHA-384 digest of that message (48 bytes, `VG.Spec.Sha512.sha384`) to `*out`. /// -/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_256`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). +/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_384` and `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha512_256_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha384_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `state` must not wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 48 bytes. +/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other or the arguments on the stack (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 1392 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { +pub unsafe fn vg_sha384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 48]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_256_init(state) } + unsafe { crate::sha512::vg_sha384_finalize(state, count, out) } } -/// Finishes a SHA-384, SHA-512, SHA-512/224 or SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (64 bytes) to `*out`. The SHA-512 digest is all of it; the SHA-384, SHA-512/224 and SHA-512/256 digests are its first 48, 28 and 32 bytes. +/// Finishes a SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/256 (as `vg_sha512_256_init` starts it), writes the SHA-512/256 digest of that message (32 bytes, `VG.Spec.Sha512.sha512_256`) to `*out`. /// -/// Contract: `VG.Spec.Sha512.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_256` and `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha512_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 64 bytes. +/// * `out` must be valid for reads and writes of 32 bytes. /// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other or the arguments on the stack (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the 1392 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 64]) { +pub unsafe fn vg_sha512_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 32]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_finalize(state, count, out) } + unsafe { crate::sha512::vg_sha512_256_finalize(state, count, out) } } -/// Finishes a SHA-384 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-384 (as `vg_sha384_init` starts it), writes the SHA-384 digest of that message (48 bytes, `VG.Spec.Sha512.sha384`) to `*out`. +/// Finishes a SHA-512/224 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/224 (as `vg_sha512_224_init` starts it), writes the SHA-512/224 digest of that message (28 bytes, `VG.Spec.Sha512.sha512_224`) to `*out`. /// -/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_384` and `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_224` and `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha384_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 48 bytes. +/// * `out` must be valid for reads and writes of 28 bytes. /// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other or the arguments on the stack (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the 1392 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 48]) { +pub unsafe fn vg_sha512_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 28]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha384_finalize(state, count, out) } + unsafe { crate::sha512::vg_sha512_224_finalize(state, count, out) } } -/// Computes the SHA-512/224 digest of the `len` bytes at `data` (28 bytes, `VG.Spec.Sha512.sha512_224`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512/224 computation with that message. +/// Computes the SHA-384 digest of the `len` bytes at `data` (48 bytes, `VG.Spec.Sha512.sha384`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-384 computation with that message. /// -/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha512_224_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha384_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 28 bytes. +/// * `out` must be valid for reads and writes of 48 bytes. /// * `out` must not overlap `data` (distinct Rust objects never do). /// * Neither `data` nor `out` may overlap the 584 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_224_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 28]) { +pub unsafe fn vg_sha384_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 48]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_224_digest(data, len, out) } + unsafe { crate::sha512::vg_sha384_digest(data, len, out) } } -/// Computes the SHA-512/256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha512.sha512_256`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512/256 computation with that message. +/// Computes the SHA-512 digest of the `len` bytes at `data` (64 bytes, `VG.Spec.Sha512.sha512`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512 computation with that message. /// -/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha512_256_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 32 bytes. +/// * `out` must be valid for reads and writes of 64 bytes. /// * `out` must not overlap `data` (distinct Rust objects never do). /// * Neither `data` nor `out` may overlap the 584 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { +pub unsafe fn vg_sha512_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 64]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_256_digest(data, len, out) } + unsafe { crate::sha512::vg_sha512_digest(data, len, out) } } -/// Finishes a SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/256 (as `vg_sha512_256_init` starts it), writes the SHA-512/256 digest of that message (32 bytes, `VG.Spec.Sha512.sha512_256`) to `*out`. +/// Computes the SHA-512/224 digest of the `len` bytes at `data` (28 bytes, `VG.Spec.Sha512.sha512_224`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512/224 computation with that message. /// -/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_256` and `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha512_256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_224_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 32 bytes. -/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other or the arguments on the stack (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 1392 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `out` must be valid for reads and writes of 28 bytes. +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the 584 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 32]) { +pub unsafe fn vg_sha512_224_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 28]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_256_finalize(state, count, out) } + unsafe { crate::sha512::vg_sha512_224_digest(data, len, out) } } -/// Finishes a SHA-512/224 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/224 (as `vg_sha512_224_init` starts it), writes the SHA-512/224 digest of that message (28 bytes, `VG.Spec.Sha512.sha512_224`) to `*out`. +/// Computes the SHA-512/256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha512.sha512_256`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512/256 computation with that message. /// -/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_224` and `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha512_224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_256_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 28 bytes. -/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other or the arguments on the stack (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 1392 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `out` must be valid for reads and writes of 32 bytes. +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the 584 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 28]) { +pub unsafe fn vg_sha512_256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_224_finalize(state, count, out) } + unsafe { crate::sha512::vg_sha512_256_digest(data, len, out) } } diff --git a/crates/verified-garbage-asm-arm/src/dispatch/sm4_gcm.rs b/crates/verified-garbage-asm-arm/src/dispatch/sm4_gcm.rs index a10ff31cdd8..18e6b5d70b6 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/sm4_gcm.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/sm4_gcm.rs @@ -69,26 +69,6 @@ pub unsafe fn vg_sm4_gcm_open_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *co unsafe { crate::sm4_gcm::vg_sm4_gcm_open(ctx, nonce, nonce_len, aad, aad_len, data, len, tag, tag_len) } } -/// Absorbs additional data into an incremental SM4-GCM encryption or decryption: with the key context `*ctx`, if the streaming state `*state` represents a message with `aad_len` bytes (modulo 2⁶⁴) of additional data and no text yet, it then represents that message with the `len` bytes at `data` appended to its additional data. All of the additional data comes before the text (§7.1 step 5): after text, the state represents no message. -/// -/// Contract: `VG.Spec.Gcm.streamAadContract` (AES-GCM's, which reads only the hash subkey of the key context). Constant time: only the pointers, `aad_len` and `len` may affect timing, not the key context, the state or the data. -/// -/// It runs `vg_sm4_gcm_stream_aad`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `ctx` must be valid for reads of 256 bytes. -/// * `state` must be valid for reads and writes of 80 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `state` must not overlap `ctx`, `data` or the arguments on the stack (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the 2304 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_sm4_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_aad(ctx, state, aad_len, data, len) } -} - /// Starts an incremental SM4-GCM encryption or decryption (NIST SP 800-38D §7): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, makes the streaming state `*state` represent the message with the IV the `nonce_len` bytes at `nonce`, and no additional data or text yet. Continue with `vg_sm4_gcm_stream_aad`, then `vg_sm4_gcm_stream_encrypt` or `vg_sm4_gcm_stream_decrypt`, then `vg_sm4_gcm_stream_finish` or `vg_sm4_gcm_stream_verify`, with the same key context. /// /// The function checks no length: GCM requires a nonce of 1 to `2^61 - 1` bytes (§5.2.1.1), which the caller must check. @@ -111,46 +91,24 @@ pub unsafe fn vg_sm4_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, c unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_init(ctx, nonce, nonce_len, state) } } -/// Finishes an incremental SM4-GCM encryption or decryption (NIST SP 800-38D §7.1 steps 5–6, §7.2 steps 6–7): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, writes its 128-bit tag to `*tag`. A shorter tag is the first bytes of this one; to check a received tag, use `vg_sm4_gcm_stream_verify`. -/// -/// Contract: `VG.Spec.Sm4.Gcm.streamFinishContract`. Constant time: only the pointers, `aad_len` and `text_len` may affect timing, not the key context or the state. -/// -/// It runs `vg_sm4_gcm_stream_finish`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `ctx` must be valid for reads of 256 bytes. -/// * `state` must be valid for reads and writes of 80 bytes. -/// * `tag` must be valid for reads and writes of 16 bytes. -/// * The contents of `state` on return are unspecified. -/// * `state` and `tag` must not overlap each other, `ctx` or the arguments on the stack (distinct Rust objects never do). -/// * None of `ctx`, `state` and `tag` may overlap the 2320 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_sm4_gcm_stream_finish_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *mut [u8; 16]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_finish(ctx, state, aad_len, text_len, tag) } -} - -/// Finishes an incremental SM4-GCM decryption and checks its tag (NIST SP 800-38D §7.2 steps 6–8): with the key context `*ctx` that `vg_sm4_gcm_init` wrote and the received tag the `tag_len` bytes at `tag`, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, returns 1 if `tag_len` is between 4 and 16 and the received tag is the first `tag_len` bytes of the message's 128-bit tag, and 0 otherwise. The tags are compared without a branch. +/// Absorbs additional data into an incremental SM4-GCM encryption or decryption: with the key context `*ctx`, if the streaming state `*state` represents a message with `aad_len` bytes (modulo 2⁶⁴) of additional data and no text yet, it then represents that message with the `len` bytes at `data` appended to its additional data. All of the additional data comes before the text (§7.1 step 5): after text, the state represents no message. /// -/// Contract: `VG.Spec.Sm4.Gcm.streamVerifyContract`. Constant time: only the pointers, `aad_len`, `text_len` and `tag_len` may affect timing, not the key context, the state or the tags. +/// Contract: `VG.Spec.Gcm.streamAadContract` (AES-GCM's, which reads only the hash subkey of the key context). Constant time: only the pointers, `aad_len` and `len` may affect timing, not the key context, the state or the data. /// -/// It runs `vg_sm4_gcm_stream_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sm4_gcm_stream_aad`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `tag` must be valid for reads of `tag_len` bytes. -/// * The contents of `state` on return are unspecified. -/// * `state` must not overlap `ctx`, `tag` or the arguments on the stack (distinct Rust objects never do). -/// * None of `ctx`, `state` and `tag` may overlap the 2320 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `state` must not overlap `ctx`, `data` or the arguments on the stack (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the 2304 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sm4_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *const u8, tag_len: usize) -> u32 { +pub unsafe fn vg_sm4_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_verify(ctx, state, aad_len, text_len, tag, tag_len) } + unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_aad(ctx, state, aad_len, data, len) } } /// Encrypts the next piece of the text of an incremental SM4-GCM encryption (NIST SP 800-38D §7.1): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and the ciphertext of exactly `text_len` bytes of plaintext so far, encrypts the `len` bytes at `data` in place, as the continuation of that plaintext, and the state then represents the message with them appended. @@ -196,3 +154,45 @@ pub unsafe fn vg_sm4_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_> // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_decrypt(ctx, state, aad_len, text_len, data, len) } } + +/// Finishes an incremental SM4-GCM encryption or decryption (NIST SP 800-38D §7.1 steps 5–6, §7.2 steps 6–7): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, writes its 128-bit tag to `*tag`. A shorter tag is the first bytes of this one; to check a received tag, use `vg_sm4_gcm_stream_verify`. +/// +/// Contract: `VG.Spec.Sm4.Gcm.streamFinishContract`. Constant time: only the pointers, `aad_len` and `text_len` may affect timing, not the key context or the state. +/// +/// It runs `vg_sm4_gcm_stream_finish`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `ctx` must be valid for reads of 256 bytes. +/// * `state` must be valid for reads and writes of 80 bytes. +/// * `tag` must be valid for reads and writes of 16 bytes. +/// * The contents of `state` on return are unspecified. +/// * `state` and `tag` must not overlap each other, `ctx` or the arguments on the stack (distinct Rust objects never do). +/// * None of `ctx`, `state` and `tag` may overlap the 2320 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_sm4_gcm_stream_finish_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *mut [u8; 16]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_finish(ctx, state, aad_len, text_len, tag) } +} + +/// Finishes an incremental SM4-GCM decryption and checks its tag (NIST SP 800-38D §7.2 steps 6–8): with the key context `*ctx` that `vg_sm4_gcm_init` wrote and the received tag the `tag_len` bytes at `tag`, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, returns 1 if `tag_len` is between 4 and 16 and the received tag is the first `tag_len` bytes of the message's 128-bit tag, and 0 otherwise. The tags are compared without a branch. +/// +/// Contract: `VG.Spec.Sm4.Gcm.streamVerifyContract`. Constant time: only the pointers, `aad_len`, `text_len` and `tag_len` may affect timing, not the key context, the state or the tags. +/// +/// It runs `vg_sm4_gcm_stream_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `ctx` must be valid for reads of 256 bytes. +/// * `state` must be valid for reads and writes of 80 bytes. +/// * `tag` must be valid for reads of `tag_len` bytes. +/// * The contents of `state` on return are unspecified. +/// * `state` must not overlap `ctx`, `tag` or the arguments on the stack (distinct Rust objects never do). +/// * None of `ctx`, `state` and `tag` may overlap the 2320 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_sm4_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *const u8, tag_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_verify(ctx, state, aad_len, text_len, tag, tag_len) } +} diff --git a/crates/verified-garbage-asm-arm/src/dispatch/x25519.rs b/crates/verified-garbage-asm-arm/src/dispatch/x25519.rs index 47aea112fc5..205ec3ff7f7 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/x25519.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/x25519.rs @@ -2,32 +2,11 @@ //! The dispatchers of the verified `x25519` functions for `arm` //! (`crate::x25519`, `VG.Rust.dispatcher`). -/// Computes an X25519 public key (RFC 7748 §6.1): writes `X25519(k, 9)`, the X25519 function of the 32-byte scalar `k` at `scalar` and the base point's u-coordinate 9, to `*out`. The scalar is decoded (clamped). -/// -/// Contract: `VG.Spec.X25519.x25519BaseContract`. Constant time: only the pointers may affect timing, not the scalar. -/// -/// It runs `vg_x25519_base`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `out` must be valid for reads and writes of 32 bytes. -/// * `scalar` must be valid for reads of 32 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other or `scalar` (distinct Rust objects never do). -/// * None of `out`, `scalar` and `scratch` may wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_x25519_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], scratch: *mut [u64; 1024]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::x25519::vg_x25519_base(out, scalar, scratch) } -} - /// Computes an X25519 shared secret (RFC 7748 §6.1): writes `X25519(k, u)` to `*out`, for the 32-byte scalar `k` at `scalar` and the 32-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped) and the most significant bit of `u` masked as RFC 7748 specifies (§5). /// /// Contract: `VG.Spec.X25519.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_x25519_exchange_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_X25519_EXCHANGE_UMAAL_FEATURES`); or else `vg_x25519_exchange`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_x25519_exchange_umaal` if it is compiled (`any(target_os = "linux", target_os = "android", target_os = "freebsd", target_os = "netbsd", target_os = "openbsd")`) and the CPU supports its features (`VG_X25519_EXCHANGE_UMAAL_FEATURES`); or else `vg_x25519_exchange`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -74,3 +53,24 @@ pub unsafe fn vg_x25519_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // either carries the caller's obligations, which are those of every implementation. unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, scalar, point, scratch) } } + +/// Computes an X25519 public key (RFC 7748 §6.1): writes `X25519(k, 9)`, the X25519 function of the 32-byte scalar `k` at `scalar` and the base point's u-coordinate 9, to `*out`. The scalar is decoded (clamped). +/// +/// Contract: `VG.Spec.X25519.x25519BaseContract`. Constant time: only the pointers may affect timing, not the scalar. +/// +/// It runs `vg_x25519_base`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `out` must be valid for reads and writes of 32 bytes. +/// * `scalar` must be valid for reads of 32 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `out` and `scratch` must not overlap each other or `scalar` (distinct Rust objects never do). +/// * None of `out`, `scalar` and `scratch` may wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_x25519_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], scratch: *mut [u64; 1024]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::x25519::vg_x25519_base(out, scalar, scratch) } +} diff --git a/crates/verified-garbage-asm-arm/src/dispatch/x448.rs b/crates/verified-garbage-asm-arm/src/dispatch/x448.rs index ce49321730c..5b0bfb3669a 100644 --- a/crates/verified-garbage-asm-arm/src/dispatch/x448.rs +++ b/crates/verified-garbage-asm-arm/src/dispatch/x448.rs @@ -2,45 +2,45 @@ //! The dispatchers of the verified `x448` functions for `arm` //! (`crate::x448`, `VG.Rust.dispatcher`). -/// Computes an X448 public key (RFC 7748 §6.2): writes `X448(k, 5)`, the X448 function of the 56-byte scalar `k` at `scalar` and the base point's u-coordinate 5, to `*out`. The scalar is decoded (clamped). +/// Computes an X448 shared secret (RFC 7748 §6.2): writes `X448(k, u)` to `*out`, for the 56-byte scalar `k` at `scalar` and the 56-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped); all 448 bits of `u` are used and noncanonical coordinates are reduced modulo the field prime (§5). /// -/// Contract: `VG.Spec.X448.x448BaseContract`. Constant time: only the pointers may affect timing, not the scalar. +/// Contract: `VG.Spec.X448.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. /// -/// It runs `vg_x448_base`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_x448_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `out` must be valid for reads and writes of 56 bytes. /// * `scalar` must be valid for reads of 56 bytes. +/// * `point` must be valid for reads of 56 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other or `scalar` (distinct Rust objects never do). -/// * None of `out`, `scalar` and `scratch` may wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `scalar` or `point` (distinct Rust objects never do). +/// * None of `out`, `scalar`, `point` and `scratch` may overlap the 8 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x448_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], scratch: *mut [u64; 1024]) { +pub unsafe fn vg_x448_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], point: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::x448::vg_x448_base(out, scalar, scratch) } + unsafe { crate::x448::vg_x448_exchange(out, scalar, point, scratch) } } -/// Computes an X448 shared secret (RFC 7748 §6.2): writes `X448(k, u)` to `*out`, for the 56-byte scalar `k` at `scalar` and the 56-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped); all 448 bits of `u` are used and noncanonical coordinates are reduced modulo the field prime (§5). +/// Computes an X448 public key (RFC 7748 §6.2): writes `X448(k, 5)`, the X448 function of the 56-byte scalar `k` at `scalar` and the base point's u-coordinate 5, to `*out`. The scalar is decoded (clamped). /// -/// Contract: `VG.Spec.X448.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. +/// Contract: `VG.Spec.X448.x448BaseContract`. Constant time: only the pointers may affect timing, not the scalar. /// -/// It runs `vg_x448_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_x448_base`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `out` must be valid for reads and writes of 56 bytes. /// * `scalar` must be valid for reads of 56 bytes. -/// * `point` must be valid for reads of 56 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `scalar` or `point` (distinct Rust objects never do). -/// * None of `out`, `scalar`, `point` and `scratch` may overlap the 8 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other or `scalar` (distinct Rust objects never do). +/// * None of `out`, `scalar` and `scratch` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x448_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], point: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_x448_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], scratch: *mut [u64; 1024]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::x448::vg_x448_exchange(out, scalar, point, scratch) } + unsafe { crate::x448::vg_x448_base(out, scalar, scratch) } } diff --git a/crates/verified-garbage-asm-arm/src/fn_tables/mod.rs b/crates/verified-garbage-asm-arm/src/fn_tables/mod.rs index 706410c50b3..efeb93bfa6b 100644 --- a/crates/verified-garbage-asm-arm/src/fn_tables/mod.rs +++ b/crates/verified-garbage-asm-arm/src/fn_tables/mod.rs @@ -2,36 +2,36 @@ //! The tables of functions of the verified functions for `arm` (`Artifact.tables`). #![allow(dead_code, unused_imports)] -/// A table of functions of the interface `Hash` (`VG.FnTable`): the addresses of its 3 functions, then its 2 words of data. Only the statics of this module are of this type. +/// A table of functions of the interface `Hmac` (`VG.FnTable`): the addresses of its 3 functions, then its 3 words of data. Only the statics of this module are of this type. #[repr(C)] -pub struct Hash { +pub struct Hmac { fns: [*const (); 3], - data: [usize; 2], + data: [usize; 3], } -// SAFETY: a `Hash` is immutable, and the functions it holds the addresses of may be called from any thread. -unsafe impl Sync for Hash {} +// SAFETY: a `Hmac` is immutable, and the functions it holds the addresses of may be called from any thread. +unsafe impl Sync for Hmac {} -impl Hash { +impl Hmac { /// A pointer to the table's words, as the verified functions that take it read them. - pub const fn as_ptr(&'static self) -> *const [usize; 5] { + pub const fn as_ptr(&'static self) -> *const [usize; 6] { core::ptr::from_ref(self).cast() } } -/// A table of functions of the interface `Hmac` (`VG.FnTable`): the addresses of its 3 functions, then its 3 words of data. Only the statics of this module are of this type. +/// A table of functions of the interface `Hash` (`VG.FnTable`): the addresses of its 3 functions, then its 2 words of data. Only the statics of this module are of this type. #[repr(C)] -pub struct Hmac { +pub struct Hash { fns: [*const (); 3], - data: [usize; 3], + data: [usize; 2], } -// SAFETY: a `Hmac` is immutable, and the functions it holds the addresses of may be called from any thread. -unsafe impl Sync for Hmac {} +// SAFETY: a `Hash` is immutable, and the functions it holds the addresses of may be called from any thread. +unsafe impl Sync for Hash {} -impl Hmac { +impl Hash { /// A pointer to the table's words, as the verified functions that take it read them. - pub const fn as_ptr(&'static self) -> *const [usize; 6] { + pub const fn as_ptr(&'static self) -> *const [usize; 5] { core::ptr::from_ref(self).cast() } } @@ -53,38 +53,6 @@ impl MdHash { } } -#[rustfmt::skip] -mod vg_hash_md5; -pub use vg_hash_md5::VG_HASH_MD5; - -#[rustfmt::skip] -mod vg_hash_sha1; -pub use vg_hash_sha1::VG_HASH_SHA1; - -#[rustfmt::skip] -mod vg_hash_sha224; -pub use vg_hash_sha224::VG_HASH_SHA224; - -#[rustfmt::skip] -mod vg_hash_sha256; -pub use vg_hash_sha256::VG_HASH_SHA256; - -#[rustfmt::skip] -mod vg_hash_sha384; -pub use vg_hash_sha384::VG_HASH_SHA384; - -#[rustfmt::skip] -mod vg_hash_sha512; -pub use vg_hash_sha512::VG_HASH_SHA512; - -#[rustfmt::skip] -mod vg_hash_sha512_224; -pub use vg_hash_sha512_224::VG_HASH_SHA512_224; - -#[rustfmt::skip] -mod vg_hash_sha512_256; -pub use vg_hash_sha512_256::VG_HASH_SHA512_256; - #[rustfmt::skip] mod vg_hmac_md5; pub use vg_hmac_md5::VG_HMAC_MD5; @@ -117,6 +85,38 @@ pub use vg_hmac_sha512_224::VG_HMAC_SHA512_224; mod vg_hmac_sha512_256; pub use vg_hmac_sha512_256::VG_HMAC_SHA512_256; +#[rustfmt::skip] +mod vg_hash_md5; +pub use vg_hash_md5::VG_HASH_MD5; + +#[rustfmt::skip] +mod vg_hash_sha1; +pub use vg_hash_sha1::VG_HASH_SHA1; + +#[rustfmt::skip] +mod vg_hash_sha224; +pub use vg_hash_sha224::VG_HASH_SHA224; + +#[rustfmt::skip] +mod vg_hash_sha256; +pub use vg_hash_sha256::VG_HASH_SHA256; + +#[rustfmt::skip] +mod vg_hash_sha384; +pub use vg_hash_sha384::VG_HASH_SHA384; + +#[rustfmt::skip] +mod vg_hash_sha512; +pub use vg_hash_sha512::VG_HASH_SHA512; + +#[rustfmt::skip] +mod vg_hash_sha512_224; +pub use vg_hash_sha512_224::VG_HASH_SHA512_224; + +#[rustfmt::skip] +mod vg_hash_sha512_256; +pub use vg_hash_sha512_256::VG_HASH_SHA512_256; + #[rustfmt::skip] mod vg_md_hash_md5; pub use vg_md_hash_md5::VG_MD_HASH_MD5; diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/chacha20.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/chacha20.rs index d250012f09f..23250dfa25b 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/chacha20.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/chacha20.rs @@ -22,40 +22,40 @@ pub unsafe fn vg_chacha20_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: unsafe { crate::chacha20::vg_chacha20_init(state, key, nonce) } } -/// Applies a ChaCha20 keystream (RFC 8439 §2.4), encrypting or decrypting: if at least `len` bytes are left of the keystream that the streaming state `*state` represents, XORs the next `len` of them into the `len` bytes at `data`, keeps the rest in `*state`, and returns 1. Otherwise (the block counter would pass 2³² − 1) returns 0, and leaves the bytes at `data` and the keystream unchanged. +/// Restarts a ChaCha20 keystream with the same key: makes the streaming state `*state` represent the whole keystream of its key for the 16-byte nonce `*nonce` (as `vg_chacha20_init` does), discarding what was left of the previous one. /// -/// Contract: `VG.Spec.ChaCha20.applyContract`. Constant time: only the pointers, `len` and the number of bytes of keystream left in `*state` (`VG.Spec.ChaCha20.leftAt`: 64 × (2³² − `c`) for the initial block counter `c`, less the bytes applied since) may affect timing, not the key, the nonce, the keystream or the data. The function may leak that number. +/// Contract: `VG.Spec.ChaCha20.setNonceContract`. Constant time: only the pointers may affect timing, not the key or the nonce. /// -/// It runs `vg_chacha20_apply`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_chacha20_set_nonce`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 768 bytes. -/// * `data` must be valid for reads and writes of `len` bytes. -/// * `state` and `data` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `data` may wrap around the end of the address space (no Rust object does). +/// * `nonce` must be valid for reads of 16 bytes. +/// * `state` must not overlap `nonce` (distinct Rust objects never do). +/// * Neither `state` nor `nonce` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_chacha20_apply_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 96], data: *mut u8, len: usize) -> u32 { +pub unsafe fn vg_chacha20_set_nonce_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 96], nonce: *const [u8; 16]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::chacha20::vg_chacha20_apply(state, data, len) } + unsafe { crate::chacha20::vg_chacha20_set_nonce(state, nonce) } } -/// Restarts a ChaCha20 keystream with the same key: makes the streaming state `*state` represent the whole keystream of its key for the 16-byte nonce `*nonce` (as `vg_chacha20_init` does), discarding what was left of the previous one. +/// Applies a ChaCha20 keystream (RFC 8439 §2.4), encrypting or decrypting: if at least `len` bytes are left of the keystream that the streaming state `*state` represents, XORs the next `len` of them into the `len` bytes at `data`, keeps the rest in `*state`, and returns 1. Otherwise (the block counter would pass 2³² − 1) returns 0, and leaves the bytes at `data` and the keystream unchanged. /// -/// Contract: `VG.Spec.ChaCha20.setNonceContract`. Constant time: only the pointers may affect timing, not the key or the nonce. +/// Contract: `VG.Spec.ChaCha20.applyContract`. Constant time: only the pointers, `len` and the number of bytes of keystream left in `*state` (`VG.Spec.ChaCha20.leftAt`: 64 × (2³² − `c`) for the initial block counter `c`, less the bytes applied since) may affect timing, not the key, the nonce, the keystream or the data. The function may leak that number. /// -/// It runs `vg_chacha20_set_nonce`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_chacha20_apply`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 768 bytes. -/// * `nonce` must be valid for reads of 16 bytes. -/// * `state` must not overlap `nonce` (distinct Rust objects never do). -/// * Neither `state` nor `nonce` may wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads and writes of `len` bytes. +/// * `state` and `data` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `data` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_chacha20_set_nonce_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 96], nonce: *const [u8; 16]) { +pub unsafe fn vg_chacha20_apply_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 96], data: *mut u8, len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::chacha20::vg_chacha20_set_nonce(state, nonce) } + unsafe { crate::chacha20::vg_chacha20_apply(state, data, len) } } diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/cmac_triple_des.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/cmac_triple_des.rs index 436342640e5..ad8d2cc53bf 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/cmac_triple_des.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/cmac_triple_des.rs @@ -2,6 +2,26 @@ //! The dispatchers of the verified `cmac_triple_des` functions for `powerpc64le` //! (`crate::cmac_triple_des`, `VG.Rust.dispatcher`). +/// Starts a TDEA-CMAC computation (NIST SP 800-38B): makes the streaming state `*state` represent the empty message under the 16- or 24-byte TDEA key at `key` (FIPS 46-3; a 16-byte key is `K1 ‖ K2` with `K3 = K1`, parity bits are ignored). The state holds the key schedule (the three DES schedules), the subkeys `K1 ‖ K2` (§6.1), the chaining value and the message's last bytes, held back (`VG.Spec.Cmac.tdesRepr`). Continue with `vg_cmac_triple_des_absorb` and `vg_cmac_triple_des_finish`. +/// +/// Contract: `VG.Spec.Cmac.tdesStreamInitContract`. Constant time: only the pointers and `key_len` may affect timing, not the key. +/// +/// It runs `vg_cmac_triple_des_stream_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 416 bytes. +/// * `key` must be valid for reads of `key_len` bytes. +/// * `key_len` must be 16 or 24. +/// * `state` must not overlap `key` (distinct Rust objects never do). +/// * Neither `state` nor `key` may overlap the 672 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_cmac_triple_des_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 52], key: *const u8, key_len: usize) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::cmac_triple_des::vg_cmac_triple_des_stream_init(state, key, key_len) } +} + /// Absorbs data into a TDEA-CMAC computation (NIST SP 800-38B §6.2): if the streaming state `*state` represents a message of `count` bytes under a TDEA key (as `vg_cmac_triple_des_stream_init` set it up), it then represents that message followed by the `len` bytes at `data`, under the same key, provided that the two together are shorter than 2⁶⁴ bytes. It chains (step 6) every block of the message but its last bytes `Mₙ*` (step 3), which it holds back in the state, since only `vg_cmac_triple_des_finish` knows they are the last. /// /// Contract: `VG.Spec.Cmac.tdesAbsorbContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. @@ -40,23 +60,3 @@ pub unsafe fn vg_cmac_triple_des_finish_auto(_dit: verified_garbage_cpu::Dit<'_> // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::cmac_triple_des::vg_cmac_triple_des_finish(state, count, out) } } - -/// Starts a TDEA-CMAC computation (NIST SP 800-38B): makes the streaming state `*state` represent the empty message under the 16- or 24-byte TDEA key at `key` (FIPS 46-3; a 16-byte key is `K1 ‖ K2` with `K3 = K1`, parity bits are ignored). The state holds the key schedule (the three DES schedules), the subkeys `K1 ‖ K2` (§6.1), the chaining value and the message's last bytes, held back (`VG.Spec.Cmac.tdesRepr`). Continue with `vg_cmac_triple_des_absorb` and `vg_cmac_triple_des_finish`. -/// -/// Contract: `VG.Spec.Cmac.tdesStreamInitContract`. Constant time: only the pointers and `key_len` may affect timing, not the key. -/// -/// It runs `vg_cmac_triple_des_stream_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 416 bytes. -/// * `key` must be valid for reads of `key_len` bytes. -/// * `key_len` must be 16 or 24. -/// * `state` must not overlap `key` (distinct Rust objects never do). -/// * Neither `state` nor `key` may overlap the 672 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_cmac_triple_des_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 52], key: *const u8, key_len: usize) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::cmac_triple_des::vg_cmac_triple_des_stream_init(state, key, key_len) } -} diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/dsa.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/dsa.rs index 9e9eaf6a05c..86c6dae6547 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/dsa.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/dsa.rs @@ -24,6 +24,32 @@ pub unsafe fn vg_dsa_derive_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: * unsafe { crate::dsa::vg_dsa_derive_key(out, out_len, params, params_len, p_len, x, q_len, scratch, scratch_len) } } +/// Randomized DSA signing of a prehashed digest, for an already validated packed private key (big-endian p||q||g||y||x; vg_dsa_validate_private_key validates it). Up to eight signing attempts, each with 128 nonce draws. Returns 1 with fixed-width r||s, 0 for retry exhaustion, or 2 for insufficient random bytes; out is zero on failure and used counts consumed complete draws on every return. Timing may depend on public p,q,g,y, sampling decisions, signing success bits and consumption, not otherwise on secrets. Contract: `VG.Spec.Dsa.randomSignPrevalidatedContract`. +/// +/// It runs `vg_dsa_sign_random_prevalidated`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `out` must be valid for reads and writes of `out_len` bytes. +/// * `used` must be valid for reads and writes of 8 bytes. +/// * `key` must be valid for reads of `key_len` bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `rand` must be valid for reads of `rand_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). +/// * key_len must be 3*p_len + 2*q_len; out_len must be 2*q_len. +/// * scratch_len must be at least 16*p_len + 64 words; destroy scratch after use. +/// * The exact key bytes must satisfy validatePrivateKeyBytes p_len q_len key = true. A successful vg_dsa_validate_private_key call establishes this only while the key bytes remain unchanged. Primality remains the caller's responsibility. +/// * Provide fresh independent random octets; never reuse a completed signing tape for another signature. +/// * `out`, `used` and `scratch` must not overlap each other, `key`, `digest`, `rand` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `used`, `key`, `digest`, `rand` and `scratch` may overlap the 304 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_dsa_sign_random_prevalidated_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], key: *const u8, key_len: usize, p_len: usize, q_len: usize, digest: *const u8, digest_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::dsa::vg_dsa_sign_random_prevalidated(out, out_len, used, key, key_len, p_len, q_len, digest, digest_len, rand, rand_len, scratch, scratch_len) } +} + /// Generate a DSA private key from p||q||g and fresh independent random bytes. Uses at most 128 rejection-sampling draws, computes y=g^x mod p, and checks algebraic consistency, not primality. Returns 1 with p||q||g||y||x, 0 for invalid input/draw limit, or 2 for insufficient randomness. Failure zeros out; used counts consumed complete draws on every return. Timing may depend on public parameters, sampling decisions, consumption and success, not otherwise on x or randomness. Contract: `VG.Spec.Dsa.generateKeyContract`. /// /// It runs `vg_dsa_generate_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. @@ -48,50 +74,42 @@ pub unsafe fn vg_dsa_generate_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: unsafe { crate::dsa::vg_dsa_generate_key(out, out_len, used, params, params_len, p_len, q_len, rand, rand_len, scratch, scratch_len) } } -/// Search for DSA domain parameters (FIPS 186-4 A.1.1.2 and A.2.1) over the random tape `rand`, for the modulus width in `state`: each seed, its `q`, the `p` candidates from it and the generator's `h` candidates are read from `rand` in turn, with the rejected seeds counted in `state`. Returns 1 and writes `p || q || g` to `out` once parameters are found; 3 if the search is exhausted, by too many rejected seeds or by one seed needing more than the entropy budget; or 0 if the tape ran out inside a seed. Writes the octets of `rand` it consumed to `used`, and the state for the next call to `state`, whose last word is, for 0, the length of the next tape, more than the octets of `rand` after the first `used` and at most the entropy budget: keep those octets, in order, and append fresh random octets up to that length. `out` is zeros but for 1. Accepted parameters pass probable-prime tests, not primality certification. -/// -/// Contract: `VG.Spec.Dsa.parameterSearchContract`. Timing may depend on everything, `rand` included: these are public domain parameters. +/// Validate packed DSA p||q||g||y, including domain consistency and both subgroup memberships. Returns 1 exactly when validation succeeds, otherwise 0. Does not test primality. The key is public; scratch contents are unspecified on return. Contract: `VG.Spec.Dsa.validatePublicKeyContract`. /// -/// It runs `vg_dsa_parameter_search`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_dsa_validate_public_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `out` must be valid for reads and writes of `out_len` bytes. -/// * `used` must be valid for reads and writes of 8 bytes. -/// * `rand` must be valid for reads of `rand_len` bytes. +/// * `key` must be valid for reads of `key_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `state` must be valid for reads and writes of 32 bytes. -/// * `state` must come from `vg_dsa_parameter_search_init`, or from the last call of this function that returned 0. -/// * `out_len` must be `2 * state[1]` plus 20 if `state[1]` is 128, else plus 32. -/// * `rand_len` must be at most the entropy budget, `state[2]`. -/// * `scratch_len` must be at least `64 * state[1] + 1024` words. -/// * `rand` must hold independent uniform random octets. They are public under this contract and must never be used for private scalars or nonces. -/// * `out`, `used`, `scratch` and `state` must not overlap each other or `rand` (distinct Rust objects never do). -/// * None of `out`, `used`, `rand`, `scratch` and `state` may overlap the 176 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). +/// * key_len must be 3*p_len + q_len; scratch_len must be at least 16*p_len words. +/// * `scratch` must not overlap `key` (distinct Rust objects never do). +/// * Neither `key` nor `scratch` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_dsa_parameter_search_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32 { +pub unsafe fn vg_dsa_validate_public_key_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *const u8, key_len: usize, p_len: usize, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::dsa::vg_dsa_parameter_search(out, out_len, used, rand, rand_len, scratch, scratch_len, state) } + unsafe { crate::dsa::vg_dsa_validate_public_key(key, key_len, p_len, q_len, scratch, scratch_len) } } -/// Validate packed DSA p||q||g||y, including domain consistency and both subgroup memberships. Returns 1 exactly when validation succeeds, otherwise 0. Does not test primality. The key is public; scratch contents are unspecified on return. Contract: `VG.Spec.Dsa.validatePublicKeyContract`. +/// Validate packed DSA p||q||g||y||x as signing checks it: domain consistency, including g^q mod p = 1, 1 < y < p, 0 < x < q and g^x mod p = y. Returns 1 exactly when validation succeeds, otherwise 0. Does not test primality. Timing may depend on p,q,g,y and the result, not otherwise on x. Scratch contents are unspecified on return and may hold secrets. Contract: `VG.Spec.Dsa.validatePrivateKeyContract`. /// -/// It runs `vg_dsa_validate_public_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_dsa_validate_private_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `key` must be valid for reads of `key_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. /// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). -/// * key_len must be 3*p_len + q_len; scratch_len must be at least 16*p_len words. +/// * key_len must be 3*p_len + 2*q_len; scratch_len must be at least 16*p_len words; destroy scratch after use. /// * `scratch` must not overlap `key` (distinct Rust objects never do). /// * Neither `key` nor `scratch` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_dsa_validate_public_key_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *const u8, key_len: usize, p_len: usize, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_dsa_validate_private_key_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *const u8, key_len: usize, p_len: usize, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::dsa::vg_dsa_validate_public_key(key, key_len, p_len, q_len, scratch, scratch_len) } + unsafe { crate::dsa::vg_dsa_validate_private_key(key, key_len, p_len, q_len, scratch, scratch_len) } } /// DSA verification of a prehashed digest, for an already validated packed public key (big-endian p||q||g||y with widths p_len, q_len, p_len, p_len; vg_dsa_validate_public_key validates it). The signature is r||s with q_len bytes each. Returns 1 if verification succeeds, otherwise 0: the result of fully checked verification (`VG.Spec.Dsa.verifyContract`), whose checks of the signature scalars and digest handling it keeps. Timing may depend on p,q,g,y and the result, not otherwise on signature or digest. Contract: `VG.Spec.Dsa.verifyPrevalidatedContract`. @@ -117,25 +135,6 @@ pub unsafe fn vg_dsa_verify_prevalidated_auto(_dit: verified_garbage_cpu::Dit<'_ unsafe { crate::dsa::vg_dsa_verify_prevalidated(key, key_len, p_len, q_len, signature, signature_len, digest, digest_len, scratch, scratch_len) } } -/// Validate packed DSA p||q||g||y||x as signing checks it: domain consistency, including g^q mod p = 1, 1 < y < p, 0 < x < q and g^x mod p = y. Returns 1 exactly when validation succeeds, otherwise 0. Does not test primality. Timing may depend on p,q,g,y and the result, not otherwise on x. Scratch contents are unspecified on return and may hold secrets. Contract: `VG.Spec.Dsa.validatePrivateKeyContract`. -/// -/// It runs `vg_dsa_validate_private_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `key` must be valid for reads of `key_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). -/// * key_len must be 3*p_len + 2*q_len; scratch_len must be at least 16*p_len words; destroy scratch after use. -/// * `scratch` must not overlap `key` (distinct Rust objects never do). -/// * Neither `key` nor `scratch` may wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_dsa_validate_private_key_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *const u8, key_len: usize, p_len: usize, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::dsa::vg_dsa_validate_private_key(key, key_len, p_len, q_len, scratch, scratch_len) } -} - /// Start a search for DSA domain parameters with a modulus of `p_len` octets (128, 256 or 384: 1024, 2048 or 3072 bits, with a subgroup of 160, 256 and 256 bits). Returns 1 and writes the search's four public words to `state`: the seeds it may still reject, `p_len`, its entropy budget, and the length of the first random tape to give `vg_dsa_parameter_search`. Returns 0 and writes zeros for any other `p_len`. /// /// Contract: `VG.Spec.Dsa.parameterInitContract`. @@ -153,28 +152,29 @@ pub unsafe fn vg_dsa_parameter_search_init_auto(_dit: verified_garbage_cpu::Dit< unsafe { crate::dsa::vg_dsa_parameter_search_init(state, p_len) } } -/// Randomized DSA signing of a prehashed digest, for an already validated packed private key (big-endian p||q||g||y||x; vg_dsa_validate_private_key validates it). Up to eight signing attempts, each with 128 nonce draws. Returns 1 with fixed-width r||s, 0 for retry exhaustion, or 2 for insufficient random bytes; out is zero on failure and used counts consumed complete draws on every return. Timing may depend on public p,q,g,y, sampling decisions, signing success bits and consumption, not otherwise on secrets. Contract: `VG.Spec.Dsa.randomSignPrevalidatedContract`. +/// Search for DSA domain parameters (FIPS 186-4 A.1.1.2 and A.2.1) over the random tape `rand`, for the modulus width in `state`: each seed, its `q`, the `p` candidates from it and the generator's `h` candidates are read from `rand` in turn, with the rejected seeds counted in `state`. Returns 1 and writes `p || q || g` to `out` once parameters are found; 3 if the search is exhausted, by too many rejected seeds or by one seed needing more than the entropy budget; or 0 if the tape ran out inside a seed. Writes the octets of `rand` it consumed to `used`, and the state for the next call to `state`, whose last word is, for 0, the length of the next tape, more than the octets of `rand` after the first `used` and at most the entropy budget: keep those octets, in order, and append fresh random octets up to that length. `out` is zeros but for 1. Accepted parameters pass probable-prime tests, not primality certification. /// -/// It runs `vg_dsa_sign_random_prevalidated`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// Contract: `VG.Spec.Dsa.parameterSearchContract`. Timing may depend on everything, `rand` included: these are public domain parameters. +/// +/// It runs `vg_dsa_parameter_search`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `out` must be valid for reads and writes of `out_len` bytes. /// * `used` must be valid for reads and writes of 8 bytes. -/// * `key` must be valid for reads of `key_len` bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. /// * `rand` must be valid for reads of `rand_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). -/// * key_len must be 3*p_len + 2*q_len; out_len must be 2*q_len. -/// * scratch_len must be at least 16*p_len + 64 words; destroy scratch after use. -/// * The exact key bytes must satisfy validatePrivateKeyBytes p_len q_len key = true. A successful vg_dsa_validate_private_key call establishes this only while the key bytes remain unchanged. Primality remains the caller's responsibility. -/// * Provide fresh independent random octets; never reuse a completed signing tape for another signature. -/// * `out`, `used` and `scratch` must not overlap each other, `key`, `digest`, `rand` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `used`, `key`, `digest`, `rand` and `scratch` may overlap the 304 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must be valid for reads and writes of 32 bytes. +/// * `state` must come from `vg_dsa_parameter_search_init`, or from the last call of this function that returned 0. +/// * `out_len` must be `2 * state[1]` plus 20 if `state[1]` is 128, else plus 32. +/// * `rand_len` must be at most the entropy budget, `state[2]`. +/// * `scratch_len` must be at least `64 * state[1] + 1024` words. +/// * `rand` must hold independent uniform random octets. They are public under this contract and must never be used for private scalars or nonces. +/// * `out`, `used`, `scratch` and `state` must not overlap each other or `rand` (distinct Rust objects never do). +/// * None of `out`, `used`, `rand`, `scratch` and `state` may overlap the 176 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_dsa_sign_random_prevalidated_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], key: *const u8, key_len: usize, p_len: usize, q_len: usize, digest: *const u8, digest_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_dsa_parameter_search_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::dsa::vg_dsa_sign_random_prevalidated(out, out_len, used, key, key_len, p_len, q_len, digest, digest_len, rand, rand_len, scratch, scratch_len) } + unsafe { crate::dsa::vg_dsa_parameter_search(out, out_len, used, rand, rand_len, scratch, scratch_len, state) } } diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_brainpoolp256r1.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_brainpoolp256r1.rs index 5672d545a2f..f5b72163bc3 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_brainpoolp256r1.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_brainpoolp256r1.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_brainpoolp256r1` functions for `powerpc64le` //! (`crate::ecdsa_brainpoolp256r1`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over brainpoolP256r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_brainpoolp256r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 65 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 64 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 112 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_brainpoolp256r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over brainpoolP256r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_brainpoolp256r1_sign_auto(_dit: verified_garbage_cpu::Dit // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over brainpoolP256r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_brainpoolp256r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 65 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 64 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 112 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_brainpoolp256r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_brainpoolp384r1.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_brainpoolp384r1.rs index 8df3690bf74..31c03903ab3 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_brainpoolp384r1.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_brainpoolp384r1.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_brainpoolp384r1` functions for `powerpc64le` //! (`crate::ecdsa_brainpoolp384r1`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over brainpoolP384r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_brainpoolp384r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 97 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 96 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 112 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_brainpoolp384r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over brainpoolP384r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (48 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (48 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_brainpoolp384r1_sign_auto(_dit: verified_garbage_cpu::Dit // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over brainpoolP384r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_brainpoolp384r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 97 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 96 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 112 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_brainpoolp384r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_brainpoolp512r1.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_brainpoolp512r1.rs index 3f9ef7e02e0..cf4124c9d77 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_brainpoolp512r1.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_brainpoolp512r1.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_brainpoolp512r1` functions for `powerpc64le` //! (`crate::ecdsa_brainpoolp512r1`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over brainpoolP512r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 64 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 512 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 64 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_brainpoolp512r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 129 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 128 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 112 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_brainpoolp512r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 129], digest: *const u8, digest_len: usize, sig: *const [u8; 128], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over brainpoolP512r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (64 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (64 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_brainpoolp512r1_sign_auto(_dit: verified_garbage_cpu::Dit // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over brainpoolP512r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 64 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 512 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 64 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_brainpoolp512r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 129 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 128 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 112 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_brainpoolp512r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 129], digest: *const u8, digest_len: usize, sig: *const [u8; 128], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_p192.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_p192.rs index afd9a3fefec..bdd95c435b9 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_p192.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_p192.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_p192` functions for `powerpc64le` //! (`crate::ecdsa_p192`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over P-192 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 24 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 192 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 24 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_p192_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 49 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 48 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 112 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_p192_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 49], digest: *const u8, digest_len: usize, sig: *const [u8; 48], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_p192::vg_ecdsa_p192_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over P-192 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (24 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (24 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_p192_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_p192::vg_ecdsa_p192_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over P-192 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 24 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 192 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 24 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_p192_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 49 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 48 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 112 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_p192_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 49], digest: *const u8, digest_len: usize, sig: *const [u8; 48], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p192::vg_ecdsa_p192_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_p224.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_p224.rs index 8fbf16bb213..bfa22573f2f 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_p224.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_p224.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_p224` functions for `powerpc64le` //! (`crate::ecdsa_p224`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over P-224 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 28 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 224 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 28 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_p224_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 57 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 56 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 112 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_p224_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 57], digest: *const u8, digest_len: usize, sig: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_p224::vg_ecdsa_p224_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over P-224 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (28 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (28 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_p224_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_p224::vg_ecdsa_p224_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over P-224 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 28 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 224 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 28 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_p224_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 57 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 56 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 112 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_p224_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 57], digest: *const u8, digest_len: usize, sig: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p224::vg_ecdsa_p224_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_p256.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_p256.rs index 1337b85c986..77ab523f8d4 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_p256.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_p256.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_p256` functions for `powerpc64le` //! (`crate::ecdsa_p256`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over P-256 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_p256_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 65 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 64 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 112 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_p256_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_p256::vg_ecdsa_p256_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over P-256 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_p256_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_p256::vg_ecdsa_p256_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over P-256 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_p256_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 65 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 64 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 112 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_p256_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p256::vg_ecdsa_p256_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_p384.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_p384.rs index eb92ae7816a..5e7d6ea8fd6 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_p384.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_p384.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_p384` functions for `powerpc64le` //! (`crate::ecdsa_p384`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over P-384 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_p384_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 97 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 96 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 112 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_p384_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_p384::vg_ecdsa_p384_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over P-384 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (48 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (48 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_p384_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_p384::vg_ecdsa_p384_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over P-384 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_p384_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 97 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 96 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 112 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_p384_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p384::vg_ecdsa_p384_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_p521.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_p521.rs index 6c1596a9ea8..1c4cd1e21ad 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_p521.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_p521.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_p521` functions for `powerpc64le` //! (`crate::ecdsa_p521`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over P-521 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 66 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 521 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 66 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_p521_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 133 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 132 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 112 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_p521_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 133], digest: *const u8, digest_len: usize, sig: *const [u8; 132], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_p521::vg_ecdsa_p521_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over P-521 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (66 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (66 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_p521_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_p521::vg_ecdsa_p521_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over P-521 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 66 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 521 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 66 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_p521_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 133 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 132 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 112 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_p521_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 133], digest: *const u8, digest_len: usize, sig: *const [u8; 132], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p521::vg_ecdsa_p521_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_secp256k1.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_secp256k1.rs index 5109f1a7a44..fc4f9be8bab 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_secp256k1.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ecdsa_secp256k1.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_secp256k1` functions for `powerpc64le` //! (`crate::ecdsa_secp256k1`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over secp256k1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_secp256k1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 65 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 64 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 112 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_secp256k1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over secp256k1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_secp256k1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over secp256k1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_secp256k1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 65 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 64 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 112 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_secp256k1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ed25519.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ed25519.rs index 5f235d93094..8ab68f5990c 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ed25519.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ed25519.rs @@ -2,26 +2,6 @@ //! The dispatchers of the verified `ed25519` functions for `powerpc64le` //! (`crate::ed25519`, `VG.Rust.dispatcher`). -/// Ed25519 verification (RFC 8032 §5.1.7): returns 1 if the 64-byte signature at `signature` verifies for the 32-byte public key at `pk` and the `len` bytes at `message`, and 0 otherwise. Uses pure Ed25519, with no context or prehash. Checks canonical point encodings, S < L, and `[S]B = R + [k]A` with the challenge k = SHA-512(R || A || M) reduced modulo L, as in RFC 8032 §6. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed25519.verifyContract`. Not constant time: timing may depend on the public key, message and signature. -/// -/// It runs `vg_ed25519_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 32 bytes. -/// * `message` must be valid for reads of `len` bytes. -/// * `signature` must be valid for reads of 64 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `scratch` must not overlap `pk`, `message` or `signature` (distinct Rust objects never do). -/// * None of `pk`, `message`, `signature` and `scratch` may overlap the 272 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ed25519_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 32], message: *const u8, len: usize, signature: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ed25519::vg_ed25519_verify(pk, message, len, signature, scratch) } -} - /// Ed25519 key-pair derivation (RFC 8032 §5.1.5): for the 32-byte private seed at `seed`, writes its 64-byte SHA-512 digest (§5.1.5, step 1) to `*expanded` and its 32-byte public key to `*out`, including pruning: the digest and public key that `vg_ed25519_sign_expanded` takes. Contract: `VG.Spec.Ed25519.keyPairContract`. Constant time: only pointers may affect timing. /// /// It runs `vg_ed25519_key_pair`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. @@ -66,3 +46,23 @@ pub unsafe fn vg_ed25519_sign_expanded_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ed25519::vg_ed25519_sign_expanded(out, expanded, pk, message, len, scratch) } } + +/// Ed25519 verification (RFC 8032 §5.1.7): returns 1 if the 64-byte signature at `signature` verifies for the 32-byte public key at `pk` and the `len` bytes at `message`, and 0 otherwise. Uses pure Ed25519, with no context or prehash. Checks canonical point encodings, S < L, and `[S]B = R + [k]A` with the challenge k = SHA-512(R || A || M) reduced modulo L, as in RFC 8032 §6. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed25519.verifyContract`. Not constant time: timing may depend on the public key, message and signature. +/// +/// It runs `vg_ed25519_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 32 bytes. +/// * `message` must be valid for reads of `len` bytes. +/// * `signature` must be valid for reads of 64 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `scratch` must not overlap `pk`, `message` or `signature` (distinct Rust objects never do). +/// * None of `pk`, `message`, `signature` and `scratch` may overlap the 272 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ed25519_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 32], message: *const u8, len: usize, signature: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ed25519::vg_ed25519_verify(pk, message, len, signature, scratch) } +} diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ed448.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ed448.rs index a3302af7ced..d32ac5f19e9 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ed448.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ed448.rs @@ -2,27 +2,6 @@ //! The dispatchers of the verified `ed448` functions for `powerpc64le` //! (`crate::ed448`, `VG.Rust.dispatcher`). -/// Ed448 verification (RFC 8032 §5.2.7): returns 1 if the 114-byte signature at `signature` verifies for the 57-byte public key at `pk`, the `ctxlen` bytes of context at `context` and the `len` bytes at `message`, and 0 otherwise (always 0 for a context longer than 255 bytes). Uses Ed448 with a context, not Ed448ph (no prehash). Checks canonical point encodings, S < L, and the cofactored equation `[4][S]B = [4]R + [4][k]A` with the challenge k = SHAKE256(dom4(0, C) || R || A || M, 114) reduced modulo L. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed448.verifyContract`. Not constant time: timing may depend on the public key, context, message and signature. -/// -/// It runs `vg_ed448_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 57 bytes. -/// * `context` must be valid for reads of `ctxlen` bytes. -/// * `message` must be valid for reads of `len` bytes. -/// * `signature` must be valid for reads of 114 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `scratch` must not overlap `pk`, `context`, `message` or `signature` (distinct Rust objects never do). -/// * None of `pk`, `context`, `message`, `signature` and `scratch` may overlap the 352 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ed448_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, signature: *const [u8; 114], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ed448::vg_ed448_verify(pk, context, ctxlen, message, len, signature, scratch) } -} - /// Ed448 public-key derivation (RFC 8032 §5.2.5): writes the 57-byte public key to `*out`, from the 57-byte private key at `seed`, including SHAKE256 and pruning. Contract: `VG.Spec.Ed448.publicKeyContract`. Constant time: only pointers may affect timing. /// /// It runs `vg_ed448_public_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. @@ -67,3 +46,24 @@ pub unsafe fn vg_ed448_sign_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, out // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ed448::vg_ed448_sign_cached(out, seed, pk, context, ctxlen, message, len, scratch) } } + +/// Ed448 verification (RFC 8032 §5.2.7): returns 1 if the 114-byte signature at `signature` verifies for the 57-byte public key at `pk`, the `ctxlen` bytes of context at `context` and the `len` bytes at `message`, and 0 otherwise (always 0 for a context longer than 255 bytes). Uses Ed448 with a context, not Ed448ph (no prehash). Checks canonical point encodings, S < L, and the cofactored equation `[4][S]B = [4]R + [4][k]A` with the challenge k = SHAKE256(dom4(0, C) || R || A || M, 114) reduced modulo L. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed448.verifyContract`. Not constant time: timing may depend on the public key, context, message and signature. +/// +/// It runs `vg_ed448_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 57 bytes. +/// * `context` must be valid for reads of `ctxlen` bytes. +/// * `message` must be valid for reads of `len` bytes. +/// * `signature` must be valid for reads of 114 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `scratch` must not overlap `pk`, `context`, `message` or `signature` (distinct Rust objects never do). +/// * None of `pk`, `context`, `message`, `signature` and `scratch` may overlap the 352 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ed448_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, signature: *const [u8; 114], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ed448::vg_ed448_verify(pk, context, ctxlen, message, len, signature, scratch) } +} diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ffdh.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ffdh.rs index 14356f20bb1..4eac9e80a40 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/ffdh.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/ffdh.rs @@ -2,34 +2,34 @@ //! The dispatchers of the verified `ffdh` functions for `powerpc64le` //! (`crate::ffdh`, `VG.Rust.dispatcher`). -/// Finite-field Diffie–Hellman (SP 800-56A Rev. 3 §5.7.1.1, PKCS #3 §8). With `domain` as `vg_ffdh_check_domain` takes it, the private scalar x (`x_len` bytes) and the peer's public value y (`p_len` bytes), most significant first, computes `z = y^x mod p`; returns 0 and writes zeros if z is 0, 1 or `p − 1`, and otherwise returns 1 and writes z (`p_len` bytes, most significant first) to `out`. Applies no KDF. +/// Initialize native finite-field Diffie–Hellman parameter generation. Validates 512..=8192 bits and generator 2 or 5. Returns 1 and initializes the controller's attempt limit, output width, entropy budget and first entropy request; returns 0 and clears those four words on invalid parameters. The remaining state words are private workspace. Contract: `VG.Spec.Ffdh.parameterInitContract`. /// -/// Contract: `VG.Spec.Ffdh.exchangeContract`. Timing may depend on the pointers, the lengths, the domain, y and the value returned, not otherwise on x; the exponentiation may run over all `x_len` bytes of x, leading zeros included. +/// It runs `vg_ffdh_parameter_generation_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// -/// It runs `vg_ffdh_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// # Safety +/// +/// * `state` must be valid for reads and writes of 128 bytes. +/// * `state` must not wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ffdh_parameter_generation_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 16], bits: usize, generator: u32) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ffdh::vg_ffdh_parameter_generation_init(state, bits, generator) } +} + +/// Initialize native finite-field Diffie–Hellman key generation. Returns 1 for a domain width in 64..=1024 bytes, initializing the rejection budgets and first entropy request; returns 0 and clears the first four state words otherwise. The remaining state words are private workspace. Contract: `VG.Spec.Ffdh.keyInitContract`. +/// +/// It runs `vg_ffdh_key_generation_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `out` must be valid for reads and writes of `out_len` bytes. -/// * `domain` must be valid for reads of `domain_len` bytes. -/// * `x` must be valid for reads of `x_len` bytes. -/// * `peer` must be valid for reads of `peer_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `p_len` must be in 64..=1024. -/// * `domain_len` must be `2 * p_len` or `3 * p_len`. -/// * `out_len` and `peer_len` must be `p_len`. -/// * `x_len` must be in 1..=`p_len`. -/// * For the result to be specified, `domain` must be one that `vg_ffdh_check_domain` accepted (returned 1 for); otherwise it is unspecified. -/// * For the result to be specified, `peer` must also be a value that `vg_ffdh_check_public` accepted for this domain. -/// * `scratch_len` must be at least `16 * p_len + 32`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `domain`, `x`, `peer` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `domain`, `x`, `peer` and `scratch` may wrap around the end of the address space (no Rust object does). +/// * `state` must be valid for reads and writes of 128 bytes. +/// * `state` must not wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ffdh_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, domain: *const u8, domain_len: usize, p_len: usize, x: *const u8, x_len: usize, peer: *const u8, peer_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_ffdh_key_generation_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 16], p_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ffdh::vg_ffdh_exchange(out, out_len, domain, domain_len, p_len, x, x_len, peer, peer_len, scratch, scratch_len) } + unsafe { crate::ffdh::vg_ffdh_key_generation_init(state, p_len) } } /// Checks an imported finite-field Diffie–Hellman public value (SP 800-56A Rev. 3 §5.6.2.3.1, and §5.6.2.3.2 with q). With `domain` as `vg_ffdh_check_domain` takes it and y (`p_len` bytes, most significant first), returns 1 if `1 < y < p − 1` and, with q, `y^q mod p = 1`; otherwise 0. @@ -81,6 +81,36 @@ pub unsafe fn vg_ffdh_check_domain_auto(_dit: verified_garbage_cpu::Dit<'_>, dom unsafe { crate::ffdh::vg_ffdh_check_domain(domain, domain_len, p_len, scratch, scratch_len) } } +/// Finite-field Diffie–Hellman (SP 800-56A Rev. 3 §5.7.1.1, PKCS #3 §8). With `domain` as `vg_ffdh_check_domain` takes it, the private scalar x (`x_len` bytes) and the peer's public value y (`p_len` bytes), most significant first, computes `z = y^x mod p`; returns 0 and writes zeros if z is 0, 1 or `p − 1`, and otherwise returns 1 and writes z (`p_len` bytes, most significant first) to `out`. Applies no KDF. +/// +/// Contract: `VG.Spec.Ffdh.exchangeContract`. Timing may depend on the pointers, the lengths, the domain, y and the value returned, not otherwise on x; the exponentiation may run over all `x_len` bytes of x, leading zeros included. +/// +/// It runs `vg_ffdh_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `out` must be valid for reads and writes of `out_len` bytes. +/// * `domain` must be valid for reads of `domain_len` bytes. +/// * `x` must be valid for reads of `x_len` bytes. +/// * `peer` must be valid for reads of `peer_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `p_len` must be in 64..=1024. +/// * `domain_len` must be `2 * p_len` or `3 * p_len`. +/// * `out_len` and `peer_len` must be `p_len`. +/// * `x_len` must be in 1..=`p_len`. +/// * For the result to be specified, `domain` must be one that `vg_ffdh_check_domain` accepted (returned 1 for); otherwise it is unspecified. +/// * For the result to be specified, `peer` must also be a value that `vg_ffdh_check_public` accepted for this domain. +/// * `scratch_len` must be at least `16 * p_len + 32`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `out` and `scratch` must not overlap each other, `domain`, `x`, `peer` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `domain`, `x`, `peer` and `scratch` may wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ffdh_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, domain: *const u8, domain_len: usize, p_len: usize, x: *const u8, x_len: usize, peer: *const u8, peer_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ffdh::vg_ffdh_exchange(out, out_len, domain, domain_len, p_len, x, x_len, peer, peer_len, scratch, scratch_len) } +} + /// Derives the public value of an imported finite-field Diffie–Hellman private key (SP 800-56A Rev. 3 §5.6.2.1.2 for x's range, §5.6.1.1 for `y = g^x mod p`). With `domain` as `vg_ffdh_check_domain` takes it and x (`x_len` bytes, most significant first): returns 2 if x is not in `1..q` (`1..p − 1` without q); 1 if `y = g^x mod p` is a valid public value (as `vg_ffdh_check_public` checks it), writing y (`p_len` bytes, most significant first) to `y`; 3 otherwise. Writes zeros to `y` unless it returns 1. /// /// Contract: `VG.Spec.Ffdh.derivePublicContract`. Timing may depend on the pointers, the lengths, the domain and the value returned, not otherwise on x; the exponentiation may run over all `x_len` bytes of x, leading zeros included. @@ -137,19 +167,28 @@ pub unsafe fn vg_ffdh_check_private_auto(_dit: verified_garbage_cpu::Dit<'_>, do unsafe { crate::ffdh::vg_ffdh_check_private(domain, domain_len, p_len, y, y_len, x, x_len, scratch, scratch_len) } } -/// Initialize native finite-field Diffie–Hellman key generation. Returns 1 for a domain width in 64..=1024 bytes, initializing the rejection budgets and first entropy request; returns 0 and clears the first four state words otherwise. The remaining state words are private workspace. Contract: `VG.Spec.Ffdh.keyInitContract`. +/// Initialize a reusable public fixed-base context for finite-field Diffie–Hellman key generation. The checked domain is p, g and optional q, each p_len bytes. Writes generationPrecompute: three public metadata words followed by RSA's modulus/R-squared values and 32 five-tooth comb entries. Returns 1. Word 0 is generationScalarBits, word 1 is its byte length and word 2 is the comb row count. The context has no pointers and is independent of the arithmetic backend. Contract: VG.Spec.Ffdh.generationPrecomputeContract. Timing may depend on the public domain, pointers and lengths. /// -/// It runs `vg_ffdh_key_generation_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ffdh_generate_key_precompute`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 128 bytes. -/// * `state` must not wrap around the end of the address space (no Rust object does). +/// * `ctx` must be valid for reads and writes of `8 * ctx_len` bytes. +/// * `domain` must be valid for reads of `domain_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `p_len` must be in 64..=1024. +/// * `domain_len` must be `2 * p_len` or `3 * p_len`. +/// * domain must be one that vg_ffdh_check_domain accepted. +/// * ctx_len must be generationPrecomputedWords(p_len), namely 3 + 34 * ceil(p_len / 8). +/// * `scratch_len` must be at least `16 * p_len + 32`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `ctx` and `scratch` must not overlap each other or `domain` (distinct Rust objects never do). +/// * None of `ctx`, `domain` and `scratch` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ffdh_key_generation_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 16], p_len: usize) -> u32 { +pub unsafe fn vg_ffdh_generate_key_precompute_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *mut u64, ctx_len: usize, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ffdh::vg_ffdh_key_generation_init(state, p_len) } + unsafe { crate::ffdh::vg_ffdh_generate_key_precompute(ctx, ctx_len, domain, domain_len, p_len, scratch, scratch_len) } } /// One native finite-field Diffie–Hellman key-generation attempt using a reusable public five-tooth comb context. Takes p_len random bytes and implements the same vettedScalarOfOctets, keyStatus and output conventions as vg_ffdh_generate_key_vetted for checked parameters. The context's public maximum width fixes the schedule, including secret leading zeros. Contract: VG.Spec.Ffdh.cachedGenerateKeyContract. Timing may depend on the public domain, pointers, lengths and returned status, not otherwise on the random bytes or private scalar. @@ -180,69 +219,6 @@ pub unsafe fn vg_ffdh_generate_key_cached_auto(_dit: verified_garbage_cpu::Dit<' unsafe { crate::ffdh::vg_ffdh_generate_key_cached(x, x_len, y, y_len, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, ctx, ctx_len) } } -/// Run native finite-field Diffie–Hellman parameter generation, with RSA's trial-division sieve. The routine owns candidate retries, attempt limits, witness selection, primality rounds and entropy budgets. Initialize with `vg_ffdh_parameter_generation_init`. Returns 1 with the probable safe prime in `out`, 3 when a bound is exhausted, or 0 only when it needs more entropy. On 0, discard `used` bytes of `rand`, preserve its remaining suffix and extend that suffix to at least `state[3]` bytes before calling again. `state[1]` is the output width and `state[2]` the maximum tape length. The output is zero for 0 and 3. Accepted p and q pass the full 64/128 independent witness counts. Contract: `VG.Spec.Ffdh.sievedParameterSearchContract`. Timing may depend on the public tape and the first four controller words. -/// -/// It runs `vg_ffdh_generate_parameters`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `out` must be valid for reads and writes of `out_len` bytes. -/// * `used` must be valid for reads and writes of 8 bytes. -/// * `rand` must be valid for reads of `rand_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `state` must be valid for reads and writes of 128 bytes. -/// * `state` must be initialized by `vg_ffdh_parameter_generation_init` for the supplied `bits` and `generator`, or be the state returned by this routine. -/// * `out_len` must equal `state[1]`, and `rand_len` must not exceed `state[2]`. -/// * `scratch_len` must be at least `16 * out_len + 32`. -/// * The last twelve controller words and the scratch contents are private workspace. -/// * `out`, `used`, `scratch` and `state` must not overlap each other, `rand` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `used`, `rand`, `scratch` and `state` may overlap the 336 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ffdh_generate_parameters_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], bits: usize, generator: u32, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ffdh::vg_ffdh_generate_parameters(out, out_len, used, bits, generator, rand, rand_len, scratch, scratch_len, state) } -} - -/// Initialize a reusable public fixed-base context for finite-field Diffie–Hellman key generation. The checked domain is p, g and optional q, each p_len bytes. Writes generationPrecompute: three public metadata words followed by RSA's modulus/R-squared values and 32 five-tooth comb entries. Returns 1. Word 0 is generationScalarBits, word 1 is its byte length and word 2 is the comb row count. The context has no pointers and is independent of the arithmetic backend. Contract: VG.Spec.Ffdh.generationPrecomputeContract. Timing may depend on the public domain, pointers and lengths. -/// -/// It runs `vg_ffdh_generate_key_precompute`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `ctx` must be valid for reads and writes of `8 * ctx_len` bytes. -/// * `domain` must be valid for reads of `domain_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `p_len` must be in 64..=1024. -/// * `domain_len` must be `2 * p_len` or `3 * p_len`. -/// * domain must be one that vg_ffdh_check_domain accepted. -/// * ctx_len must be generationPrecomputedWords(p_len), namely 3 + 34 * ceil(p_len / 8). -/// * `scratch_len` must be at least `16 * p_len + 32`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `ctx` and `scratch` must not overlap each other or `domain` (distinct Rust objects never do). -/// * None of `ctx`, `domain` and `scratch` may wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ffdh_generate_key_precompute_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *mut u64, ctx_len: usize, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ffdh::vg_ffdh_generate_key_precompute(ctx, ctx_len, domain, domain_len, p_len, scratch, scratch_len) } -} - -/// Initialize native finite-field Diffie–Hellman parameter generation. Validates 512..=8192 bits and generator 2 or 5. Returns 1 and initializes the controller's attempt limit, output width, entropy budget and first entropy request; returns 0 and clears those four words on invalid parameters. The remaining state words are private workspace. Contract: `VG.Spec.Ffdh.parameterInitContract`. -/// -/// It runs `vg_ffdh_parameter_generation_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 128 bytes. -/// * `state` must not wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ffdh_parameter_generation_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 16], bits: usize, generator: u32) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ffdh::vg_ffdh_parameter_generation_init(state, bits, generator) } -} - /// Generate a finite-field Diffie–Hellman key pair using a reusable public five-tooth comb context and the existing vettedKeySearch rejection policy. Returns 1 with a key pair, 3 when a budget is exhausted, or 0 to request entropy. On 0, discard used bytes, preserve the suffix and extend it to state[3] bytes before calling again. On 0 and 3 both outputs are zero. Entropy consumption, retry budgets and statuses are unchanged. Contract: VG.Spec.Ffdh.cachedKeySearchContract. Timing may depend on the public domain, pointers, lengths, controller policy and sequence of attempt statuses, not otherwise on private scalar bytes. /// /// It runs `vg_ffdh_generate_key_search_cached`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. @@ -273,3 +249,27 @@ pub unsafe fn vg_ffdh_generate_key_search_cached_auto(_dit: verified_garbage_cpu // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ffdh::vg_ffdh_generate_key_search_cached(x, x_len, y, y_len, used, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, state, ctx, ctx_len) } } + +/// Run native finite-field Diffie–Hellman parameter generation, with RSA's trial-division sieve. The routine owns candidate retries, attempt limits, witness selection, primality rounds and entropy budgets. Initialize with `vg_ffdh_parameter_generation_init`. Returns 1 with the probable safe prime in `out`, 3 when a bound is exhausted, or 0 only when it needs more entropy. On 0, discard `used` bytes of `rand`, preserve its remaining suffix and extend that suffix to at least `state[3]` bytes before calling again. `state[1]` is the output width and `state[2]` the maximum tape length. The output is zero for 0 and 3. Accepted p and q pass the full 64/128 independent witness counts. Contract: `VG.Spec.Ffdh.sievedParameterSearchContract`. Timing may depend on the public tape and the first four controller words. +/// +/// It runs `vg_ffdh_generate_parameters`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `out` must be valid for reads and writes of `out_len` bytes. +/// * `used` must be valid for reads and writes of 8 bytes. +/// * `rand` must be valid for reads of `rand_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `state` must be valid for reads and writes of 128 bytes. +/// * `state` must be initialized by `vg_ffdh_parameter_generation_init` for the supplied `bits` and `generator`, or be the state returned by this routine. +/// * `out_len` must equal `state[1]`, and `rand_len` must not exceed `state[2]`. +/// * `scratch_len` must be at least `16 * out_len + 32`. +/// * The last twelve controller words and the scratch contents are private workspace. +/// * `out`, `used`, `scratch` and `state` must not overlap each other, `rand` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `used`, `rand`, `scratch` and `state` may overlap the 336 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ffdh_generate_parameters_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], bits: usize, generator: u32, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ffdh::vg_ffdh_generate_parameters(out, out_len, used, bits, generator, rand, rand_len, scratch, scratch_len, state) } +} diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/fn_tables.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/fn_tables.rs index 6c86fb10e40..cc2097fee18 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/fn_tables.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/fn_tables.rs @@ -2,30 +2,66 @@ //! The dispatchers of the tables of functions for `powerpc64le` //! (`crate::fn_tables`, `VG.Rust.tableDispatcher`). -/// The table of functions `VG_HASH_MD5` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hash_md5_auto() -> &'static crate::fn_tables::Hash { - &crate::fn_tables::VG_HASH_MD5 -} - /// The table of functions `VG_HMAC_MD5` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] pub fn vg_hmac_md5_auto() -> &'static crate::fn_tables::Hmac { &crate::fn_tables::VG_HMAC_MD5 } -/// The table of functions `VG_HASH_SHA1` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hash_sha1_auto() -> &'static crate::fn_tables::Hash { - &crate::fn_tables::VG_HASH_SHA1 -} - /// The table of functions `VG_HMAC_SHA1` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] pub fn vg_hmac_sha1_auto() -> &'static crate::fn_tables::Hmac { &crate::fn_tables::VG_HMAC_SHA1 } +/// The table of functions `VG_HMAC_SHA224` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha224_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA224 +} + +/// The table of functions `VG_HMAC_SHA256` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha256_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA256 +} + +/// The table of functions `VG_HMAC_SHA384` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha384_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA384 +} + +/// The table of functions `VG_HMAC_SHA512` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha512_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA512 +} + +/// The table of functions `VG_HMAC_SHA512_224` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha512_224_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA512_224 +} + +/// The table of functions `VG_HMAC_SHA512_256` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha512_256_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA512_256 +} + +/// The table of functions `VG_HASH_MD5` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hash_md5_auto() -> &'static crate::fn_tables::Hash { + &crate::fn_tables::VG_HASH_MD5 +} + +/// The table of functions `VG_HASH_SHA1` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hash_sha1_auto() -> &'static crate::fn_tables::Hash { + &crate::fn_tables::VG_HASH_SHA1 +} + /// The table of functions `VG_HASH_SHA224` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] pub fn vg_hash_sha224_auto() -> &'static crate::fn_tables::Hash { @@ -50,28 +86,16 @@ pub fn vg_hash_sha512_auto() -> &'static crate::fn_tables::Hash { &crate::fn_tables::VG_HASH_SHA512 } -/// The table of functions `VG_HMAC_SHA224` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hmac_sha224_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA224 -} - -/// The table of functions `VG_HMAC_SHA256` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hmac_sha256_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA256 -} - -/// The table of functions `VG_HMAC_SHA384` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +/// The table of functions `VG_HASH_SHA512_224` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] -pub fn vg_hmac_sha384_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA384 +pub fn vg_hash_sha512_224_auto() -> &'static crate::fn_tables::Hash { + &crate::fn_tables::VG_HASH_SHA512_224 } -/// The table of functions `VG_HMAC_SHA512` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +/// The table of functions `VG_HASH_SHA512_256` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] -pub fn vg_hmac_sha512_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA512 +pub fn vg_hash_sha512_256_auto() -> &'static crate::fn_tables::Hash { + &crate::fn_tables::VG_HASH_SHA512_256 } /// The table of functions `VG_MD_HASH_MD5` of the interface `MdHash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). @@ -110,30 +134,6 @@ pub fn vg_md_hash_sha512_auto() -> &'static crate::fn_tables::MdHash { &crate::fn_tables::VG_MD_HASH_SHA512 } -/// The table of functions `VG_HASH_SHA512_224` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hash_sha512_224_auto() -> &'static crate::fn_tables::Hash { - &crate::fn_tables::VG_HASH_SHA512_224 -} - -/// The table of functions `VG_HASH_SHA512_256` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hash_sha512_256_auto() -> &'static crate::fn_tables::Hash { - &crate::fn_tables::VG_HASH_SHA512_256 -} - -/// The table of functions `VG_HMAC_SHA512_224` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hmac_sha512_224_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA512_224 -} - -/// The table of functions `VG_HMAC_SHA512_256` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hmac_sha512_256_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA512_256 -} - /// The table of functions `VG_MD_HASH_SHA512_224` of the interface `MdHash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] pub fn vg_md_hash_sha512_224_auto() -> &'static crate::fn_tables::MdHash { diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/gcm.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/gcm.rs index 9b672b1d5d7..55987d96c81 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/gcm.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/gcm.rs @@ -22,81 +22,26 @@ pub unsafe fn vg_aes_gcm_init_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *co unsafe { crate::gcm::vg_aes_gcm_init(key, key_len, ctx) } } -/// AES-GCM authenticated encryption (NIST SP 800-38D §7.1, GCM-AE, with a 128-bit tag): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, encrypts the `len` bytes at `data` in place, under the IV the `nonce_len` bytes at `nonce`, and writes the tag of the ciphertext and the `aad_len` bytes of additional data at `aad` to `*tag`. A shorter tag is the first bytes of this one. -/// -/// The function checks no length. GCM is secure only for a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of data and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must ensure; and a nonce must never be used twice with the same key. -/// -/// Contract: `VG.Spec.Gcm.sealContract`. Constant time: only the pointers, `rounds` and the lengths may affect timing, not the key context, the nonce, the additional data or the data. -/// -/// It runs `vg_aes_gcm_seal`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `ctx` must be valid for reads of 256 bytes. -/// * `nonce` must be valid for reads of `nonce_len` bytes. -/// * `aad` must be valid for reads of `aad_len` bytes. -/// * `data` must be valid for reads and writes of `len` bytes. -/// * `tag` must be valid for reads and writes of 16 bytes. -/// * `rounds` must be 10, 12 or 14. -/// * `data` and `tag` must not overlap each other, `ctx`, `nonce`, `aad` or the arguments on the stack (distinct Rust objects never do). -/// * None of `ctx`, `nonce`, `aad`, `data` and `tag` may overlap the 2672 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_aes_gcm_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *mut [u8; 16]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::gcm::vg_aes_gcm_seal(ctx, rounds, nonce, nonce_len, aad, aad_len, data, len, tag) } -} - -/// AES-GCM authenticated decryption (NIST SP 800-38D §7.2, GCM-AD): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds and the received tag the `tag_len` bytes at `tag`, returns 1 if `tag_len` is between 4 and 16 and the tag is the first `tag_len` bytes of that of the `len` bytes of ciphertext at `data` and the `aad_len` bytes of additional data at `aad`, under the IV the `nonce_len` bytes at `nonce`, having then decrypted the ciphertext in place; otherwise returns 0, and the bytes at `data` are unchanged. The tags are compared without a branch. -/// -/// The function checks no other length: GCM requires a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of ciphertext and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. -/// -/// Contract: `VG.Spec.Gcm.openContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the additional data, the data or the tag. -/// -/// It runs `vg_aes_gcm_open`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `ctx` must be valid for reads of 256 bytes. -/// * `nonce` must be valid for reads of `nonce_len` bytes. -/// * `aad` must be valid for reads of `aad_len` bytes. -/// * `data` must be valid for reads and writes of `len` bytes. -/// * `tag` must be valid for reads of `tag_len` bytes. -/// * `rounds` must be 10, 12 or 14. -/// * `data` must not overlap `ctx`, `nonce`, `aad`, `tag` or the arguments on the stack (distinct Rust objects never do). -/// * None of `ctx`, `nonce`, `aad`, `data` and `tag` may overlap the 2688 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_aes_gcm_open_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *const u8, tag_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::gcm::vg_aes_gcm_open(ctx, rounds, nonce, nonce_len, aad, aad_len, data, len, tag, tag_len) } -} - -/// AES-GCM authenticated decryption (NIST SP 800-38D §7.2, GCM-AD), out of place: with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds and the received tag the `tag_len` bytes at `tag`, returns 1 if `tag_len` is between 4 and 16 and the tag is the first `tag_len` bytes of that of the `len` bytes of ciphertext at `src` and the `aad_len` bytes of additional data at `aad`, under the IV the `nonce_len` bytes at `nonce`, having then written the plaintext to the `dst_len = len` bytes at `dst`; otherwise returns 0, having written zeros to them: `vg_aes_gcm_open` with the ciphertext read from `src` and the plaintext written to `dst`. `src` is not written, and `dst` is a buffer of its own: it overlaps no other buffer, `src` included. The tags are compared without a branch. +/// Starts an incremental AES-GCM encryption or decryption (NIST SP 800-38D §7): with the key context `*ctx` that `vg_aes_gcm_init` wrote, makes the streaming state `*state` represent the message with the IV the `nonce_len` bytes at `nonce`, and no additional data or text yet. Continue with `vg_aes_gcm_stream_aad`, then `vg_aes_gcm_stream_encrypt` or `vg_aes_gcm_stream_decrypt`, then `vg_aes_gcm_stream_finish` or `vg_aes_gcm_stream_verify`, with the same key context. /// -/// The function checks no other length: GCM requires a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of ciphertext and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// The function checks no length: GCM requires a nonce of 1 to `2^61 - 1` bytes (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.openToContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the additional data, the data or the tag. +/// Contract: `VG.Spec.Gcm.streamInitContract`. The streaming state is `J₀`, the GHASH accumulator, a partial block, the next counter block and a keystream block (`VG.Spec.Gcm.StreamRepr`). Constant time: only the pointers and `nonce_len` may affect timing, not the key context or the nonce. /// -/// It runs `vg_aes_gcm_open_to`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_aes_gcm_stream_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. /// * `nonce` must be valid for reads of `nonce_len` bytes. -/// * `aad` must be valid for reads of `aad_len` bytes. -/// * `src` must be valid for reads of `len` bytes. -/// * `dst` must be valid for reads and writes of `dst_len` bytes. -/// * `tag` must be valid for reads of `tag_len` bytes. -/// * `rounds` must be 10, 12 or 14. -/// * `dst_len` must equal `len`. -/// * `dst` must not overlap `ctx`, `nonce`, `aad`, `src`, `tag` or the arguments on the stack (distinct Rust objects never do). -/// * None of `ctx`, `nonce`, `aad`, `src`, `dst` and `tag` may overlap the 2720 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must be valid for reads and writes of 80 bytes. +/// * `state` must not overlap `ctx` or `nonce` (distinct Rust objects never do). +/// * None of `ctx`, `nonce` and `state` may overlap the 2592 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_open_to_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const u8, len: usize, dst: *mut u8, dst_len: usize, tag: *const u8, tag_len: usize) -> u32 { +pub unsafe fn vg_aes_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::gcm::vg_aes_gcm_open_to(ctx, rounds, nonce, nonce_len, aad, aad_len, src, len, dst, dst_len, tag, tag_len) } + unsafe { crate::gcm::vg_aes_gcm_stream_init(ctx, nonce, nonce_len, state) } } /// Absorbs additional data into an incremental AES-GCM encryption or decryption: with the key context `*ctx`, if the streaming state `*state` represents a message with `aad_len` bytes (modulo 2⁶⁴) of additional data and no text yet, it then represents that message with the `len` bytes at `data` appended to its additional data. All of the additional data comes before the text (§7.1 step 5): after text, the state represents no message. @@ -119,53 +64,50 @@ pub unsafe fn vg_aes_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ct unsafe { crate::gcm::vg_aes_gcm_stream_aad(ctx, state, aad_len, data, len) } } -/// Starts an incremental AES-GCM encryption or decryption (NIST SP 800-38D §7): with the key context `*ctx` that `vg_aes_gcm_init` wrote, makes the streaming state `*state` represent the message with the IV the `nonce_len` bytes at `nonce`, and no additional data or text yet. Continue with `vg_aes_gcm_stream_aad`, then `vg_aes_gcm_stream_encrypt` or `vg_aes_gcm_stream_decrypt`, then `vg_aes_gcm_stream_finish` or `vg_aes_gcm_stream_verify`, with the same key context. +/// Encrypts the next piece of the text of an incremental AES-GCM encryption (NIST SP 800-38D §7.1): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and the ciphertext of exactly `text_len` bytes of plaintext so far, encrypts the `len` bytes at `data` in place, as the continuation of that plaintext, and the state then represents the message with them appended. /// -/// The function checks no length: GCM requires a nonce of 1 to `2^61 - 1` bytes (§5.2.1.1), which the caller must check. +/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.streamInitContract`. The streaming state is `J₀`, the GHASH accumulator, a partial block, the next counter block and a keystream block (`VG.Spec.Gcm.StreamRepr`). Constant time: only the pointers and `nonce_len` may affect timing, not the key context or the nonce. +/// Contract: `VG.Spec.Gcm.streamEncryptContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. /// -/// It runs `vg_aes_gcm_stream_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_aes_gcm_stream_encrypt`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `nonce` must be valid for reads of `nonce_len` bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `state` must not overlap `ctx` or `nonce` (distinct Rust objects never do). -/// * None of `ctx`, `nonce` and `state` may overlap the 2592 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads and writes of `len` bytes. +/// * `rounds` must be 10, 12 or 14. +/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the 2592 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { +pub unsafe fn vg_aes_gcm_stream_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::gcm::vg_aes_gcm_stream_init(ctx, nonce, nonce_len, state) } + unsafe { crate::gcm::vg_aes_gcm_stream_encrypt(ctx, rounds, state, aad_len, text_len, data, len) } } -/// AES-GCM authenticated encryption (NIST SP 800-38D §7.1, GCM-AE, with a 128-bit tag), out of place, of a plaintext in pieces: with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, encrypts the concatenation of the `src_count` slices that `src` lists (each an address and a length, in bytes), under the IV the `nonce_len` bytes at `nonce`, writes the ciphertext to the `len` bytes at `dst`, and writes the tag of the ciphertext and the `aad_len` bytes of additional data at `aad` to `*tag`: `vg_aes_gcm_seal` with the plaintext gathered from `src`. A shorter tag is the first bytes of this one. +/// Decrypts the next piece of the text of an incremental AES-GCM decryption (NIST SP 800-38D §7.2): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext so far, decrypts the `len` bytes of ciphertext at `data` in place, as the continuation of that ciphertext, and the state then represents the message with them appended. The plaintext is not authenticated until `vg_aes_gcm_stream_verify` has returned 1. /// -/// The function checks no length. GCM is secure only for a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of data and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must ensure; and a nonce must never be used twice with the same key. +/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.sealGatherContract`. Constant time: only the pointers, `rounds`, the lengths, `src_count` and where the slices are (their addresses and lengths) may affect timing, not the key context, the nonce, the additional data or the data. +/// Contract: `VG.Spec.Gcm.streamDecryptContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. /// -/// It runs `vg_aes_gcm_seal_gather`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_aes_gcm_stream_decrypt`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `nonce` must be valid for reads of `nonce_len` bytes. -/// * `aad` must be valid for reads of `aad_len` bytes. -/// * `src` must be valid for reads of `2 * size_of::() * src_count` bytes, and each slice it lists for reads of its length in bytes. -/// * `dst` must be valid for reads and writes of `len` bytes. -/// * `tag` must be valid for reads and writes of 16 bytes. +/// * `state` must be valid for reads and writes of 80 bytes. +/// * `data` must be valid for reads and writes of `len` bytes. /// * `rounds` must be 10, 12 or 14. -/// * The slices `src` lists must be `len` bytes long in all. -/// * `dst` and `tag` must not overlap each other, `ctx`, `nonce`, `aad`, `src`, the slices `src` lists or the arguments on the stack (distinct Rust objects never do). -/// * None of `ctx`, `nonce`, `aad`, `src`, the slices `src` lists, `dst` and `tag` may overlap the 2960 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the 2592 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_seal_gather_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const [usize; 2], src_count: usize, dst: *mut u8, len: usize, tag: *mut [u8; 16]) { +pub unsafe fn vg_aes_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::gcm::vg_aes_gcm_seal_gather(ctx, rounds, nonce, nonce_len, aad, aad_len, src, src_count, dst, len, tag) } + unsafe { crate::gcm::vg_aes_gcm_stream_decrypt(ctx, rounds, state, aad_len, text_len, data, len) } } /// Finishes an incremental AES-GCM encryption or decryption (NIST SP 800-38D §7.1 steps 5–6, §7.2 steps 6–7): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, writes its 128-bit tag to `*tag`. A shorter tag is the first bytes of this one; to check a received tag, use `vg_aes_gcm_stream_verify`. @@ -212,48 +154,106 @@ pub unsafe fn vg_aes_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, unsafe { crate::gcm::vg_aes_gcm_stream_verify(ctx, rounds, state, aad_len, text_len, tag, tag_len) } } -/// Encrypts the next piece of the text of an incremental AES-GCM encryption (NIST SP 800-38D §7.1): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and the ciphertext of exactly `text_len` bytes of plaintext so far, encrypts the `len` bytes at `data` in place, as the continuation of that plaintext, and the state then represents the message with them appended. +/// AES-GCM authenticated encryption (NIST SP 800-38D §7.1, GCM-AE, with a 128-bit tag): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, encrypts the `len` bytes at `data` in place, under the IV the `nonce_len` bytes at `nonce`, and writes the tag of the ciphertext and the `aad_len` bytes of additional data at `aad` to `*tag`. A shorter tag is the first bytes of this one. /// -/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// The function checks no length. GCM is secure only for a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of data and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must ensure; and a nonce must never be used twice with the same key. /// -/// Contract: `VG.Spec.Gcm.streamEncryptContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. +/// Contract: `VG.Spec.Gcm.sealContract`. Constant time: only the pointers, `rounds` and the lengths may affect timing, not the key context, the nonce, the additional data or the data. /// -/// It runs `vg_aes_gcm_stream_encrypt`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_aes_gcm_seal`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `state` must be valid for reads and writes of 80 bytes. +/// * `nonce` must be valid for reads of `nonce_len` bytes. +/// * `aad` must be valid for reads of `aad_len` bytes. /// * `data` must be valid for reads and writes of `len` bytes. +/// * `tag` must be valid for reads and writes of 16 bytes. /// * `rounds` must be 10, 12 or 14. -/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the 2592 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` and `tag` must not overlap each other, `ctx`, `nonce`, `aad` or the arguments on the stack (distinct Rust objects never do). +/// * None of `ctx`, `nonce`, `aad`, `data` and `tag` may overlap the 2672 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_stream_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { +pub unsafe fn vg_aes_gcm_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *mut [u8; 16]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::gcm::vg_aes_gcm_stream_encrypt(ctx, rounds, state, aad_len, text_len, data, len) } + unsafe { crate::gcm::vg_aes_gcm_seal(ctx, rounds, nonce, nonce_len, aad, aad_len, data, len, tag) } } -/// Decrypts the next piece of the text of an incremental AES-GCM decryption (NIST SP 800-38D §7.2): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext so far, decrypts the `len` bytes of ciphertext at `data` in place, as the continuation of that ciphertext, and the state then represents the message with them appended. The plaintext is not authenticated until `vg_aes_gcm_stream_verify` has returned 1. +/// AES-GCM authenticated decryption (NIST SP 800-38D §7.2, GCM-AD): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds and the received tag the `tag_len` bytes at `tag`, returns 1 if `tag_len` is between 4 and 16 and the tag is the first `tag_len` bytes of that of the `len` bytes of ciphertext at `data` and the `aad_len` bytes of additional data at `aad`, under the IV the `nonce_len` bytes at `nonce`, having then decrypted the ciphertext in place; otherwise returns 0, and the bytes at `data` are unchanged. The tags are compared without a branch. /// -/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// The function checks no other length: GCM requires a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of ciphertext and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.streamDecryptContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. +/// Contract: `VG.Spec.Gcm.openContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the additional data, the data or the tag. /// -/// It runs `vg_aes_gcm_stream_decrypt`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_aes_gcm_open`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `state` must be valid for reads and writes of 80 bytes. +/// * `nonce` must be valid for reads of `nonce_len` bytes. +/// * `aad` must be valid for reads of `aad_len` bytes. /// * `data` must be valid for reads and writes of `len` bytes. +/// * `tag` must be valid for reads of `tag_len` bytes. /// * `rounds` must be 10, 12 or 14. -/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the 2592 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must not overlap `ctx`, `nonce`, `aad`, `tag` or the arguments on the stack (distinct Rust objects never do). +/// * None of `ctx`, `nonce`, `aad`, `data` and `tag` may overlap the 2688 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { +pub unsafe fn vg_aes_gcm_open_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *const u8, tag_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::gcm::vg_aes_gcm_stream_decrypt(ctx, rounds, state, aad_len, text_len, data, len) } + unsafe { crate::gcm::vg_aes_gcm_open(ctx, rounds, nonce, nonce_len, aad, aad_len, data, len, tag, tag_len) } +} + +/// AES-GCM authenticated encryption (NIST SP 800-38D §7.1, GCM-AE, with a 128-bit tag), out of place, of a plaintext in pieces: with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, encrypts the concatenation of the `src_count` slices that `src` lists (each an address and a length, in bytes), under the IV the `nonce_len` bytes at `nonce`, writes the ciphertext to the `len` bytes at `dst`, and writes the tag of the ciphertext and the `aad_len` bytes of additional data at `aad` to `*tag`: `vg_aes_gcm_seal` with the plaintext gathered from `src`. A shorter tag is the first bytes of this one. +/// +/// The function checks no length. GCM is secure only for a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of data and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must ensure; and a nonce must never be used twice with the same key. +/// +/// Contract: `VG.Spec.Gcm.sealGatherContract`. Constant time: only the pointers, `rounds`, the lengths, `src_count` and where the slices are (their addresses and lengths) may affect timing, not the key context, the nonce, the additional data or the data. +/// +/// It runs `vg_aes_gcm_seal_gather`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `ctx` must be valid for reads of 256 bytes. +/// * `nonce` must be valid for reads of `nonce_len` bytes. +/// * `aad` must be valid for reads of `aad_len` bytes. +/// * `src` must be valid for reads of `2 * size_of::() * src_count` bytes, and each slice it lists for reads of its length in bytes. +/// * `dst` must be valid for reads and writes of `len` bytes. +/// * `tag` must be valid for reads and writes of 16 bytes. +/// * `rounds` must be 10, 12 or 14. +/// * The slices `src` lists must be `len` bytes long in all. +/// * `dst` and `tag` must not overlap each other, `ctx`, `nonce`, `aad`, `src`, the slices `src` lists or the arguments on the stack (distinct Rust objects never do). +/// * None of `ctx`, `nonce`, `aad`, `src`, the slices `src` lists, `dst` and `tag` may overlap the 2960 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_aes_gcm_seal_gather_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const [usize; 2], src_count: usize, dst: *mut u8, len: usize, tag: *mut [u8; 16]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::gcm::vg_aes_gcm_seal_gather(ctx, rounds, nonce, nonce_len, aad, aad_len, src, src_count, dst, len, tag) } +} + +/// AES-GCM authenticated decryption (NIST SP 800-38D §7.2, GCM-AD), out of place: with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds and the received tag the `tag_len` bytes at `tag`, returns 1 if `tag_len` is between 4 and 16 and the tag is the first `tag_len` bytes of that of the `len` bytes of ciphertext at `src` and the `aad_len` bytes of additional data at `aad`, under the IV the `nonce_len` bytes at `nonce`, having then written the plaintext to the `dst_len = len` bytes at `dst`; otherwise returns 0, having written zeros to them: `vg_aes_gcm_open` with the ciphertext read from `src` and the plaintext written to `dst`. `src` is not written, and `dst` is a buffer of its own: it overlaps no other buffer, `src` included. The tags are compared without a branch. +/// +/// The function checks no other length: GCM requires a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of ciphertext and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// +/// Contract: `VG.Spec.Gcm.openToContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the additional data, the data or the tag. +/// +/// It runs `vg_aes_gcm_open_to`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `ctx` must be valid for reads of 256 bytes. +/// * `nonce` must be valid for reads of `nonce_len` bytes. +/// * `aad` must be valid for reads of `aad_len` bytes. +/// * `src` must be valid for reads of `len` bytes. +/// * `dst` must be valid for reads and writes of `dst_len` bytes. +/// * `tag` must be valid for reads of `tag_len` bytes. +/// * `rounds` must be 10, 12 or 14. +/// * `dst_len` must equal `len`. +/// * `dst` must not overlap `ctx`, `nonce`, `aad`, `src`, `tag` or the arguments on the stack (distinct Rust objects never do). +/// * None of `ctx`, `nonce`, `aad`, `src`, `dst` and `tag` may overlap the 2720 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_aes_gcm_open_to_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const u8, len: usize, dst: *mut u8, dst_len: usize, tag: *const u8, tag_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::gcm::vg_aes_gcm_open_to(ctx, rounds, nonce, nonce_len, aad, aad_len, src, len, dst, dst_len, tag, tag_len) } } diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/idea.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/idea.rs index ef4ea4867da..bb0ca4e4b9a 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/idea.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/idea.rs @@ -2,25 +2,6 @@ //! The dispatchers of the verified `idea` functions for `powerpc64le` //! (`crate::idea`, `VG.Rust.dispatcher`). -/// IDEA in ECB mode (Lai, 1992, §3.3; SP 800-38A §6.1) on `n` complete 8-byte blocks at `data`, in place, under the 52 subkeys at `schedule`: encryption under the subkeys `vg_idea_expand_key` writes, decryption under those `vg_idea_invert_key` writes. No padding is added or removed. For `n = 0`, no data is transformed. -/// -/// Contract: `VG.Spec.Idea.ecbContract`. Constant time: only pointers and `n` may affect timing, not the subkeys or data, including words or subkeys that are 0. -/// -/// It runs `vg_idea_ecb`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `schedule` must be valid for reads of 104 bytes. -/// * `data` must be valid for reads and writes of `8 * n` bytes. -/// * `data` must not overlap `schedule` (distinct Rust objects never do). -/// * Neither `schedule` nor `data` may wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_idea_ecb_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *const [u8; 104], data: *mut [u8; 8], n: usize) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::idea::vg_idea_ecb(schedule, data, n) } -} - /// IDEA encryption key schedule (Lai, 1992, §3.3): writes the 52 encryption subkeys of the 16-byte key at `key` (as eight big-endian 16-bit words, rotated left by 25 bits after every eight subkeys) to `*schedule`, as little-endian 16-bit words. /// /// Contract: `VG.Spec.Idea.expandKeyContract`. Constant time: only pointers may affect timing, not the key. @@ -58,3 +39,22 @@ pub unsafe fn vg_idea_invert_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sched // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::idea::vg_idea_invert_key(schedule, inverse) } } + +/// IDEA in ECB mode (Lai, 1992, §3.3; SP 800-38A §6.1) on `n` complete 8-byte blocks at `data`, in place, under the 52 subkeys at `schedule`: encryption under the subkeys `vg_idea_expand_key` writes, decryption under those `vg_idea_invert_key` writes. No padding is added or removed. For `n = 0`, no data is transformed. +/// +/// Contract: `VG.Spec.Idea.ecbContract`. Constant time: only pointers and `n` may affect timing, not the subkeys or data, including words or subkeys that are 0. +/// +/// It runs `vg_idea_ecb`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `schedule` must be valid for reads of 104 bytes. +/// * `data` must be valid for reads and writes of `8 * n` bytes. +/// * `data` must not overlap `schedule` (distinct Rust objects never do). +/// * Neither `schedule` nor `data` may wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_idea_ecb_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *const [u8; 104], data: *mut [u8; 8], n: usize) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::idea::vg_idea_ecb(schedule, data, n) } +} diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/mldsa44.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/mldsa44.rs index f3b6a74e51e..62da328d8bd 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/mldsa44.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/mldsa44.rs @@ -2,29 +2,27 @@ //! The dispatchers of the verified `mldsa44` functions for `powerpc64le` //! (`crate::mldsa44`, `VG.Rust.dispatcher`). -/// ML-DSA-44 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-44 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. +/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. /// -/// It runs `vg_mldsa44_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa44_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `sk` must be valid for reads of 2560 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `rnd` must be valid for reads of 32 bytes. -/// * `sig` must be valid for reads and writes of 2420 bytes. +/// * `seed` must be valid for reads of 32 bytes. +/// * `pk` must be valid for reads and writes of 1312 bytes. +/// * `sk` must be valid for reads and writes of 2560 bytes. /// * `scratch` must be valid for reads and writes of 77824 bytes. -/// * `sk` must have been written by `vg_mldsa44_keygen`. -/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. +/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { +pub unsafe fn vg_mldsa44_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1312], sk: *mut [u8; 2560], scratch: *mut [u64; 9728]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_sign(sk, mu, rnd, sig, scratch) } + unsafe { crate::mldsa44::vg_mldsa44_keygen(seed, pk, sk, scratch) } } /// ML-DSA-44 public-key hash: writes `tr = H(pk, 64)` (FIPS 204 Algorithm 8, line 6), SHAKE256 of the public key `*pk` to 64 bytes, to `*tr`, the digest `vg_mldsa44_verify_message_cached` takes. @@ -48,118 +46,98 @@ pub unsafe fn vg_mldsa44_pk_tr_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *co unsafe { crate::mldsa44::vg_mldsa44_pk_tr(pk, tr, scratch) } } -/// ML-DSA-44 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-44 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa44_verify_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. +/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. /// -/// It runs `vg_mldsa44_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa44_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `seed` must be valid for reads of 32 bytes. -/// * `pk` must be valid for reads and writes of 1312 bytes. -/// * `sk` must be valid for reads and writes of 2560 bytes. -/// * `scratch` must be valid for reads and writes of 77824 bytes. -/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. +/// * `pk` must be valid for reads of 1312 bytes. +/// * `prepared` must be valid for reads and writes of 5408 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). +/// * None of `pk`, `prepared` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1312], sk: *mut [u8; 2560], scratch: *mut [u64; 9728]) -> u32 { +pub unsafe fn vg_mldsa44_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], prepared: *mut [u8; 5408], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_keygen(seed, pk, sk, scratch) } + unsafe { crate::mldsa44::vg_mldsa44_prepare_verifying_key(pk, prepared, scratch) } } -/// ML-DSA-44 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// ML-DSA-44 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa44_sign_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. /// -/// It runs `vg_mldsa44_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa44_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `pk` must be valid for reads of 1312 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `sig` must be valid for reads of 2420 bytes. -/// * `scratch` must be valid for reads and writes of 77824 bytes. +/// * `sk` must be valid for reads of 2560 bytes. +/// * `prepared` must be valid for reads and writes of 14848 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). -/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). +/// * None of `sk`, `prepared` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], mu: *const [u8; 64], sig: *const [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { +pub unsafe fn vg_mldsa44_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], prepared: *mut [u8; 14848], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_verify(pk, mu, sig, scratch) } + unsafe { crate::mldsa44::vg_mldsa44_prepare_signing_key(sk, prepared, scratch) } } -/// ML-DSA-44 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. +/// ML-DSA-44 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. +/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. /// -/// It runs `vg_mldsa44_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa44_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `sk` must be valid for reads of 2560 bytes. -/// * `msg` must be valid for reads of `msg_len` bytes. -/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `mu` must be valid for reads of 64 bytes. /// * `rnd` must be valid for reads of 32 bytes. /// * `sig` must be valid for reads and writes of 2420 bytes. -/// * `scratch` must be valid for reads and writes of 78848 bytes. +/// * `scratch` must be valid for reads and writes of 77824 bytes. /// * `sk` must have been written by `vg_mldsa44_keygen`. /// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9856]) -> u32 { +pub unsafe fn vg_mldsa44_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { crate::mldsa44::vg_mldsa44_sign(sk, mu, rnd, sig, scratch) } } -/// ML-DSA-44 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa44_sign_message_prepared`. +/// ML-DSA-44 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. /// -/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. +/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. /// -/// It runs `vg_mldsa44_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa44_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `sk` must be valid for reads of 2560 bytes. -/// * `prepared` must be valid for reads and writes of 14848 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. -/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). -/// * None of `sk`, `prepared` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mldsa44_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], prepared: *mut [u8; 14848], scratch: *mut [u64; 128]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_prepare_signing_key(sk, prepared, scratch) } -} - -/// ML-DSA-44 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa44_verify_message_prepared`. -/// -/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. -/// -/// It runs `vg_mldsa44_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 1312 bytes. -/// * `prepared` must be valid for reads and writes of 5408 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `msg` must be valid for reads of `msg_len` bytes. +/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `rnd` must be valid for reads of 32 bytes. +/// * `sig` must be valid for reads and writes of 2420 bytes. +/// * `scratch` must be valid for reads and writes of 78848 bytes. +/// * `sk` must have been written by `vg_mldsa44_keygen`. +/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). -/// * None of `pk`, `prepared` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], prepared: *mut [u8; 5408], scratch: *mut [u64; 128]) { +pub unsafe fn vg_mldsa44_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9856]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_prepare_verifying_key(pk, prepared, scratch) } + unsafe { crate::mldsa44::vg_mldsa44_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } /// ML-DSA-44 signing with a prepared private key: `vg_mldsa44_sign_message` (with its results, timing and leakage) of the private key that `*sk` begins with. @@ -189,6 +167,28 @@ pub unsafe fn vg_mldsa44_sign_message_prepared_auto(_dit: verified_garbage_cpu:: unsafe { crate::mldsa44::vg_mldsa44_sign_message_prepared(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } +/// ML-DSA-44 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// +/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// +/// It runs `vg_mldsa44_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 1312 bytes. +/// * `mu` must be valid for reads of 64 bytes. +/// * `sig` must be valid for reads of 2420 bytes. +/// * `scratch` must be valid for reads and writes of 77824 bytes. +/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). +/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). +/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mldsa44_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], mu: *const [u8; 64], sig: *const [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mldsa44::vg_mldsa44_verify(pk, mu, sig, scratch) } +} + /// ML-DSA-44 message verification with a cached public-key digest. Returns 1 for a valid signature, 0 for an invalid signature or exhausted sampling bound, and 2 if the context exceeds 255 bytes. It is `ML-DSA.Verify(pk, M, σ, ctx)` (FIPS 204 Algorithm 3) with `H(pk, 64)` read from `*tr`: with the public key `*pk`, the `msg_len` bytes of the message at `msg`, the `ctx_len` bytes of the context string at `ctx` and the signature `*sig`, it formats the message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 5), computes `μ = H(tr ‖ M′, 64)`, and verifies `*sig` on `μ` with `*pk`, `ML-DSA.Verify_internal(pk, M′, σ)` (Algorithm 8). /// /// Contract: `VG.Spec.MlDsa.verifyMessageCachedContract`. Timing may depend on the public key, message, context string and signature, and on argument addresses and lengths. diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/mldsa65.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/mldsa65.rs index e6cc9f6accc..47d91b305a8 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/mldsa65.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/mldsa65.rs @@ -2,29 +2,27 @@ //! The dispatchers of the verified `mldsa65` functions for `powerpc64le` //! (`crate::mldsa65`, `VG.Rust.dispatcher`). -/// ML-DSA-65 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-65 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. +/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. /// -/// It runs `vg_mldsa65_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa65_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `sk` must be valid for reads of 4032 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `rnd` must be valid for reads of 32 bytes. -/// * `sig` must be valid for reads and writes of 3309 bytes. +/// * `seed` must be valid for reads of 32 bytes. +/// * `pk` must be valid for reads and writes of 1952 bytes. +/// * `sk` must be valid for reads and writes of 4032 bytes. /// * `scratch` must be valid for reads and writes of 103424 bytes. -/// * `sk` must have been written by `vg_mldsa65_keygen`. -/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. +/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { +pub unsafe fn vg_mldsa65_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1952], sk: *mut [u8; 4032], scratch: *mut [u64; 12928]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_sign(sk, mu, rnd, sig, scratch) } + unsafe { crate::mldsa65::vg_mldsa65_keygen(seed, pk, sk, scratch) } } /// ML-DSA-65 public-key hash: writes `tr = H(pk, 64)` (FIPS 204 Algorithm 8, line 6), SHAKE256 of the public key `*pk` to 64 bytes, to `*tr`, the digest `vg_mldsa65_verify_message_cached` takes. @@ -48,118 +46,98 @@ pub unsafe fn vg_mldsa65_pk_tr_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *co unsafe { crate::mldsa65::vg_mldsa65_pk_tr(pk, tr, scratch) } } -/// ML-DSA-65 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-65 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa65_verify_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. +/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. /// -/// It runs `vg_mldsa65_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa65_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `seed` must be valid for reads of 32 bytes. -/// * `pk` must be valid for reads and writes of 1952 bytes. -/// * `sk` must be valid for reads and writes of 4032 bytes. -/// * `scratch` must be valid for reads and writes of 103424 bytes. -/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. +/// * `pk` must be valid for reads of 1952 bytes. +/// * `prepared` must be valid for reads and writes of 8096 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). +/// * None of `pk`, `prepared` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1952], sk: *mut [u8; 4032], scratch: *mut [u64; 12928]) -> u32 { +pub unsafe fn vg_mldsa65_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], prepared: *mut [u8; 8096], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_keygen(seed, pk, sk, scratch) } + unsafe { crate::mldsa65::vg_mldsa65_prepare_verifying_key(pk, prepared, scratch) } } -/// ML-DSA-65 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// ML-DSA-65 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa65_sign_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. /// -/// It runs `vg_mldsa65_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa65_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `pk` must be valid for reads of 1952 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `sig` must be valid for reads of 3309 bytes. -/// * `scratch` must be valid for reads and writes of 103424 bytes. +/// * `sk` must be valid for reads of 4032 bytes. +/// * `prepared` must be valid for reads and writes of 21440 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). -/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). +/// * None of `sk`, `prepared` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], mu: *const [u8; 64], sig: *const [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { +pub unsafe fn vg_mldsa65_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], prepared: *mut [u8; 21440], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_verify(pk, mu, sig, scratch) } + unsafe { crate::mldsa65::vg_mldsa65_prepare_signing_key(sk, prepared, scratch) } } -/// ML-DSA-65 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. +/// ML-DSA-65 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. +/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. /// -/// It runs `vg_mldsa65_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa65_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `sk` must be valid for reads of 4032 bytes. -/// * `msg` must be valid for reads of `msg_len` bytes. -/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `mu` must be valid for reads of 64 bytes. /// * `rnd` must be valid for reads of 32 bytes. /// * `sig` must be valid for reads and writes of 3309 bytes. -/// * `scratch` must be valid for reads and writes of 104448 bytes. +/// * `scratch` must be valid for reads and writes of 103424 bytes. /// * `sk` must have been written by `vg_mldsa65_keygen`. /// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 13056]) -> u32 { +pub unsafe fn vg_mldsa65_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { crate::mldsa65::vg_mldsa65_sign(sk, mu, rnd, sig, scratch) } } -/// ML-DSA-65 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa65_sign_message_prepared`. +/// ML-DSA-65 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. /// -/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. +/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. /// -/// It runs `vg_mldsa65_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa65_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `sk` must be valid for reads of 4032 bytes. -/// * `prepared` must be valid for reads and writes of 21440 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. -/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). -/// * None of `sk`, `prepared` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mldsa65_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], prepared: *mut [u8; 21440], scratch: *mut [u64; 128]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_prepare_signing_key(sk, prepared, scratch) } -} - -/// ML-DSA-65 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa65_verify_message_prepared`. -/// -/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. -/// -/// It runs `vg_mldsa65_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 1952 bytes. -/// * `prepared` must be valid for reads and writes of 8096 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `msg` must be valid for reads of `msg_len` bytes. +/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `rnd` must be valid for reads of 32 bytes. +/// * `sig` must be valid for reads and writes of 3309 bytes. +/// * `scratch` must be valid for reads and writes of 104448 bytes. +/// * `sk` must have been written by `vg_mldsa65_keygen`. +/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). -/// * None of `pk`, `prepared` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], prepared: *mut [u8; 8096], scratch: *mut [u64; 128]) { +pub unsafe fn vg_mldsa65_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 13056]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_prepare_verifying_key(pk, prepared, scratch) } + unsafe { crate::mldsa65::vg_mldsa65_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } /// ML-DSA-65 signing with a prepared private key: `vg_mldsa65_sign_message` (with its results, timing and leakage) of the private key that `*sk` begins with. @@ -189,6 +167,28 @@ pub unsafe fn vg_mldsa65_sign_message_prepared_auto(_dit: verified_garbage_cpu:: unsafe { crate::mldsa65::vg_mldsa65_sign_message_prepared(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } +/// ML-DSA-65 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// +/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// +/// It runs `vg_mldsa65_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 1952 bytes. +/// * `mu` must be valid for reads of 64 bytes. +/// * `sig` must be valid for reads of 3309 bytes. +/// * `scratch` must be valid for reads and writes of 103424 bytes. +/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). +/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). +/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mldsa65_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], mu: *const [u8; 64], sig: *const [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mldsa65::vg_mldsa65_verify(pk, mu, sig, scratch) } +} + /// ML-DSA-65 message verification with a cached public-key digest. Returns 1 for a valid signature, 0 for an invalid signature or exhausted sampling bound, and 2 if the context exceeds 255 bytes. It is `ML-DSA.Verify(pk, M, σ, ctx)` (FIPS 204 Algorithm 3) with `H(pk, 64)` read from `*tr`: with the public key `*pk`, the `msg_len` bytes of the message at `msg`, the `ctx_len` bytes of the context string at `ctx` and the signature `*sig`, it formats the message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 5), computes `μ = H(tr ‖ M′, 64)`, and verifies `*sig` on `μ` with `*pk`, `ML-DSA.Verify_internal(pk, M′, σ)` (Algorithm 8). /// /// Contract: `VG.Spec.MlDsa.verifyMessageCachedContract`. Timing may depend on the public key, message, context string and signature, and on argument addresses and lengths. diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/mldsa87.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/mldsa87.rs index fb2d5aa68f9..70497d40771 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/mldsa87.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/mldsa87.rs @@ -2,29 +2,27 @@ //! The dispatchers of the verified `mldsa87` functions for `powerpc64le` //! (`crate::mldsa87`, `VG.Rust.dispatcher`). -/// ML-DSA-87 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-87 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. +/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. /// -/// It runs `vg_mldsa87_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa87_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `sk` must be valid for reads of 4896 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `rnd` must be valid for reads of 32 bytes. -/// * `sig` must be valid for reads and writes of 4627 bytes. +/// * `seed` must be valid for reads of 32 bytes. +/// * `pk` must be valid for reads and writes of 2592 bytes. +/// * `sk` must be valid for reads and writes of 4896 bytes. /// * `scratch` must be valid for reads and writes of 144384 bytes. -/// * `sk` must have been written by `vg_mldsa87_keygen`. -/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. +/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { +pub unsafe fn vg_mldsa87_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 2592], sk: *mut [u8; 4896], scratch: *mut [u64; 18048]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_sign(sk, mu, rnd, sig, scratch) } + unsafe { crate::mldsa87::vg_mldsa87_keygen(seed, pk, sk, scratch) } } /// ML-DSA-87 public-key hash: writes `tr = H(pk, 64)` (FIPS 204 Algorithm 8, line 6), SHAKE256 of the public key `*pk` to 64 bytes, to `*tr`, the digest `vg_mldsa87_verify_message_cached` takes. @@ -48,118 +46,98 @@ pub unsafe fn vg_mldsa87_pk_tr_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *co unsafe { crate::mldsa87::vg_mldsa87_pk_tr(pk, tr, scratch) } } -/// ML-DSA-87 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-87 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa87_verify_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. +/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. /// -/// It runs `vg_mldsa87_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa87_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `seed` must be valid for reads of 32 bytes. -/// * `pk` must be valid for reads and writes of 2592 bytes. -/// * `sk` must be valid for reads and writes of 4896 bytes. -/// * `scratch` must be valid for reads and writes of 144384 bytes. -/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. +/// * `pk` must be valid for reads of 2592 bytes. +/// * `prepared` must be valid for reads and writes of 10784 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). +/// * None of `pk`, `prepared` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 2592], sk: *mut [u8; 4896], scratch: *mut [u64; 18048]) -> u32 { +pub unsafe fn vg_mldsa87_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], prepared: *mut [u8; 10784], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_keygen(seed, pk, sk, scratch) } + unsafe { crate::mldsa87::vg_mldsa87_prepare_verifying_key(pk, prepared, scratch) } } -/// ML-DSA-87 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// ML-DSA-87 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa87_sign_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. /// -/// It runs `vg_mldsa87_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa87_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `pk` must be valid for reads of 2592 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `sig` must be valid for reads of 4627 bytes. -/// * `scratch` must be valid for reads and writes of 144384 bytes. +/// * `sk` must be valid for reads of 4896 bytes. +/// * `prepared` must be valid for reads and writes of 28448 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). -/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). +/// * None of `sk`, `prepared` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], mu: *const [u8; 64], sig: *const [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { +pub unsafe fn vg_mldsa87_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], prepared: *mut [u8; 28448], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_verify(pk, mu, sig, scratch) } + unsafe { crate::mldsa87::vg_mldsa87_prepare_signing_key(sk, prepared, scratch) } } -/// ML-DSA-87 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. +/// ML-DSA-87 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. +/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. /// -/// It runs `vg_mldsa87_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa87_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `sk` must be valid for reads of 4896 bytes. -/// * `msg` must be valid for reads of `msg_len` bytes. -/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `mu` must be valid for reads of 64 bytes. /// * `rnd` must be valid for reads of 32 bytes. /// * `sig` must be valid for reads and writes of 4627 bytes. -/// * `scratch` must be valid for reads and writes of 145408 bytes. +/// * `scratch` must be valid for reads and writes of 144384 bytes. /// * `sk` must have been written by `vg_mldsa87_keygen`. /// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18176]) -> u32 { +pub unsafe fn vg_mldsa87_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { crate::mldsa87::vg_mldsa87_sign(sk, mu, rnd, sig, scratch) } } -/// ML-DSA-87 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa87_sign_message_prepared`. +/// ML-DSA-87 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. /// -/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. +/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. /// -/// It runs `vg_mldsa87_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa87_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `sk` must be valid for reads of 4896 bytes. -/// * `prepared` must be valid for reads and writes of 28448 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. -/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). -/// * None of `sk`, `prepared` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mldsa87_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], prepared: *mut [u8; 28448], scratch: *mut [u64; 128]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_prepare_signing_key(sk, prepared, scratch) } -} - -/// ML-DSA-87 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa87_verify_message_prepared`. -/// -/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. -/// -/// It runs `vg_mldsa87_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 2592 bytes. -/// * `prepared` must be valid for reads and writes of 10784 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `msg` must be valid for reads of `msg_len` bytes. +/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `rnd` must be valid for reads of 32 bytes. +/// * `sig` must be valid for reads and writes of 4627 bytes. +/// * `scratch` must be valid for reads and writes of 145408 bytes. +/// * `sk` must have been written by `vg_mldsa87_keygen`. +/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). -/// * None of `pk`, `prepared` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], prepared: *mut [u8; 10784], scratch: *mut [u64; 128]) { +pub unsafe fn vg_mldsa87_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18176]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_prepare_verifying_key(pk, prepared, scratch) } + unsafe { crate::mldsa87::vg_mldsa87_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } /// ML-DSA-87 signing with a prepared private key: `vg_mldsa87_sign_message` (with its results, timing and leakage) of the private key that `*sk` begins with. @@ -189,6 +167,28 @@ pub unsafe fn vg_mldsa87_sign_message_prepared_auto(_dit: verified_garbage_cpu:: unsafe { crate::mldsa87::vg_mldsa87_sign_message_prepared(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } +/// ML-DSA-87 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// +/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// +/// It runs `vg_mldsa87_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 2592 bytes. +/// * `mu` must be valid for reads of 64 bytes. +/// * `sig` must be valid for reads of 4627 bytes. +/// * `scratch` must be valid for reads and writes of 144384 bytes. +/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). +/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). +/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mldsa87_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], mu: *const [u8; 64], sig: *const [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mldsa87::vg_mldsa87_verify(pk, mu, sig, scratch) } +} + /// ML-DSA-87 message verification with a cached public-key digest. Returns 1 for a valid signature, 0 for an invalid signature or exhausted sampling bound, and 2 if the context exceeds 255 bytes. It is `ML-DSA.Verify(pk, M, σ, ctx)` (FIPS 204 Algorithm 3) with `H(pk, 64)` read from `*tr`: with the public key `*pk`, the `msg_len` bytes of the message at `msg`, the `ctx_len` bytes of the context string at `ctx` and the signature `*sig`, it formats the message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 5), computes `μ = H(tr ‖ M′, 64)`, and verifies `*sig` on `μ` with `*pk`, `ML-DSA.Verify_internal(pk, M′, σ)` (Algorithm 8). /// /// Contract: `VG.Spec.MlDsa.verifyMessageCachedContract`. Timing may depend on the public key, message, context string and signature, and on argument addresses and lengths. diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/mlkem1024.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/mlkem1024.rs index 3f481f21f02..0b129b6fdd0 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/mlkem1024.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/mlkem1024.rs @@ -2,6 +2,23 @@ //! The dispatchers of the verified `mlkem1024` functions for `powerpc64le` //! (`crate::mlkem1024`, `VG.Rust.dispatcher`). +/// The ML-KEM-1024 encapsulation key check (FIPS 203 §7.2): returns 1 if every 12-bit integer that the first 1536 bytes of `*ek` encode is less than `q` = 3329 (the modulus check), and 0 otherwise. An encapsulation key must pass it before it is given to `vg_mlkem1024_encaps`. +/// +/// Contract: `VG.Spec.MlKem1024.checkEkContract`. Constant time: only the pointer may affect timing. +/// +/// It runs `vg_mlkem1024_check_ek`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `ek` must be valid for reads of 1568 bytes. +/// * `ek` must not wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mlkem1024_check_ek_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1568]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mlkem1024::vg_mlkem1024_check_ek(ek) } +} + /// ML-KEM-1024 key generation from a seed, `ML-KEM.KeyGen_internal(d, z)` (FIPS 203 Algorithm 16): with `d` in bytes 0–31 of `*seed` and `z` in bytes 32–63, writes the encapsulation key to `*ek` and the decapsulation key to `*dk`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// /// Contract: `VG.Spec.MlKem1024.keyGenContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of the encapsulation key), but not on anything else of the seed or the keys. @@ -50,46 +67,6 @@ pub unsafe fn vg_mlkem1024_encaps_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: unsafe { crate::mlkem1024::vg_mlkem1024_encaps(ek, m, key, ct, scratch) } } -/// ML-KEM-1024 decapsulation, `ML-KEM.Decaps_internal(dk, c)` (FIPS 203 Algorithm 18): with the decapsulation key `*dk` and the ciphertext `*ct`, writes the shared secret key to `*key`, which is the implicit rejection key `J(z ‖ c)` if the ciphertext does not re-encrypt to itself. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. -/// -/// Contract: `VG.Spec.MlKem1024.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 3072–3103 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. -/// -/// It runs `vg_mlkem1024_decaps`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `dk` must be valid for reads of 3168 bytes. -/// * `ct` must be valid for reads of 1568 bytes. -/// * `key` must be valid for reads and writes of 32 bytes. -/// * `scratch` must be valid for reads and writes of 49152 bytes. -/// * `dk` must have been written by `vg_mlkem1024_keygen` (so that it passes the checks of FIPS 203 §7.3). -/// * `scratch` is working space: on return each of its bytes is unchanged or zero. -/// * `key` and `scratch` must not overlap each other, `dk` or `ct` (distinct Rust objects never do). -/// * None of `dk`, `ct`, `key` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mlkem1024_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: *const [u8; 3168], ct: *const [u8; 1568], key: *mut [u8; 32], scratch: *mut [u64; 6144]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mlkem1024::vg_mlkem1024_decaps(dk, ct, key, scratch) } -} - -/// The ML-KEM-1024 encapsulation key check (FIPS 203 §7.2): returns 1 if every 12-bit integer that the first 1536 bytes of `*ek` encode is less than `q` = 3329 (the modulus check), and 0 otherwise. An encapsulation key must pass it before it is given to `vg_mlkem1024_encaps`. -/// -/// Contract: `VG.Spec.MlKem1024.checkEkContract`. Constant time: only the pointer may affect timing. -/// -/// It runs `vg_mlkem1024_check_ek`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `ek` must be valid for reads of 1568 bytes. -/// * `ek` must not wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mlkem1024_check_ek_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1568]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mlkem1024::vg_mlkem1024_check_ek(ek) } -} - /// ML-KEM-1024 encapsulation with given randomness and the hash of the key, `ML-KEM.Encaps_internal(ek, m)` (FIPS 203 Algorithm 17): with the encapsulation key `*ek`, its hash `H(ek)` in `*h` and the randomness `*m`, writes the shared secret key to `*key` and the ciphertext to `*ct`, as `vg_mlkem1024_encaps` does, using `*h` instead of hashing `*ek`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// /// Contract: `VG.Spec.MlKem1024.encapsHContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on its hash, on the randomness or on the outputs. @@ -116,3 +93,26 @@ pub unsafe fn vg_mlkem1024_encaps_h_auto(_dit: verified_garbage_cpu::Dit<'_>, ek // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::mlkem1024::vg_mlkem1024_encaps_h(ek, h, m, key, ct, scratch) } } + +/// ML-KEM-1024 decapsulation, `ML-KEM.Decaps_internal(dk, c)` (FIPS 203 Algorithm 18): with the decapsulation key `*dk` and the ciphertext `*ct`, writes the shared secret key to `*key`, which is the implicit rejection key `J(z ‖ c)` if the ciphertext does not re-encrypt to itself. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// +/// Contract: `VG.Spec.MlKem1024.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 3072–3103 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. +/// +/// It runs `vg_mlkem1024_decaps`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `dk` must be valid for reads of 3168 bytes. +/// * `ct` must be valid for reads of 1568 bytes. +/// * `key` must be valid for reads and writes of 32 bytes. +/// * `scratch` must be valid for reads and writes of 49152 bytes. +/// * `dk` must have been written by `vg_mlkem1024_keygen` (so that it passes the checks of FIPS 203 §7.3). +/// * `scratch` is working space: on return each of its bytes is unchanged or zero. +/// * `key` and `scratch` must not overlap each other, `dk` or `ct` (distinct Rust objects never do). +/// * None of `dk`, `ct`, `key` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mlkem1024_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: *const [u8; 3168], ct: *const [u8; 1568], key: *mut [u8; 32], scratch: *mut [u64; 6144]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mlkem1024::vg_mlkem1024_decaps(dk, ct, key, scratch) } +} diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/mlkem768.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/mlkem768.rs index 1fd2f10f01e..4dff7cbcac4 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/mlkem768.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/mlkem768.rs @@ -50,29 +50,6 @@ pub unsafe fn vg_mlkem768_encaps_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: * unsafe { crate::mlkem768::vg_mlkem768_encaps(ek, m, key, ct, scratch) } } -/// ML-KEM-768 decapsulation, `ML-KEM.Decaps_internal(dk, c)` (FIPS 203 Algorithm 18): with the decapsulation key `*dk` and the ciphertext `*ct`, writes the shared secret key to `*key`, which is the implicit rejection key `J(z ‖ c)` if the ciphertext does not re-encrypt to itself. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. -/// -/// Contract: `VG.Spec.MlKem.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 2304–2335 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. -/// -/// It runs `vg_mlkem768_decaps`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `dk` must be valid for reads of 2400 bytes. -/// * `ct` must be valid for reads of 1088 bytes. -/// * `key` must be valid for reads and writes of 32 bytes. -/// * `scratch` must be valid for reads and writes of 32768 bytes. -/// * `dk` must have been written by `vg_mlkem768_keygen` (so that it passes the checks of FIPS 203 §7.3). -/// * `scratch` is working space: on return each of its bytes is unchanged or zero. -/// * `key` and `scratch` must not overlap each other, `dk` or `ct` (distinct Rust objects never do). -/// * None of `dk`, `ct`, `key` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mlkem768_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: *const [u8; 2400], ct: *const [u8; 1088], key: *mut [u8; 32], scratch: *mut [u64; 4096]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mlkem768::vg_mlkem768_decaps(dk, ct, key, scratch) } -} - /// ML-KEM-768 encapsulation with given randomness and the hash of the key, `ML-KEM.Encaps_internal(ek, m)` (FIPS 203 Algorithm 17): with the encapsulation key `*ek`, its hash `H(ek)` in `*h` and the randomness `*m`, writes the shared secret key to `*key` and the ciphertext to `*ct`, as `vg_mlkem768_encaps` does, using `*h` instead of hashing `*ek`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// /// Contract: `VG.Spec.MlKem.encapsHContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on its hash, on the randomness or on the outputs. @@ -100,6 +77,29 @@ pub unsafe fn vg_mlkem768_encaps_h_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: unsafe { crate::mlkem768::vg_mlkem768_encaps_h(ek, h, m, key, ct, scratch) } } +/// ML-KEM-768 decapsulation, `ML-KEM.Decaps_internal(dk, c)` (FIPS 203 Algorithm 18): with the decapsulation key `*dk` and the ciphertext `*ct`, writes the shared secret key to `*key`, which is the implicit rejection key `J(z ‖ c)` if the ciphertext does not re-encrypt to itself. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// +/// Contract: `VG.Spec.MlKem.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 2304–2335 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. +/// +/// It runs `vg_mlkem768_decaps`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `dk` must be valid for reads of 2400 bytes. +/// * `ct` must be valid for reads of 1088 bytes. +/// * `key` must be valid for reads and writes of 32 bytes. +/// * `scratch` must be valid for reads and writes of 32768 bytes. +/// * `dk` must have been written by `vg_mlkem768_keygen` (so that it passes the checks of FIPS 203 §7.3). +/// * `scratch` is working space: on return each of its bytes is unchanged or zero. +/// * `key` and `scratch` must not overlap each other, `dk` or `ct` (distinct Rust objects never do). +/// * None of `dk`, `ct`, `key` and `scratch` may overlap the 96 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mlkem768_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: *const [u8; 2400], ct: *const [u8; 1088], key: *mut [u8; 32], scratch: *mut [u64; 4096]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mlkem768::vg_mlkem768_decaps(dk, ct, key, scratch) } +} + /// The ML-KEM-768 encapsulation key check (FIPS 203 §7.2): returns 1 if every 12-bit integer that the first 1152 bytes of `*ek` encode is less than `q` = 3329 (the modulus check), and 0 otherwise. An encapsulation key must pass it before it is given to `vg_mlkem768_encaps`. /// /// Contract: `VG.Spec.MlKem.checkEkContract`. Constant time: only the pointer may affect timing. diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/rsa.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/rsa.rs index dd4a965be9c..0a0bcdf6bcc 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/rsa.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/rsa.rs @@ -2,36 +2,28 @@ //! The dispatchers of the verified `rsa` functions for `powerpc64le` //! (`crate::rsa`, `VG.Rust.dispatcher`). -/// Checks an RSA private key `(n, e, d, p, q, dP, dQ, qInv)` exactly as BoringSSL's `RSA_check_key` does. With the modulus `n` (`n_len` bytes), the public exponent `e` (`e_len` bytes), the private exponent `d` (`d_len` bytes), `p`, `dp` and `qinv` (`p_len` bytes) and `q` and `dq` (`q_len` bytes), all most significant first, returns 1 if `n` is odd, from 512 to 8192 bits and its first byte not zero, `e` is odd and from 3 to `2^33 - 1`, `d < n`, `p < n`, `q < n`, `p q = n`, `d e ≡ 1 (mod p - 1)`, `d e ≡ 1 (mod q - 1)`, `dP < p - 1`, `e dP ≡ 1 (mod p - 1)`, `dQ < q - 1`, `e dQ ≡ 1 (mod q - 1)`, `qInv < p` and `q qInv ≡ 1 (mod p)`; and 0 otherwise. Like `RSA_check_key`, it does not check that `p` and `q` are prime; if they are, `vg_rsa_private_checked` never returns its internal error for a key it accepts. +/// The values of an RSA modulus that `vg_rsa_public_precomputed_checked` takes, so that they are computed once per public key rather than once per operation. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero) of `w = ⌈n_len / 8⌉` words of 64 bits, writes `n` and then `R² mod n` for `R = 2^(64 w)` to `pre` (`w` words each, least significant first) and returns 1; or writes zeros and returns 0 if `n` is not such a modulus. /// -/// Contract: `VG.Spec.Rsa.checkKeyContract`. Constant time but for the public key: timing may depend on the pointers, the lengths and the contents of `n` and `e`, not on the private key. +/// Contract: `VG.Spec.Rsa.publicPrecomputeContract`. Timing may depend on the pointers, the lengths and the contents of `n`. /// -/// It runs `vg_rsa_check_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_rsa_public_precompute`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// +/// * `pre` must be valid for reads and writes of `8 * pre_len` bytes. /// * `n` must be valid for reads of `n_len` bytes. -/// * `e` must be valid for reads of `e_len` bytes. -/// * `d` must be valid for reads of `d_len` bytes. -/// * `p` must be valid for reads of `p_len` bytes. -/// * `q` must be valid for reads of `q_len` bytes. -/// * `dp` must be valid for reads of `dp_len` bytes. -/// * `dq` must be valid for reads of `dq_len` bytes. -/// * `qinv` must be valid for reads of `qinv_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. /// * `n_len` must be in 64..=1024. -/// * `e_len` and `d_len` must be in 1..=`n_len`. -/// * `p_len` and `q_len` must be in 1..`n_len`. -/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. -/// * `scratch_len` must be at least `16 * n_len + 32`. +/// * `pre_len` must be `2 * ⌈n_len / 8⌉`. +/// * `scratch_len` must be at least `8 * ⌈n_len / 8⌉ + 48`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `scratch` must not overlap `n`, `e`, `d`, `p`, `q`, `dp`, `dq`, `qinv` or the arguments on the stack (distinct Rust objects never do). -/// * None of `n`, `e`, `d`, `p`, `q`, `dp`, `dq`, `qinv` and `scratch` may wrap around the end of the address space (no Rust object does). +/// * `pre` and `scratch` must not overlap each other or `n` (distinct Rust objects never do). +/// * None of `pre`, `n` and `scratch` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_rsa_check_key_auto(_dit: verified_garbage_cpu::Dit<'_>, n: *const u8, n_len: usize, e: *const u8, e_len: usize, d: *const u8, d_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_rsa_public_precompute_auto(_dit: verified_garbage_cpu::Dit<'_>, pre: *mut u64, pre_len: usize, n: *const u8, n_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa::vg_rsa_check_key(n, n_len, e, e_len, d, d_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, scratch, scratch_len) } + unsafe { crate::rsa::vg_rsa_public_precompute(pre, pre_len, n, n_len, scratch, scratch_len) } } /// The CRT values of an RSA private key `(p, q, d)` (SP 800-56B Rev. 2 §6.2.2's prime-factor format), which bring it to the CRT format `(p, q, dP, dQ, qInv)` that `vg_rsa_private_checked` takes: `dP = d mod (p - 1)`, `dQ = d mod (q - 1)` and `qInv = q⁻¹ mod p` (§6.2.2's CRT format). With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), `p` (`p_len` bytes), `q` (`q_len` bytes) and `d` (`d_len` bytes), all most significant first, writes `dP` to `dp` and `qInv` to `qinv` (`p_len` bytes each) and `dQ` to `dq` (`q_len` bytes), most significant first, and returns 1; or writes zeros to all three and returns 0 if `n` is not such a modulus, `p q ≠ n`, or `q` has no inverse modulo `p`. Nothing checks that `p` and `q` are prime or that `d` is the key's private exponent. @@ -65,28 +57,36 @@ pub unsafe fn vg_rsa_crt_values_auto(_dit: verified_garbage_cpu::Dit<'_>, dp: *m unsafe { crate::rsa::vg_rsa_crt_values(dp, dp_len, dq, dq_len, qinv, qinv_len, n, n_len, p, p_len, q, q_len, d, d_len, scratch, scratch_len) } } -/// The values of an RSA modulus that `vg_rsa_public_precomputed_checked` takes, so that they are computed once per public key rather than once per operation. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero) of `w = ⌈n_len / 8⌉` words of 64 bits, writes `n` and then `R² mod n` for `R = 2^(64 w)` to `pre` (`w` words each, least significant first) and returns 1; or writes zeros and returns 0 if `n` is not such a modulus. +/// Checks an RSA private key `(n, e, d, p, q, dP, dQ, qInv)` exactly as BoringSSL's `RSA_check_key` does. With the modulus `n` (`n_len` bytes), the public exponent `e` (`e_len` bytes), the private exponent `d` (`d_len` bytes), `p`, `dp` and `qinv` (`p_len` bytes) and `q` and `dq` (`q_len` bytes), all most significant first, returns 1 if `n` is odd, from 512 to 8192 bits and its first byte not zero, `e` is odd and from 3 to `2^33 - 1`, `d < n`, `p < n`, `q < n`, `p q = n`, `d e ≡ 1 (mod p - 1)`, `d e ≡ 1 (mod q - 1)`, `dP < p - 1`, `e dP ≡ 1 (mod p - 1)`, `dQ < q - 1`, `e dQ ≡ 1 (mod q - 1)`, `qInv < p` and `q qInv ≡ 1 (mod p)`; and 0 otherwise. Like `RSA_check_key`, it does not check that `p` and `q` are prime; if they are, `vg_rsa_private_checked` never returns its internal error for a key it accepts. /// -/// Contract: `VG.Spec.Rsa.publicPrecomputeContract`. Timing may depend on the pointers, the lengths and the contents of `n`. +/// Contract: `VG.Spec.Rsa.checkKeyContract`. Constant time but for the public key: timing may depend on the pointers, the lengths and the contents of `n` and `e`, not on the private key. /// -/// It runs `vg_rsa_public_precompute`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_rsa_check_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `pre` must be valid for reads and writes of `8 * pre_len` bytes. /// * `n` must be valid for reads of `n_len` bytes. +/// * `e` must be valid for reads of `e_len` bytes. +/// * `d` must be valid for reads of `d_len` bytes. +/// * `p` must be valid for reads of `p_len` bytes. +/// * `q` must be valid for reads of `q_len` bytes. +/// * `dp` must be valid for reads of `dp_len` bytes. +/// * `dq` must be valid for reads of `dq_len` bytes. +/// * `qinv` must be valid for reads of `qinv_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. /// * `n_len` must be in 64..=1024. -/// * `pre_len` must be `2 * ⌈n_len / 8⌉`. -/// * `scratch_len` must be at least `8 * ⌈n_len / 8⌉ + 48`. +/// * `e_len` and `d_len` must be in 1..=`n_len`. +/// * `p_len` and `q_len` must be in 1..`n_len`. +/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. +/// * `scratch_len` must be at least `16 * n_len + 32`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `pre` and `scratch` must not overlap each other or `n` (distinct Rust objects never do). -/// * None of `pre`, `n` and `scratch` may wrap around the end of the address space (no Rust object does). +/// * `scratch` must not overlap `n`, `e`, `d`, `p`, `q`, `dp`, `dq`, `qinv` or the arguments on the stack (distinct Rust objects never do). +/// * None of `n`, `e`, `d`, `p`, `q`, `dp`, `dq`, `qinv` and `scratch` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_rsa_public_precompute_auto(_dit: verified_garbage_cpu::Dit<'_>, pre: *mut u64, pre_len: usize, n: *const u8, n_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_rsa_check_key_auto(_dit: verified_garbage_cpu::Dit<'_>, n: *const u8, n_len: usize, e: *const u8, e_len: usize, d: *const u8, d_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa::vg_rsa_public_precompute(pre, pre_len, n, n_len, scratch, scratch_len) } + unsafe { crate::rsa::vg_rsa_check_key(n, n_len, e, e_len, d, d_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, scratch, scratch_len) } } /// The prime factors `p > q` of the modulus of an RSA private key `(n, e, d)` (SP 800-56B Rev. 2 §6.2.2's basic format), which bring it to the prime-factor format `(p, q, d)` that `vg_rsa_crt_values` takes, recovered by SP 800-56B Rev. 2 Appendix C.2's deterministic method. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes) and the private exponent `d` (`d_len` bytes), both most significant first, writes `p` and `q` (`n_len` bytes each, most significant first) and returns 1; or writes zeros to both and returns 0 if `n` is not such a modulus or the method fails. It finds the factors of a valid two-prime key whenever `K (p + q − 1) ≤ n` for `a = K φ(n)` (Appendix C.2's step 1): with the least private exponent, whenever the smaller prime exceeds `e²`, and in practice for every key whose primes are not tiny. diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/rsa_keygen.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/rsa_keygen.rs index 587bd63476b..4a95f26e690 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/rsa_keygen.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/rsa_keygen.rs @@ -2,6 +2,25 @@ //! The dispatchers of the verified `rsa_keygen` functions for `powerpc64le` //! (`crate::rsa_keygen`, `VG.Rust.dispatcher`). +/// Start generating an RSA key, as BoringSSL's `RSA_generate_key_ex` (FIPS 186-5 Appendix A.1.3), with a modulus of `bits` bits rounded down to a multiple of 128, and the public exponent `e` (`e_len` octets, most significant first, with any number of leading zeros). Returns 0 if `e` is not odd from 3 to `2^32 - 1`, or 2 if `bits` is above 8192 or, rounded down, below 512, and writes zeros to `state`. Otherwise returns 1 and writes the search's seven public words to `state`: the rejected candidates the first prime's search allows, the primes' length in octets (`nlen / 16`), the entropy budget, the length of the first random tape to give `vg_rsa_generate`, the phase (0), the generations allowed (4), and the exponent's eight octets, most significant first. +/// +/// Contract: `VG.Spec.RsaKeyGen.keyInitContract`. Timing may depend on `bits` and `e`. +/// +/// It runs `vg_rsa_generate_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 56 bytes. +/// * `e` must be valid for reads of `e_len` bytes. +/// * `state` must not overlap `e` (distinct Rust objects never do). +/// * Neither `state` nor `e` may wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_rsa_generate_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 7], bits: usize, e: *const u8, e_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::rsa_keygen::vg_rsa_generate_init(state, bits, e, e_len) } +} + /// Generate an RSA key, as BoringSSL's `RSA_generate_key_ex` (FIPS 186-5 Appendix A.1.3), over the random tape `rand`, from the state `state` that `vg_rsa_generate_init` started: the search for `p`, then for `q` not too close to `p`, each over candidates of `p_len` octets with their top two bits and bottom bit set, rejected by trial division, a common factor of `p − 1` and `e` or Miller–Rabin, up to BoringSSL's limit of rejected candidates; then the key, `d = e⁻¹ mod lcm(p − 1, q − 1)`, `dP`, `dQ` and `qInv`, with `p` the larger prime, checked as BoringSSL's `RSA_check_key` checks it, drawing both primes again if `d` is too small; a generation whose prime's search is exhausted is started again, up to four in all. Returns 1 and writes the key to `key` (`key_len` is `9 p_len`, for the primes' length `p_len = state[1]`): `n` and `d` (`2 p_len` octets each), then `p`, `q`, `dP`, `dQ` and `qInv` (`p_len` each), all most significant first; 2 if the key failed a check; 3 if every generation was exhausted; or 0 if the tape ran out inside a candidate. Writes the octets of `rand` it consumed to `used`, and the state for the next call to `state`, whose fourth word is, for 0, the length of the next tape, more than the octets of `rand` after the first `used` and at most the entropy budget: keep those octets, in order, and append fresh random octets up to that length. For 0, `key` holds the search's `p` once it is found, which the next call reads: pass it back unchanged. `key` is otherwise zeros but for 1. /// /// Contract: `VG.Spec.RsaKeyGen.keySearchContract`. Timing may depend on the pointers, the lengths, `state`, the status and consumption it returns, and, for each prime's search, its rejected candidates and the octets its accepted candidate's tests read, and each key's status. Not on the primes or the key. @@ -28,22 +47,3 @@ pub unsafe fn vg_rsa_generate_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *mu // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::rsa_keygen::vg_rsa_generate(key, key_len, used, rand, rand_len, scratch, scratch_len, state) } } - -/// Start generating an RSA key, as BoringSSL's `RSA_generate_key_ex` (FIPS 186-5 Appendix A.1.3), with a modulus of `bits` bits rounded down to a multiple of 128, and the public exponent `e` (`e_len` octets, most significant first, with any number of leading zeros). Returns 0 if `e` is not odd from 3 to `2^32 - 1`, or 2 if `bits` is above 8192 or, rounded down, below 512, and writes zeros to `state`. Otherwise returns 1 and writes the search's seven public words to `state`: the rejected candidates the first prime's search allows, the primes' length in octets (`nlen / 16`), the entropy budget, the length of the first random tape to give `vg_rsa_generate`, the phase (0), the generations allowed (4), and the exponent's eight octets, most significant first. -/// -/// Contract: `VG.Spec.RsaKeyGen.keyInitContract`. Timing may depend on `bits` and `e`. -/// -/// It runs `vg_rsa_generate_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 56 bytes. -/// * `e` must be valid for reads of `e_len` bytes. -/// * `state` must not overlap `e` (distinct Rust objects never do). -/// * Neither `state` nor `e` may wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_rsa_generate_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 7], bits: usize, e: *const u8, e_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa_keygen::vg_rsa_generate_init(state, bits, e, e_len) } -} diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/rsa_pkcs1_sig.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/rsa_pkcs1_sig.rs index 620a26db71c..5d5361b3e74 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/rsa_pkcs1_sig.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/rsa_pkcs1_sig.rs @@ -2,40 +2,6 @@ //! The dispatchers of the verified `rsa_pkcs1_sig` functions for `powerpc64le` //! (`crate::rsa_pkcs1_sig`, `VG.Rust.dispatcher`). -/// RSASSA-PKCS1-v1_5 signature generation (RFC 8017 §8.2.1) of a hash value, as BoringSSL's `RSA_sign` does it. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits) and the private key `(p, q, dP, dQ, qInv)` (as `vg_rsa_private_checked` takes it), encodes `digest` with EMSA-PKCS1-v1_5 (RFC 8017 §9.2: `0x00 0x01`, bytes `0xff`, `0x00` and the DER encoding of its `DigestInfo`) to `n_len` bytes and computes the signature from it as `vg_rsa_private_checked` does: writes it to `out` (`n_len` bytes, most significant first) and returns 1; writes zeros and returns 0 if `hash` names no hash function, `digest` is not as long as its values, `n_len` is less than the encoding's 11 bytes more than the `DigestInfo`, or `vg_rsa_private_checked` refuses the key; and writes zeros and returns 2 (an internal error) if the signature fails the check against `e`, which is the case for no hash value if `vg_rsa_check_key` accepts the key and `p` and `q` are prime. A signature it writes is one `vg_rsa_pkcs1_verify` accepts. `hash` names the hash function: 0 MD5, 1 SHA-1, 2 SHA-224, 3 SHA-256, 4 SHA-384, 5 SHA-512, 6 SHA-512/224, 7 SHA-512/256, 8 SHA3-224, 9 SHA3-256, 10 SHA3-384, 11 SHA3-512 (`VG.Spec.RsaPkcs1Sig.Hash.ofId`). -/// -/// Contract: `VG.Spec.RsaPkcs1Sig.signContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, `hash` and the contents of `n` and `e`, not on `digest` or the private key. -/// -/// It runs `vg_rsa_pkcs1_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `out` must be valid for reads and writes of `out_len` bytes. -/// * `n` must be valid for reads of `n_len` bytes. -/// * `e` must be valid for reads of `e_len` bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `p` must be valid for reads of `p_len` bytes. -/// * `q` must be valid for reads of `q_len` bytes. -/// * `dp` must be valid for reads of `dp_len` bytes. -/// * `dq` must be valid for reads of `dq_len` bytes. -/// * `qinv` must be valid for reads of `qinv_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `n_len` must be in 64..=1024. -/// * `out_len` must be `n_len`. -/// * `e_len` must be in 1..=`n_len`. -/// * `p_len` and `q_len` must be in 1..`n_len`. -/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. -/// * `scratch_len` must be at least `16 * n_len + 32`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `n`, `e`, `digest`, `p`, `q`, `dp`, `dq`, `qinv` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `n`, `e`, `digest`, `p`, `q`, `dp`, `dq`, `qinv` and `scratch` may overlap the 4592 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_rsa_pkcs1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, n: *const u8, n_len: usize, e: *const u8, e_len: usize, hash: u32, digest: *const u8, digest_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa_pkcs1_sig::vg_rsa_pkcs1_sign(out, out_len, n, n_len, e, e_len, hash, digest, digest_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, scratch, scratch_len) } -} - /// RSASSA-PKCS1-v1_5 signature verification (RFC 8017 §8.2.2) of a hash value, as BoringSSL's `RSA_verify` does it. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero) and the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits), returns 1 if `sig` is `n_len` bytes, the integer `s` of `sig` (most significant byte first) is below `n`, and `s^e mod n` (as `n_len` bytes) is `0x00 0x01`, at least 8 bytes `0xff`, `0x00` and then exactly the DER encoding of the `DigestInfo` of `digest` (RFC 8017 §9.2 note 1), which must be as long as the hash function's values; and 0 otherwise, or if `n` or `e` is not such a number or `hash` names no hash function. That is, exactly when `s^e mod n` is EMSA-PKCS1-v1_5's encoding of `digest`: the signed `DigestInfo` is compared, never parsed. `hash` names the hash function: 0 MD5, 1 SHA-1, 2 SHA-224, 3 SHA-256, 4 SHA-384, 5 SHA-512, 6 SHA-512/224, 7 SHA-512/256, 8 SHA3-224, 9 SHA3-256, 10 SHA3-384, 11 SHA3-512 (`VG.Spec.RsaPkcs1Sig.Hash.ofId`). /// /// Contract: `VG.Spec.RsaPkcs1Sig.verifyContract`. Not constant time: timing may depend on the pointers, the lengths, `hash` and the contents of `n`, `e`, `digest` and `sig`. @@ -102,3 +68,37 @@ pub unsafe fn vg_rsa_pkcs1_recover_precomputed_auto(_dit: verified_garbage_cpu:: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::rsa_pkcs1_sig::vg_rsa_pkcs1_recover_precomputed(out, out_len, n, n_len, e, e_len, hash, sig, sig_len, scratch, scratch_len, pre, pre_len) } } + +/// RSASSA-PKCS1-v1_5 signature generation (RFC 8017 §8.2.1) of a hash value, as BoringSSL's `RSA_sign` does it. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits) and the private key `(p, q, dP, dQ, qInv)` (as `vg_rsa_private_checked` takes it), encodes `digest` with EMSA-PKCS1-v1_5 (RFC 8017 §9.2: `0x00 0x01`, bytes `0xff`, `0x00` and the DER encoding of its `DigestInfo`) to `n_len` bytes and computes the signature from it as `vg_rsa_private_checked` does: writes it to `out` (`n_len` bytes, most significant first) and returns 1; writes zeros and returns 0 if `hash` names no hash function, `digest` is not as long as its values, `n_len` is less than the encoding's 11 bytes more than the `DigestInfo`, or `vg_rsa_private_checked` refuses the key; and writes zeros and returns 2 (an internal error) if the signature fails the check against `e`, which is the case for no hash value if `vg_rsa_check_key` accepts the key and `p` and `q` are prime. A signature it writes is one `vg_rsa_pkcs1_verify` accepts. `hash` names the hash function: 0 MD5, 1 SHA-1, 2 SHA-224, 3 SHA-256, 4 SHA-384, 5 SHA-512, 6 SHA-512/224, 7 SHA-512/256, 8 SHA3-224, 9 SHA3-256, 10 SHA3-384, 11 SHA3-512 (`VG.Spec.RsaPkcs1Sig.Hash.ofId`). +/// +/// Contract: `VG.Spec.RsaPkcs1Sig.signContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, `hash` and the contents of `n` and `e`, not on `digest` or the private key. +/// +/// It runs `vg_rsa_pkcs1_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `out` must be valid for reads and writes of `out_len` bytes. +/// * `n` must be valid for reads of `n_len` bytes. +/// * `e` must be valid for reads of `e_len` bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `p` must be valid for reads of `p_len` bytes. +/// * `q` must be valid for reads of `q_len` bytes. +/// * `dp` must be valid for reads of `dp_len` bytes. +/// * `dq` must be valid for reads of `dq_len` bytes. +/// * `qinv` must be valid for reads of `qinv_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `n_len` must be in 64..=1024. +/// * `out_len` must be `n_len`. +/// * `e_len` must be in 1..=`n_len`. +/// * `p_len` and `q_len` must be in 1..`n_len`. +/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. +/// * `scratch_len` must be at least `16 * n_len + 32`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `out` and `scratch` must not overlap each other, `n`, `e`, `digest`, `p`, `q`, `dp`, `dq`, `qinv` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `n`, `e`, `digest`, `p`, `q`, `dp`, `dq`, `qinv` and `scratch` may overlap the 4592 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_rsa_pkcs1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, n: *const u8, n_len: usize, e: *const u8, e_len: usize, hash: u32, digest: *const u8, digest_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::rsa_pkcs1_sig::vg_rsa_pkcs1_sign(out, out_len, n, n_len, e, e_len, hash, digest, digest_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, scratch, scratch_len) } +} diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/rsa_pss.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/rsa_pss.rs index d8c3b0b7c2d..5b6d6bd4548 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/rsa_pss.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/rsa_pss.rs @@ -2,81 +2,81 @@ //! The dispatchers of the verified `rsa_pss` functions for `powerpc64le` //! (`crate::rsa_pss`, `VG.Rust.dispatcher`). -/// RSASSA-PSS signature generation (RFC 8017 §8.1.1) with the hash function of the table `hash` and MGF1 with that of the table `mgf1`, of a digest of the former, with the private key `(p, q, dP, dQ, qInv)` checked against the public exponent as `vg_rsa_private_checked` checks it. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero) and the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits), encodes the `digest_len`-byte digest `digest` with the salt `salt` (EMSA-PSS, RFC 8017 §9.1.1) and computes RSASP1 of the encoding: if it passes the check against `e`, writes the signature (`n_len` bytes, most significant first) to `out` and returns 1. Writes zeros and returns 0 if the salt does not fit (`salt_len` more than `⌈(bits of n - 1) / 8⌉ - digest_len - 2`), or the key is refused as `vg_rsa_private_checked` refuses it; writes zeros and returns 2 (an internal error) if the result fails the check against `e`, which it does for no digest if `vg_rsa_check_key` accepts the key and `p` and `q` are prime. The salt must be fresh random bytes for each signature (or empty). +/// RSASSA-PSS signature verification (RFC 8017 §8.1.2) with the hash function of the table `hash` and MGF1 with that of the table `mgf1`, of a signature of a digest of the former. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits) and `pre`, the `pre_len` words written by `vg_rsa_public_precompute` for `n` (returning 1), returns 1 if `sig` (`n_len` bytes, most significant first) is an RSASSA-PSS signature of the `digest_len`-byte digest `digest` with a salt of `salt_len` bytes, or of any length if `any_salt_len` is not zero (BoringSSL's `RSA_PSS_SALTLEN_AUTO`); and 0 if it is not, or if `n` is not such a modulus, `e` is not such a number, or `sig` is not below `n`. If `pre` does not hold those values, the result is unspecified; memory safety and the timing guarantee still apply. /// -/// Contract: `VG.Spec.RsaPss.signTableContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, the contents of `n` and `e`, and which tables `hash` and `mgf1` are, not on the private key, the digest or the salt. +/// Contract: `VG.Spec.RsaPss.verifyPrecomputedTableContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, `salt_len`, `any_salt_len`, the contents of `n`, `e` and `pre`, and which tables `hash` and `mgf1` are, not on the digest, the signature or anything computed from them but the result. /// -/// It runs `vg_rsa_pss_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_rsa_pss_verify_precomputed`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `out` must be valid for reads and writes of `out_len` bytes. /// * `n` must be valid for reads of `n_len` bytes. /// * `e` must be valid for reads of `e_len` bytes. -/// * `p` must be valid for reads of `p_len` bytes. -/// * `q` must be valid for reads of `q_len` bytes. -/// * `dp` must be valid for reads of `dp_len` bytes. -/// * `dq` must be valid for reads of `dq_len` bytes. -/// * `qinv` must be valid for reads of `qinv_len` bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `salt` must be valid for reads of `salt_len` bytes. +/// * `sig` must be valid for reads of `sig_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `pre` must be valid for reads of `8 * pre_len` bytes. /// * `hash` must be valid for reads of `11 * size_of::()` bytes. /// * `mgf1` must be valid for reads of `5 * size_of::()` bytes. /// * `n_len` must be in 64..=1024. -/// * `out_len` must be `n_len`. +/// * `sig_len` must be `n_len`. /// * `e_len` must be in 1..=`n_len`. -/// * `p_len` and `q_len` must be in 1..`n_len`. -/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. -/// * `digest_len` must be the digest length of the hash function `hash` names. -/// * `scratch_len` must be at least `16 * n_len + 1056`. +/// * `scratch_len` must be at least `8 * ⌈n_len / 8⌉ + 1072`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `digest_len` must be the digest length of the hash function `hash` names. +/// * `pre_len` must be `2 * ⌈n_len / 8⌉`. +/// * For the result to be signature verification's, `pre` must hold what `vg_rsa_public_precompute` wrote for `n` (returning 1). /// * `hash` names the hash function of the digest and of the encoding: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the hash functions' `MdHash` tables of this crate, `fn_tables`, say which they name). /// * `mgf1` names MGF1's hash function: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the hash functions' tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `n`, `e`, `p`, `q`, `dp`, `dq`, `qinv`, `digest`, `salt`, `hash`, `mgf1` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `n`, `e`, `p`, `q`, `dp`, `dq`, `qinv`, `digest`, `salt`, `scratch`, `hash` and `mgf1` may overlap the 3776 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `scratch` must not overlap `n`, `e`, `digest`, `sig`, `pre`, `hash`, `mgf1` or the arguments on the stack (distinct Rust objects never do). +/// * None of `n`, `e`, `digest`, `sig`, `scratch`, `pre`, `hash` and `mgf1` may overlap the 304 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). /// * `hash` must be the `as_ptr()` of one of the `MdHash` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. /// * `mgf1` must be the `as_ptr()` of one of the `Hash` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_rsa_pss_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, n: *const u8, n_len: usize, e: *const u8, e_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, digest: *const u8, digest_len: usize, salt: *const u8, salt_len: usize, scratch: *mut u64, scratch_len: usize, hash: *const [usize; 11], mgf1: *const [usize; 5]) -> u32 { +pub unsafe fn vg_rsa_pss_verify_precomputed_auto(_dit: verified_garbage_cpu::Dit<'_>, n: *const u8, n_len: usize, e: *const u8, e_len: usize, digest: *const u8, digest_len: usize, sig: *const u8, sig_len: usize, salt_len: usize, any_salt_len: u32, scratch: *mut u64, scratch_len: usize, pre: *const u64, pre_len: usize, hash: *const [usize; 11], mgf1: *const [usize; 5]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa_pss::vg_rsa_pss_sign(out, out_len, n, n_len, e, e_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, digest, digest_len, salt, salt_len, scratch, scratch_len, hash, mgf1) } + unsafe { crate::rsa_pss::vg_rsa_pss_verify_precomputed(n, n_len, e, e_len, digest, digest_len, sig, sig_len, salt_len, any_salt_len, scratch, scratch_len, pre, pre_len, hash, mgf1) } } -/// RSASSA-PSS signature verification (RFC 8017 §8.1.2) with the hash function of the table `hash` and MGF1 with that of the table `mgf1`, of a signature of a digest of the former. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits) and `pre`, the `pre_len` words written by `vg_rsa_public_precompute` for `n` (returning 1), returns 1 if `sig` (`n_len` bytes, most significant first) is an RSASSA-PSS signature of the `digest_len`-byte digest `digest` with a salt of `salt_len` bytes, or of any length if `any_salt_len` is not zero (BoringSSL's `RSA_PSS_SALTLEN_AUTO`); and 0 if it is not, or if `n` is not such a modulus, `e` is not such a number, or `sig` is not below `n`. If `pre` does not hold those values, the result is unspecified; memory safety and the timing guarantee still apply. +/// RSASSA-PSS signature generation (RFC 8017 §8.1.1) with the hash function of the table `hash` and MGF1 with that of the table `mgf1`, of a digest of the former, with the private key `(p, q, dP, dQ, qInv)` checked against the public exponent as `vg_rsa_private_checked` checks it. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero) and the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits), encodes the `digest_len`-byte digest `digest` with the salt `salt` (EMSA-PSS, RFC 8017 §9.1.1) and computes RSASP1 of the encoding: if it passes the check against `e`, writes the signature (`n_len` bytes, most significant first) to `out` and returns 1. Writes zeros and returns 0 if the salt does not fit (`salt_len` more than `⌈(bits of n - 1) / 8⌉ - digest_len - 2`), or the key is refused as `vg_rsa_private_checked` refuses it; writes zeros and returns 2 (an internal error) if the result fails the check against `e`, which it does for no digest if `vg_rsa_check_key` accepts the key and `p` and `q` are prime. The salt must be fresh random bytes for each signature (or empty). /// -/// Contract: `VG.Spec.RsaPss.verifyPrecomputedTableContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, `salt_len`, `any_salt_len`, the contents of `n`, `e` and `pre`, and which tables `hash` and `mgf1` are, not on the digest, the signature or anything computed from them but the result. +/// Contract: `VG.Spec.RsaPss.signTableContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, the contents of `n` and `e`, and which tables `hash` and `mgf1` are, not on the private key, the digest or the salt. /// -/// It runs `vg_rsa_pss_verify_precomputed`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_rsa_pss_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// +/// * `out` must be valid for reads and writes of `out_len` bytes. /// * `n` must be valid for reads of `n_len` bytes. /// * `e` must be valid for reads of `e_len` bytes. +/// * `p` must be valid for reads of `p_len` bytes. +/// * `q` must be valid for reads of `q_len` bytes. +/// * `dp` must be valid for reads of `dp_len` bytes. +/// * `dq` must be valid for reads of `dq_len` bytes. +/// * `qinv` must be valid for reads of `qinv_len` bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of `sig_len` bytes. +/// * `salt` must be valid for reads of `salt_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `pre` must be valid for reads of `8 * pre_len` bytes. /// * `hash` must be valid for reads of `11 * size_of::()` bytes. /// * `mgf1` must be valid for reads of `5 * size_of::()` bytes. /// * `n_len` must be in 64..=1024. -/// * `sig_len` must be `n_len`. +/// * `out_len` must be `n_len`. /// * `e_len` must be in 1..=`n_len`. -/// * `scratch_len` must be at least `8 * ⌈n_len / 8⌉ + 1072`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `p_len` and `q_len` must be in 1..`n_len`. +/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. /// * `digest_len` must be the digest length of the hash function `hash` names. -/// * `pre_len` must be `2 * ⌈n_len / 8⌉`. -/// * For the result to be signature verification's, `pre` must hold what `vg_rsa_public_precompute` wrote for `n` (returning 1). +/// * `scratch_len` must be at least `16 * n_len + 1056`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. /// * `hash` names the hash function of the digest and of the encoding: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the hash functions' `MdHash` tables of this crate, `fn_tables`, say which they name). /// * `mgf1` names MGF1's hash function: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the hash functions' tables of this crate, `fn_tables`, say which they name). -/// * `scratch` must not overlap `n`, `e`, `digest`, `sig`, `pre`, `hash`, `mgf1` or the arguments on the stack (distinct Rust objects never do). -/// * None of `n`, `e`, `digest`, `sig`, `scratch`, `pre`, `hash` and `mgf1` may overlap the 304 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `n`, `e`, `p`, `q`, `dp`, `dq`, `qinv`, `digest`, `salt`, `hash`, `mgf1` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `n`, `e`, `p`, `q`, `dp`, `dq`, `qinv`, `digest`, `salt`, `scratch`, `hash` and `mgf1` may overlap the 3776 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). /// * `hash` must be the `as_ptr()` of one of the `MdHash` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. /// * `mgf1` must be the `as_ptr()` of one of the `Hash` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_rsa_pss_verify_precomputed_auto(_dit: verified_garbage_cpu::Dit<'_>, n: *const u8, n_len: usize, e: *const u8, e_len: usize, digest: *const u8, digest_len: usize, sig: *const u8, sig_len: usize, salt_len: usize, any_salt_len: u32, scratch: *mut u64, scratch_len: usize, pre: *const u64, pre_len: usize, hash: *const [usize; 11], mgf1: *const [usize; 5]) -> u32 { +pub unsafe fn vg_rsa_pss_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, n: *const u8, n_len: usize, e: *const u8, e_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, digest: *const u8, digest_len: usize, salt: *const u8, salt_len: usize, scratch: *mut u64, scratch_len: usize, hash: *const [usize; 11], mgf1: *const [usize; 5]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa_pss::vg_rsa_pss_verify_precomputed(n, n_len, e, e_len, digest, digest_len, sig, sig_len, salt_len, any_salt_len, scratch, scratch_len, pre, pre_len, hash, mgf1) } + unsafe { crate::rsa_pss::vg_rsa_pss_sign(out, out_len, n, n_len, e, e_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, digest, digest_len, salt, salt_len, scratch, scratch_len, hash, mgf1) } } diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/sha1.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/sha1.rs index d774404837f..203cc9834e5 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/sha1.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/sha1.rs @@ -38,41 +38,41 @@ pub unsafe fn vg_sha1_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *m unsafe { crate::sha1::vg_sha1_update(state, count, data, len) } } -/// Computes the SHA-1 digest of the `len` bytes at `data` (20 bytes, `VG.Spec.Sha1.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-1 computation with that message. +/// Finishes a SHA-1 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), writes the SHA-1 digest of that message to `*out`. /// -/// Contract: `VG.Spec.Sha1.digestContract`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha1.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha1_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha1_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `data` must be valid for reads of `len` bytes. +/// * `state` must be valid for reads and writes of 84 bytes. /// * `out` must be valid for reads and writes of 20 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the 496 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha1_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 20]) { +pub unsafe fn vg_sha1_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 84], count: u64, out: *mut [u8; 20]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha1::vg_sha1_digest(data, len, out) } + unsafe { crate::sha1::vg_sha1_finalize(state, count, out) } } -/// Finishes a SHA-1 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), writes the SHA-1 digest of that message to `*out`. +/// Computes the SHA-1 digest of the `len` bytes at `data` (20 bytes, `VG.Spec.Sha1.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-1 computation with that message. /// -/// Contract: `VG.Spec.Sha1.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha1.digestContract`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha1_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha1_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 84 bytes. +/// * `data` must be valid for reads of `len` bytes. /// * `out` must be valid for reads and writes of 20 bytes. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 240 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the 496 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha1_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 84], count: u64, out: *mut [u8; 20]) { +pub unsafe fn vg_sha1_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 20]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha1::vg_sha1_finalize(state, count, out) } + unsafe { crate::sha1::vg_sha1_digest(data, len, out) } } diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/sha256.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/sha256.rs index 7392d4e7424..dd5c3ad8b0d 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/sha256.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/sha256.rs @@ -19,21 +19,24 @@ pub unsafe fn vg_sha224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *m unsafe { crate::sha256::vg_sha224_init(state) } } -/// Starts a SHA-256 computation: makes the streaming state `*state` represent the empty message. +/// Finishes a SHA-224 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), hashed from the initial hash value of SHA-224 (as `vg_sha224_init` starts it), writes the SHA-224 digest of that message (28 bytes, `VG.Spec.Sha256.sha224`) to `*out`. /// -/// Contract: `VG.Spec.Sha256.initContract`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha256.Repr`). +/// Contract: `VG.Spec.Sha256.finalize224Contract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha256_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 96 bytes. -/// * `state` must not wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 28 bytes. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 688 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96]) { +pub unsafe fn vg_sha224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, out: *mut [u8; 28]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha256::vg_sha256_init(state) } + unsafe { crate::sha256::vg_sha224_finalize(state, count, out) } } /// Computes the SHA-224 digest of the `len` bytes at `data` (28 bytes, `VG.Spec.Sha256.sha224`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-224 computation with that message. @@ -55,80 +58,77 @@ pub unsafe fn vg_sha224_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: * unsafe { crate::sha256::vg_sha224_digest(data, len, out) } } -/// Absorbs data into a SHA-224 or SHA-256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), it then represents that message followed by the `len` bytes at `data`. +/// Starts a SHA-256 computation: makes the streaming state `*state` represent the empty message. /// -/// Contract: `VG.Spec.Sha256.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha256.initContract`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha256.Repr`). /// -/// It runs `vg_sha256_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha256_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 96 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `state` must not overlap `data` (distinct Rust objects never do). -/// * Neither `state` nor `data` may overlap the 688 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must not wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, data: *const u8, len: usize) { +pub unsafe fn vg_sha256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha256::vg_sha256_update(state, count, data, len) } + unsafe { crate::sha256::vg_sha256_init(state) } } -/// Computes the SHA-256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha256.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-256 computation with that message. +/// Absorbs data into a SHA-224 or SHA-256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), it then represents that message followed by the `len` bytes at `data`. /// -/// Contract: `VG.Spec.Sha256.digestContract` for `VG.Spec.Sha256.hash`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha256.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_sha256_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha256_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// +/// * `state` must be valid for reads and writes of 96 bytes. /// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 32 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the 512 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must not overlap `data` (distinct Rust objects never do). +/// * Neither `state` nor `data` may overlap the 688 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { +pub unsafe fn vg_sha256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, data: *const u8, len: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha256::vg_sha256_digest(data, len, out) } + unsafe { crate::sha256::vg_sha256_update(state, count, data, len) } } -/// Finishes a SHA-224 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), hashed from the initial hash value of SHA-224 (as `vg_sha224_init` starts it), writes the SHA-224 digest of that message (28 bytes, `VG.Spec.Sha256.sha224`) to `*out`. +/// Finishes a SHA-224 or SHA-256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (32 bytes) to `*out`. The SHA-256 digest is all of it; the SHA-224 digest is its first 28 bytes. /// -/// Contract: `VG.Spec.Sha256.finalize224Contract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha256.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 96 bytes. -/// * `out` must be valid for reads and writes of 28 bytes. +/// * `out` must be valid for reads and writes of 32 bytes. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the 688 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, out: *mut [u8; 28]) { +pub unsafe fn vg_sha256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, out: *mut [u8; 32]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha256::vg_sha224_finalize(state, count, out) } + unsafe { crate::sha256::vg_sha256_finalize(state, count, out) } } -/// Finishes a SHA-224 or SHA-256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (32 bytes) to `*out`. The SHA-256 digest is all of it; the SHA-224 digest is its first 28 bytes. +/// Computes the SHA-256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha256.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-256 computation with that message. /// -/// Contract: `VG.Spec.Sha256.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha256.digestContract` for `VG.Spec.Sha256.hash`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha256_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 96 bytes. +/// * `data` must be valid for reads of `len` bytes. /// * `out` must be valid for reads and writes of 32 bytes. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 688 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the 512 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, out: *mut [u8; 32]) { +pub unsafe fn vg_sha256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha256::vg_sha256_finalize(state, count, out) } + unsafe { crate::sha256::vg_sha256_digest(data, len, out) } } diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/sha3.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/sha3.rs index c4435ef9357..49c702b9042 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/sha3.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/sha3.rs @@ -24,250 +24,250 @@ pub unsafe fn vg_sha3_224_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state unsafe { crate::sha3::vg_sha3_224_update(state, pos, data, len, scratch) } } -/// Absorbs data into a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, 136), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 136`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Finishes a SHA3-224 computation: if the state `*state` represents a message whose length is `pos` modulo 144 (`VG.Spec.Sha3.Repr` with SHA3-224's rate, as `vg_sha3_224_update` leaves it), writes the SHA3-224 digest of that message (28 bytes, `VG.Spec.Sha3.sha3_224`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 136. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_224`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs `vg_sha3_256_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `scratch` must be valid for reads and writes of 640 bytes. -/// * `pos` must be less than 136. -/// * The contents of `scratch` on return are unspecified. -/// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). -/// * None of `state`, `data` and `scratch` may overlap the 48 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 28 bytes. +/// * `pos` must be less than 144. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 752 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 28]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_256_update(state, pos, data, len, scratch) } + unsafe { crate::sha3::vg_sha3_224_finalize(state, pos, out) } } -/// Absorbs data into a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, 104), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 104`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Absorbs data into a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, 136), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 136`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 104. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 136. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_sha3_384_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_256_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. /// * `data` must be valid for reads of `len` bytes. /// * `scratch` must be valid for reads and writes of 640 bytes. -/// * `pos` must be less than 104. +/// * `pos` must be less than 136. /// * The contents of `scratch` on return are unspecified. /// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). /// * None of `state`, `data` and `scratch` may overlap the 48 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_384_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_384_update(state, pos, data, len, scratch) } + unsafe { crate::sha3::vg_sha3_256_update(state, pos, data, len, scratch) } } -/// Absorbs data into a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, 72), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 72`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Finishes a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, as `vg_sha3_256_update` leaves it), writes the SHA3-256 digest of that message (32 bytes, `VG.Spec.Sha3.sha3_256`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 72. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_256`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs `vg_sha3_512_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `scratch` must be valid for reads and writes of 640 bytes. -/// * `pos` must be less than 72. -/// * The contents of `scratch` on return are unspecified. -/// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). -/// * None of `state`, `data` and `scratch` may overlap the 48 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 32 bytes. +/// * `pos` must be less than 136. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 752 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 32]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_512_update(state, pos, data, len, scratch) } + unsafe { crate::sha3::vg_sha3_256_finalize(state, pos, out) } } -/// Absorbs data into a SHAKE128 computation: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, 168), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 168`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Absorbs data into a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, 104), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 104`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 168. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 104. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_shake128_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_384_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. /// * `data` must be valid for reads of `len` bytes. /// * `scratch` must be valid for reads and writes of 640 bytes. -/// * `pos` must be less than 168. +/// * `pos` must be less than 104. /// * The contents of `scratch` on return are unspecified. /// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). /// * None of `state`, `data` and `scratch` may overlap the 48 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake128_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_384_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake128_update(state, pos, data, len, scratch) } + unsafe { crate::sha3::vg_sha3_384_update(state, pos, data, len, scratch) } } -/// Squeezes SHAKE128 output: if the state `*state`, from position `pos`, continues the output of SHAKE128 of a message (`VG.Spec.Sha3.shake128`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake128_finalize` leaves and position 0. +/// Finishes a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, as `vg_sha3_384_update` leaves it), writes the SHA3-384 digest of that message (48 bytes, `VG.Spec.Sha3.sha3_384`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_384`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs `vg_shake128_squeeze`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_384_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of `outlen` bytes. -/// * `pos` must be at most 168. +/// * `out` must be valid for reads and writes of 48 bytes. +/// * `pos` must be less than 104. +/// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 720 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * Neither `state` nor `out` may overlap the 752 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake128_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { +pub unsafe fn vg_sha3_384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 48]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake128_squeeze(state, pos, out, outlen) } + unsafe { crate::sha3::vg_sha3_384_finalize(state, pos, out) } } -/// Squeezes SHAKE256 output: if the state `*state`, from position `pos`, continues the output of SHAKE256 of a message (`VG.Spec.Sha3.shake256`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake256_finalize` leaves and position 0. +/// Absorbs data into a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, 72), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 72`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 72. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_shake256_squeeze`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_512_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of `outlen` bytes. -/// * `pos` must be at most 136. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 720 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `scratch` must be valid for reads and writes of 640 bytes. +/// * `pos` must be less than 72. +/// * The contents of `scratch` on return are unspecified. +/// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). +/// * None of `state`, `data` and `scratch` may overlap the 48 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake256_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { +pub unsafe fn vg_sha3_512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake256_squeeze(state, pos, out, outlen) } + unsafe { crate::sha3::vg_sha3_512_update(state, pos, data, len, scratch) } } -/// Finishes a SHA3-224 computation: if the state `*state` represents a message whose length is `pos` modulo 144 (`VG.Spec.Sha3.Repr` with SHA3-224's rate, as `vg_sha3_224_update` leaves it), writes the SHA3-224 digest of that message (28 bytes, `VG.Spec.Sha3.sha3_224`) to `*out`. +/// Finishes a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, as `vg_sha3_512_update` leaves it), writes the SHA3-512 digest of that message (64 bytes, `VG.Spec.Sha3.sha3_512`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_224`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_512`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs `vg_sha3_224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_512_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of 28 bytes. -/// * `pos` must be less than 144. +/// * `out` must be valid for reads and writes of 64 bytes. +/// * `pos` must be less than 72. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the 752 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 28]) { +pub unsafe fn vg_sha3_512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 64]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_224_finalize(state, pos, out) } + unsafe { crate::sha3::vg_sha3_512_finalize(state, pos, out) } } -/// Finishes a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, as `vg_sha3_256_update` leaves it), writes the SHA3-256 digest of that message (32 bytes, `VG.Spec.Sha3.sha3_256`) to `*out`. +/// Absorbs data into a SHAKE128 computation: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, 168), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 168`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_256`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 168. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_sha3_256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake128_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of 32 bytes. -/// * `pos` must be less than 136. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 752 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `scratch` must be valid for reads and writes of 640 bytes. +/// * `pos` must be less than 168. +/// * The contents of `scratch` on return are unspecified. +/// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). +/// * None of `state`, `data` and `scratch` may overlap the 48 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 32]) { +pub unsafe fn vg_shake128_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_256_finalize(state, pos, out) } + unsafe { crate::sha3::vg_shake128_update(state, pos, data, len, scratch) } } -/// Finishes a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, as `vg_sha3_384_update` leaves it), writes the SHA3-384 digest of that message (48 bytes, `VG.Spec.Sha3.sha3_384`) to `*out`. +/// Finishes absorbing a SHAKE128 message: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, as `vg_shake128_update` leaves it), it becomes a state that, squeezed by `vg_shake128_squeeze` from position 0, gives the output of SHAKE128 of that message (`VG.Spec.Sha3.shake128`) from its first byte on (`VG.Spec.Sha3.XofRepr`). /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_384`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointer and `pos` may affect timing, not the state. /// -/// It runs `vg_sha3_384_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake128_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of 48 bytes. -/// * `pos` must be less than 104. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 752 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `pos` must be less than 168. +/// * `state` must not overlap the 720 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 48]) { +pub unsafe fn vg_shake128_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_384_finalize(state, pos, out) } + unsafe { crate::sha3::vg_shake128_finalize(state, pos) } } -/// Finishes a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, as `vg_sha3_512_update` leaves it), writes the SHA3-512 digest of that message (64 bytes, `VG.Spec.Sha3.sha3_512`) to `*out`. +/// Squeezes SHAKE128 output: if the state `*state`, from position `pos`, continues the output of SHAKE128 of a message (`VG.Spec.Sha3.shake128`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake128_finalize` leaves and position 0. /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_512`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. /// -/// It runs `vg_sha3_512_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake128_squeeze`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of 64 bytes. -/// * `pos` must be less than 72. -/// * The contents of `state` on return are unspecified. +/// * `out` must be valid for reads and writes of `outlen` bytes. +/// * `pos` must be at most 168. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 752 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * Neither `state` nor `out` may overlap the 720 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 64]) { +pub unsafe fn vg_shake128_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_512_finalize(state, pos, out) } + unsafe { crate::sha3::vg_shake128_squeeze(state, pos, out, outlen) } } -/// Finishes absorbing a SHAKE128 message: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, as `vg_shake128_update` leaves it), it becomes a state that, squeezed by `vg_shake128_squeeze` from position 0, gives the output of SHAKE128 of that message (`VG.Spec.Sha3.shake128`) from its first byte on (`VG.Spec.Sha3.XofRepr`). +/// Finishes absorbing a SHAKE256 message: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHAKE256's rate, as `vg_sha3_256_update` leaves it), it becomes a state that, squeezed by `vg_shake256_squeeze` from position 0, gives the output of SHAKE256 of that message (`VG.Spec.Sha3.shake256`) from its first byte on (`VG.Spec.Sha3.XofRepr`). /// -/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointer and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointer and `pos` may affect timing, not the state. /// -/// It runs `vg_shake128_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `pos` must be less than 168. +/// * `pos` must be less than 136. /// * `state` must not overlap the 720 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake128_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { +pub unsafe fn vg_shake256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake128_finalize(state, pos) } + unsafe { crate::sha3::vg_shake256_finalize(state, pos) } } -/// Finishes absorbing a SHAKE256 message: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHAKE256's rate, as `vg_sha3_256_update` leaves it), it becomes a state that, squeezed by `vg_shake256_squeeze` from position 0, gives the output of SHAKE256 of that message (`VG.Spec.Sha3.shake256`) from its first byte on (`VG.Spec.Sha3.XofRepr`). +/// Squeezes SHAKE256 output: if the state `*state`, from position `pos`, continues the output of SHAKE256 of a message (`VG.Spec.Sha3.shake256`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake256_finalize` leaves and position 0. /// -/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointer and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. /// -/// It runs `vg_shake256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake256_squeeze`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `pos` must be less than 136. -/// * `state` must not overlap the 720 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of `outlen` bytes. +/// * `pos` must be at most 136. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 720 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { +pub unsafe fn vg_shake256_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake256_finalize(state, pos) } + unsafe { crate::sha3::vg_shake256_squeeze(state, pos, out, outlen) } } diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/sha512.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/sha512.rs index f6bcd566a19..fd9ba247dda 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/sha512.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/sha512.rs @@ -36,215 +36,215 @@ pub unsafe fn vg_sha512_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *m unsafe { crate::sha512::vg_sha512_init(state) } } -/// Absorbs data into a SHA-384, SHA-512, SHA-512/224 or SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), it then represents that message followed by the `len` bytes at `data`. +/// Starts a SHA-512/224 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/224 (`VG.Spec.Sha512.H0_512_224`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. /// -/// Contract: `VG.Spec.Sha512.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_224`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). /// -/// It runs `vg_sha512_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_224_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `state` must not overlap `data` (distinct Rust objects never do). -/// * Neither `state` nor `data` may overlap the 1408 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must not wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, data: *const u8, len: usize) { +pub unsafe fn vg_sha512_224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_update(state, count, data, len) } + unsafe { crate::sha512::vg_sha512_224_init(state) } } -/// Computes the SHA-384 digest of the `len` bytes at `data` (48 bytes, `VG.Spec.Sha512.sha384`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-384 computation with that message. +/// Starts a SHA-512/256 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/256 (`VG.Spec.Sha512.H0_512_256`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. /// -/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_256`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). /// -/// It runs `vg_sha384_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_256_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 48 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the 704 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must be valid for reads and writes of 192 bytes. +/// * `state` must not wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha384_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 48]) { +pub unsafe fn vg_sha512_256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha384_digest(data, len, out) } + unsafe { crate::sha512::vg_sha512_256_init(state) } } -/// Computes the SHA-512 digest of the `len` bytes at `data` (64 bytes, `VG.Spec.Sha512.sha512`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512 computation with that message. +/// Absorbs data into a SHA-384, SHA-512, SHA-512/224 or SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), it then represents that message followed by the `len` bytes at `data`. /// -/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha512.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_sha512_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// +/// * `state` must be valid for reads and writes of 192 bytes. /// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 64 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the 704 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must not overlap `data` (distinct Rust objects never do). +/// * Neither `state` nor `data` may overlap the 1408 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 64]) { +pub unsafe fn vg_sha512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, data: *const u8, len: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_digest(data, len, out) } + unsafe { crate::sha512::vg_sha512_update(state, count, data, len) } } -/// Starts a SHA-512/224 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/224 (`VG.Spec.Sha512.H0_512_224`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. +/// Finishes a SHA-384, SHA-512, SHA-512/224 or SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (64 bytes) to `*out`. The SHA-512 digest is all of it; the SHA-384, SHA-512/224 and SHA-512/256 digests are its first 48, 28 and 32 bytes. /// -/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_224`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). +/// Contract: `VG.Spec.Sha512.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha512_224_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `state` must not wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 64 bytes. +/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 1408 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { +pub unsafe fn vg_sha512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 64]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_224_init(state) } + unsafe { crate::sha512::vg_sha512_finalize(state, count, out) } } -/// Starts a SHA-512/256 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/256 (`VG.Spec.Sha512.H0_512_256`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. +/// Finishes a SHA-384 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-384 (as `vg_sha384_init` starts it), writes the SHA-384 digest of that message (48 bytes, `VG.Spec.Sha512.sha384`) to `*out`. /// -/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_256`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). +/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_384` and `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha512_256_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha384_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `state` must not wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 48 bytes. +/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 1408 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { +pub unsafe fn vg_sha384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 48]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_256_init(state) } + unsafe { crate::sha512::vg_sha384_finalize(state, count, out) } } -/// Finishes a SHA-384, SHA-512, SHA-512/224 or SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (64 bytes) to `*out`. The SHA-512 digest is all of it; the SHA-384, SHA-512/224 and SHA-512/256 digests are its first 48, 28 and 32 bytes. +/// Finishes a SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/256 (as `vg_sha512_256_init` starts it), writes the SHA-512/256 digest of that message (32 bytes, `VG.Spec.Sha512.sha512_256`) to `*out`. /// -/// Contract: `VG.Spec.Sha512.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_256` and `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha512_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 64 bytes. +/// * `out` must be valid for reads and writes of 32 bytes. /// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the 1408 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 64]) { +pub unsafe fn vg_sha512_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 32]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_finalize(state, count, out) } + unsafe { crate::sha512::vg_sha512_256_finalize(state, count, out) } } -/// Finishes a SHA-384 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-384 (as `vg_sha384_init` starts it), writes the SHA-384 digest of that message (48 bytes, `VG.Spec.Sha512.sha384`) to `*out`. +/// Finishes a SHA-512/224 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/224 (as `vg_sha512_224_init` starts it), writes the SHA-512/224 digest of that message (28 bytes, `VG.Spec.Sha512.sha512_224`) to `*out`. /// -/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_384` and `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_224` and `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha384_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 48 bytes. +/// * `out` must be valid for reads and writes of 28 bytes. /// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the 1408 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 48]) { +pub unsafe fn vg_sha512_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 28]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha384_finalize(state, count, out) } + unsafe { crate::sha512::vg_sha512_224_finalize(state, count, out) } } -/// Computes the SHA-512/224 digest of the `len` bytes at `data` (28 bytes, `VG.Spec.Sha512.sha512_224`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512/224 computation with that message. +/// Computes the SHA-384 digest of the `len` bytes at `data` (48 bytes, `VG.Spec.Sha512.sha384`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-384 computation with that message. /// -/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha512_224_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha384_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 28 bytes. +/// * `out` must be valid for reads and writes of 48 bytes. /// * `out` must not overlap `data` (distinct Rust objects never do). /// * Neither `data` nor `out` may overlap the 704 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_224_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 28]) { +pub unsafe fn vg_sha384_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 48]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_224_digest(data, len, out) } + unsafe { crate::sha512::vg_sha384_digest(data, len, out) } } -/// Computes the SHA-512/256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha512.sha512_256`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512/256 computation with that message. +/// Computes the SHA-512 digest of the `len` bytes at `data` (64 bytes, `VG.Spec.Sha512.sha512`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512 computation with that message. /// -/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha512_256_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 32 bytes. +/// * `out` must be valid for reads and writes of 64 bytes. /// * `out` must not overlap `data` (distinct Rust objects never do). /// * Neither `data` nor `out` may overlap the 704 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { +pub unsafe fn vg_sha512_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 64]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_256_digest(data, len, out) } + unsafe { crate::sha512::vg_sha512_digest(data, len, out) } } -/// Finishes a SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/256 (as `vg_sha512_256_init` starts it), writes the SHA-512/256 digest of that message (32 bytes, `VG.Spec.Sha512.sha512_256`) to `*out`. +/// Computes the SHA-512/224 digest of the `len` bytes at `data` (28 bytes, `VG.Spec.Sha512.sha512_224`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512/224 computation with that message. /// -/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_256` and `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha512_256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_224_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 32 bytes. -/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 1408 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `out` must be valid for reads and writes of 28 bytes. +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the 704 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 32]) { +pub unsafe fn vg_sha512_224_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 28]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_256_finalize(state, count, out) } + unsafe { crate::sha512::vg_sha512_224_digest(data, len, out) } } -/// Finishes a SHA-512/224 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/224 (as `vg_sha512_224_init` starts it), writes the SHA-512/224 digest of that message (28 bytes, `VG.Spec.Sha512.sha512_224`) to `*out`. +/// Computes the SHA-512/256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha512.sha512_256`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512/256 computation with that message. /// -/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_224` and `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha512_224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_256_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 28 bytes. -/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 1408 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `out` must be valid for reads and writes of 32 bytes. +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the 704 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 28]) { +pub unsafe fn vg_sha512_256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_224_finalize(state, count, out) } + unsafe { crate::sha512::vg_sha512_256_digest(data, len, out) } } diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/sm4_gcm.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/sm4_gcm.rs index 1938a8f581d..68f635d1d53 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/sm4_gcm.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/sm4_gcm.rs @@ -69,26 +69,6 @@ pub unsafe fn vg_sm4_gcm_open_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *co unsafe { crate::sm4_gcm::vg_sm4_gcm_open(ctx, nonce, nonce_len, aad, aad_len, data, len, tag, tag_len) } } -/// Absorbs additional data into an incremental SM4-GCM encryption or decryption: with the key context `*ctx`, if the streaming state `*state` represents a message with `aad_len` bytes (modulo 2⁶⁴) of additional data and no text yet, it then represents that message with the `len` bytes at `data` appended to its additional data. All of the additional data comes before the text (§7.1 step 5): after text, the state represents no message. -/// -/// Contract: `VG.Spec.Gcm.streamAadContract` (AES-GCM's, which reads only the hash subkey of the key context). Constant time: only the pointers, `aad_len` and `len` may affect timing, not the key context, the state or the data. -/// -/// It runs `vg_sm4_gcm_stream_aad`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `ctx` must be valid for reads of 256 bytes. -/// * `state` must be valid for reads and writes of 80 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `state` must not overlap `ctx` or `data` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the 2592 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_sm4_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_aad(ctx, state, aad_len, data, len) } -} - /// Starts an incremental SM4-GCM encryption or decryption (NIST SP 800-38D §7): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, makes the streaming state `*state` represent the message with the IV the `nonce_len` bytes at `nonce`, and no additional data or text yet. Continue with `vg_sm4_gcm_stream_aad`, then `vg_sm4_gcm_stream_encrypt` or `vg_sm4_gcm_stream_decrypt`, then `vg_sm4_gcm_stream_finish` or `vg_sm4_gcm_stream_verify`, with the same key context. /// /// The function checks no length: GCM requires a nonce of 1 to `2^61 - 1` bytes (§5.2.1.1), which the caller must check. @@ -111,46 +91,24 @@ pub unsafe fn vg_sm4_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, c unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_init(ctx, nonce, nonce_len, state) } } -/// Finishes an incremental SM4-GCM encryption or decryption (NIST SP 800-38D §7.1 steps 5–6, §7.2 steps 6–7): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, writes its 128-bit tag to `*tag`. A shorter tag is the first bytes of this one; to check a received tag, use `vg_sm4_gcm_stream_verify`. -/// -/// Contract: `VG.Spec.Sm4.Gcm.streamFinishContract`. Constant time: only the pointers, `aad_len` and `text_len` may affect timing, not the key context or the state. -/// -/// It runs `vg_sm4_gcm_stream_finish`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `ctx` must be valid for reads of 256 bytes. -/// * `state` must be valid for reads and writes of 80 bytes. -/// * `tag` must be valid for reads and writes of 16 bytes. -/// * The contents of `state` on return are unspecified. -/// * `state` and `tag` must not overlap each other or `ctx` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `tag` may overlap the 5888 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_sm4_gcm_stream_finish_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *mut [u8; 16]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_finish(ctx, state, aad_len, text_len, tag) } -} - -/// Finishes an incremental SM4-GCM decryption and checks its tag (NIST SP 800-38D §7.2 steps 6–8): with the key context `*ctx` that `vg_sm4_gcm_init` wrote and the received tag the `tag_len` bytes at `tag`, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, returns 1 if `tag_len` is between 4 and 16 and the received tag is the first `tag_len` bytes of the message's 128-bit tag, and 0 otherwise. The tags are compared without a branch. +/// Absorbs additional data into an incremental SM4-GCM encryption or decryption: with the key context `*ctx`, if the streaming state `*state` represents a message with `aad_len` bytes (modulo 2⁶⁴) of additional data and no text yet, it then represents that message with the `len` bytes at `data` appended to its additional data. All of the additional data comes before the text (§7.1 step 5): after text, the state represents no message. /// -/// Contract: `VG.Spec.Sm4.Gcm.streamVerifyContract`. Constant time: only the pointers, `aad_len`, `text_len` and `tag_len` may affect timing, not the key context, the state or the tags. +/// Contract: `VG.Spec.Gcm.streamAadContract` (AES-GCM's, which reads only the hash subkey of the key context). Constant time: only the pointers, `aad_len` and `len` may affect timing, not the key context, the state or the data. /// -/// It runs `vg_sm4_gcm_stream_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sm4_gcm_stream_aad`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `tag` must be valid for reads of `tag_len` bytes. -/// * The contents of `state` on return are unspecified. -/// * `state` must not overlap `ctx` or `tag` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `tag` may overlap the 5888 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `state` must not overlap `ctx` or `data` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the 2592 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sm4_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *const u8, tag_len: usize) -> u32 { +pub unsafe fn vg_sm4_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_verify(ctx, state, aad_len, text_len, tag, tag_len) } + unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_aad(ctx, state, aad_len, data, len) } } /// Encrypts the next piece of the text of an incremental SM4-GCM encryption (NIST SP 800-38D §7.1): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and the ciphertext of exactly `text_len` bytes of plaintext so far, encrypts the `len` bytes at `data` in place, as the continuation of that plaintext, and the state then represents the message with them appended. @@ -196,3 +154,45 @@ pub unsafe fn vg_sm4_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_> // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_decrypt(ctx, state, aad_len, text_len, data, len) } } + +/// Finishes an incremental SM4-GCM encryption or decryption (NIST SP 800-38D §7.1 steps 5–6, §7.2 steps 6–7): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, writes its 128-bit tag to `*tag`. A shorter tag is the first bytes of this one; to check a received tag, use `vg_sm4_gcm_stream_verify`. +/// +/// Contract: `VG.Spec.Sm4.Gcm.streamFinishContract`. Constant time: only the pointers, `aad_len` and `text_len` may affect timing, not the key context or the state. +/// +/// It runs `vg_sm4_gcm_stream_finish`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `ctx` must be valid for reads of 256 bytes. +/// * `state` must be valid for reads and writes of 80 bytes. +/// * `tag` must be valid for reads and writes of 16 bytes. +/// * The contents of `state` on return are unspecified. +/// * `state` and `tag` must not overlap each other or `ctx` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `tag` may overlap the 5888 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_sm4_gcm_stream_finish_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *mut [u8; 16]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_finish(ctx, state, aad_len, text_len, tag) } +} + +/// Finishes an incremental SM4-GCM decryption and checks its tag (NIST SP 800-38D §7.2 steps 6–8): with the key context `*ctx` that `vg_sm4_gcm_init` wrote and the received tag the `tag_len` bytes at `tag`, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, returns 1 if `tag_len` is between 4 and 16 and the received tag is the first `tag_len` bytes of the message's 128-bit tag, and 0 otherwise. The tags are compared without a branch. +/// +/// Contract: `VG.Spec.Sm4.Gcm.streamVerifyContract`. Constant time: only the pointers, `aad_len`, `text_len` and `tag_len` may affect timing, not the key context, the state or the tags. +/// +/// It runs `vg_sm4_gcm_stream_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `ctx` must be valid for reads of 256 bytes. +/// * `state` must be valid for reads and writes of 80 bytes. +/// * `tag` must be valid for reads of `tag_len` bytes. +/// * The contents of `state` on return are unspecified. +/// * `state` must not overlap `ctx` or `tag` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `tag` may overlap the 5888 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_sm4_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *const u8, tag_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_verify(ctx, state, aad_len, text_len, tag, tag_len) } +} diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/x25519.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/x25519.rs index e2c080707dc..dbc48ad6b44 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/x25519.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/x25519.rs @@ -2,45 +2,45 @@ //! The dispatchers of the verified `x25519` functions for `powerpc64le` //! (`crate::x25519`, `VG.Rust.dispatcher`). -/// Computes an X25519 public key (RFC 7748 §6.1): writes `X25519(k, 9)`, the X25519 function of the 32-byte scalar `k` at `scalar` and the base point's u-coordinate 9, to `*out`. The scalar is decoded (clamped). +/// Computes an X25519 shared secret (RFC 7748 §6.1): writes `X25519(k, u)` to `*out`, for the 32-byte scalar `k` at `scalar` and the 32-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped) and the most significant bit of `u` masked as RFC 7748 specifies (§5). /// -/// Contract: `VG.Spec.X25519.x25519BaseContract`. Constant time: only the pointers may affect timing, not the scalar. +/// Contract: `VG.Spec.X25519.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. /// -/// It runs `vg_x25519_base`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_x25519_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `out` must be valid for reads and writes of 32 bytes. /// * `scalar` must be valid for reads of 32 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * `point` must be valid for reads of 32 bytes. +/// * `scratch` must be valid for reads and writes of 4096 bytes. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other or `scalar` (distinct Rust objects never do). -/// * None of `out`, `scalar` and `scratch` may wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `scalar` or `point` (distinct Rust objects never do). +/// * None of `out`, `scalar`, `point` and `scratch` may overlap the 48 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x25519_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], scratch: *mut [u64; 1024]) { +pub unsafe fn vg_x25519_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], point: *const [u8; 32], scratch: *mut [u64; 512]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::x25519::vg_x25519_base(out, scalar, scratch) } + unsafe { crate::x25519::vg_x25519_exchange(out, scalar, point, scratch) } } -/// Computes an X25519 shared secret (RFC 7748 §6.1): writes `X25519(k, u)` to `*out`, for the 32-byte scalar `k` at `scalar` and the 32-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped) and the most significant bit of `u` masked as RFC 7748 specifies (§5). +/// Computes an X25519 public key (RFC 7748 §6.1): writes `X25519(k, 9)`, the X25519 function of the 32-byte scalar `k` at `scalar` and the base point's u-coordinate 9, to `*out`. The scalar is decoded (clamped). /// -/// Contract: `VG.Spec.X25519.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. +/// Contract: `VG.Spec.X25519.x25519BaseContract`. Constant time: only the pointers may affect timing, not the scalar. /// -/// It runs `vg_x25519_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_x25519_base`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `out` must be valid for reads and writes of 32 bytes. /// * `scalar` must be valid for reads of 32 bytes. -/// * `point` must be valid for reads of 32 bytes. -/// * `scratch` must be valid for reads and writes of 4096 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `scalar` or `point` (distinct Rust objects never do). -/// * None of `out`, `scalar`, `point` and `scratch` may overlap the 48 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other or `scalar` (distinct Rust objects never do). +/// * None of `out`, `scalar` and `scratch` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x25519_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], point: *const [u8; 32], scratch: *mut [u64; 512]) -> u32 { +pub unsafe fn vg_x25519_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], scratch: *mut [u64; 1024]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::x25519::vg_x25519_exchange(out, scalar, point, scratch) } + unsafe { crate::x25519::vg_x25519_base(out, scalar, scratch) } } diff --git a/crates/verified-garbage-asm-powerpc64le/src/dispatch/x448.rs b/crates/verified-garbage-asm-powerpc64le/src/dispatch/x448.rs index 030a5a51da7..26eb80ce904 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/dispatch/x448.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/dispatch/x448.rs @@ -2,45 +2,45 @@ //! The dispatchers of the verified `x448` functions for `powerpc64le` //! (`crate::x448`, `VG.Rust.dispatcher`). -/// Computes an X448 public key (RFC 7748 §6.2): writes `X448(k, 5)`, the X448 function of the 56-byte scalar `k` at `scalar` and the base point's u-coordinate 5, to `*out`. The scalar is decoded (clamped). +/// Computes an X448 shared secret (RFC 7748 §6.2): writes `X448(k, u)` to `*out`, for the 56-byte scalar `k` at `scalar` and the 56-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped); all 448 bits of `u` are used and noncanonical coordinates are reduced modulo the field prime (§5). /// -/// Contract: `VG.Spec.X448.x448BaseContract`. Constant time: only the pointers may affect timing, not the scalar. +/// Contract: `VG.Spec.X448.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. /// -/// It runs `vg_x448_base`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_x448_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `out` must be valid for reads and writes of 56 bytes. /// * `scalar` must be valid for reads of 56 bytes. +/// * `point` must be valid for reads of 56 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other or `scalar` (distinct Rust objects never do). -/// * None of `out`, `scalar` and `scratch` may wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `scalar` or `point` (distinct Rust objects never do). +/// * None of `out`, `scalar`, `point` and `scratch` may overlap the 48 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x448_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], scratch: *mut [u64; 1024]) { +pub unsafe fn vg_x448_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], point: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::x448::vg_x448_base(out, scalar, scratch) } + unsafe { crate::x448::vg_x448_exchange(out, scalar, point, scratch) } } -/// Computes an X448 shared secret (RFC 7748 §6.2): writes `X448(k, u)` to `*out`, for the 56-byte scalar `k` at `scalar` and the 56-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped); all 448 bits of `u` are used and noncanonical coordinates are reduced modulo the field prime (§5). +/// Computes an X448 public key (RFC 7748 §6.2): writes `X448(k, 5)`, the X448 function of the 56-byte scalar `k` at `scalar` and the base point's u-coordinate 5, to `*out`. The scalar is decoded (clamped). /// -/// Contract: `VG.Spec.X448.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. +/// Contract: `VG.Spec.X448.x448BaseContract`. Constant time: only the pointers may affect timing, not the scalar. /// -/// It runs `vg_x448_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_x448_base`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `out` must be valid for reads and writes of 56 bytes. /// * `scalar` must be valid for reads of 56 bytes. -/// * `point` must be valid for reads of 56 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `scalar` or `point` (distinct Rust objects never do). -/// * None of `out`, `scalar`, `point` and `scratch` may overlap the 48 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other or `scalar` (distinct Rust objects never do). +/// * None of `out`, `scalar` and `scratch` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x448_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], point: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_x448_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], scratch: *mut [u64; 1024]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::x448::vg_x448_exchange(out, scalar, point, scratch) } + unsafe { crate::x448::vg_x448_base(out, scalar, scratch) } } diff --git a/crates/verified-garbage-asm-powerpc64le/src/fn_tables/mod.rs b/crates/verified-garbage-asm-powerpc64le/src/fn_tables/mod.rs index cd9436c8e57..0b5495b83e1 100644 --- a/crates/verified-garbage-asm-powerpc64le/src/fn_tables/mod.rs +++ b/crates/verified-garbage-asm-powerpc64le/src/fn_tables/mod.rs @@ -2,36 +2,36 @@ //! The tables of functions of the verified functions for `powerpc64le` (`Artifact.tables`). #![allow(dead_code, unused_imports)] -/// A table of functions of the interface `Hash` (`VG.FnTable`): the addresses of its 3 functions, then its 2 words of data. Only the statics of this module are of this type. +/// A table of functions of the interface `Hmac` (`VG.FnTable`): the addresses of its 3 functions, then its 3 words of data. Only the statics of this module are of this type. #[repr(C)] -pub struct Hash { +pub struct Hmac { fns: [*const (); 3], - data: [usize; 2], + data: [usize; 3], } -// SAFETY: a `Hash` is immutable, and the functions it holds the addresses of may be called from any thread. -unsafe impl Sync for Hash {} +// SAFETY: a `Hmac` is immutable, and the functions it holds the addresses of may be called from any thread. +unsafe impl Sync for Hmac {} -impl Hash { +impl Hmac { /// A pointer to the table's words, as the verified functions that take it read them. - pub const fn as_ptr(&'static self) -> *const [usize; 5] { + pub const fn as_ptr(&'static self) -> *const [usize; 6] { core::ptr::from_ref(self).cast() } } -/// A table of functions of the interface `Hmac` (`VG.FnTable`): the addresses of its 3 functions, then its 3 words of data. Only the statics of this module are of this type. +/// A table of functions of the interface `Hash` (`VG.FnTable`): the addresses of its 3 functions, then its 2 words of data. Only the statics of this module are of this type. #[repr(C)] -pub struct Hmac { +pub struct Hash { fns: [*const (); 3], - data: [usize; 3], + data: [usize; 2], } -// SAFETY: a `Hmac` is immutable, and the functions it holds the addresses of may be called from any thread. -unsafe impl Sync for Hmac {} +// SAFETY: a `Hash` is immutable, and the functions it holds the addresses of may be called from any thread. +unsafe impl Sync for Hash {} -impl Hmac { +impl Hash { /// A pointer to the table's words, as the verified functions that take it read them. - pub const fn as_ptr(&'static self) -> *const [usize; 6] { + pub const fn as_ptr(&'static self) -> *const [usize; 5] { core::ptr::from_ref(self).cast() } } @@ -53,38 +53,6 @@ impl MdHash { } } -#[rustfmt::skip] -mod vg_hash_md5; -pub use vg_hash_md5::VG_HASH_MD5; - -#[rustfmt::skip] -mod vg_hash_sha1; -pub use vg_hash_sha1::VG_HASH_SHA1; - -#[rustfmt::skip] -mod vg_hash_sha224; -pub use vg_hash_sha224::VG_HASH_SHA224; - -#[rustfmt::skip] -mod vg_hash_sha256; -pub use vg_hash_sha256::VG_HASH_SHA256; - -#[rustfmt::skip] -mod vg_hash_sha384; -pub use vg_hash_sha384::VG_HASH_SHA384; - -#[rustfmt::skip] -mod vg_hash_sha512; -pub use vg_hash_sha512::VG_HASH_SHA512; - -#[rustfmt::skip] -mod vg_hash_sha512_224; -pub use vg_hash_sha512_224::VG_HASH_SHA512_224; - -#[rustfmt::skip] -mod vg_hash_sha512_256; -pub use vg_hash_sha512_256::VG_HASH_SHA512_256; - #[rustfmt::skip] mod vg_hmac_md5; pub use vg_hmac_md5::VG_HMAC_MD5; @@ -117,6 +85,38 @@ pub use vg_hmac_sha512_224::VG_HMAC_SHA512_224; mod vg_hmac_sha512_256; pub use vg_hmac_sha512_256::VG_HMAC_SHA512_256; +#[rustfmt::skip] +mod vg_hash_md5; +pub use vg_hash_md5::VG_HASH_MD5; + +#[rustfmt::skip] +mod vg_hash_sha1; +pub use vg_hash_sha1::VG_HASH_SHA1; + +#[rustfmt::skip] +mod vg_hash_sha224; +pub use vg_hash_sha224::VG_HASH_SHA224; + +#[rustfmt::skip] +mod vg_hash_sha256; +pub use vg_hash_sha256::VG_HASH_SHA256; + +#[rustfmt::skip] +mod vg_hash_sha384; +pub use vg_hash_sha384::VG_HASH_SHA384; + +#[rustfmt::skip] +mod vg_hash_sha512; +pub use vg_hash_sha512::VG_HASH_SHA512; + +#[rustfmt::skip] +mod vg_hash_sha512_224; +pub use vg_hash_sha512_224::VG_HASH_SHA512_224; + +#[rustfmt::skip] +mod vg_hash_sha512_256; +pub use vg_hash_sha512_256::VG_HASH_SHA512_256; + #[rustfmt::skip] mod vg_md_hash_md5; pub use vg_md_hash_md5::VG_MD_HASH_MD5; diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/chacha20.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/chacha20.rs index 138531ffc56..648eee1fb74 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/chacha20.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/chacha20.rs @@ -22,40 +22,40 @@ pub unsafe fn vg_chacha20_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: unsafe { crate::chacha20::vg_chacha20_init(state, key, nonce) } } -/// Applies a ChaCha20 keystream (RFC 8439 §2.4), encrypting or decrypting: if at least `len` bytes are left of the keystream that the streaming state `*state` represents, XORs the next `len` of them into the `len` bytes at `data`, keeps the rest in `*state`, and returns 1. Otherwise (the block counter would pass 2³² − 1) returns 0, and leaves the bytes at `data` and the keystream unchanged. +/// Restarts a ChaCha20 keystream with the same key: makes the streaming state `*state` represent the whole keystream of its key for the 16-byte nonce `*nonce` (as `vg_chacha20_init` does), discarding what was left of the previous one. /// -/// Contract: `VG.Spec.ChaCha20.applyContract`. Constant time: only the pointers, `len` and the number of bytes of keystream left in `*state` (`VG.Spec.ChaCha20.leftAt`: 64 × (2³² − `c`) for the initial block counter `c`, less the bytes applied since) may affect timing, not the key, the nonce, the keystream or the data. The function may leak that number. +/// Contract: `VG.Spec.ChaCha20.setNonceContract`. Constant time: only the pointers may affect timing, not the key or the nonce. /// -/// It runs `vg_chacha20_apply`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_chacha20_set_nonce`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 768 bytes, and aligned for `u64`. -/// * `data` must be valid for reads and writes of `len` bytes. -/// * `state` and `data` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `data` may wrap around the end of the address space (no Rust object does). +/// * `nonce` must be valid for reads of 16 bytes. +/// * `state` must not overlap `nonce` (distinct Rust objects never do). +/// * Neither `state` nor `nonce` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_chacha20_apply_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 96], data: *mut u8, len: usize) -> u32 { +pub unsafe fn vg_chacha20_set_nonce_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 96], nonce: *const [u8; 16]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::chacha20::vg_chacha20_apply(state, data, len) } + unsafe { crate::chacha20::vg_chacha20_set_nonce(state, nonce) } } -/// Restarts a ChaCha20 keystream with the same key: makes the streaming state `*state` represent the whole keystream of its key for the 16-byte nonce `*nonce` (as `vg_chacha20_init` does), discarding what was left of the previous one. +/// Applies a ChaCha20 keystream (RFC 8439 §2.4), encrypting or decrypting: if at least `len` bytes are left of the keystream that the streaming state `*state` represents, XORs the next `len` of them into the `len` bytes at `data`, keeps the rest in `*state`, and returns 1. Otherwise (the block counter would pass 2³² − 1) returns 0, and leaves the bytes at `data` and the keystream unchanged. /// -/// Contract: `VG.Spec.ChaCha20.setNonceContract`. Constant time: only the pointers may affect timing, not the key or the nonce. +/// Contract: `VG.Spec.ChaCha20.applyContract`. Constant time: only the pointers, `len` and the number of bytes of keystream left in `*state` (`VG.Spec.ChaCha20.leftAt`: 64 × (2³² − `c`) for the initial block counter `c`, less the bytes applied since) may affect timing, not the key, the nonce, the keystream or the data. The function may leak that number. /// -/// It runs `vg_chacha20_set_nonce`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_chacha20_apply`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 768 bytes, and aligned for `u64`. -/// * `nonce` must be valid for reads of 16 bytes. -/// * `state` must not overlap `nonce` (distinct Rust objects never do). -/// * Neither `state` nor `nonce` may wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads and writes of `len` bytes. +/// * `state` and `data` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `data` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_chacha20_set_nonce_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 96], nonce: *const [u8; 16]) { +pub unsafe fn vg_chacha20_apply_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 96], data: *mut u8, len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::chacha20::vg_chacha20_set_nonce(state, nonce) } + unsafe { crate::chacha20::vg_chacha20_apply(state, data, len) } } diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/cmac_triple_des.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/cmac_triple_des.rs index 9910f0eccaa..31d23ae26e4 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/cmac_triple_des.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/cmac_triple_des.rs @@ -2,6 +2,26 @@ //! The dispatchers of the verified `cmac_triple_des` functions for `riscv64` //! (`crate::cmac_triple_des`, `VG.Rust.dispatcher`). +/// Starts a TDEA-CMAC computation (NIST SP 800-38B): makes the streaming state `*state` represent the empty message under the 16- or 24-byte TDEA key at `key` (FIPS 46-3; a 16-byte key is `K1 ‖ K2` with `K3 = K1`, parity bits are ignored). The state holds the key schedule (the three DES schedules), the subkeys `K1 ‖ K2` (§6.1), the chaining value and the message's last bytes, held back (`VG.Spec.Cmac.tdesRepr`). Continue with `vg_cmac_triple_des_absorb` and `vg_cmac_triple_des_finish`. +/// +/// Contract: `VG.Spec.Cmac.tdesStreamInitContract`. Constant time: only the pointers and `key_len` may affect timing, not the key. +/// +/// It runs `vg_cmac_triple_des_stream_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 416 bytes, and aligned for `u64`. +/// * `key` must be valid for reads of `key_len` bytes. +/// * `key_len` must be 16 or 24. +/// * `state` must not overlap `key` (distinct Rust objects never do). +/// * Neither `state` nor `key` may overlap the 1584 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_cmac_triple_des_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 52], key: *const u8, key_len: usize) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::cmac_triple_des::vg_cmac_triple_des_stream_init(state, key, key_len) } +} + /// Absorbs data into a TDEA-CMAC computation (NIST SP 800-38B §6.2): if the streaming state `*state` represents a message of `count` bytes under a TDEA key (as `vg_cmac_triple_des_stream_init` set it up), it then represents that message followed by the `len` bytes at `data`, under the same key, provided that the two together are shorter than 2⁶⁴ bytes. It chains (step 6) every block of the message but its last bytes `Mₙ*` (step 3), which it holds back in the state, since only `vg_cmac_triple_des_finish` knows they are the last. /// /// Contract: `VG.Spec.Cmac.tdesAbsorbContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. @@ -40,23 +60,3 @@ pub unsafe fn vg_cmac_triple_des_finish_auto(_dit: verified_garbage_cpu::Dit<'_> // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::cmac_triple_des::vg_cmac_triple_des_finish(state, count, out) } } - -/// Starts a TDEA-CMAC computation (NIST SP 800-38B): makes the streaming state `*state` represent the empty message under the 16- or 24-byte TDEA key at `key` (FIPS 46-3; a 16-byte key is `K1 ‖ K2` with `K3 = K1`, parity bits are ignored). The state holds the key schedule (the three DES schedules), the subkeys `K1 ‖ K2` (§6.1), the chaining value and the message's last bytes, held back (`VG.Spec.Cmac.tdesRepr`). Continue with `vg_cmac_triple_des_absorb` and `vg_cmac_triple_des_finish`. -/// -/// Contract: `VG.Spec.Cmac.tdesStreamInitContract`. Constant time: only the pointers and `key_len` may affect timing, not the key. -/// -/// It runs `vg_cmac_triple_des_stream_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 416 bytes, and aligned for `u64`. -/// * `key` must be valid for reads of `key_len` bytes. -/// * `key_len` must be 16 or 24. -/// * `state` must not overlap `key` (distinct Rust objects never do). -/// * Neither `state` nor `key` may overlap the 1584 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_cmac_triple_des_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 52], key: *const u8, key_len: usize) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::cmac_triple_des::vg_cmac_triple_des_stream_init(state, key, key_len) } -} diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/dsa.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/dsa.rs index 1f2c1a038cf..d834e6ec912 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/dsa.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/dsa.rs @@ -24,6 +24,51 @@ pub unsafe fn vg_dsa_derive_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: * unsafe { crate::dsa::vg_dsa_derive_key(out, out_len, params, params_len, p_len, x, q_len, scratch, scratch_len) } } +/// Validate packed DSA p||q||g||y||x as signing checks it: domain consistency, including g^q mod p = 1, 1 < y < p, 0 < x < q and g^x mod p = y. Returns 1 exactly when validation succeeds, otherwise 0. Does not test primality. Timing may depend on p,q,g,y and the result, not otherwise on x. Scratch contents are unspecified on return and may hold secrets. Contract: `VG.Spec.Dsa.validatePrivateKeyContract`. +/// +/// It runs `vg_dsa_validate_private_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `key` must be valid for reads of `key_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes, and aligned for `u64`. +/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). +/// * key_len must be 3*p_len + 2*q_len; scratch_len must be at least 16*p_len words; destroy scratch after use. +/// * `scratch` must not overlap `key` (distinct Rust objects never do). +/// * Neither `key` nor `scratch` may wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_dsa_validate_private_key_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *const u8, key_len: usize, p_len: usize, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::dsa::vg_dsa_validate_private_key(key, key_len, p_len, q_len, scratch, scratch_len) } +} + +/// Randomized DSA signing of a prehashed digest, for an already validated packed private key (big-endian p||q||g||y||x; vg_dsa_validate_private_key validates it). Up to eight signing attempts, each with 128 nonce draws. Returns 1 with fixed-width r||s, 0 for retry exhaustion, or 2 for insufficient random bytes; out is zero on failure and used counts consumed complete draws on every return. Timing may depend on public p,q,g,y, sampling decisions, signing success bits and consumption, not otherwise on secrets. Contract: `VG.Spec.Dsa.randomSignPrevalidatedContract`. +/// +/// It runs `vg_dsa_sign_random_prevalidated`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `out` must be valid for reads and writes of `out_len` bytes. +/// * `used` must be valid for reads and writes of 8 bytes, and aligned for `u64`. +/// * `key` must be valid for reads of `key_len` bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `rand` must be valid for reads of `rand_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes, and aligned for `u64`. +/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). +/// * key_len must be 3*p_len + 2*q_len; out_len must be 2*q_len. +/// * scratch_len must be at least 16*p_len + 64 words; destroy scratch after use. +/// * The exact key bytes must satisfy validatePrivateKeyBytes p_len q_len key = true. A successful vg_dsa_validate_private_key call establishes this only while the key bytes remain unchanged. Primality remains the caller's responsibility. +/// * Provide fresh independent random octets; never reuse a completed signing tape for another signature. +/// * `out`, `used` and `scratch` must not overlap each other, `key`, `digest`, `rand` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `used`, `key`, `digest`, `rand` and `scratch` may overlap the 192 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_dsa_sign_random_prevalidated_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], key: *const u8, key_len: usize, p_len: usize, q_len: usize, digest: *const u8, digest_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::dsa::vg_dsa_sign_random_prevalidated(out, out_len, used, key, key_len, p_len, q_len, digest, digest_len, rand, rand_len, scratch, scratch_len) } +} + /// Generate a DSA private key from p||q||g and fresh independent random bytes. Uses at most 128 rejection-sampling draws, computes y=g^x mod p, and checks algebraic consistency, not primality. Returns 1 with p||q||g||y||x, 0 for invalid input/draw limit, or 2 for insufficient randomness. Failure zeros out; used counts consumed complete draws on every return. Timing may depend on public parameters, sampling decisions, consumption and success, not otherwise on x or randomness. Contract: `VG.Spec.Dsa.generateKeyContract`. /// /// It runs `vg_dsa_generate_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. @@ -89,48 +134,3 @@ pub unsafe fn vg_dsa_verify_prevalidated_auto(_dit: verified_garbage_cpu::Dit<'_ // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::dsa::vg_dsa_verify_prevalidated(key, key_len, p_len, q_len, signature, signature_len, digest, digest_len, scratch, scratch_len) } } - -/// Validate packed DSA p||q||g||y||x as signing checks it: domain consistency, including g^q mod p = 1, 1 < y < p, 0 < x < q and g^x mod p = y. Returns 1 exactly when validation succeeds, otherwise 0. Does not test primality. Timing may depend on p,q,g,y and the result, not otherwise on x. Scratch contents are unspecified on return and may hold secrets. Contract: `VG.Spec.Dsa.validatePrivateKeyContract`. -/// -/// It runs `vg_dsa_validate_private_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `key` must be valid for reads of `key_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes, and aligned for `u64`. -/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). -/// * key_len must be 3*p_len + 2*q_len; scratch_len must be at least 16*p_len words; destroy scratch after use. -/// * `scratch` must not overlap `key` (distinct Rust objects never do). -/// * Neither `key` nor `scratch` may wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_dsa_validate_private_key_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *const u8, key_len: usize, p_len: usize, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::dsa::vg_dsa_validate_private_key(key, key_len, p_len, q_len, scratch, scratch_len) } -} - -/// Randomized DSA signing of a prehashed digest, for an already validated packed private key (big-endian p||q||g||y||x; vg_dsa_validate_private_key validates it). Up to eight signing attempts, each with 128 nonce draws. Returns 1 with fixed-width r||s, 0 for retry exhaustion, or 2 for insufficient random bytes; out is zero on failure and used counts consumed complete draws on every return. Timing may depend on public p,q,g,y, sampling decisions, signing success bits and consumption, not otherwise on secrets. Contract: `VG.Spec.Dsa.randomSignPrevalidatedContract`. -/// -/// It runs `vg_dsa_sign_random_prevalidated`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `out` must be valid for reads and writes of `out_len` bytes. -/// * `used` must be valid for reads and writes of 8 bytes, and aligned for `u64`. -/// * `key` must be valid for reads of `key_len` bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `rand` must be valid for reads of `rand_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes, and aligned for `u64`. -/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). -/// * key_len must be 3*p_len + 2*q_len; out_len must be 2*q_len. -/// * scratch_len must be at least 16*p_len + 64 words; destroy scratch after use. -/// * The exact key bytes must satisfy validatePrivateKeyBytes p_len q_len key = true. A successful vg_dsa_validate_private_key call establishes this only while the key bytes remain unchanged. Primality remains the caller's responsibility. -/// * Provide fresh independent random octets; never reuse a completed signing tape for another signature. -/// * `out`, `used` and `scratch` must not overlap each other, `key`, `digest`, `rand` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `used`, `key`, `digest`, `rand` and `scratch` may overlap the 192 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_dsa_sign_random_prevalidated_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], key: *const u8, key_len: usize, p_len: usize, q_len: usize, digest: *const u8, digest_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::dsa::vg_dsa_sign_random_prevalidated(out, out_len, used, key, key_len, p_len, q_len, digest, digest_len, rand, rand_len, scratch, scratch_len) } -} diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_brainpoolp256r1.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_brainpoolp256r1.rs index fc0cf4292c4..7c0ddb01e31 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_brainpoolp256r1.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_brainpoolp256r1.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_brainpoolp256r1` functions for `riscv64` //! (`crate::ecdsa_brainpoolp256r1`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over brainpoolP256r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_brainpoolp256r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 65 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 64 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_brainpoolp256r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over brainpoolP256r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_brainpoolp256r1_sign_auto(_dit: verified_garbage_cpu::Dit // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over brainpoolP256r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_brainpoolp256r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 65 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 64 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_brainpoolp256r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_brainpoolp384r1.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_brainpoolp384r1.rs index 650319a08d3..da896341004 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_brainpoolp384r1.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_brainpoolp384r1.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_brainpoolp384r1` functions for `riscv64` //! (`crate::ecdsa_brainpoolp384r1`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over brainpoolP384r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_brainpoolp384r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 97 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 96 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_brainpoolp384r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over brainpoolP384r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (48 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (48 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_brainpoolp384r1_sign_auto(_dit: verified_garbage_cpu::Dit // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over brainpoolP384r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_brainpoolp384r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 97 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 96 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_brainpoolp384r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_brainpoolp512r1.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_brainpoolp512r1.rs index d326aea32b0..8764be53811 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_brainpoolp512r1.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_brainpoolp512r1.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_brainpoolp512r1` functions for `riscv64` //! (`crate::ecdsa_brainpoolp512r1`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over brainpoolP512r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 64 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 512 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 64 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_brainpoolp512r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 129 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 128 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_brainpoolp512r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 129], digest: *const u8, digest_len: usize, sig: *const [u8; 128], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over brainpoolP512r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (64 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (64 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_brainpoolp512r1_sign_auto(_dit: verified_garbage_cpu::Dit // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over brainpoolP512r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 64 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 512 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 64 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_brainpoolp512r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 129 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 128 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_brainpoolp512r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 129], digest: *const u8, digest_len: usize, sig: *const [u8; 128], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_p192.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_p192.rs index d5028bf962a..fa1834820be 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_p192.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_p192.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_p192` functions for `riscv64` //! (`crate::ecdsa_p192`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over P-192 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 24 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 192 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 24 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_p192_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 49 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 48 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_p192_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 49], digest: *const u8, digest_len: usize, sig: *const [u8; 48], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_p192::vg_ecdsa_p192_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over P-192 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (24 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (24 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_p192_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_p192::vg_ecdsa_p192_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over P-192 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 24 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 192 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 24 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_p192_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 49 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 48 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_p192_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 49], digest: *const u8, digest_len: usize, sig: *const [u8; 48], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p192::vg_ecdsa_p192_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_p224.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_p224.rs index e87119c1e5f..918b0fbd851 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_p224.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_p224.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_p224` functions for `riscv64` //! (`crate::ecdsa_p224`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over P-224 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 28 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 224 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 28 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_p224_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 57 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 56 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_p224_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 57], digest: *const u8, digest_len: usize, sig: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_p224::vg_ecdsa_p224_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over P-224 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (28 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (28 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_p224_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_p224::vg_ecdsa_p224_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over P-224 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 28 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 224 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 28 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_p224_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 57 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 56 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_p224_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 57], digest: *const u8, digest_len: usize, sig: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p224::vg_ecdsa_p224_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_p256.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_p256.rs index 76df2711efe..f921d218093 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_p256.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_p256.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_p256` functions for `riscv64` //! (`crate::ecdsa_p256`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over P-256 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_p256_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 65 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 64 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_p256_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_p256::vg_ecdsa_p256_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over P-256 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_p256_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_p256::vg_ecdsa_p256_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over P-256 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_p256_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 65 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 64 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_p256_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p256::vg_ecdsa_p256_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_p384.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_p384.rs index a5680a1d791..1218bb628aa 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_p384.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_p384.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_p384` functions for `riscv64` //! (`crate::ecdsa_p384`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over P-384 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_p384_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 97 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 96 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_p384_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_p384::vg_ecdsa_p384_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over P-384 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (48 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (48 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_p384_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_p384::vg_ecdsa_p384_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over P-384 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_p384_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 97 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 96 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_p384_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p384::vg_ecdsa_p384_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_p521.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_p521.rs index 706c8ec02de..ddd3480e5ae 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_p521.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_p521.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_p521` functions for `riscv64` //! (`crate::ecdsa_p521`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over P-521 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 66 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 521 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 66 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_p521_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 133 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 132 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_p521_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 133], digest: *const u8, digest_len: usize, sig: *const [u8; 132], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_p521::vg_ecdsa_p521_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over P-521 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (66 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (66 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_p521_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_p521::vg_ecdsa_p521_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over P-521 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 66 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 521 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 66 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_p521_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 133 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 132 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_p521_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 133], digest: *const u8, digest_len: usize, sig: *const [u8; 132], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p521::vg_ecdsa_p521_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_secp256k1.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_secp256k1.rs index c6a280519ad..c14393e2840 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_secp256k1.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/ecdsa_secp256k1.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_secp256k1` functions for `riscv64` //! (`crate::ecdsa_secp256k1`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over secp256k1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_secp256k1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 65 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 64 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_secp256k1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over secp256k1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_secp256k1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over secp256k1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_secp256k1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 65 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 64 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the 80 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_secp256k1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/ed25519.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/ed25519.rs index 9a94b69e7af..090356d2c1a 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/ed25519.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/ed25519.rs @@ -2,26 +2,6 @@ //! The dispatchers of the verified `ed25519` functions for `riscv64` //! (`crate::ed25519`, `VG.Rust.dispatcher`). -/// Ed25519 verification (RFC 8032 §5.1.7): returns 1 if the 64-byte signature at `signature` verifies for the 32-byte public key at `pk` and the `len` bytes at `message`, and 0 otherwise. Uses pure Ed25519, with no context or prehash. Checks canonical point encodings, S < L, and `[S]B = R + [k]A` with the challenge k = SHA-512(R || A || M) reduced modulo L, as in RFC 8032 §6. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed25519.verifyContract`. Not constant time: timing may depend on the public key, message and signature. -/// -/// It runs `vg_ed25519_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 32 bytes. -/// * `message` must be valid for reads of `len` bytes. -/// * `signature` must be valid for reads of 64 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `scratch` must not overlap `pk`, `message` or `signature` (distinct Rust objects never do). -/// * None of `pk`, `message`, `signature` and `scratch` may overlap the 480 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ed25519_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 32], message: *const u8, len: usize, signature: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ed25519::vg_ed25519_verify(pk, message, len, signature, scratch) } -} - /// Ed25519 key-pair derivation (RFC 8032 §5.1.5): for the 32-byte private seed at `seed`, writes its 64-byte SHA-512 digest (§5.1.5, step 1) to `*expanded` and its 32-byte public key to `*out`, including pruning: the digest and public key that `vg_ed25519_sign_expanded` takes. Contract: `VG.Spec.Ed25519.keyPairContract`. Constant time: only pointers may affect timing. /// /// It runs `vg_ed25519_key_pair`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. @@ -66,3 +46,23 @@ pub unsafe fn vg_ed25519_sign_expanded_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ed25519::vg_ed25519_sign_expanded(out, expanded, pk, message, len, scratch) } } + +/// Ed25519 verification (RFC 8032 §5.1.7): returns 1 if the 64-byte signature at `signature` verifies for the 32-byte public key at `pk` and the `len` bytes at `message`, and 0 otherwise. Uses pure Ed25519, with no context or prehash. Checks canonical point encodings, S < L, and `[S]B = R + [k]A` with the challenge k = SHA-512(R || A || M) reduced modulo L, as in RFC 8032 §6. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed25519.verifyContract`. Not constant time: timing may depend on the public key, message and signature. +/// +/// It runs `vg_ed25519_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 32 bytes. +/// * `message` must be valid for reads of `len` bytes. +/// * `signature` must be valid for reads of 64 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `scratch` must not overlap `pk`, `message` or `signature` (distinct Rust objects never do). +/// * None of `pk`, `message`, `signature` and `scratch` may overlap the 480 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ed25519_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 32], message: *const u8, len: usize, signature: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ed25519::vg_ed25519_verify(pk, message, len, signature, scratch) } +} diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/ed448.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/ed448.rs index 700d802feb3..563cac52370 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/ed448.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/ed448.rs @@ -2,27 +2,6 @@ //! The dispatchers of the verified `ed448` functions for `riscv64` //! (`crate::ed448`, `VG.Rust.dispatcher`). -/// Ed448 verification (RFC 8032 §5.2.7): returns 1 if the 114-byte signature at `signature` verifies for the 57-byte public key at `pk`, the `ctxlen` bytes of context at `context` and the `len` bytes at `message`, and 0 otherwise (always 0 for a context longer than 255 bytes). Uses Ed448 with a context, not Ed448ph (no prehash). Checks canonical point encodings, S < L, and the cofactored equation `[4][S]B = [4]R + [4][k]A` with the challenge k = SHAKE256(dom4(0, C) || R || A || M, 114) reduced modulo L. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed448.verifyContract`. Not constant time: timing may depend on the public key, context, message and signature. -/// -/// It runs `vg_ed448_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 57 bytes. -/// * `context` must be valid for reads of `ctxlen` bytes. -/// * `message` must be valid for reads of `len` bytes. -/// * `signature` must be valid for reads of 114 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `scratch` must not overlap `pk`, `context`, `message` or `signature` (distinct Rust objects never do). -/// * None of `pk`, `context`, `message`, `signature` and `scratch` may overlap the 1616 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ed448_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, signature: *const [u8; 114], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ed448::vg_ed448_verify(pk, context, ctxlen, message, len, signature, scratch) } -} - /// Ed448 public-key derivation (RFC 8032 §5.2.5): writes the 57-byte public key to `*out`, from the 57-byte private key at `seed`, including SHAKE256 and pruning. Contract: `VG.Spec.Ed448.publicKeyContract`. Constant time: only pointers may affect timing. /// /// It runs `vg_ed448_public_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. @@ -67,3 +46,24 @@ pub unsafe fn vg_ed448_sign_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, out // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ed448::vg_ed448_sign_cached(out, seed, pk, context, ctxlen, message, len, scratch) } } + +/// Ed448 verification (RFC 8032 §5.2.7): returns 1 if the 114-byte signature at `signature` verifies for the 57-byte public key at `pk`, the `ctxlen` bytes of context at `context` and the `len` bytes at `message`, and 0 otherwise (always 0 for a context longer than 255 bytes). Uses Ed448 with a context, not Ed448ph (no prehash). Checks canonical point encodings, S < L, and the cofactored equation `[4][S]B = [4]R + [4][k]A` with the challenge k = SHAKE256(dom4(0, C) || R || A || M, 114) reduced modulo L. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed448.verifyContract`. Not constant time: timing may depend on the public key, context, message and signature. +/// +/// It runs `vg_ed448_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 57 bytes. +/// * `context` must be valid for reads of `ctxlen` bytes. +/// * `message` must be valid for reads of `len` bytes. +/// * `signature` must be valid for reads of 114 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `scratch` must not overlap `pk`, `context`, `message` or `signature` (distinct Rust objects never do). +/// * None of `pk`, `context`, `message`, `signature` and `scratch` may overlap the 1616 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ed448_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, signature: *const [u8; 114], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ed448::vg_ed448_verify(pk, context, ctxlen, message, len, signature, scratch) } +} diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/fn_tables.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/fn_tables.rs index 0fd55841598..c9ef4a592dd 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/fn_tables.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/fn_tables.rs @@ -2,30 +2,66 @@ //! The dispatchers of the tables of functions for `riscv64` //! (`crate::fn_tables`, `VG.Rust.tableDispatcher`). -/// The table of functions `VG_HASH_MD5` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hash_md5_auto() -> &'static crate::fn_tables::Hash { - &crate::fn_tables::VG_HASH_MD5 -} - /// The table of functions `VG_HMAC_MD5` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] pub fn vg_hmac_md5_auto() -> &'static crate::fn_tables::Hmac { &crate::fn_tables::VG_HMAC_MD5 } -/// The table of functions `VG_HASH_SHA1` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hash_sha1_auto() -> &'static crate::fn_tables::Hash { - &crate::fn_tables::VG_HASH_SHA1 -} - /// The table of functions `VG_HMAC_SHA1` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] pub fn vg_hmac_sha1_auto() -> &'static crate::fn_tables::Hmac { &crate::fn_tables::VG_HMAC_SHA1 } +/// The table of functions `VG_HMAC_SHA224` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha224_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA224 +} + +/// The table of functions `VG_HMAC_SHA256` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha256_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA256 +} + +/// The table of functions `VG_HMAC_SHA384` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha384_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA384 +} + +/// The table of functions `VG_HMAC_SHA512` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha512_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA512 +} + +/// The table of functions `VG_HMAC_SHA512_224` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha512_224_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA512_224 +} + +/// The table of functions `VG_HMAC_SHA512_256` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha512_256_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA512_256 +} + +/// The table of functions `VG_HASH_MD5` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hash_md5_auto() -> &'static crate::fn_tables::Hash { + &crate::fn_tables::VG_HASH_MD5 +} + +/// The table of functions `VG_HASH_SHA1` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hash_sha1_auto() -> &'static crate::fn_tables::Hash { + &crate::fn_tables::VG_HASH_SHA1 +} + /// The table of functions `VG_HASH_SHA224` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] pub fn vg_hash_sha224_auto() -> &'static crate::fn_tables::Hash { @@ -50,28 +86,16 @@ pub fn vg_hash_sha512_auto() -> &'static crate::fn_tables::Hash { &crate::fn_tables::VG_HASH_SHA512 } -/// The table of functions `VG_HMAC_SHA224` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hmac_sha224_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA224 -} - -/// The table of functions `VG_HMAC_SHA256` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hmac_sha256_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA256 -} - -/// The table of functions `VG_HMAC_SHA384` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +/// The table of functions `VG_HASH_SHA512_224` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] -pub fn vg_hmac_sha384_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA384 +pub fn vg_hash_sha512_224_auto() -> &'static crate::fn_tables::Hash { + &crate::fn_tables::VG_HASH_SHA512_224 } -/// The table of functions `VG_HMAC_SHA512` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +/// The table of functions `VG_HASH_SHA512_256` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] -pub fn vg_hmac_sha512_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA512 +pub fn vg_hash_sha512_256_auto() -> &'static crate::fn_tables::Hash { + &crate::fn_tables::VG_HASH_SHA512_256 } /// The table of functions `VG_MD_HASH_MD5` of the interface `MdHash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). @@ -110,30 +134,6 @@ pub fn vg_md_hash_sha512_auto() -> &'static crate::fn_tables::MdHash { &crate::fn_tables::VG_MD_HASH_SHA512 } -/// The table of functions `VG_HASH_SHA512_224` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hash_sha512_224_auto() -> &'static crate::fn_tables::Hash { - &crate::fn_tables::VG_HASH_SHA512_224 -} - -/// The table of functions `VG_HASH_SHA512_256` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hash_sha512_256_auto() -> &'static crate::fn_tables::Hash { - &crate::fn_tables::VG_HASH_SHA512_256 -} - -/// The table of functions `VG_HMAC_SHA512_224` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hmac_sha512_224_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA512_224 -} - -/// The table of functions `VG_HMAC_SHA512_256` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hmac_sha512_256_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA512_256 -} - /// The table of functions `VG_MD_HASH_SHA512_224` of the interface `MdHash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] pub fn vg_md_hash_sha512_224_auto() -> &'static crate::fn_tables::MdHash { diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/gcm.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/gcm.rs index 582090e1d31..3d3a8aeb2ca 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/gcm.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/gcm.rs @@ -22,81 +22,26 @@ pub unsafe fn vg_aes_gcm_init_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *co unsafe { crate::gcm::vg_aes_gcm_init(key, key_len, ctx) } } -/// AES-GCM authenticated encryption (NIST SP 800-38D §7.1, GCM-AE, with a 128-bit tag): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, encrypts the `len` bytes at `data` in place, under the IV the `nonce_len` bytes at `nonce`, and writes the tag of the ciphertext and the `aad_len` bytes of additional data at `aad` to `*tag`. A shorter tag is the first bytes of this one. -/// -/// The function checks no length. GCM is secure only for a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of data and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must ensure; and a nonce must never be used twice with the same key. -/// -/// Contract: `VG.Spec.Gcm.sealContract`. Constant time: only the pointers, `rounds` and the lengths may affect timing, not the key context, the nonce, the additional data or the data. -/// -/// It runs `vg_aes_gcm_seal`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `ctx` must be valid for reads of 256 bytes, and aligned for `u64`. -/// * `nonce` must be valid for reads of `nonce_len` bytes. -/// * `aad` must be valid for reads of `aad_len` bytes. -/// * `data` must be valid for reads and writes of `len` bytes. -/// * `tag` must be valid for reads and writes of 16 bytes. -/// * `rounds` must be 10, 12 or 14. -/// * `data` and `tag` must not overlap each other, `ctx`, `nonce`, `aad` or the arguments on the stack (distinct Rust objects never do). -/// * None of `ctx`, `nonce`, `aad`, `data` and `tag` may overlap the 4624 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_aes_gcm_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *mut [u8; 16]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::gcm::vg_aes_gcm_seal(ctx, rounds, nonce, nonce_len, aad, aad_len, data, len, tag) } -} - -/// AES-GCM authenticated decryption (NIST SP 800-38D §7.2, GCM-AD): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds and the received tag the `tag_len` bytes at `tag`, returns 1 if `tag_len` is between 4 and 16 and the tag is the first `tag_len` bytes of that of the `len` bytes of ciphertext at `data` and the `aad_len` bytes of additional data at `aad`, under the IV the `nonce_len` bytes at `nonce`, having then decrypted the ciphertext in place; otherwise returns 0, and the bytes at `data` are unchanged. The tags are compared without a branch. -/// -/// The function checks no other length: GCM requires a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of ciphertext and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. -/// -/// Contract: `VG.Spec.Gcm.openContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the additional data, the data or the tag. -/// -/// It runs `vg_aes_gcm_open`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `ctx` must be valid for reads of 256 bytes, and aligned for `u64`. -/// * `nonce` must be valid for reads of `nonce_len` bytes. -/// * `aad` must be valid for reads of `aad_len` bytes. -/// * `data` must be valid for reads and writes of `len` bytes. -/// * `tag` must be valid for reads of `tag_len` bytes. -/// * `rounds` must be 10, 12 or 14. -/// * `data` must not overlap `ctx`, `nonce`, `aad`, `tag` or the arguments on the stack (distinct Rust objects never do). -/// * None of `ctx`, `nonce`, `aad`, `data` and `tag` may overlap the 4640 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_aes_gcm_open_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *const u8, tag_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::gcm::vg_aes_gcm_open(ctx, rounds, nonce, nonce_len, aad, aad_len, data, len, tag, tag_len) } -} - -/// AES-GCM authenticated decryption (NIST SP 800-38D §7.2, GCM-AD), out of place: with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds and the received tag the `tag_len` bytes at `tag`, returns 1 if `tag_len` is between 4 and 16 and the tag is the first `tag_len` bytes of that of the `len` bytes of ciphertext at `src` and the `aad_len` bytes of additional data at `aad`, under the IV the `nonce_len` bytes at `nonce`, having then written the plaintext to the `dst_len = len` bytes at `dst`; otherwise returns 0, having written zeros to them: `vg_aes_gcm_open` with the ciphertext read from `src` and the plaintext written to `dst`. `src` is not written, and `dst` is a buffer of its own: it overlaps no other buffer, `src` included. The tags are compared without a branch. +/// Starts an incremental AES-GCM encryption or decryption (NIST SP 800-38D §7): with the key context `*ctx` that `vg_aes_gcm_init` wrote, makes the streaming state `*state` represent the message with the IV the `nonce_len` bytes at `nonce`, and no additional data or text yet. Continue with `vg_aes_gcm_stream_aad`, then `vg_aes_gcm_stream_encrypt` or `vg_aes_gcm_stream_decrypt`, then `vg_aes_gcm_stream_finish` or `vg_aes_gcm_stream_verify`, with the same key context. /// -/// The function checks no other length: GCM requires a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of ciphertext and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// The function checks no length: GCM requires a nonce of 1 to `2^61 - 1` bytes (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.openToContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the additional data, the data or the tag. +/// Contract: `VG.Spec.Gcm.streamInitContract`. The streaming state is `J₀`, the GHASH accumulator, a partial block, the next counter block and a keystream block (`VG.Spec.Gcm.StreamRepr`). Constant time: only the pointers and `nonce_len` may affect timing, not the key context or the nonce. /// -/// It runs `vg_aes_gcm_open_to`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_aes_gcm_stream_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes, and aligned for `u64`. /// * `nonce` must be valid for reads of `nonce_len` bytes. -/// * `aad` must be valid for reads of `aad_len` bytes. -/// * `src` must be valid for reads of `len` bytes. -/// * `dst` must be valid for reads and writes of `dst_len` bytes. -/// * `tag` must be valid for reads of `tag_len` bytes. -/// * `rounds` must be 10, 12 or 14. -/// * `dst_len` must equal `len`. -/// * `dst` must not overlap `ctx`, `nonce`, `aad`, `src`, `tag` or the arguments on the stack (distinct Rust objects never do). -/// * None of `ctx`, `nonce`, `aad`, `src`, `dst` and `tag` may overlap the 4656 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must be valid for reads and writes of 80 bytes, and aligned for `u64`. +/// * `state` must not overlap `ctx` or `nonce` (distinct Rust objects never do). +/// * None of `ctx`, `nonce` and `state` may overlap the 4608 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_open_to_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const u8, len: usize, dst: *mut u8, dst_len: usize, tag: *const u8, tag_len: usize) -> u32 { +pub unsafe fn vg_aes_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::gcm::vg_aes_gcm_open_to(ctx, rounds, nonce, nonce_len, aad, aad_len, src, len, dst, dst_len, tag, tag_len) } + unsafe { crate::gcm::vg_aes_gcm_stream_init(ctx, nonce, nonce_len, state) } } /// Absorbs additional data into an incremental AES-GCM encryption or decryption: with the key context `*ctx`, if the streaming state `*state` represents a message with `aad_len` bytes (modulo 2⁶⁴) of additional data and no text yet, it then represents that message with the `len` bytes at `data` appended to its additional data. All of the additional data comes before the text (§7.1 step 5): after text, the state represents no message. @@ -119,53 +64,50 @@ pub unsafe fn vg_aes_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ct unsafe { crate::gcm::vg_aes_gcm_stream_aad(ctx, state, aad_len, data, len) } } -/// Starts an incremental AES-GCM encryption or decryption (NIST SP 800-38D §7): with the key context `*ctx` that `vg_aes_gcm_init` wrote, makes the streaming state `*state` represent the message with the IV the `nonce_len` bytes at `nonce`, and no additional data or text yet. Continue with `vg_aes_gcm_stream_aad`, then `vg_aes_gcm_stream_encrypt` or `vg_aes_gcm_stream_decrypt`, then `vg_aes_gcm_stream_finish` or `vg_aes_gcm_stream_verify`, with the same key context. +/// Encrypts the next piece of the text of an incremental AES-GCM encryption (NIST SP 800-38D §7.1): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and the ciphertext of exactly `text_len` bytes of plaintext so far, encrypts the `len` bytes at `data` in place, as the continuation of that plaintext, and the state then represents the message with them appended. /// -/// The function checks no length: GCM requires a nonce of 1 to `2^61 - 1` bytes (§5.2.1.1), which the caller must check. +/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.streamInitContract`. The streaming state is `J₀`, the GHASH accumulator, a partial block, the next counter block and a keystream block (`VG.Spec.Gcm.StreamRepr`). Constant time: only the pointers and `nonce_len` may affect timing, not the key context or the nonce. +/// Contract: `VG.Spec.Gcm.streamEncryptContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. /// -/// It runs `vg_aes_gcm_stream_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_aes_gcm_stream_encrypt`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes, and aligned for `u64`. -/// * `nonce` must be valid for reads of `nonce_len` bytes. /// * `state` must be valid for reads and writes of 80 bytes, and aligned for `u64`. -/// * `state` must not overlap `ctx` or `nonce` (distinct Rust objects never do). -/// * None of `ctx`, `nonce` and `state` may overlap the 4608 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads and writes of `len` bytes. +/// * `rounds` must be 10, 12 or 14. +/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the 4608 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { +pub unsafe fn vg_aes_gcm_stream_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::gcm::vg_aes_gcm_stream_init(ctx, nonce, nonce_len, state) } + unsafe { crate::gcm::vg_aes_gcm_stream_encrypt(ctx, rounds, state, aad_len, text_len, data, len) } } -/// AES-GCM authenticated encryption (NIST SP 800-38D §7.1, GCM-AE, with a 128-bit tag), out of place, of a plaintext in pieces: with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, encrypts the concatenation of the `src_count` slices that `src` lists (each an address and a length, in bytes), under the IV the `nonce_len` bytes at `nonce`, writes the ciphertext to the `len` bytes at `dst`, and writes the tag of the ciphertext and the `aad_len` bytes of additional data at `aad` to `*tag`: `vg_aes_gcm_seal` with the plaintext gathered from `src`. A shorter tag is the first bytes of this one. +/// Decrypts the next piece of the text of an incremental AES-GCM decryption (NIST SP 800-38D §7.2): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext so far, decrypts the `len` bytes of ciphertext at `data` in place, as the continuation of that ciphertext, and the state then represents the message with them appended. The plaintext is not authenticated until `vg_aes_gcm_stream_verify` has returned 1. /// -/// The function checks no length. GCM is secure only for a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of data and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must ensure; and a nonce must never be used twice with the same key. +/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.sealGatherContract`. Constant time: only the pointers, `rounds`, the lengths, `src_count` and where the slices are (their addresses and lengths) may affect timing, not the key context, the nonce, the additional data or the data. +/// Contract: `VG.Spec.Gcm.streamDecryptContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. /// -/// It runs `vg_aes_gcm_seal_gather`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_aes_gcm_stream_decrypt`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes, and aligned for `u64`. -/// * `nonce` must be valid for reads of `nonce_len` bytes. -/// * `aad` must be valid for reads of `aad_len` bytes. -/// * `src` must be valid for reads of `2 * size_of::() * src_count` bytes, and each slice it lists for reads of its length in bytes. -/// * `dst` must be valid for reads and writes of `len` bytes. -/// * `tag` must be valid for reads and writes of 16 bytes. +/// * `state` must be valid for reads and writes of 80 bytes, and aligned for `u64`. +/// * `data` must be valid for reads and writes of `len` bytes. /// * `rounds` must be 10, 12 or 14. -/// * The slices `src` lists must be `len` bytes long in all. -/// * `dst` and `tag` must not overlap each other, `ctx`, `nonce`, `aad`, `src`, the slices `src` lists or the arguments on the stack (distinct Rust objects never do). -/// * None of `ctx`, `nonce`, `aad`, `src`, the slices `src` lists, `dst` and `tag` may overlap the 4640 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the 4608 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_seal_gather_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const [usize; 2], src_count: usize, dst: *mut u8, len: usize, tag: *mut [u8; 16]) { +pub unsafe fn vg_aes_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::gcm::vg_aes_gcm_seal_gather(ctx, rounds, nonce, nonce_len, aad, aad_len, src, src_count, dst, len, tag) } + unsafe { crate::gcm::vg_aes_gcm_stream_decrypt(ctx, rounds, state, aad_len, text_len, data, len) } } /// Finishes an incremental AES-GCM encryption or decryption (NIST SP 800-38D §7.1 steps 5–6, §7.2 steps 6–7): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, writes its 128-bit tag to `*tag`. A shorter tag is the first bytes of this one; to check a received tag, use `vg_aes_gcm_stream_verify`. @@ -212,48 +154,106 @@ pub unsafe fn vg_aes_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, unsafe { crate::gcm::vg_aes_gcm_stream_verify(ctx, rounds, state, aad_len, text_len, tag, tag_len) } } -/// Encrypts the next piece of the text of an incremental AES-GCM encryption (NIST SP 800-38D §7.1): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and the ciphertext of exactly `text_len` bytes of plaintext so far, encrypts the `len` bytes at `data` in place, as the continuation of that plaintext, and the state then represents the message with them appended. +/// AES-GCM authenticated encryption (NIST SP 800-38D §7.1, GCM-AE, with a 128-bit tag): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, encrypts the `len` bytes at `data` in place, under the IV the `nonce_len` bytes at `nonce`, and writes the tag of the ciphertext and the `aad_len` bytes of additional data at `aad` to `*tag`. A shorter tag is the first bytes of this one. /// -/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// The function checks no length. GCM is secure only for a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of data and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must ensure; and a nonce must never be used twice with the same key. /// -/// Contract: `VG.Spec.Gcm.streamEncryptContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. +/// Contract: `VG.Spec.Gcm.sealContract`. Constant time: only the pointers, `rounds` and the lengths may affect timing, not the key context, the nonce, the additional data or the data. /// -/// It runs `vg_aes_gcm_stream_encrypt`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_aes_gcm_seal`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes, and aligned for `u64`. -/// * `state` must be valid for reads and writes of 80 bytes, and aligned for `u64`. +/// * `nonce` must be valid for reads of `nonce_len` bytes. +/// * `aad` must be valid for reads of `aad_len` bytes. /// * `data` must be valid for reads and writes of `len` bytes. +/// * `tag` must be valid for reads and writes of 16 bytes. /// * `rounds` must be 10, 12 or 14. -/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the 4608 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` and `tag` must not overlap each other, `ctx`, `nonce`, `aad` or the arguments on the stack (distinct Rust objects never do). +/// * None of `ctx`, `nonce`, `aad`, `data` and `tag` may overlap the 4624 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_stream_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { +pub unsafe fn vg_aes_gcm_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *mut [u8; 16]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::gcm::vg_aes_gcm_stream_encrypt(ctx, rounds, state, aad_len, text_len, data, len) } + unsafe { crate::gcm::vg_aes_gcm_seal(ctx, rounds, nonce, nonce_len, aad, aad_len, data, len, tag) } } -/// Decrypts the next piece of the text of an incremental AES-GCM decryption (NIST SP 800-38D §7.2): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext so far, decrypts the `len` bytes of ciphertext at `data` in place, as the continuation of that ciphertext, and the state then represents the message with them appended. The plaintext is not authenticated until `vg_aes_gcm_stream_verify` has returned 1. +/// AES-GCM authenticated decryption (NIST SP 800-38D §7.2, GCM-AD): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds and the received tag the `tag_len` bytes at `tag`, returns 1 if `tag_len` is between 4 and 16 and the tag is the first `tag_len` bytes of that of the `len` bytes of ciphertext at `data` and the `aad_len` bytes of additional data at `aad`, under the IV the `nonce_len` bytes at `nonce`, having then decrypted the ciphertext in place; otherwise returns 0, and the bytes at `data` are unchanged. The tags are compared without a branch. /// -/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// The function checks no other length: GCM requires a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of ciphertext and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.streamDecryptContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. +/// Contract: `VG.Spec.Gcm.openContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the additional data, the data or the tag. /// -/// It runs `vg_aes_gcm_stream_decrypt`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_aes_gcm_open`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes, and aligned for `u64`. -/// * `state` must be valid for reads and writes of 80 bytes, and aligned for `u64`. +/// * `nonce` must be valid for reads of `nonce_len` bytes. +/// * `aad` must be valid for reads of `aad_len` bytes. /// * `data` must be valid for reads and writes of `len` bytes. +/// * `tag` must be valid for reads of `tag_len` bytes. /// * `rounds` must be 10, 12 or 14. -/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the 4608 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must not overlap `ctx`, `nonce`, `aad`, `tag` or the arguments on the stack (distinct Rust objects never do). +/// * None of `ctx`, `nonce`, `aad`, `data` and `tag` may overlap the 4640 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { +pub unsafe fn vg_aes_gcm_open_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *const u8, tag_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::gcm::vg_aes_gcm_stream_decrypt(ctx, rounds, state, aad_len, text_len, data, len) } + unsafe { crate::gcm::vg_aes_gcm_open(ctx, rounds, nonce, nonce_len, aad, aad_len, data, len, tag, tag_len) } +} + +/// AES-GCM authenticated encryption (NIST SP 800-38D §7.1, GCM-AE, with a 128-bit tag), out of place, of a plaintext in pieces: with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, encrypts the concatenation of the `src_count` slices that `src` lists (each an address and a length, in bytes), under the IV the `nonce_len` bytes at `nonce`, writes the ciphertext to the `len` bytes at `dst`, and writes the tag of the ciphertext and the `aad_len` bytes of additional data at `aad` to `*tag`: `vg_aes_gcm_seal` with the plaintext gathered from `src`. A shorter tag is the first bytes of this one. +/// +/// The function checks no length. GCM is secure only for a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of data and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must ensure; and a nonce must never be used twice with the same key. +/// +/// Contract: `VG.Spec.Gcm.sealGatherContract`. Constant time: only the pointers, `rounds`, the lengths, `src_count` and where the slices are (their addresses and lengths) may affect timing, not the key context, the nonce, the additional data or the data. +/// +/// It runs `vg_aes_gcm_seal_gather`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `ctx` must be valid for reads of 256 bytes, and aligned for `u64`. +/// * `nonce` must be valid for reads of `nonce_len` bytes. +/// * `aad` must be valid for reads of `aad_len` bytes. +/// * `src` must be valid for reads of `2 * size_of::() * src_count` bytes, and each slice it lists for reads of its length in bytes. +/// * `dst` must be valid for reads and writes of `len` bytes. +/// * `tag` must be valid for reads and writes of 16 bytes. +/// * `rounds` must be 10, 12 or 14. +/// * The slices `src` lists must be `len` bytes long in all. +/// * `dst` and `tag` must not overlap each other, `ctx`, `nonce`, `aad`, `src`, the slices `src` lists or the arguments on the stack (distinct Rust objects never do). +/// * None of `ctx`, `nonce`, `aad`, `src`, the slices `src` lists, `dst` and `tag` may overlap the 4640 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_aes_gcm_seal_gather_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const [usize; 2], src_count: usize, dst: *mut u8, len: usize, tag: *mut [u8; 16]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::gcm::vg_aes_gcm_seal_gather(ctx, rounds, nonce, nonce_len, aad, aad_len, src, src_count, dst, len, tag) } +} + +/// AES-GCM authenticated decryption (NIST SP 800-38D §7.2, GCM-AD), out of place: with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds and the received tag the `tag_len` bytes at `tag`, returns 1 if `tag_len` is between 4 and 16 and the tag is the first `tag_len` bytes of that of the `len` bytes of ciphertext at `src` and the `aad_len` bytes of additional data at `aad`, under the IV the `nonce_len` bytes at `nonce`, having then written the plaintext to the `dst_len = len` bytes at `dst`; otherwise returns 0, having written zeros to them: `vg_aes_gcm_open` with the ciphertext read from `src` and the plaintext written to `dst`. `src` is not written, and `dst` is a buffer of its own: it overlaps no other buffer, `src` included. The tags are compared without a branch. +/// +/// The function checks no other length: GCM requires a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of ciphertext and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// +/// Contract: `VG.Spec.Gcm.openToContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the additional data, the data or the tag. +/// +/// It runs `vg_aes_gcm_open_to`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `ctx` must be valid for reads of 256 bytes, and aligned for `u64`. +/// * `nonce` must be valid for reads of `nonce_len` bytes. +/// * `aad` must be valid for reads of `aad_len` bytes. +/// * `src` must be valid for reads of `len` bytes. +/// * `dst` must be valid for reads and writes of `dst_len` bytes. +/// * `tag` must be valid for reads of `tag_len` bytes. +/// * `rounds` must be 10, 12 or 14. +/// * `dst_len` must equal `len`. +/// * `dst` must not overlap `ctx`, `nonce`, `aad`, `src`, `tag` or the arguments on the stack (distinct Rust objects never do). +/// * None of `ctx`, `nonce`, `aad`, `src`, `dst` and `tag` may overlap the 4656 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_aes_gcm_open_to_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const u8, len: usize, dst: *mut u8, dst_len: usize, tag: *const u8, tag_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::gcm::vg_aes_gcm_open_to(ctx, rounds, nonce, nonce_len, aad, aad_len, src, len, dst, dst_len, tag, tag_len) } } diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/idea.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/idea.rs index 6d7decce201..058a1b3b478 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/idea.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/idea.rs @@ -2,25 +2,6 @@ //! The dispatchers of the verified `idea` functions for `riscv64` //! (`crate::idea`, `VG.Rust.dispatcher`). -/// IDEA in ECB mode (Lai, 1992, §3.3; SP 800-38A §6.1) on `n` complete 8-byte blocks at `data`, in place, under the 52 subkeys at `schedule`: encryption under the subkeys `vg_idea_expand_key` writes, decryption under those `vg_idea_invert_key` writes. No padding is added or removed. For `n = 0`, no data is transformed. -/// -/// Contract: `VG.Spec.Idea.ecbContract`. Constant time: only pointers and `n` may affect timing, not the subkeys or data, including words or subkeys that are 0. -/// -/// It runs `vg_idea_ecb`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `schedule` must be valid for reads of 104 bytes. -/// * `data` must be valid for reads and writes of `8 * n` bytes. -/// * `data` must not overlap `schedule` (distinct Rust objects never do). -/// * Neither `schedule` nor `data` may wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_idea_ecb_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *const [u8; 104], data: *mut [u8; 8], n: usize) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::idea::vg_idea_ecb(schedule, data, n) } -} - /// IDEA encryption key schedule (Lai, 1992, §3.3): writes the 52 encryption subkeys of the 16-byte key at `key` (as eight big-endian 16-bit words, rotated left by 25 bits after every eight subkeys) to `*schedule`, as little-endian 16-bit words. /// /// Contract: `VG.Spec.Idea.expandKeyContract`. Constant time: only pointers may affect timing, not the key. @@ -58,3 +39,22 @@ pub unsafe fn vg_idea_invert_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sched // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::idea::vg_idea_invert_key(schedule, inverse) } } + +/// IDEA in ECB mode (Lai, 1992, §3.3; SP 800-38A §6.1) on `n` complete 8-byte blocks at `data`, in place, under the 52 subkeys at `schedule`: encryption under the subkeys `vg_idea_expand_key` writes, decryption under those `vg_idea_invert_key` writes. No padding is added or removed. For `n = 0`, no data is transformed. +/// +/// Contract: `VG.Spec.Idea.ecbContract`. Constant time: only pointers and `n` may affect timing, not the subkeys or data, including words or subkeys that are 0. +/// +/// It runs `vg_idea_ecb`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `schedule` must be valid for reads of 104 bytes. +/// * `data` must be valid for reads and writes of `8 * n` bytes. +/// * `data` must not overlap `schedule` (distinct Rust objects never do). +/// * Neither `schedule` nor `data` may wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_idea_ecb_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *const [u8; 104], data: *mut [u8; 8], n: usize) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::idea::vg_idea_ecb(schedule, data, n) } +} diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/mldsa44.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/mldsa44.rs index e58d5495b45..b4c0ea60252 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/mldsa44.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/mldsa44.rs @@ -2,29 +2,27 @@ //! The dispatchers of the verified `mldsa44` functions for `riscv64` //! (`crate::mldsa44`, `VG.Rust.dispatcher`). -/// ML-DSA-44 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-44 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. +/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. /// -/// It runs `vg_mldsa44_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa44_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `sk` must be valid for reads of 2560 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `rnd` must be valid for reads of 32 bytes. -/// * `sig` must be valid for reads and writes of 2420 bytes. +/// * `seed` must be valid for reads of 32 bytes. +/// * `pk` must be valid for reads and writes of 1312 bytes. +/// * `sk` must be valid for reads and writes of 2560 bytes. /// * `scratch` must be valid for reads and writes of 77824 bytes, and aligned for `u64`. -/// * `sk` must have been written by `vg_mldsa44_keygen`. -/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. +/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 1184 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 1168 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { +pub unsafe fn vg_mldsa44_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1312], sk: *mut [u8; 2560], scratch: *mut [u64; 9728]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_sign(sk, mu, rnd, sig, scratch) } + unsafe { crate::mldsa44::vg_mldsa44_keygen(seed, pk, sk, scratch) } } /// ML-DSA-44 public-key hash: writes `tr = H(pk, 64)` (FIPS 204 Algorithm 8, line 6), SHAKE256 of the public key `*pk` to 64 bytes, to `*tr`, the digest `vg_mldsa44_verify_message_cached` takes. @@ -48,118 +46,98 @@ pub unsafe fn vg_mldsa44_pk_tr_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *co unsafe { crate::mldsa44::vg_mldsa44_pk_tr(pk, tr, scratch) } } -/// ML-DSA-44 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-44 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa44_verify_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. +/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. /// -/// It runs `vg_mldsa44_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa44_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `seed` must be valid for reads of 32 bytes. -/// * `pk` must be valid for reads and writes of 1312 bytes. -/// * `sk` must be valid for reads and writes of 2560 bytes. -/// * `scratch` must be valid for reads and writes of 77824 bytes, and aligned for `u64`. -/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. +/// * `pk` must be valid for reads of 1312 bytes. +/// * `prepared` must be valid for reads and writes of 5408 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes, and aligned for `u64`. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 1168 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). +/// * None of `pk`, `prepared` and `scratch` may overlap the 1072 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1312], sk: *mut [u8; 2560], scratch: *mut [u64; 9728]) -> u32 { +pub unsafe fn vg_mldsa44_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], prepared: *mut [u8; 5408], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_keygen(seed, pk, sk, scratch) } + unsafe { crate::mldsa44::vg_mldsa44_prepare_verifying_key(pk, prepared, scratch) } } -/// ML-DSA-44 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// ML-DSA-44 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa44_sign_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. /// -/// It runs `vg_mldsa44_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa44_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `pk` must be valid for reads of 1312 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `sig` must be valid for reads of 2420 bytes. -/// * `scratch` must be valid for reads and writes of 77824 bytes, and aligned for `u64`. +/// * `sk` must be valid for reads of 2560 bytes. +/// * `prepared` must be valid for reads and writes of 14848 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes, and aligned for `u64`. +/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). -/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 1184 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). +/// * None of `sk`, `prepared` and `scratch` may overlap the 1072 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], mu: *const [u8; 64], sig: *const [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { +pub unsafe fn vg_mldsa44_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], prepared: *mut [u8; 14848], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_verify(pk, mu, sig, scratch) } + unsafe { crate::mldsa44::vg_mldsa44_prepare_signing_key(sk, prepared, scratch) } } -/// ML-DSA-44 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. +/// ML-DSA-44 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. +/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. /// -/// It runs `vg_mldsa44_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa44_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `sk` must be valid for reads of 2560 bytes. -/// * `msg` must be valid for reads of `msg_len` bytes. -/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `mu` must be valid for reads of 64 bytes. /// * `rnd` must be valid for reads of 32 bytes. /// * `sig` must be valid for reads and writes of 2420 bytes. -/// * `scratch` must be valid for reads and writes of 78848 bytes, and aligned for `u64`. +/// * `scratch` must be valid for reads and writes of 77824 bytes, and aligned for `u64`. /// * `sk` must have been written by `vg_mldsa44_keygen`. /// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the 1184 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 1184 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9856]) -> u32 { +pub unsafe fn vg_mldsa44_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { crate::mldsa44::vg_mldsa44_sign(sk, mu, rnd, sig, scratch) } } -/// ML-DSA-44 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa44_sign_message_prepared`. +/// ML-DSA-44 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. /// -/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. +/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. /// -/// It runs `vg_mldsa44_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa44_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `sk` must be valid for reads of 2560 bytes. -/// * `prepared` must be valid for reads and writes of 14848 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes, and aligned for `u64`. -/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). -/// * None of `sk`, `prepared` and `scratch` may overlap the 1072 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mldsa44_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], prepared: *mut [u8; 14848], scratch: *mut [u64; 128]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_prepare_signing_key(sk, prepared, scratch) } -} - -/// ML-DSA-44 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa44_verify_message_prepared`. -/// -/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. -/// -/// It runs `vg_mldsa44_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 1312 bytes. -/// * `prepared` must be valid for reads and writes of 5408 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes, and aligned for `u64`. +/// * `msg` must be valid for reads of `msg_len` bytes. +/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `rnd` must be valid for reads of 32 bytes. +/// * `sig` must be valid for reads and writes of 2420 bytes. +/// * `scratch` must be valid for reads and writes of 78848 bytes, and aligned for `u64`. +/// * `sk` must have been written by `vg_mldsa44_keygen`. +/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). -/// * None of `pk`, `prepared` and `scratch` may overlap the 1072 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the 1184 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], prepared: *mut [u8; 5408], scratch: *mut [u64; 128]) { +pub unsafe fn vg_mldsa44_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9856]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_prepare_verifying_key(pk, prepared, scratch) } + unsafe { crate::mldsa44::vg_mldsa44_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } /// ML-DSA-44 signing with a prepared private key: `vg_mldsa44_sign_message` (with its results, timing and leakage) of the private key that `*sk` begins with. @@ -189,6 +167,28 @@ pub unsafe fn vg_mldsa44_sign_message_prepared_auto(_dit: verified_garbage_cpu:: unsafe { crate::mldsa44::vg_mldsa44_sign_message_prepared(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } +/// ML-DSA-44 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// +/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// +/// It runs `vg_mldsa44_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 1312 bytes. +/// * `mu` must be valid for reads of 64 bytes. +/// * `sig` must be valid for reads of 2420 bytes. +/// * `scratch` must be valid for reads and writes of 77824 bytes, and aligned for `u64`. +/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). +/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). +/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 1184 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mldsa44_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], mu: *const [u8; 64], sig: *const [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mldsa44::vg_mldsa44_verify(pk, mu, sig, scratch) } +} + /// ML-DSA-44 message verification with a cached public-key digest. Returns 1 for a valid signature, 0 for an invalid signature or exhausted sampling bound, and 2 if the context exceeds 255 bytes. It is `ML-DSA.Verify(pk, M, σ, ctx)` (FIPS 204 Algorithm 3) with `H(pk, 64)` read from `*tr`: with the public key `*pk`, the `msg_len` bytes of the message at `msg`, the `ctx_len` bytes of the context string at `ctx` and the signature `*sig`, it formats the message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 5), computes `μ = H(tr ‖ M′, 64)`, and verifies `*sig` on `μ` with `*pk`, `ML-DSA.Verify_internal(pk, M′, σ)` (Algorithm 8). /// /// Contract: `VG.Spec.MlDsa.verifyMessageCachedContract`. Timing may depend on the public key, message, context string and signature, and on argument addresses and lengths. diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/mldsa65.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/mldsa65.rs index b5d0258e5a8..1f8fc792658 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/mldsa65.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/mldsa65.rs @@ -2,29 +2,27 @@ //! The dispatchers of the verified `mldsa65` functions for `riscv64` //! (`crate::mldsa65`, `VG.Rust.dispatcher`). -/// ML-DSA-65 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-65 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. +/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. /// -/// It runs `vg_mldsa65_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa65_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `sk` must be valid for reads of 4032 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `rnd` must be valid for reads of 32 bytes. -/// * `sig` must be valid for reads and writes of 3309 bytes. +/// * `seed` must be valid for reads of 32 bytes. +/// * `pk` must be valid for reads and writes of 1952 bytes. +/// * `sk` must be valid for reads and writes of 4032 bytes. /// * `scratch` must be valid for reads and writes of 103424 bytes, and aligned for `u64`. -/// * `sk` must have been written by `vg_mldsa65_keygen`. -/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. +/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 1184 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 1168 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { +pub unsafe fn vg_mldsa65_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1952], sk: *mut [u8; 4032], scratch: *mut [u64; 12928]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_sign(sk, mu, rnd, sig, scratch) } + unsafe { crate::mldsa65::vg_mldsa65_keygen(seed, pk, sk, scratch) } } /// ML-DSA-65 public-key hash: writes `tr = H(pk, 64)` (FIPS 204 Algorithm 8, line 6), SHAKE256 of the public key `*pk` to 64 bytes, to `*tr`, the digest `vg_mldsa65_verify_message_cached` takes. @@ -48,118 +46,98 @@ pub unsafe fn vg_mldsa65_pk_tr_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *co unsafe { crate::mldsa65::vg_mldsa65_pk_tr(pk, tr, scratch) } } -/// ML-DSA-65 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-65 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa65_verify_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. +/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. /// -/// It runs `vg_mldsa65_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa65_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `seed` must be valid for reads of 32 bytes. -/// * `pk` must be valid for reads and writes of 1952 bytes. -/// * `sk` must be valid for reads and writes of 4032 bytes. -/// * `scratch` must be valid for reads and writes of 103424 bytes, and aligned for `u64`. -/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. +/// * `pk` must be valid for reads of 1952 bytes. +/// * `prepared` must be valid for reads and writes of 8096 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes, and aligned for `u64`. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 1168 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). +/// * None of `pk`, `prepared` and `scratch` may overlap the 1072 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1952], sk: *mut [u8; 4032], scratch: *mut [u64; 12928]) -> u32 { +pub unsafe fn vg_mldsa65_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], prepared: *mut [u8; 8096], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_keygen(seed, pk, sk, scratch) } + unsafe { crate::mldsa65::vg_mldsa65_prepare_verifying_key(pk, prepared, scratch) } } -/// ML-DSA-65 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// ML-DSA-65 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa65_sign_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. /// -/// It runs `vg_mldsa65_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa65_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `pk` must be valid for reads of 1952 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `sig` must be valid for reads of 3309 bytes. -/// * `scratch` must be valid for reads and writes of 103424 bytes, and aligned for `u64`. +/// * `sk` must be valid for reads of 4032 bytes. +/// * `prepared` must be valid for reads and writes of 21440 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes, and aligned for `u64`. +/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). -/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 1184 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). +/// * None of `sk`, `prepared` and `scratch` may overlap the 1072 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], mu: *const [u8; 64], sig: *const [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { +pub unsafe fn vg_mldsa65_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], prepared: *mut [u8; 21440], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_verify(pk, mu, sig, scratch) } + unsafe { crate::mldsa65::vg_mldsa65_prepare_signing_key(sk, prepared, scratch) } } -/// ML-DSA-65 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. +/// ML-DSA-65 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. +/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. /// -/// It runs `vg_mldsa65_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa65_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `sk` must be valid for reads of 4032 bytes. -/// * `msg` must be valid for reads of `msg_len` bytes. -/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `mu` must be valid for reads of 64 bytes. /// * `rnd` must be valid for reads of 32 bytes. /// * `sig` must be valid for reads and writes of 3309 bytes. -/// * `scratch` must be valid for reads and writes of 104448 bytes, and aligned for `u64`. +/// * `scratch` must be valid for reads and writes of 103424 bytes, and aligned for `u64`. /// * `sk` must have been written by `vg_mldsa65_keygen`. /// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the 1184 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 1184 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 13056]) -> u32 { +pub unsafe fn vg_mldsa65_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { crate::mldsa65::vg_mldsa65_sign(sk, mu, rnd, sig, scratch) } } -/// ML-DSA-65 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa65_sign_message_prepared`. +/// ML-DSA-65 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. /// -/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. +/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. /// -/// It runs `vg_mldsa65_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa65_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `sk` must be valid for reads of 4032 bytes. -/// * `prepared` must be valid for reads and writes of 21440 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes, and aligned for `u64`. -/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). -/// * None of `sk`, `prepared` and `scratch` may overlap the 1072 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mldsa65_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], prepared: *mut [u8; 21440], scratch: *mut [u64; 128]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_prepare_signing_key(sk, prepared, scratch) } -} - -/// ML-DSA-65 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa65_verify_message_prepared`. -/// -/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. -/// -/// It runs `vg_mldsa65_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 1952 bytes. -/// * `prepared` must be valid for reads and writes of 8096 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes, and aligned for `u64`. +/// * `msg` must be valid for reads of `msg_len` bytes. +/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `rnd` must be valid for reads of 32 bytes. +/// * `sig` must be valid for reads and writes of 3309 bytes. +/// * `scratch` must be valid for reads and writes of 104448 bytes, and aligned for `u64`. +/// * `sk` must have been written by `vg_mldsa65_keygen`. +/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). -/// * None of `pk`, `prepared` and `scratch` may overlap the 1072 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the 1184 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], prepared: *mut [u8; 8096], scratch: *mut [u64; 128]) { +pub unsafe fn vg_mldsa65_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 13056]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_prepare_verifying_key(pk, prepared, scratch) } + unsafe { crate::mldsa65::vg_mldsa65_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } /// ML-DSA-65 signing with a prepared private key: `vg_mldsa65_sign_message` (with its results, timing and leakage) of the private key that `*sk` begins with. @@ -189,6 +167,28 @@ pub unsafe fn vg_mldsa65_sign_message_prepared_auto(_dit: verified_garbage_cpu:: unsafe { crate::mldsa65::vg_mldsa65_sign_message_prepared(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } +/// ML-DSA-65 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// +/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// +/// It runs `vg_mldsa65_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 1952 bytes. +/// * `mu` must be valid for reads of 64 bytes. +/// * `sig` must be valid for reads of 3309 bytes. +/// * `scratch` must be valid for reads and writes of 103424 bytes, and aligned for `u64`. +/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). +/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). +/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 1184 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mldsa65_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], mu: *const [u8; 64], sig: *const [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mldsa65::vg_mldsa65_verify(pk, mu, sig, scratch) } +} + /// ML-DSA-65 message verification with a cached public-key digest. Returns 1 for a valid signature, 0 for an invalid signature or exhausted sampling bound, and 2 if the context exceeds 255 bytes. It is `ML-DSA.Verify(pk, M, σ, ctx)` (FIPS 204 Algorithm 3) with `H(pk, 64)` read from `*tr`: with the public key `*pk`, the `msg_len` bytes of the message at `msg`, the `ctx_len` bytes of the context string at `ctx` and the signature `*sig`, it formats the message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 5), computes `μ = H(tr ‖ M′, 64)`, and verifies `*sig` on `μ` with `*pk`, `ML-DSA.Verify_internal(pk, M′, σ)` (Algorithm 8). /// /// Contract: `VG.Spec.MlDsa.verifyMessageCachedContract`. Timing may depend on the public key, message, context string and signature, and on argument addresses and lengths. diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/mldsa87.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/mldsa87.rs index 558a9d5ae3a..961f020554f 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/mldsa87.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/mldsa87.rs @@ -2,29 +2,27 @@ //! The dispatchers of the verified `mldsa87` functions for `riscv64` //! (`crate::mldsa87`, `VG.Rust.dispatcher`). -/// ML-DSA-87 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-87 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. +/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. /// -/// It runs `vg_mldsa87_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa87_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `sk` must be valid for reads of 4896 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `rnd` must be valid for reads of 32 bytes. -/// * `sig` must be valid for reads and writes of 4627 bytes. +/// * `seed` must be valid for reads of 32 bytes. +/// * `pk` must be valid for reads and writes of 2592 bytes. +/// * `sk` must be valid for reads and writes of 4896 bytes. /// * `scratch` must be valid for reads and writes of 144384 bytes, and aligned for `u64`. -/// * `sk` must have been written by `vg_mldsa87_keygen`. -/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. +/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 1184 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 1168 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { +pub unsafe fn vg_mldsa87_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 2592], sk: *mut [u8; 4896], scratch: *mut [u64; 18048]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_sign(sk, mu, rnd, sig, scratch) } + unsafe { crate::mldsa87::vg_mldsa87_keygen(seed, pk, sk, scratch) } } /// ML-DSA-87 public-key hash: writes `tr = H(pk, 64)` (FIPS 204 Algorithm 8, line 6), SHAKE256 of the public key `*pk` to 64 bytes, to `*tr`, the digest `vg_mldsa87_verify_message_cached` takes. @@ -48,118 +46,98 @@ pub unsafe fn vg_mldsa87_pk_tr_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *co unsafe { crate::mldsa87::vg_mldsa87_pk_tr(pk, tr, scratch) } } -/// ML-DSA-87 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-87 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa87_verify_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. +/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. /// -/// It runs `vg_mldsa87_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa87_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `seed` must be valid for reads of 32 bytes. -/// * `pk` must be valid for reads and writes of 2592 bytes. -/// * `sk` must be valid for reads and writes of 4896 bytes. -/// * `scratch` must be valid for reads and writes of 144384 bytes, and aligned for `u64`. -/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. +/// * `pk` must be valid for reads of 2592 bytes. +/// * `prepared` must be valid for reads and writes of 10784 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes, and aligned for `u64`. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the 1168 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). +/// * None of `pk`, `prepared` and `scratch` may overlap the 1072 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 2592], sk: *mut [u8; 4896], scratch: *mut [u64; 18048]) -> u32 { +pub unsafe fn vg_mldsa87_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], prepared: *mut [u8; 10784], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_keygen(seed, pk, sk, scratch) } + unsafe { crate::mldsa87::vg_mldsa87_prepare_verifying_key(pk, prepared, scratch) } } -/// ML-DSA-87 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// ML-DSA-87 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa87_sign_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. /// -/// It runs `vg_mldsa87_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa87_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `pk` must be valid for reads of 2592 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `sig` must be valid for reads of 4627 bytes. -/// * `scratch` must be valid for reads and writes of 144384 bytes, and aligned for `u64`. +/// * `sk` must be valid for reads of 4896 bytes. +/// * `prepared` must be valid for reads and writes of 28448 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes, and aligned for `u64`. +/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). -/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 1184 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). +/// * None of `sk`, `prepared` and `scratch` may overlap the 1072 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], mu: *const [u8; 64], sig: *const [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { +pub unsafe fn vg_mldsa87_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], prepared: *mut [u8; 28448], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_verify(pk, mu, sig, scratch) } + unsafe { crate::mldsa87::vg_mldsa87_prepare_signing_key(sk, prepared, scratch) } } -/// ML-DSA-87 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. +/// ML-DSA-87 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. +/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. /// -/// It runs `vg_mldsa87_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa87_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `sk` must be valid for reads of 4896 bytes. -/// * `msg` must be valid for reads of `msg_len` bytes. -/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `mu` must be valid for reads of 64 bytes. /// * `rnd` must be valid for reads of 32 bytes. /// * `sig` must be valid for reads and writes of 4627 bytes. -/// * `scratch` must be valid for reads and writes of 145408 bytes, and aligned for `u64`. +/// * `scratch` must be valid for reads and writes of 144384 bytes, and aligned for `u64`. /// * `sk` must have been written by `vg_mldsa87_keygen`. /// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the 1184 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the 1184 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18176]) -> u32 { +pub unsafe fn vg_mldsa87_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { crate::mldsa87::vg_mldsa87_sign(sk, mu, rnd, sig, scratch) } } -/// ML-DSA-87 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa87_sign_message_prepared`. +/// ML-DSA-87 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. /// -/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. +/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. /// -/// It runs `vg_mldsa87_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa87_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `sk` must be valid for reads of 4896 bytes. -/// * `prepared` must be valid for reads and writes of 28448 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes, and aligned for `u64`. -/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). -/// * None of `sk`, `prepared` and `scratch` may overlap the 1072 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mldsa87_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], prepared: *mut [u8; 28448], scratch: *mut [u64; 128]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_prepare_signing_key(sk, prepared, scratch) } -} - -/// ML-DSA-87 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa87_verify_message_prepared`. -/// -/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. -/// -/// It runs `vg_mldsa87_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 2592 bytes. -/// * `prepared` must be valid for reads and writes of 10784 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes, and aligned for `u64`. +/// * `msg` must be valid for reads of `msg_len` bytes. +/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `rnd` must be valid for reads of 32 bytes. +/// * `sig` must be valid for reads and writes of 4627 bytes. +/// * `scratch` must be valid for reads and writes of 145408 bytes, and aligned for `u64`. +/// * `sk` must have been written by `vg_mldsa87_keygen`. +/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). -/// * None of `pk`, `prepared` and `scratch` may overlap the 1072 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the 1184 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], prepared: *mut [u8; 10784], scratch: *mut [u64; 128]) { +pub unsafe fn vg_mldsa87_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18176]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_prepare_verifying_key(pk, prepared, scratch) } + unsafe { crate::mldsa87::vg_mldsa87_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } /// ML-DSA-87 signing with a prepared private key: `vg_mldsa87_sign_message` (with its results, timing and leakage) of the private key that `*sk` begins with. @@ -189,6 +167,28 @@ pub unsafe fn vg_mldsa87_sign_message_prepared_auto(_dit: verified_garbage_cpu:: unsafe { crate::mldsa87::vg_mldsa87_sign_message_prepared(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } +/// ML-DSA-87 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// +/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// +/// It runs `vg_mldsa87_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 2592 bytes. +/// * `mu` must be valid for reads of 64 bytes. +/// * `sig` must be valid for reads of 4627 bytes. +/// * `scratch` must be valid for reads and writes of 144384 bytes, and aligned for `u64`. +/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). +/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). +/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the 1184 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mldsa87_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], mu: *const [u8; 64], sig: *const [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mldsa87::vg_mldsa87_verify(pk, mu, sig, scratch) } +} + /// ML-DSA-87 message verification with a cached public-key digest. Returns 1 for a valid signature, 0 for an invalid signature or exhausted sampling bound, and 2 if the context exceeds 255 bytes. It is `ML-DSA.Verify(pk, M, σ, ctx)` (FIPS 204 Algorithm 3) with `H(pk, 64)` read from `*tr`: with the public key `*pk`, the `msg_len` bytes of the message at `msg`, the `ctx_len` bytes of the context string at `ctx` and the signature `*sig`, it formats the message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 5), computes `μ = H(tr ‖ M′, 64)`, and verifies `*sig` on `μ` with `*pk`, `ML-DSA.Verify_internal(pk, M′, σ)` (Algorithm 8). /// /// Contract: `VG.Spec.MlDsa.verifyMessageCachedContract`. Timing may depend on the public key, message, context string and signature, and on argument addresses and lengths. diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/mlkem1024.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/mlkem1024.rs index 5412344d704..ea2c2bb72ed 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/mlkem1024.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/mlkem1024.rs @@ -2,6 +2,23 @@ //! The dispatchers of the verified `mlkem1024` functions for `riscv64` //! (`crate::mlkem1024`, `VG.Rust.dispatcher`). +/// The ML-KEM-1024 encapsulation key check (FIPS 203 §7.2): returns 1 if every 12-bit integer that the first 1536 bytes of `*ek` encode is less than `q` = 3329 (the modulus check), and 0 otherwise. An encapsulation key must pass it before it is given to `vg_mlkem1024_encaps`. +/// +/// Contract: `VG.Spec.MlKem1024.checkEkContract`. Constant time: only the pointer may affect timing. +/// +/// It runs `vg_mlkem1024_check_ek`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `ek` must be valid for reads of 1568 bytes. +/// * `ek` must not wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mlkem1024_check_ek_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1568]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mlkem1024::vg_mlkem1024_check_ek(ek) } +} + /// ML-KEM-1024 key generation from a seed, `ML-KEM.KeyGen_internal(d, z)` (FIPS 203 Algorithm 16): with `d` in bytes 0–31 of `*seed` and `z` in bytes 32–63, writes the encapsulation key to `*ek` and the decapsulation key to `*dk`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// /// Contract: `VG.Spec.MlKem1024.keyGenContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of the encapsulation key), but not on anything else of the seed or the keys. @@ -50,46 +67,6 @@ pub unsafe fn vg_mlkem1024_encaps_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: unsafe { crate::mlkem1024::vg_mlkem1024_encaps(ek, m, key, ct, scratch) } } -/// ML-KEM-1024 decapsulation, `ML-KEM.Decaps_internal(dk, c)` (FIPS 203 Algorithm 18): with the decapsulation key `*dk` and the ciphertext `*ct`, writes the shared secret key to `*key`, which is the implicit rejection key `J(z ‖ c)` if the ciphertext does not re-encrypt to itself. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. -/// -/// Contract: `VG.Spec.MlKem1024.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 3072–3103 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. -/// -/// It runs `vg_mlkem1024_decaps`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `dk` must be valid for reads of 3168 bytes. -/// * `ct` must be valid for reads of 1568 bytes. -/// * `key` must be valid for reads and writes of 32 bytes. -/// * `scratch` must be valid for reads and writes of 49152 bytes, and aligned for `u64`. -/// * `dk` must have been written by `vg_mlkem1024_keygen` (so that it passes the checks of FIPS 203 §7.3). -/// * `scratch` is working space: on return each of its bytes is unchanged or zero. -/// * `key` and `scratch` must not overlap each other, `dk` or `ct` (distinct Rust objects never do). -/// * None of `dk`, `ct`, `key` and `scratch` may overlap the 1200 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mlkem1024_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: *const [u8; 3168], ct: *const [u8; 1568], key: *mut [u8; 32], scratch: *mut [u64; 6144]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mlkem1024::vg_mlkem1024_decaps(dk, ct, key, scratch) } -} - -/// The ML-KEM-1024 encapsulation key check (FIPS 203 §7.2): returns 1 if every 12-bit integer that the first 1536 bytes of `*ek` encode is less than `q` = 3329 (the modulus check), and 0 otherwise. An encapsulation key must pass it before it is given to `vg_mlkem1024_encaps`. -/// -/// Contract: `VG.Spec.MlKem1024.checkEkContract`. Constant time: only the pointer may affect timing. -/// -/// It runs `vg_mlkem1024_check_ek`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `ek` must be valid for reads of 1568 bytes. -/// * `ek` must not wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mlkem1024_check_ek_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1568]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mlkem1024::vg_mlkem1024_check_ek(ek) } -} - /// ML-KEM-1024 encapsulation with given randomness and the hash of the key, `ML-KEM.Encaps_internal(ek, m)` (FIPS 203 Algorithm 17): with the encapsulation key `*ek`, its hash `H(ek)` in `*h` and the randomness `*m`, writes the shared secret key to `*key` and the ciphertext to `*ct`, as `vg_mlkem1024_encaps` does, using `*h` instead of hashing `*ek`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// /// Contract: `VG.Spec.MlKem1024.encapsHContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on its hash, on the randomness or on the outputs. @@ -116,3 +93,26 @@ pub unsafe fn vg_mlkem1024_encaps_h_auto(_dit: verified_garbage_cpu::Dit<'_>, ek // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::mlkem1024::vg_mlkem1024_encaps_h(ek, h, m, key, ct, scratch) } } + +/// ML-KEM-1024 decapsulation, `ML-KEM.Decaps_internal(dk, c)` (FIPS 203 Algorithm 18): with the decapsulation key `*dk` and the ciphertext `*ct`, writes the shared secret key to `*key`, which is the implicit rejection key `J(z ‖ c)` if the ciphertext does not re-encrypt to itself. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// +/// Contract: `VG.Spec.MlKem1024.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 3072–3103 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. +/// +/// It runs `vg_mlkem1024_decaps`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `dk` must be valid for reads of 3168 bytes. +/// * `ct` must be valid for reads of 1568 bytes. +/// * `key` must be valid for reads and writes of 32 bytes. +/// * `scratch` must be valid for reads and writes of 49152 bytes, and aligned for `u64`. +/// * `dk` must have been written by `vg_mlkem1024_keygen` (so that it passes the checks of FIPS 203 §7.3). +/// * `scratch` is working space: on return each of its bytes is unchanged or zero. +/// * `key` and `scratch` must not overlap each other, `dk` or `ct` (distinct Rust objects never do). +/// * None of `dk`, `ct`, `key` and `scratch` may overlap the 1200 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mlkem1024_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: *const [u8; 3168], ct: *const [u8; 1568], key: *mut [u8; 32], scratch: *mut [u64; 6144]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mlkem1024::vg_mlkem1024_decaps(dk, ct, key, scratch) } +} diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/mlkem768.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/mlkem768.rs index d79ca575365..9631f06113b 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/mlkem768.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/mlkem768.rs @@ -50,29 +50,6 @@ pub unsafe fn vg_mlkem768_encaps_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: * unsafe { crate::mlkem768::vg_mlkem768_encaps(ek, m, key, ct, scratch) } } -/// ML-KEM-768 decapsulation, `ML-KEM.Decaps_internal(dk, c)` (FIPS 203 Algorithm 18): with the decapsulation key `*dk` and the ciphertext `*ct`, writes the shared secret key to `*key`, which is the implicit rejection key `J(z ‖ c)` if the ciphertext does not re-encrypt to itself. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. -/// -/// Contract: `VG.Spec.MlKem.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 2304–2335 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. -/// -/// It runs `vg_mlkem768_decaps`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `dk` must be valid for reads of 2400 bytes. -/// * `ct` must be valid for reads of 1088 bytes. -/// * `key` must be valid for reads and writes of 32 bytes. -/// * `scratch` must be valid for reads and writes of 32768 bytes, and aligned for `u64`. -/// * `dk` must have been written by `vg_mlkem768_keygen` (so that it passes the checks of FIPS 203 §7.3). -/// * `scratch` is working space: on return each of its bytes is unchanged or zero. -/// * `key` and `scratch` must not overlap each other, `dk` or `ct` (distinct Rust objects never do). -/// * None of `dk`, `ct`, `key` and `scratch` may overlap the 1200 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mlkem768_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: *const [u8; 2400], ct: *const [u8; 1088], key: *mut [u8; 32], scratch: *mut [u64; 4096]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mlkem768::vg_mlkem768_decaps(dk, ct, key, scratch) } -} - /// ML-KEM-768 encapsulation with given randomness and the hash of the key, `ML-KEM.Encaps_internal(ek, m)` (FIPS 203 Algorithm 17): with the encapsulation key `*ek`, its hash `H(ek)` in `*h` and the randomness `*m`, writes the shared secret key to `*key` and the ciphertext to `*ct`, as `vg_mlkem768_encaps` does, using `*h` instead of hashing `*ek`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// /// Contract: `VG.Spec.MlKem.encapsHContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on its hash, on the randomness or on the outputs. @@ -100,6 +77,29 @@ pub unsafe fn vg_mlkem768_encaps_h_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: unsafe { crate::mlkem768::vg_mlkem768_encaps_h(ek, h, m, key, ct, scratch) } } +/// ML-KEM-768 decapsulation, `ML-KEM.Decaps_internal(dk, c)` (FIPS 203 Algorithm 18): with the decapsulation key `*dk` and the ciphertext `*ct`, writes the shared secret key to `*key`, which is the implicit rejection key `J(z ‖ c)` if the ciphertext does not re-encrypt to itself. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// +/// Contract: `VG.Spec.MlKem.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 2304–2335 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. +/// +/// It runs `vg_mlkem768_decaps`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `dk` must be valid for reads of 2400 bytes. +/// * `ct` must be valid for reads of 1088 bytes. +/// * `key` must be valid for reads and writes of 32 bytes. +/// * `scratch` must be valid for reads and writes of 32768 bytes, and aligned for `u64`. +/// * `dk` must have been written by `vg_mlkem768_keygen` (so that it passes the checks of FIPS 203 §7.3). +/// * `scratch` is working space: on return each of its bytes is unchanged or zero. +/// * `key` and `scratch` must not overlap each other, `dk` or `ct` (distinct Rust objects never do). +/// * None of `dk`, `ct`, `key` and `scratch` may overlap the 1200 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mlkem768_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: *const [u8; 2400], ct: *const [u8; 1088], key: *mut [u8; 32], scratch: *mut [u64; 4096]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mlkem768::vg_mlkem768_decaps(dk, ct, key, scratch) } +} + /// The ML-KEM-768 encapsulation key check (FIPS 203 §7.2): returns 1 if every 12-bit integer that the first 1152 bytes of `*ek` encode is less than `q` = 3329 (the modulus check), and 0 otherwise. An encapsulation key must pass it before it is given to `vg_mlkem768_encaps`. /// /// Contract: `VG.Spec.MlKem.checkEkContract`. Constant time: only the pointer may affect timing. diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/rsa.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/rsa.rs index 0114bd66e65..bea11c34e10 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/rsa.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/rsa.rs @@ -2,36 +2,28 @@ //! The dispatchers of the verified `rsa` functions for `riscv64` //! (`crate::rsa`, `VG.Rust.dispatcher`). -/// Checks an RSA private key `(n, e, d, p, q, dP, dQ, qInv)` exactly as BoringSSL's `RSA_check_key` does. With the modulus `n` (`n_len` bytes), the public exponent `e` (`e_len` bytes), the private exponent `d` (`d_len` bytes), `p`, `dp` and `qinv` (`p_len` bytes) and `q` and `dq` (`q_len` bytes), all most significant first, returns 1 if `n` is odd, from 512 to 8192 bits and its first byte not zero, `e` is odd and from 3 to `2^33 - 1`, `d < n`, `p < n`, `q < n`, `p q = n`, `d e ≡ 1 (mod p - 1)`, `d e ≡ 1 (mod q - 1)`, `dP < p - 1`, `e dP ≡ 1 (mod p - 1)`, `dQ < q - 1`, `e dQ ≡ 1 (mod q - 1)`, `qInv < p` and `q qInv ≡ 1 (mod p)`; and 0 otherwise. Like `RSA_check_key`, it does not check that `p` and `q` are prime; if they are, `vg_rsa_private_checked` never returns its internal error for a key it accepts. +/// The values of an RSA modulus that `vg_rsa_public_precomputed_checked` takes, so that they are computed once per public key rather than once per operation. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero) of `w = ⌈n_len / 8⌉` words of 64 bits, writes `n` and then `R² mod n` for `R = 2^(64 w)` to `pre` (`w` words each, least significant first) and returns 1; or writes zeros and returns 0 if `n` is not such a modulus. /// -/// Contract: `VG.Spec.Rsa.checkKeyContract`. Constant time but for the public key: timing may depend on the pointers, the lengths and the contents of `n` and `e`, not on the private key. +/// Contract: `VG.Spec.Rsa.publicPrecomputeContract`. Timing may depend on the pointers, the lengths and the contents of `n`. /// -/// It runs `vg_rsa_check_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_rsa_public_precompute`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// +/// * `pre` must be valid for reads and writes of `8 * pre_len` bytes, and aligned for `u64`. /// * `n` must be valid for reads of `n_len` bytes. -/// * `e` must be valid for reads of `e_len` bytes. -/// * `d` must be valid for reads of `d_len` bytes. -/// * `p` must be valid for reads of `p_len` bytes. -/// * `q` must be valid for reads of `q_len` bytes. -/// * `dp` must be valid for reads of `dp_len` bytes. -/// * `dq` must be valid for reads of `dq_len` bytes. -/// * `qinv` must be valid for reads of `qinv_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes, and aligned for `u64`. /// * `n_len` must be in 64..=1024. -/// * `e_len` and `d_len` must be in 1..=`n_len`. -/// * `p_len` and `q_len` must be in 1..`n_len`. -/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. -/// * `scratch_len` must be at least `16 * n_len + 32`. +/// * `pre_len` must be `2 * ⌈n_len / 8⌉`. +/// * `scratch_len` must be at least `8 * ⌈n_len / 8⌉ + 48`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `scratch` must not overlap `n`, `e`, `d`, `p`, `q`, `dp`, `dq`, `qinv` or the arguments on the stack (distinct Rust objects never do). -/// * None of `n`, `e`, `d`, `p`, `q`, `dp`, `dq`, `qinv` and `scratch` may wrap around the end of the address space (no Rust object does). +/// * `pre` and `scratch` must not overlap each other or `n` (distinct Rust objects never do). +/// * None of `pre`, `n` and `scratch` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_rsa_check_key_auto(_dit: verified_garbage_cpu::Dit<'_>, n: *const u8, n_len: usize, e: *const u8, e_len: usize, d: *const u8, d_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_rsa_public_precompute_auto(_dit: verified_garbage_cpu::Dit<'_>, pre: *mut u64, pre_len: usize, n: *const u8, n_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa::vg_rsa_check_key(n, n_len, e, e_len, d, d_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, scratch, scratch_len) } + unsafe { crate::rsa::vg_rsa_public_precompute(pre, pre_len, n, n_len, scratch, scratch_len) } } /// The CRT values of an RSA private key `(p, q, d)` (SP 800-56B Rev. 2 §6.2.2's prime-factor format), which bring it to the CRT format `(p, q, dP, dQ, qInv)` that `vg_rsa_private_checked` takes: `dP = d mod (p - 1)`, `dQ = d mod (q - 1)` and `qInv = q⁻¹ mod p` (§6.2.2's CRT format). With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), `p` (`p_len` bytes), `q` (`q_len` bytes) and `d` (`d_len` bytes), all most significant first, writes `dP` to `dp` and `qInv` to `qinv` (`p_len` bytes each) and `dQ` to `dq` (`q_len` bytes), most significant first, and returns 1; or writes zeros to all three and returns 0 if `n` is not such a modulus, `p q ≠ n`, or `q` has no inverse modulo `p`. Nothing checks that `p` and `q` are prime or that `d` is the key's private exponent. @@ -65,28 +57,36 @@ pub unsafe fn vg_rsa_crt_values_auto(_dit: verified_garbage_cpu::Dit<'_>, dp: *m unsafe { crate::rsa::vg_rsa_crt_values(dp, dp_len, dq, dq_len, qinv, qinv_len, n, n_len, p, p_len, q, q_len, d, d_len, scratch, scratch_len) } } -/// The values of an RSA modulus that `vg_rsa_public_precomputed_checked` takes, so that they are computed once per public key rather than once per operation. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero) of `w = ⌈n_len / 8⌉` words of 64 bits, writes `n` and then `R² mod n` for `R = 2^(64 w)` to `pre` (`w` words each, least significant first) and returns 1; or writes zeros and returns 0 if `n` is not such a modulus. +/// Checks an RSA private key `(n, e, d, p, q, dP, dQ, qInv)` exactly as BoringSSL's `RSA_check_key` does. With the modulus `n` (`n_len` bytes), the public exponent `e` (`e_len` bytes), the private exponent `d` (`d_len` bytes), `p`, `dp` and `qinv` (`p_len` bytes) and `q` and `dq` (`q_len` bytes), all most significant first, returns 1 if `n` is odd, from 512 to 8192 bits and its first byte not zero, `e` is odd and from 3 to `2^33 - 1`, `d < n`, `p < n`, `q < n`, `p q = n`, `d e ≡ 1 (mod p - 1)`, `d e ≡ 1 (mod q - 1)`, `dP < p - 1`, `e dP ≡ 1 (mod p - 1)`, `dQ < q - 1`, `e dQ ≡ 1 (mod q - 1)`, `qInv < p` and `q qInv ≡ 1 (mod p)`; and 0 otherwise. Like `RSA_check_key`, it does not check that `p` and `q` are prime; if they are, `vg_rsa_private_checked` never returns its internal error for a key it accepts. /// -/// Contract: `VG.Spec.Rsa.publicPrecomputeContract`. Timing may depend on the pointers, the lengths and the contents of `n`. +/// Contract: `VG.Spec.Rsa.checkKeyContract`. Constant time but for the public key: timing may depend on the pointers, the lengths and the contents of `n` and `e`, not on the private key. /// -/// It runs `vg_rsa_public_precompute`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_rsa_check_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `pre` must be valid for reads and writes of `8 * pre_len` bytes, and aligned for `u64`. /// * `n` must be valid for reads of `n_len` bytes. +/// * `e` must be valid for reads of `e_len` bytes. +/// * `d` must be valid for reads of `d_len` bytes. +/// * `p` must be valid for reads of `p_len` bytes. +/// * `q` must be valid for reads of `q_len` bytes. +/// * `dp` must be valid for reads of `dp_len` bytes. +/// * `dq` must be valid for reads of `dq_len` bytes. +/// * `qinv` must be valid for reads of `qinv_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes, and aligned for `u64`. /// * `n_len` must be in 64..=1024. -/// * `pre_len` must be `2 * ⌈n_len / 8⌉`. -/// * `scratch_len` must be at least `8 * ⌈n_len / 8⌉ + 48`. +/// * `e_len` and `d_len` must be in 1..=`n_len`. +/// * `p_len` and `q_len` must be in 1..`n_len`. +/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. +/// * `scratch_len` must be at least `16 * n_len + 32`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `pre` and `scratch` must not overlap each other or `n` (distinct Rust objects never do). -/// * None of `pre`, `n` and `scratch` may wrap around the end of the address space (no Rust object does). +/// * `scratch` must not overlap `n`, `e`, `d`, `p`, `q`, `dp`, `dq`, `qinv` or the arguments on the stack (distinct Rust objects never do). +/// * None of `n`, `e`, `d`, `p`, `q`, `dp`, `dq`, `qinv` and `scratch` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_rsa_public_precompute_auto(_dit: verified_garbage_cpu::Dit<'_>, pre: *mut u64, pre_len: usize, n: *const u8, n_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_rsa_check_key_auto(_dit: verified_garbage_cpu::Dit<'_>, n: *const u8, n_len: usize, e: *const u8, e_len: usize, d: *const u8, d_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa::vg_rsa_public_precompute(pre, pre_len, n, n_len, scratch, scratch_len) } + unsafe { crate::rsa::vg_rsa_check_key(n, n_len, e, e_len, d, d_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, scratch, scratch_len) } } /// The prime factors `p > q` of the modulus of an RSA private key `(n, e, d)` (SP 800-56B Rev. 2 §6.2.2's basic format), which bring it to the prime-factor format `(p, q, d)` that `vg_rsa_crt_values` takes, recovered by SP 800-56B Rev. 2 Appendix C.2's deterministic method. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes) and the private exponent `d` (`d_len` bytes), both most significant first, writes `p` and `q` (`n_len` bytes each, most significant first) and returns 1; or writes zeros to both and returns 0 if `n` is not such a modulus or the method fails. It finds the factors of a valid two-prime key whenever `K (p + q − 1) ≤ n` for `a = K φ(n)` (Appendix C.2's step 1): with the least private exponent, whenever the smaller prime exceeds `e²`, and in practice for every key whose primes are not tiny. diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/rsa_keygen.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/rsa_keygen.rs index a8eb6de7729..ccd72c662a8 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/rsa_keygen.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/rsa_keygen.rs @@ -2,6 +2,25 @@ //! The dispatchers of the verified `rsa_keygen` functions for `riscv64` //! (`crate::rsa_keygen`, `VG.Rust.dispatcher`). +/// Start generating an RSA key, as BoringSSL's `RSA_generate_key_ex` (FIPS 186-5 Appendix A.1.3), with a modulus of `bits` bits rounded down to a multiple of 128, and the public exponent `e` (`e_len` octets, most significant first, with any number of leading zeros). Returns 0 if `e` is not odd from 3 to `2^32 - 1`, or 2 if `bits` is above 8192 or, rounded down, below 512, and writes zeros to `state`. Otherwise returns 1 and writes the search's seven public words to `state`: the rejected candidates the first prime's search allows, the primes' length in octets (`nlen / 16`), the entropy budget, the length of the first random tape to give `vg_rsa_generate`, the phase (0), the generations allowed (4), and the exponent's eight octets, most significant first. +/// +/// Contract: `VG.Spec.RsaKeyGen.keyInitContract`. Timing may depend on `bits` and `e`. +/// +/// It runs `vg_rsa_generate_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 56 bytes, and aligned for `u64`. +/// * `e` must be valid for reads of `e_len` bytes. +/// * `state` must not overlap `e` (distinct Rust objects never do). +/// * Neither `state` nor `e` may wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_rsa_generate_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 7], bits: usize, e: *const u8, e_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::rsa_keygen::vg_rsa_generate_init(state, bits, e, e_len) } +} + /// Generate an RSA key, as BoringSSL's `RSA_generate_key_ex` (FIPS 186-5 Appendix A.1.3), over the random tape `rand`, from the state `state` that `vg_rsa_generate_init` started: the search for `p`, then for `q` not too close to `p`, each over candidates of `p_len` octets with their top two bits and bottom bit set, rejected by trial division, a common factor of `p − 1` and `e` or Miller–Rabin, up to BoringSSL's limit of rejected candidates; then the key, `d = e⁻¹ mod lcm(p − 1, q − 1)`, `dP`, `dQ` and `qInv`, with `p` the larger prime, checked as BoringSSL's `RSA_check_key` checks it, drawing both primes again if `d` is too small; a generation whose prime's search is exhausted is started again, up to four in all. Returns 1 and writes the key to `key` (`key_len` is `9 p_len`, for the primes' length `p_len = state[1]`): `n` and `d` (`2 p_len` octets each), then `p`, `q`, `dP`, `dQ` and `qInv` (`p_len` each), all most significant first; 2 if the key failed a check; 3 if every generation was exhausted; or 0 if the tape ran out inside a candidate. Writes the octets of `rand` it consumed to `used`, and the state for the next call to `state`, whose fourth word is, for 0, the length of the next tape, more than the octets of `rand` after the first `used` and at most the entropy budget: keep those octets, in order, and append fresh random octets up to that length. For 0, `key` holds the search's `p` once it is found, which the next call reads: pass it back unchanged. `key` is otherwise zeros but for 1. /// /// Contract: `VG.Spec.RsaKeyGen.keySearchContract`. Timing may depend on the pointers, the lengths, `state`, the status and consumption it returns, and, for each prime's search, its rejected candidates and the octets its accepted candidate's tests read, and each key's status. Not on the primes or the key. @@ -28,22 +47,3 @@ pub unsafe fn vg_rsa_generate_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *mu // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::rsa_keygen::vg_rsa_generate(key, key_len, used, rand, rand_len, scratch, scratch_len, state) } } - -/// Start generating an RSA key, as BoringSSL's `RSA_generate_key_ex` (FIPS 186-5 Appendix A.1.3), with a modulus of `bits` bits rounded down to a multiple of 128, and the public exponent `e` (`e_len` octets, most significant first, with any number of leading zeros). Returns 0 if `e` is not odd from 3 to `2^32 - 1`, or 2 if `bits` is above 8192 or, rounded down, below 512, and writes zeros to `state`. Otherwise returns 1 and writes the search's seven public words to `state`: the rejected candidates the first prime's search allows, the primes' length in octets (`nlen / 16`), the entropy budget, the length of the first random tape to give `vg_rsa_generate`, the phase (0), the generations allowed (4), and the exponent's eight octets, most significant first. -/// -/// Contract: `VG.Spec.RsaKeyGen.keyInitContract`. Timing may depend on `bits` and `e`. -/// -/// It runs `vg_rsa_generate_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 56 bytes, and aligned for `u64`. -/// * `e` must be valid for reads of `e_len` bytes. -/// * `state` must not overlap `e` (distinct Rust objects never do). -/// * Neither `state` nor `e` may wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_rsa_generate_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 7], bits: usize, e: *const u8, e_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa_keygen::vg_rsa_generate_init(state, bits, e, e_len) } -} diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/rsa_pkcs1_sig.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/rsa_pkcs1_sig.rs index aaf907810f2..bd12c2db38f 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/rsa_pkcs1_sig.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/rsa_pkcs1_sig.rs @@ -2,40 +2,6 @@ //! The dispatchers of the verified `rsa_pkcs1_sig` functions for `riscv64` //! (`crate::rsa_pkcs1_sig`, `VG.Rust.dispatcher`). -/// RSASSA-PKCS1-v1_5 signature generation (RFC 8017 §8.2.1) of a hash value, as BoringSSL's `RSA_sign` does it. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits) and the private key `(p, q, dP, dQ, qInv)` (as `vg_rsa_private_checked` takes it), encodes `digest` with EMSA-PKCS1-v1_5 (RFC 8017 §9.2: `0x00 0x01`, bytes `0xff`, `0x00` and the DER encoding of its `DigestInfo`) to `n_len` bytes and computes the signature from it as `vg_rsa_private_checked` does: writes it to `out` (`n_len` bytes, most significant first) and returns 1; writes zeros and returns 0 if `hash` names no hash function, `digest` is not as long as its values, `n_len` is less than the encoding's 11 bytes more than the `DigestInfo`, or `vg_rsa_private_checked` refuses the key; and writes zeros and returns 2 (an internal error) if the signature fails the check against `e`, which is the case for no hash value if `vg_rsa_check_key` accepts the key and `p` and `q` are prime. A signature it writes is one `vg_rsa_pkcs1_verify` accepts. `hash` names the hash function: 0 MD5, 1 SHA-1, 2 SHA-224, 3 SHA-256, 4 SHA-384, 5 SHA-512, 6 SHA-512/224, 7 SHA-512/256, 8 SHA3-224, 9 SHA3-256, 10 SHA3-384, 11 SHA3-512 (`VG.Spec.RsaPkcs1Sig.Hash.ofId`). -/// -/// Contract: `VG.Spec.RsaPkcs1Sig.signContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, `hash` and the contents of `n` and `e`, not on `digest` or the private key. -/// -/// It runs `vg_rsa_pkcs1_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `out` must be valid for reads and writes of `out_len` bytes. -/// * `n` must be valid for reads of `n_len` bytes. -/// * `e` must be valid for reads of `e_len` bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `p` must be valid for reads of `p_len` bytes. -/// * `q` must be valid for reads of `q_len` bytes. -/// * `dp` must be valid for reads of `dp_len` bytes. -/// * `dq` must be valid for reads of `dq_len` bytes. -/// * `qinv` must be valid for reads of `qinv_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes, and aligned for `u64`. -/// * `n_len` must be in 64..=1024. -/// * `out_len` must be `n_len`. -/// * `e_len` must be in 1..=`n_len`. -/// * `p_len` and `q_len` must be in 1..`n_len`. -/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. -/// * `scratch_len` must be at least `16 * n_len + 32`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `n`, `e`, `digest`, `p`, `q`, `dp`, `dq`, `qinv` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `n`, `e`, `digest`, `p`, `q`, `dp`, `dq`, `qinv` and `scratch` may overlap the 4416 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_rsa_pkcs1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, n: *const u8, n_len: usize, e: *const u8, e_len: usize, hash: u32, digest: *const u8, digest_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa_pkcs1_sig::vg_rsa_pkcs1_sign(out, out_len, n, n_len, e, e_len, hash, digest, digest_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, scratch, scratch_len) } -} - /// RSASSA-PKCS1-v1_5 signature verification (RFC 8017 §8.2.2) of a hash value, as BoringSSL's `RSA_verify` does it. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero) and the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits), returns 1 if `sig` is `n_len` bytes, the integer `s` of `sig` (most significant byte first) is below `n`, and `s^e mod n` (as `n_len` bytes) is `0x00 0x01`, at least 8 bytes `0xff`, `0x00` and then exactly the DER encoding of the `DigestInfo` of `digest` (RFC 8017 §9.2 note 1), which must be as long as the hash function's values; and 0 otherwise, or if `n` or `e` is not such a number or `hash` names no hash function. That is, exactly when `s^e mod n` is EMSA-PKCS1-v1_5's encoding of `digest`: the signed `DigestInfo` is compared, never parsed. `hash` names the hash function: 0 MD5, 1 SHA-1, 2 SHA-224, 3 SHA-256, 4 SHA-384, 5 SHA-512, 6 SHA-512/224, 7 SHA-512/256, 8 SHA3-224, 9 SHA3-256, 10 SHA3-384, 11 SHA3-512 (`VG.Spec.RsaPkcs1Sig.Hash.ofId`). /// /// Contract: `VG.Spec.RsaPkcs1Sig.verifyContract`. Not constant time: timing may depend on the pointers, the lengths, `hash` and the contents of `n`, `e`, `digest` and `sig`. @@ -102,3 +68,37 @@ pub unsafe fn vg_rsa_pkcs1_recover_precomputed_auto(_dit: verified_garbage_cpu:: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::rsa_pkcs1_sig::vg_rsa_pkcs1_recover_precomputed(out, out_len, n, n_len, e, e_len, hash, sig, sig_len, scratch, scratch_len, pre, pre_len) } } + +/// RSASSA-PKCS1-v1_5 signature generation (RFC 8017 §8.2.1) of a hash value, as BoringSSL's `RSA_sign` does it. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits) and the private key `(p, q, dP, dQ, qInv)` (as `vg_rsa_private_checked` takes it), encodes `digest` with EMSA-PKCS1-v1_5 (RFC 8017 §9.2: `0x00 0x01`, bytes `0xff`, `0x00` and the DER encoding of its `DigestInfo`) to `n_len` bytes and computes the signature from it as `vg_rsa_private_checked` does: writes it to `out` (`n_len` bytes, most significant first) and returns 1; writes zeros and returns 0 if `hash` names no hash function, `digest` is not as long as its values, `n_len` is less than the encoding's 11 bytes more than the `DigestInfo`, or `vg_rsa_private_checked` refuses the key; and writes zeros and returns 2 (an internal error) if the signature fails the check against `e`, which is the case for no hash value if `vg_rsa_check_key` accepts the key and `p` and `q` are prime. A signature it writes is one `vg_rsa_pkcs1_verify` accepts. `hash` names the hash function: 0 MD5, 1 SHA-1, 2 SHA-224, 3 SHA-256, 4 SHA-384, 5 SHA-512, 6 SHA-512/224, 7 SHA-512/256, 8 SHA3-224, 9 SHA3-256, 10 SHA3-384, 11 SHA3-512 (`VG.Spec.RsaPkcs1Sig.Hash.ofId`). +/// +/// Contract: `VG.Spec.RsaPkcs1Sig.signContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, `hash` and the contents of `n` and `e`, not on `digest` or the private key. +/// +/// It runs `vg_rsa_pkcs1_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `out` must be valid for reads and writes of `out_len` bytes. +/// * `n` must be valid for reads of `n_len` bytes. +/// * `e` must be valid for reads of `e_len` bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `p` must be valid for reads of `p_len` bytes. +/// * `q` must be valid for reads of `q_len` bytes. +/// * `dp` must be valid for reads of `dp_len` bytes. +/// * `dq` must be valid for reads of `dq_len` bytes. +/// * `qinv` must be valid for reads of `qinv_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes, and aligned for `u64`. +/// * `n_len` must be in 64..=1024. +/// * `out_len` must be `n_len`. +/// * `e_len` must be in 1..=`n_len`. +/// * `p_len` and `q_len` must be in 1..`n_len`. +/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. +/// * `scratch_len` must be at least `16 * n_len + 32`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `out` and `scratch` must not overlap each other, `n`, `e`, `digest`, `p`, `q`, `dp`, `dq`, `qinv` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `n`, `e`, `digest`, `p`, `q`, `dp`, `dq`, `qinv` and `scratch` may overlap the 4416 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_rsa_pkcs1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, n: *const u8, n_len: usize, e: *const u8, e_len: usize, hash: u32, digest: *const u8, digest_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::rsa_pkcs1_sig::vg_rsa_pkcs1_sign(out, out_len, n, n_len, e, e_len, hash, digest, digest_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, scratch, scratch_len) } +} diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/rsa_pss.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/rsa_pss.rs index 58fd54096a5..1635ae78c11 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/rsa_pss.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/rsa_pss.rs @@ -2,81 +2,81 @@ //! The dispatchers of the verified `rsa_pss` functions for `riscv64` //! (`crate::rsa_pss`, `VG.Rust.dispatcher`). -/// RSASSA-PSS signature generation (RFC 8017 §8.1.1) with the hash function of the table `hash` and MGF1 with that of the table `mgf1`, of a digest of the former, with the private key `(p, q, dP, dQ, qInv)` checked against the public exponent as `vg_rsa_private_checked` checks it. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero) and the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits), encodes the `digest_len`-byte digest `digest` with the salt `salt` (EMSA-PSS, RFC 8017 §9.1.1) and computes RSASP1 of the encoding: if it passes the check against `e`, writes the signature (`n_len` bytes, most significant first) to `out` and returns 1. Writes zeros and returns 0 if the salt does not fit (`salt_len` more than `⌈(bits of n - 1) / 8⌉ - digest_len - 2`), or the key is refused as `vg_rsa_private_checked` refuses it; writes zeros and returns 2 (an internal error) if the result fails the check against `e`, which it does for no digest if `vg_rsa_check_key` accepts the key and `p` and `q` are prime. The salt must be fresh random bytes for each signature (or empty). +/// RSASSA-PSS signature verification (RFC 8017 §8.1.2) with the hash function of the table `hash` and MGF1 with that of the table `mgf1`, of a signature of a digest of the former. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits) and `pre`, the `pre_len` words written by `vg_rsa_public_precompute` for `n` (returning 1), returns 1 if `sig` (`n_len` bytes, most significant first) is an RSASSA-PSS signature of the `digest_len`-byte digest `digest` with a salt of `salt_len` bytes, or of any length if `any_salt_len` is not zero (BoringSSL's `RSA_PSS_SALTLEN_AUTO`); and 0 if it is not, or if `n` is not such a modulus, `e` is not such a number, or `sig` is not below `n`. If `pre` does not hold those values, the result is unspecified; memory safety and the timing guarantee still apply. /// -/// Contract: `VG.Spec.RsaPss.signTableContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, the contents of `n` and `e`, and which tables `hash` and `mgf1` are, not on the private key, the digest or the salt. +/// Contract: `VG.Spec.RsaPss.verifyPrecomputedTableContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, `salt_len`, `any_salt_len`, the contents of `n`, `e` and `pre`, and which tables `hash` and `mgf1` are, not on the digest, the signature or anything computed from them but the result. /// -/// It runs `vg_rsa_pss_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_rsa_pss_verify_precomputed`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `out` must be valid for reads and writes of `out_len` bytes. /// * `n` must be valid for reads of `n_len` bytes. /// * `e` must be valid for reads of `e_len` bytes. -/// * `p` must be valid for reads of `p_len` bytes. -/// * `q` must be valid for reads of `q_len` bytes. -/// * `dp` must be valid for reads of `dp_len` bytes. -/// * `dq` must be valid for reads of `dq_len` bytes. -/// * `qinv` must be valid for reads of `qinv_len` bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `salt` must be valid for reads of `salt_len` bytes. +/// * `sig` must be valid for reads of `sig_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes, and aligned for `u64`. +/// * `pre` must be valid for reads of `8 * pre_len` bytes, and aligned for `u64`. /// * `hash` must be valid for reads of `11 * size_of::()` bytes. /// * `mgf1` must be valid for reads of `5 * size_of::()` bytes. /// * `n_len` must be in 64..=1024. -/// * `out_len` must be `n_len`. +/// * `sig_len` must be `n_len`. /// * `e_len` must be in 1..=`n_len`. -/// * `p_len` and `q_len` must be in 1..`n_len`. -/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. -/// * `digest_len` must be the digest length of the hash function `hash` names. -/// * `scratch_len` must be at least `16 * n_len + 1056`. +/// * `scratch_len` must be at least `8 * ⌈n_len / 8⌉ + 1072`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `digest_len` must be the digest length of the hash function `hash` names. +/// * `pre_len` must be `2 * ⌈n_len / 8⌉`. +/// * For the result to be signature verification's, `pre` must hold what `vg_rsa_public_precompute` wrote for `n` (returning 1). /// * `hash` names the hash function of the digest and of the encoding: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the hash functions' `MdHash` tables of this crate, `fn_tables`, say which they name). /// * `mgf1` names MGF1's hash function: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the hash functions' tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `n`, `e`, `p`, `q`, `dp`, `dq`, `qinv`, `digest`, `salt`, `hash`, `mgf1` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `n`, `e`, `p`, `q`, `dp`, `dq`, `qinv`, `digest`, `salt`, `scratch`, `hash` and `mgf1` may overlap the 3472 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `scratch` must not overlap `n`, `e`, `digest`, `sig`, `pre`, `hash`, `mgf1` or the arguments on the stack (distinct Rust objects never do). +/// * None of `n`, `e`, `digest`, `sig`, `scratch`, `pre`, `hash` and `mgf1` may overlap the 336 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). /// * `hash` must be the `as_ptr()` of one of the `MdHash` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. /// * `mgf1` must be the `as_ptr()` of one of the `Hash` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_rsa_pss_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, n: *const u8, n_len: usize, e: *const u8, e_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, digest: *const u8, digest_len: usize, salt: *const u8, salt_len: usize, scratch: *mut u64, scratch_len: usize, hash: *const [usize; 11], mgf1: *const [usize; 5]) -> u32 { +pub unsafe fn vg_rsa_pss_verify_precomputed_auto(_dit: verified_garbage_cpu::Dit<'_>, n: *const u8, n_len: usize, e: *const u8, e_len: usize, digest: *const u8, digest_len: usize, sig: *const u8, sig_len: usize, salt_len: usize, any_salt_len: u32, scratch: *mut u64, scratch_len: usize, pre: *const u64, pre_len: usize, hash: *const [usize; 11], mgf1: *const [usize; 5]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa_pss::vg_rsa_pss_sign(out, out_len, n, n_len, e, e_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, digest, digest_len, salt, salt_len, scratch, scratch_len, hash, mgf1) } + unsafe { crate::rsa_pss::vg_rsa_pss_verify_precomputed(n, n_len, e, e_len, digest, digest_len, sig, sig_len, salt_len, any_salt_len, scratch, scratch_len, pre, pre_len, hash, mgf1) } } -/// RSASSA-PSS signature verification (RFC 8017 §8.1.2) with the hash function of the table `hash` and MGF1 with that of the table `mgf1`, of a signature of a digest of the former. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits) and `pre`, the `pre_len` words written by `vg_rsa_public_precompute` for `n` (returning 1), returns 1 if `sig` (`n_len` bytes, most significant first) is an RSASSA-PSS signature of the `digest_len`-byte digest `digest` with a salt of `salt_len` bytes, or of any length if `any_salt_len` is not zero (BoringSSL's `RSA_PSS_SALTLEN_AUTO`); and 0 if it is not, or if `n` is not such a modulus, `e` is not such a number, or `sig` is not below `n`. If `pre` does not hold those values, the result is unspecified; memory safety and the timing guarantee still apply. +/// RSASSA-PSS signature generation (RFC 8017 §8.1.1) with the hash function of the table `hash` and MGF1 with that of the table `mgf1`, of a digest of the former, with the private key `(p, q, dP, dQ, qInv)` checked against the public exponent as `vg_rsa_private_checked` checks it. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero) and the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits), encodes the `digest_len`-byte digest `digest` with the salt `salt` (EMSA-PSS, RFC 8017 §9.1.1) and computes RSASP1 of the encoding: if it passes the check against `e`, writes the signature (`n_len` bytes, most significant first) to `out` and returns 1. Writes zeros and returns 0 if the salt does not fit (`salt_len` more than `⌈(bits of n - 1) / 8⌉ - digest_len - 2`), or the key is refused as `vg_rsa_private_checked` refuses it; writes zeros and returns 2 (an internal error) if the result fails the check against `e`, which it does for no digest if `vg_rsa_check_key` accepts the key and `p` and `q` are prime. The salt must be fresh random bytes for each signature (or empty). /// -/// Contract: `VG.Spec.RsaPss.verifyPrecomputedTableContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, `salt_len`, `any_salt_len`, the contents of `n`, `e` and `pre`, and which tables `hash` and `mgf1` are, not on the digest, the signature or anything computed from them but the result. +/// Contract: `VG.Spec.RsaPss.signTableContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, the contents of `n` and `e`, and which tables `hash` and `mgf1` are, not on the private key, the digest or the salt. /// -/// It runs `vg_rsa_pss_verify_precomputed`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_rsa_pss_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// +/// * `out` must be valid for reads and writes of `out_len` bytes. /// * `n` must be valid for reads of `n_len` bytes. /// * `e` must be valid for reads of `e_len` bytes. +/// * `p` must be valid for reads of `p_len` bytes. +/// * `q` must be valid for reads of `q_len` bytes. +/// * `dp` must be valid for reads of `dp_len` bytes. +/// * `dq` must be valid for reads of `dq_len` bytes. +/// * `qinv` must be valid for reads of `qinv_len` bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of `sig_len` bytes. +/// * `salt` must be valid for reads of `salt_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes, and aligned for `u64`. -/// * `pre` must be valid for reads of `8 * pre_len` bytes, and aligned for `u64`. /// * `hash` must be valid for reads of `11 * size_of::()` bytes. /// * `mgf1` must be valid for reads of `5 * size_of::()` bytes. /// * `n_len` must be in 64..=1024. -/// * `sig_len` must be `n_len`. +/// * `out_len` must be `n_len`. /// * `e_len` must be in 1..=`n_len`. -/// * `scratch_len` must be at least `8 * ⌈n_len / 8⌉ + 1072`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `p_len` and `q_len` must be in 1..`n_len`. +/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. /// * `digest_len` must be the digest length of the hash function `hash` names. -/// * `pre_len` must be `2 * ⌈n_len / 8⌉`. -/// * For the result to be signature verification's, `pre` must hold what `vg_rsa_public_precompute` wrote for `n` (returning 1). +/// * `scratch_len` must be at least `16 * n_len + 1056`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. /// * `hash` names the hash function of the digest and of the encoding: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the hash functions' `MdHash` tables of this crate, `fn_tables`, say which they name). /// * `mgf1` names MGF1's hash function: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the hash functions' tables of this crate, `fn_tables`, say which they name). -/// * `scratch` must not overlap `n`, `e`, `digest`, `sig`, `pre`, `hash`, `mgf1` or the arguments on the stack (distinct Rust objects never do). -/// * None of `n`, `e`, `digest`, `sig`, `scratch`, `pre`, `hash` and `mgf1` may overlap the 336 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `n`, `e`, `p`, `q`, `dp`, `dq`, `qinv`, `digest`, `salt`, `hash`, `mgf1` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `n`, `e`, `p`, `q`, `dp`, `dq`, `qinv`, `digest`, `salt`, `scratch`, `hash` and `mgf1` may overlap the 3472 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). /// * `hash` must be the `as_ptr()` of one of the `MdHash` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. /// * `mgf1` must be the `as_ptr()` of one of the `Hash` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_rsa_pss_verify_precomputed_auto(_dit: verified_garbage_cpu::Dit<'_>, n: *const u8, n_len: usize, e: *const u8, e_len: usize, digest: *const u8, digest_len: usize, sig: *const u8, sig_len: usize, salt_len: usize, any_salt_len: u32, scratch: *mut u64, scratch_len: usize, pre: *const u64, pre_len: usize, hash: *const [usize; 11], mgf1: *const [usize; 5]) -> u32 { +pub unsafe fn vg_rsa_pss_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, n: *const u8, n_len: usize, e: *const u8, e_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, digest: *const u8, digest_len: usize, salt: *const u8, salt_len: usize, scratch: *mut u64, scratch_len: usize, hash: *const [usize; 11], mgf1: *const [usize; 5]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa_pss::vg_rsa_pss_verify_precomputed(n, n_len, e, e_len, digest, digest_len, sig, sig_len, salt_len, any_salt_len, scratch, scratch_len, pre, pre_len, hash, mgf1) } + unsafe { crate::rsa_pss::vg_rsa_pss_sign(out, out_len, n, n_len, e, e_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, digest, digest_len, salt, salt_len, scratch, scratch_len, hash, mgf1) } } diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/sha1.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/sha1.rs index 6a263d9995f..a6bf1d8cdc8 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/sha1.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/sha1.rs @@ -38,41 +38,41 @@ pub unsafe fn vg_sha1_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *m unsafe { crate::sha1::vg_sha1_update(state, count, data, len) } } -/// Computes the SHA-1 digest of the `len` bytes at `data` (20 bytes, `VG.Spec.Sha1.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-1 computation with that message. +/// Finishes a SHA-1 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), writes the SHA-1 digest of that message to `*out`. /// -/// Contract: `VG.Spec.Sha1.digestContract`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha1.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha1_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha1_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `data` must be valid for reads of `len` bytes. +/// * `state` must be valid for reads and writes of 84 bytes. /// * `out` must be valid for reads and writes of 20 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the 288 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 144 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha1_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 20]) { +pub unsafe fn vg_sha1_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 84], count: u64, out: *mut [u8; 20]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha1::vg_sha1_digest(data, len, out) } + unsafe { crate::sha1::vg_sha1_finalize(state, count, out) } } -/// Finishes a SHA-1 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), writes the SHA-1 digest of that message to `*out`. +/// Computes the SHA-1 digest of the `len` bytes at `data` (20 bytes, `VG.Spec.Sha1.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-1 computation with that message. /// -/// Contract: `VG.Spec.Sha1.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha1.digestContract`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha1_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha1_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 84 bytes. +/// * `data` must be valid for reads of `len` bytes. /// * `out` must be valid for reads and writes of 20 bytes. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 144 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the 288 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha1_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 84], count: u64, out: *mut [u8; 20]) { +pub unsafe fn vg_sha1_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 20]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha1::vg_sha1_finalize(state, count, out) } + unsafe { crate::sha1::vg_sha1_digest(data, len, out) } } diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/sha256.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/sha256.rs index 0e29a624a4f..3c05f2d66ba 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/sha256.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/sha256.rs @@ -19,21 +19,24 @@ pub unsafe fn vg_sha224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *m unsafe { crate::sha256::vg_sha224_init(state) } } -/// Starts a SHA-256 computation: makes the streaming state `*state` represent the empty message. +/// Finishes a SHA-224 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), hashed from the initial hash value of SHA-224 (as `vg_sha224_init` starts it), writes the SHA-224 digest of that message (28 bytes, `VG.Spec.Sha256.sha224`) to `*out`. /// -/// Contract: `VG.Spec.Sha256.initContract`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha256.Repr`). +/// Contract: `VG.Spec.Sha256.finalize224Contract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha256_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 96 bytes. -/// * `state` must not wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 28 bytes. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 144 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96]) { +pub unsafe fn vg_sha224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, out: *mut [u8; 28]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha256::vg_sha256_init(state) } + unsafe { crate::sha256::vg_sha224_finalize(state, count, out) } } /// Computes the SHA-224 digest of the `len` bytes at `data` (28 bytes, `VG.Spec.Sha256.sha224`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-224 computation with that message. @@ -55,80 +58,77 @@ pub unsafe fn vg_sha224_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: * unsafe { crate::sha256::vg_sha224_digest(data, len, out) } } -/// Absorbs data into a SHA-224 or SHA-256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), it then represents that message followed by the `len` bytes at `data`. +/// Starts a SHA-256 computation: makes the streaming state `*state` represent the empty message. /// -/// Contract: `VG.Spec.Sha256.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha256.initContract`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha256.Repr`). /// -/// It runs `vg_sha256_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha256_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 96 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `state` must not overlap `data` (distinct Rust objects never do). -/// * Neither `state` nor `data` may overlap the 144 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must not wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, data: *const u8, len: usize) { +pub unsafe fn vg_sha256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha256::vg_sha256_update(state, count, data, len) } + unsafe { crate::sha256::vg_sha256_init(state) } } -/// Computes the SHA-256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha256.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-256 computation with that message. +/// Absorbs data into a SHA-224 or SHA-256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), it then represents that message followed by the `len` bytes at `data`. /// -/// Contract: `VG.Spec.Sha256.digestContract` for `VG.Spec.Sha256.hash`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha256.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_sha256_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha256_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// +/// * `state` must be valid for reads and writes of 96 bytes. /// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 32 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the 288 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must not overlap `data` (distinct Rust objects never do). +/// * Neither `state` nor `data` may overlap the 144 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { +pub unsafe fn vg_sha256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, data: *const u8, len: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha256::vg_sha256_digest(data, len, out) } + unsafe { crate::sha256::vg_sha256_update(state, count, data, len) } } -/// Finishes a SHA-224 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), hashed from the initial hash value of SHA-224 (as `vg_sha224_init` starts it), writes the SHA-224 digest of that message (28 bytes, `VG.Spec.Sha256.sha224`) to `*out`. +/// Finishes a SHA-224 or SHA-256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (32 bytes) to `*out`. The SHA-256 digest is all of it; the SHA-224 digest is its first 28 bytes. /// -/// Contract: `VG.Spec.Sha256.finalize224Contract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha256.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 96 bytes. -/// * `out` must be valid for reads and writes of 28 bytes. +/// * `out` must be valid for reads and writes of 32 bytes. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the 144 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, out: *mut [u8; 28]) { +pub unsafe fn vg_sha256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, out: *mut [u8; 32]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha256::vg_sha224_finalize(state, count, out) } + unsafe { crate::sha256::vg_sha256_finalize(state, count, out) } } -/// Finishes a SHA-224 or SHA-256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (32 bytes) to `*out`. The SHA-256 digest is all of it; the SHA-224 digest is its first 28 bytes. +/// Computes the SHA-256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha256.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-256 computation with that message. /// -/// Contract: `VG.Spec.Sha256.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha256.digestContract` for `VG.Spec.Sha256.hash`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha256_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 96 bytes. +/// * `data` must be valid for reads of `len` bytes. /// * `out` must be valid for reads and writes of 32 bytes. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 144 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the 288 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, out: *mut [u8; 32]) { +pub unsafe fn vg_sha256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha256::vg_sha256_finalize(state, count, out) } + unsafe { crate::sha256::vg_sha256_digest(data, len, out) } } diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/sha3.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/sha3.rs index 8f5c041e60b..58072d7422a 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/sha3.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/sha3.rs @@ -24,250 +24,250 @@ pub unsafe fn vg_sha3_224_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state unsafe { crate::sha3::vg_sha3_224_update(state, pos, data, len, scratch) } } -/// Absorbs data into a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, 136), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 136`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Finishes a SHA3-224 computation: if the state `*state` represents a message whose length is `pos` modulo 144 (`VG.Spec.Sha3.Repr` with SHA3-224's rate, as `vg_sha3_224_update` leaves it), writes the SHA3-224 digest of that message (28 bytes, `VG.Spec.Sha3.sha3_224`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 136. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_224`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs `vg_sha3_256_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes, and aligned for `u64`. -/// * `data` must be valid for reads of `len` bytes. -/// * `scratch` must be valid for reads and writes of 640 bytes, and aligned for `u64`. -/// * `pos` must be less than 136. -/// * The contents of `scratch` on return are unspecified. -/// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). -/// * None of `state`, `data` and `scratch` may overlap the 608 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 28 bytes. +/// * `pos` must be less than 144. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 1200 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 28]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_256_update(state, pos, data, len, scratch) } + unsafe { crate::sha3::vg_sha3_224_finalize(state, pos, out) } } -/// Absorbs data into a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, 104), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 104`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Absorbs data into a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, 136), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 136`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 104. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 136. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_sha3_384_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_256_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes, and aligned for `u64`. /// * `data` must be valid for reads of `len` bytes. /// * `scratch` must be valid for reads and writes of 640 bytes, and aligned for `u64`. -/// * `pos` must be less than 104. +/// * `pos` must be less than 136. /// * The contents of `scratch` on return are unspecified. /// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). /// * None of `state`, `data` and `scratch` may overlap the 608 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_384_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_384_update(state, pos, data, len, scratch) } + unsafe { crate::sha3::vg_sha3_256_update(state, pos, data, len, scratch) } } -/// Absorbs data into a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, 72), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 72`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Finishes a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, as `vg_sha3_256_update` leaves it), writes the SHA3-256 digest of that message (32 bytes, `VG.Spec.Sha3.sha3_256`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 72. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_256`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs `vg_sha3_512_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes, and aligned for `u64`. -/// * `data` must be valid for reads of `len` bytes. -/// * `scratch` must be valid for reads and writes of 640 bytes, and aligned for `u64`. -/// * `pos` must be less than 72. -/// * The contents of `scratch` on return are unspecified. -/// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). -/// * None of `state`, `data` and `scratch` may overlap the 608 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 32 bytes. +/// * `pos` must be less than 136. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 1200 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 32]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_512_update(state, pos, data, len, scratch) } + unsafe { crate::sha3::vg_sha3_256_finalize(state, pos, out) } } -/// Absorbs data into a SHAKE128 computation: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, 168), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 168`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Absorbs data into a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, 104), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 104`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 168. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 104. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_shake128_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_384_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes, and aligned for `u64`. /// * `data` must be valid for reads of `len` bytes. /// * `scratch` must be valid for reads and writes of 640 bytes, and aligned for `u64`. -/// * `pos` must be less than 168. +/// * `pos` must be less than 104. /// * The contents of `scratch` on return are unspecified. /// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). /// * None of `state`, `data` and `scratch` may overlap the 608 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake128_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_384_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake128_update(state, pos, data, len, scratch) } + unsafe { crate::sha3::vg_sha3_384_update(state, pos, data, len, scratch) } } -/// Squeezes SHAKE128 output: if the state `*state`, from position `pos`, continues the output of SHAKE128 of a message (`VG.Spec.Sha3.shake128`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake128_finalize` leaves and position 0. +/// Finishes a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, as `vg_sha3_384_update` leaves it), writes the SHA3-384 digest of that message (48 bytes, `VG.Spec.Sha3.sha3_384`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_384`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs `vg_shake128_squeeze`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_384_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes, and aligned for `u64`. -/// * `out` must be valid for reads and writes of `outlen` bytes. -/// * `pos` must be at most 168. +/// * `out` must be valid for reads and writes of 48 bytes. +/// * `pos` must be less than 104. +/// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 1168 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * Neither `state` nor `out` may overlap the 1200 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake128_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { +pub unsafe fn vg_sha3_384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 48]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake128_squeeze(state, pos, out, outlen) } + unsafe { crate::sha3::vg_sha3_384_finalize(state, pos, out) } } -/// Squeezes SHAKE256 output: if the state `*state`, from position `pos`, continues the output of SHAKE256 of a message (`VG.Spec.Sha3.shake256`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake256_finalize` leaves and position 0. +/// Absorbs data into a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, 72), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 72`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 72. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_shake256_squeeze`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_512_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes, and aligned for `u64`. -/// * `out` must be valid for reads and writes of `outlen` bytes. -/// * `pos` must be at most 136. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 1168 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `scratch` must be valid for reads and writes of 640 bytes, and aligned for `u64`. +/// * `pos` must be less than 72. +/// * The contents of `scratch` on return are unspecified. +/// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). +/// * None of `state`, `data` and `scratch` may overlap the 608 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake256_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { +pub unsafe fn vg_sha3_512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake256_squeeze(state, pos, out, outlen) } + unsafe { crate::sha3::vg_sha3_512_update(state, pos, data, len, scratch) } } -/// Finishes a SHA3-224 computation: if the state `*state` represents a message whose length is `pos` modulo 144 (`VG.Spec.Sha3.Repr` with SHA3-224's rate, as `vg_sha3_224_update` leaves it), writes the SHA3-224 digest of that message (28 bytes, `VG.Spec.Sha3.sha3_224`) to `*out`. +/// Finishes a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, as `vg_sha3_512_update` leaves it), writes the SHA3-512 digest of that message (64 bytes, `VG.Spec.Sha3.sha3_512`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_224`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_512`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs `vg_sha3_224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_512_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes, and aligned for `u64`. -/// * `out` must be valid for reads and writes of 28 bytes. -/// * `pos` must be less than 144. +/// * `out` must be valid for reads and writes of 64 bytes. +/// * `pos` must be less than 72. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the 1200 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 28]) { +pub unsafe fn vg_sha3_512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 64]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_224_finalize(state, pos, out) } + unsafe { crate::sha3::vg_sha3_512_finalize(state, pos, out) } } -/// Finishes a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, as `vg_sha3_256_update` leaves it), writes the SHA3-256 digest of that message (32 bytes, `VG.Spec.Sha3.sha3_256`) to `*out`. +/// Absorbs data into a SHAKE128 computation: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, 168), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 168`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_256`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 168. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_sha3_256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake128_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes, and aligned for `u64`. -/// * `out` must be valid for reads and writes of 32 bytes. -/// * `pos` must be less than 136. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 1200 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `scratch` must be valid for reads and writes of 640 bytes, and aligned for `u64`. +/// * `pos` must be less than 168. +/// * The contents of `scratch` on return are unspecified. +/// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). +/// * None of `state`, `data` and `scratch` may overlap the 608 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 32]) { +pub unsafe fn vg_shake128_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_256_finalize(state, pos, out) } + unsafe { crate::sha3::vg_shake128_update(state, pos, data, len, scratch) } } -/// Finishes a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, as `vg_sha3_384_update` leaves it), writes the SHA3-384 digest of that message (48 bytes, `VG.Spec.Sha3.sha3_384`) to `*out`. +/// Finishes absorbing a SHAKE128 message: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, as `vg_shake128_update` leaves it), it becomes a state that, squeezed by `vg_shake128_squeeze` from position 0, gives the output of SHAKE128 of that message (`VG.Spec.Sha3.shake128`) from its first byte on (`VG.Spec.Sha3.XofRepr`). /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_384`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointer and `pos` may affect timing, not the state. /// -/// It runs `vg_sha3_384_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake128_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes, and aligned for `u64`. -/// * `out` must be valid for reads and writes of 48 bytes. -/// * `pos` must be less than 104. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 1200 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `pos` must be less than 168. +/// * `state` must not overlap the 1136 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 48]) { +pub unsafe fn vg_shake128_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_384_finalize(state, pos, out) } + unsafe { crate::sha3::vg_shake128_finalize(state, pos) } } -/// Finishes a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, as `vg_sha3_512_update` leaves it), writes the SHA3-512 digest of that message (64 bytes, `VG.Spec.Sha3.sha3_512`) to `*out`. +/// Squeezes SHAKE128 output: if the state `*state`, from position `pos`, continues the output of SHAKE128 of a message (`VG.Spec.Sha3.shake128`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake128_finalize` leaves and position 0. /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_512`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. /// -/// It runs `vg_sha3_512_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake128_squeeze`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes, and aligned for `u64`. -/// * `out` must be valid for reads and writes of 64 bytes. -/// * `pos` must be less than 72. -/// * The contents of `state` on return are unspecified. +/// * `out` must be valid for reads and writes of `outlen` bytes. +/// * `pos` must be at most 168. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 1200 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * Neither `state` nor `out` may overlap the 1168 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 64]) { +pub unsafe fn vg_shake128_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_512_finalize(state, pos, out) } + unsafe { crate::sha3::vg_shake128_squeeze(state, pos, out, outlen) } } -/// Finishes absorbing a SHAKE128 message: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, as `vg_shake128_update` leaves it), it becomes a state that, squeezed by `vg_shake128_squeeze` from position 0, gives the output of SHAKE128 of that message (`VG.Spec.Sha3.shake128`) from its first byte on (`VG.Spec.Sha3.XofRepr`). +/// Finishes absorbing a SHAKE256 message: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHAKE256's rate, as `vg_sha3_256_update` leaves it), it becomes a state that, squeezed by `vg_shake256_squeeze` from position 0, gives the output of SHAKE256 of that message (`VG.Spec.Sha3.shake256`) from its first byte on (`VG.Spec.Sha3.XofRepr`). /// -/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointer and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointer and `pos` may affect timing, not the state. /// -/// It runs `vg_shake128_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes, and aligned for `u64`. -/// * `pos` must be less than 168. +/// * `pos` must be less than 136. /// * `state` must not overlap the 1136 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake128_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { +pub unsafe fn vg_shake256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake128_finalize(state, pos) } + unsafe { crate::sha3::vg_shake256_finalize(state, pos) } } -/// Finishes absorbing a SHAKE256 message: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHAKE256's rate, as `vg_sha3_256_update` leaves it), it becomes a state that, squeezed by `vg_shake256_squeeze` from position 0, gives the output of SHAKE256 of that message (`VG.Spec.Sha3.shake256`) from its first byte on (`VG.Spec.Sha3.XofRepr`). +/// Squeezes SHAKE256 output: if the state `*state`, from position `pos`, continues the output of SHAKE256 of a message (`VG.Spec.Sha3.shake256`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake256_finalize` leaves and position 0. /// -/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointer and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. /// -/// It runs `vg_shake256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake256_squeeze`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes, and aligned for `u64`. -/// * `pos` must be less than 136. -/// * `state` must not overlap the 1136 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of `outlen` bytes. +/// * `pos` must be at most 136. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 1168 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { +pub unsafe fn vg_shake256_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake256_finalize(state, pos) } + unsafe { crate::sha3::vg_shake256_squeeze(state, pos, out, outlen) } } diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/sha512.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/sha512.rs index a3dcb38c967..c295971c0c3 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/sha512.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/sha512.rs @@ -36,215 +36,215 @@ pub unsafe fn vg_sha512_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *m unsafe { crate::sha512::vg_sha512_init(state) } } -/// Absorbs data into a SHA-384, SHA-512, SHA-512/224 or SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), it then represents that message followed by the `len` bytes at `data`. +/// Starts a SHA-512/224 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/224 (`VG.Spec.Sha512.H0_512_224`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. /// -/// Contract: `VG.Spec.Sha512.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_224`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). /// -/// It runs `vg_sha512_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_224_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `state` must not overlap `data` (distinct Rust objects never do). -/// * Neither `state` nor `data` may overlap the 208 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must not wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, data: *const u8, len: usize) { +pub unsafe fn vg_sha512_224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_update(state, count, data, len) } + unsafe { crate::sha512::vg_sha512_224_init(state) } } -/// Computes the SHA-384 digest of the `len` bytes at `data` (48 bytes, `VG.Spec.Sha512.sha384`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-384 computation with that message. +/// Starts a SHA-512/256 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/256 (`VG.Spec.Sha512.H0_512_256`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. /// -/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_256`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). /// -/// It runs `vg_sha384_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_256_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 48 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the 448 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must be valid for reads and writes of 192 bytes. +/// * `state` must not wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha384_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 48]) { +pub unsafe fn vg_sha512_256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha384_digest(data, len, out) } + unsafe { crate::sha512::vg_sha512_256_init(state) } } -/// Computes the SHA-512 digest of the `len` bytes at `data` (64 bytes, `VG.Spec.Sha512.sha512`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512 computation with that message. +/// Absorbs data into a SHA-384, SHA-512, SHA-512/224 or SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), it then represents that message followed by the `len` bytes at `data`. /// -/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha512.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_sha512_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// +/// * `state` must be valid for reads and writes of 192 bytes. /// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 64 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the 448 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `state` must not overlap `data` (distinct Rust objects never do). +/// * Neither `state` nor `data` may overlap the 208 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 64]) { +pub unsafe fn vg_sha512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, data: *const u8, len: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_digest(data, len, out) } + unsafe { crate::sha512::vg_sha512_update(state, count, data, len) } } -/// Starts a SHA-512/224 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/224 (`VG.Spec.Sha512.H0_512_224`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. +/// Finishes a SHA-384, SHA-512, SHA-512/224 or SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (64 bytes) to `*out`. The SHA-512 digest is all of it; the SHA-384, SHA-512/224 and SHA-512/256 digests are its first 48, 28 and 32 bytes. /// -/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_224`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). +/// Contract: `VG.Spec.Sha512.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha512_224_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `state` must not wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 64 bytes. +/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 208 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { +pub unsafe fn vg_sha512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 64]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_224_init(state) } + unsafe { crate::sha512::vg_sha512_finalize(state, count, out) } } -/// Starts a SHA-512/256 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/256 (`VG.Spec.Sha512.H0_512_256`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. +/// Finishes a SHA-384 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-384 (as `vg_sha384_init` starts it), writes the SHA-384 digest of that message (48 bytes, `VG.Spec.Sha512.sha384`) to `*out`. /// -/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_256`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). +/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_384` and `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha512_256_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha384_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `state` must not wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 48 bytes. +/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the 208 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { +pub unsafe fn vg_sha384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 48]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_256_init(state) } + unsafe { crate::sha512::vg_sha384_finalize(state, count, out) } } -/// Finishes a SHA-384, SHA-512, SHA-512/224 or SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (64 bytes) to `*out`. The SHA-512 digest is all of it; the SHA-384, SHA-512/224 and SHA-512/256 digests are its first 48, 28 and 32 bytes. +/// Finishes a SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/256 (as `vg_sha512_256_init` starts it), writes the SHA-512/256 digest of that message (32 bytes, `VG.Spec.Sha512.sha512_256`) to `*out`. /// -/// Contract: `VG.Spec.Sha512.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_256` and `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha512_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 64 bytes. +/// * `out` must be valid for reads and writes of 32 bytes. /// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the 208 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 64]) { +pub unsafe fn vg_sha512_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 32]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_finalize(state, count, out) } + unsafe { crate::sha512::vg_sha512_256_finalize(state, count, out) } } -/// Finishes a SHA-384 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-384 (as `vg_sha384_init` starts it), writes the SHA-384 digest of that message (48 bytes, `VG.Spec.Sha512.sha384`) to `*out`. +/// Finishes a SHA-512/224 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/224 (as `vg_sha512_224_init` starts it), writes the SHA-512/224 digest of that message (28 bytes, `VG.Spec.Sha512.sha512_224`) to `*out`. /// -/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_384` and `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_224` and `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha384_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 48 bytes. +/// * `out` must be valid for reads and writes of 28 bytes. /// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the 208 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 48]) { +pub unsafe fn vg_sha512_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 28]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha384_finalize(state, count, out) } + unsafe { crate::sha512::vg_sha512_224_finalize(state, count, out) } } -/// Computes the SHA-512/224 digest of the `len` bytes at `data` (28 bytes, `VG.Spec.Sha512.sha512_224`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512/224 computation with that message. +/// Computes the SHA-384 digest of the `len` bytes at `data` (48 bytes, `VG.Spec.Sha512.sha384`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-384 computation with that message. /// -/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha512_224_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha384_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 28 bytes. +/// * `out` must be valid for reads and writes of 48 bytes. /// * `out` must not overlap `data` (distinct Rust objects never do). /// * Neither `data` nor `out` may overlap the 448 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_224_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 28]) { +pub unsafe fn vg_sha384_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 48]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_224_digest(data, len, out) } + unsafe { crate::sha512::vg_sha384_digest(data, len, out) } } -/// Computes the SHA-512/256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha512.sha512_256`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512/256 computation with that message. +/// Computes the SHA-512 digest of the `len` bytes at `data` (64 bytes, `VG.Spec.Sha512.sha512`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512 computation with that message. /// -/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha512_256_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 32 bytes. +/// * `out` must be valid for reads and writes of 64 bytes. /// * `out` must not overlap `data` (distinct Rust objects never do). /// * Neither `data` nor `out` may overlap the 448 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { +pub unsafe fn vg_sha512_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 64]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_256_digest(data, len, out) } + unsafe { crate::sha512::vg_sha512_digest(data, len, out) } } -/// Finishes a SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/256 (as `vg_sha512_256_init` starts it), writes the SHA-512/256 digest of that message (32 bytes, `VG.Spec.Sha512.sha512_256`) to `*out`. +/// Computes the SHA-512/224 digest of the `len` bytes at `data` (28 bytes, `VG.Spec.Sha512.sha512_224`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512/224 computation with that message. /// -/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_256` and `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha512_256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_224_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 32 bytes. -/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 208 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `out` must be valid for reads and writes of 28 bytes. +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the 448 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 32]) { +pub unsafe fn vg_sha512_224_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 28]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_256_finalize(state, count, out) } + unsafe { crate::sha512::vg_sha512_224_digest(data, len, out) } } -/// Finishes a SHA-512/224 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/224 (as `vg_sha512_224_init` starts it), writes the SHA-512/224 digest of that message (28 bytes, `VG.Spec.Sha512.sha512_224`) to `*out`. +/// Computes the SHA-512/256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha512.sha512_256`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512/256 computation with that message. /// -/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_224` and `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha512_224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_256_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 28 bytes. -/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the 208 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `out` must be valid for reads and writes of 32 bytes. +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the 448 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 28]) { +pub unsafe fn vg_sha512_256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_224_finalize(state, count, out) } + unsafe { crate::sha512::vg_sha512_256_digest(data, len, out) } } diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/sm4_gcm.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/sm4_gcm.rs index 9b653e04fd6..9bbbdd0cfb0 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/sm4_gcm.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/sm4_gcm.rs @@ -69,26 +69,6 @@ pub unsafe fn vg_sm4_gcm_open_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *co unsafe { crate::sm4_gcm::vg_sm4_gcm_open(ctx, nonce, nonce_len, aad, aad_len, data, len, tag, tag_len) } } -/// Absorbs additional data into an incremental SM4-GCM encryption or decryption: with the key context `*ctx`, if the streaming state `*state` represents a message with `aad_len` bytes (modulo 2⁶⁴) of additional data and no text yet, it then represents that message with the `len` bytes at `data` appended to its additional data. All of the additional data comes before the text (§7.1 step 5): after text, the state represents no message. -/// -/// Contract: `VG.Spec.Gcm.streamAadContract` (AES-GCM's, which reads only the hash subkey of the key context). Constant time: only the pointers, `aad_len` and `len` may affect timing, not the key context, the state or the data. -/// -/// It runs `vg_sm4_gcm_stream_aad`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `ctx` must be valid for reads of 256 bytes, and aligned for `u64`. -/// * `state` must be valid for reads and writes of 80 bytes, and aligned for `u64`. -/// * `data` must be valid for reads of `len` bytes. -/// * `state` must not overlap `ctx` or `data` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the 4608 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_sm4_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_aad(ctx, state, aad_len, data, len) } -} - /// Starts an incremental SM4-GCM encryption or decryption (NIST SP 800-38D §7): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, makes the streaming state `*state` represent the message with the IV the `nonce_len` bytes at `nonce`, and no additional data or text yet. Continue with `vg_sm4_gcm_stream_aad`, then `vg_sm4_gcm_stream_encrypt` or `vg_sm4_gcm_stream_decrypt`, then `vg_sm4_gcm_stream_finish` or `vg_sm4_gcm_stream_verify`, with the same key context. /// /// The function checks no length: GCM requires a nonce of 1 to `2^61 - 1` bytes (§5.2.1.1), which the caller must check. @@ -111,46 +91,24 @@ pub unsafe fn vg_sm4_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, c unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_init(ctx, nonce, nonce_len, state) } } -/// Finishes an incremental SM4-GCM encryption or decryption (NIST SP 800-38D §7.1 steps 5–6, §7.2 steps 6–7): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, writes its 128-bit tag to `*tag`. A shorter tag is the first bytes of this one; to check a received tag, use `vg_sm4_gcm_stream_verify`. -/// -/// Contract: `VG.Spec.Sm4.Gcm.streamFinishContract`. Constant time: only the pointers, `aad_len` and `text_len` may affect timing, not the key context or the state. -/// -/// It runs `vg_sm4_gcm_stream_finish`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `ctx` must be valid for reads of 256 bytes, and aligned for `u64`. -/// * `state` must be valid for reads and writes of 80 bytes, and aligned for `u64`. -/// * `tag` must be valid for reads and writes of 16 bytes. -/// * The contents of `state` on return are unspecified. -/// * `state` and `tag` must not overlap each other or `ctx` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `tag` may overlap the 5808 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_sm4_gcm_stream_finish_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *mut [u8; 16]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_finish(ctx, state, aad_len, text_len, tag) } -} - -/// Finishes an incremental SM4-GCM decryption and checks its tag (NIST SP 800-38D §7.2 steps 6–8): with the key context `*ctx` that `vg_sm4_gcm_init` wrote and the received tag the `tag_len` bytes at `tag`, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, returns 1 if `tag_len` is between 4 and 16 and the received tag is the first `tag_len` bytes of the message's 128-bit tag, and 0 otherwise. The tags are compared without a branch. +/// Absorbs additional data into an incremental SM4-GCM encryption or decryption: with the key context `*ctx`, if the streaming state `*state` represents a message with `aad_len` bytes (modulo 2⁶⁴) of additional data and no text yet, it then represents that message with the `len` bytes at `data` appended to its additional data. All of the additional data comes before the text (§7.1 step 5): after text, the state represents no message. /// -/// Contract: `VG.Spec.Sm4.Gcm.streamVerifyContract`. Constant time: only the pointers, `aad_len`, `text_len` and `tag_len` may affect timing, not the key context, the state or the tags. +/// Contract: `VG.Spec.Gcm.streamAadContract` (AES-GCM's, which reads only the hash subkey of the key context). Constant time: only the pointers, `aad_len` and `len` may affect timing, not the key context, the state or the data. /// -/// It runs `vg_sm4_gcm_stream_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sm4_gcm_stream_aad`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes, and aligned for `u64`. /// * `state` must be valid for reads and writes of 80 bytes, and aligned for `u64`. -/// * `tag` must be valid for reads of `tag_len` bytes. -/// * The contents of `state` on return are unspecified. -/// * `state` must not overlap `ctx` or `tag` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `tag` may overlap the 5808 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `state` must not overlap `ctx` or `data` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the 4608 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sm4_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *const u8, tag_len: usize) -> u32 { +pub unsafe fn vg_sm4_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_verify(ctx, state, aad_len, text_len, tag, tag_len) } + unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_aad(ctx, state, aad_len, data, len) } } /// Encrypts the next piece of the text of an incremental SM4-GCM encryption (NIST SP 800-38D §7.1): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and the ciphertext of exactly `text_len` bytes of plaintext so far, encrypts the `len` bytes at `data` in place, as the continuation of that plaintext, and the state then represents the message with them appended. @@ -196,3 +154,45 @@ pub unsafe fn vg_sm4_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_> // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_decrypt(ctx, state, aad_len, text_len, data, len) } } + +/// Finishes an incremental SM4-GCM encryption or decryption (NIST SP 800-38D §7.1 steps 5–6, §7.2 steps 6–7): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, writes its 128-bit tag to `*tag`. A shorter tag is the first bytes of this one; to check a received tag, use `vg_sm4_gcm_stream_verify`. +/// +/// Contract: `VG.Spec.Sm4.Gcm.streamFinishContract`. Constant time: only the pointers, `aad_len` and `text_len` may affect timing, not the key context or the state. +/// +/// It runs `vg_sm4_gcm_stream_finish`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `ctx` must be valid for reads of 256 bytes, and aligned for `u64`. +/// * `state` must be valid for reads and writes of 80 bytes, and aligned for `u64`. +/// * `tag` must be valid for reads and writes of 16 bytes. +/// * The contents of `state` on return are unspecified. +/// * `state` and `tag` must not overlap each other or `ctx` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `tag` may overlap the 5808 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_sm4_gcm_stream_finish_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *mut [u8; 16]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_finish(ctx, state, aad_len, text_len, tag) } +} + +/// Finishes an incremental SM4-GCM decryption and checks its tag (NIST SP 800-38D §7.2 steps 6–8): with the key context `*ctx` that `vg_sm4_gcm_init` wrote and the received tag the `tag_len` bytes at `tag`, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, returns 1 if `tag_len` is between 4 and 16 and the received tag is the first `tag_len` bytes of the message's 128-bit tag, and 0 otherwise. The tags are compared without a branch. +/// +/// Contract: `VG.Spec.Sm4.Gcm.streamVerifyContract`. Constant time: only the pointers, `aad_len`, `text_len` and `tag_len` may affect timing, not the key context, the state or the tags. +/// +/// It runs `vg_sm4_gcm_stream_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `ctx` must be valid for reads of 256 bytes, and aligned for `u64`. +/// * `state` must be valid for reads and writes of 80 bytes, and aligned for `u64`. +/// * `tag` must be valid for reads of `tag_len` bytes. +/// * The contents of `state` on return are unspecified. +/// * `state` must not overlap `ctx` or `tag` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `tag` may overlap the 5808 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_sm4_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *const u8, tag_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::sm4_gcm::vg_sm4_gcm_stream_verify(ctx, state, aad_len, text_len, tag, tag_len) } +} diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/x25519.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/x25519.rs index 12bbf0908d0..88d70424ecb 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/x25519.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/x25519.rs @@ -2,45 +2,45 @@ //! The dispatchers of the verified `x25519` functions for `riscv64` //! (`crate::x25519`, `VG.Rust.dispatcher`). -/// Computes an X25519 public key (RFC 7748 §6.1): writes `X25519(k, 9)`, the X25519 function of the 32-byte scalar `k` at `scalar` and the base point's u-coordinate 9, to `*out`. The scalar is decoded (clamped). +/// Computes an X25519 shared secret (RFC 7748 §6.1): writes `X25519(k, u)` to `*out`, for the 32-byte scalar `k` at `scalar` and the 32-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped) and the most significant bit of `u` masked as RFC 7748 specifies (§5). /// -/// Contract: `VG.Spec.X25519.x25519BaseContract`. Constant time: only the pointers may affect timing, not the scalar. +/// Contract: `VG.Spec.X25519.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. /// -/// It runs `vg_x25519_base`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_x25519_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `out` must be valid for reads and writes of 32 bytes. /// * `scalar` must be valid for reads of 32 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. +/// * `point` must be valid for reads of 32 bytes. +/// * `scratch` must be valid for reads and writes of 4096 bytes, and aligned for `u64`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other or `scalar` (distinct Rust objects never do). -/// * None of `out`, `scalar` and `scratch` may wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `scalar` or `point` (distinct Rust objects never do). +/// * None of `out`, `scalar`, `point` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x25519_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], scratch: *mut [u64; 1024]) { +pub unsafe fn vg_x25519_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], point: *const [u8; 32], scratch: *mut [u64; 512]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::x25519::vg_x25519_base(out, scalar, scratch) } + unsafe { crate::x25519::vg_x25519_exchange(out, scalar, point, scratch) } } -/// Computes an X25519 shared secret (RFC 7748 §6.1): writes `X25519(k, u)` to `*out`, for the 32-byte scalar `k` at `scalar` and the 32-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped) and the most significant bit of `u` masked as RFC 7748 specifies (§5). +/// Computes an X25519 public key (RFC 7748 §6.1): writes `X25519(k, 9)`, the X25519 function of the 32-byte scalar `k` at `scalar` and the base point's u-coordinate 9, to `*out`. The scalar is decoded (clamped). /// -/// Contract: `VG.Spec.X25519.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. +/// Contract: `VG.Spec.X25519.x25519BaseContract`. Constant time: only the pointers may affect timing, not the scalar. /// -/// It runs `vg_x25519_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_x25519_base`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `out` must be valid for reads and writes of 32 bytes. /// * `scalar` must be valid for reads of 32 bytes. -/// * `point` must be valid for reads of 32 bytes. -/// * `scratch` must be valid for reads and writes of 4096 bytes, and aligned for `u64`. +/// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `scalar` or `point` (distinct Rust objects never do). -/// * None of `out`, `scalar`, `point` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other or `scalar` (distinct Rust objects never do). +/// * None of `out`, `scalar` and `scratch` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x25519_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], point: *const [u8; 32], scratch: *mut [u64; 512]) -> u32 { +pub unsafe fn vg_x25519_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], scratch: *mut [u64; 1024]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::x25519::vg_x25519_exchange(out, scalar, point, scratch) } + unsafe { crate::x25519::vg_x25519_base(out, scalar, scratch) } } diff --git a/crates/verified-garbage-asm-riscv64/src/dispatch/x448.rs b/crates/verified-garbage-asm-riscv64/src/dispatch/x448.rs index 157b0cb1298..d022c36408c 100644 --- a/crates/verified-garbage-asm-riscv64/src/dispatch/x448.rs +++ b/crates/verified-garbage-asm-riscv64/src/dispatch/x448.rs @@ -2,45 +2,45 @@ //! The dispatchers of the verified `x448` functions for `riscv64` //! (`crate::x448`, `VG.Rust.dispatcher`). -/// Computes an X448 public key (RFC 7748 §6.2): writes `X448(k, 5)`, the X448 function of the 56-byte scalar `k` at `scalar` and the base point's u-coordinate 5, to `*out`. The scalar is decoded (clamped). +/// Computes an X448 shared secret (RFC 7748 §6.2): writes `X448(k, u)` to `*out`, for the 56-byte scalar `k` at `scalar` and the 56-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped); all 448 bits of `u` are used and noncanonical coordinates are reduced modulo the field prime (§5). /// -/// Contract: `VG.Spec.X448.x448BaseContract`. Constant time: only the pointers may affect timing, not the scalar. +/// Contract: `VG.Spec.X448.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. /// -/// It runs `vg_x448_base`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_x448_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `out` must be valid for reads and writes of 56 bytes. /// * `scalar` must be valid for reads of 56 bytes. +/// * `point` must be valid for reads of 56 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other or `scalar` (distinct Rust objects never do). -/// * None of `out`, `scalar` and `scratch` may wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `scalar` or `point` (distinct Rust objects never do). +/// * None of `out`, `scalar`, `point` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x448_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], scratch: *mut [u64; 1024]) { +pub unsafe fn vg_x448_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], point: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::x448::vg_x448_base(out, scalar, scratch) } + unsafe { crate::x448::vg_x448_exchange(out, scalar, point, scratch) } } -/// Computes an X448 shared secret (RFC 7748 §6.2): writes `X448(k, u)` to `*out`, for the 56-byte scalar `k` at `scalar` and the 56-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped); all 448 bits of `u` are used and noncanonical coordinates are reduced modulo the field prime (§5). +/// Computes an X448 public key (RFC 7748 §6.2): writes `X448(k, 5)`, the X448 function of the 56-byte scalar `k` at `scalar` and the base point's u-coordinate 5, to `*out`. The scalar is decoded (clamped). /// -/// Contract: `VG.Spec.X448.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. +/// Contract: `VG.Spec.X448.x448BaseContract`. Constant time: only the pointers may affect timing, not the scalar. /// -/// It runs `vg_x448_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_x448_base`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `out` must be valid for reads and writes of 56 bytes. /// * `scalar` must be valid for reads of 56 bytes. -/// * `point` must be valid for reads of 56 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes, and aligned for `u64`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `scalar` or `point` (distinct Rust objects never do). -/// * None of `out`, `scalar`, `point` and `scratch` may overlap the 16 bytes of stack below the stack pointer, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other or `scalar` (distinct Rust objects never do). +/// * None of `out`, `scalar` and `scratch` may wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x448_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], point: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_x448_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], scratch: *mut [u64; 1024]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::x448::vg_x448_exchange(out, scalar, point, scratch) } + unsafe { crate::x448::vg_x448_base(out, scalar, scratch) } } diff --git a/crates/verified-garbage-asm-riscv64/src/fn_tables/mod.rs b/crates/verified-garbage-asm-riscv64/src/fn_tables/mod.rs index 947d4aa89a0..d308b3c24c4 100644 --- a/crates/verified-garbage-asm-riscv64/src/fn_tables/mod.rs +++ b/crates/verified-garbage-asm-riscv64/src/fn_tables/mod.rs @@ -2,36 +2,36 @@ //! The tables of functions of the verified functions for `riscv64` (`Artifact.tables`). #![allow(dead_code, unused_imports)] -/// A table of functions of the interface `Hash` (`VG.FnTable`): the addresses of its 3 functions, then its 2 words of data. Only the statics of this module are of this type. +/// A table of functions of the interface `Hmac` (`VG.FnTable`): the addresses of its 3 functions, then its 3 words of data. Only the statics of this module are of this type. #[repr(C)] -pub struct Hash { +pub struct Hmac { fns: [*const (); 3], - data: [usize; 2], + data: [usize; 3], } -// SAFETY: a `Hash` is immutable, and the functions it holds the addresses of may be called from any thread. -unsafe impl Sync for Hash {} +// SAFETY: a `Hmac` is immutable, and the functions it holds the addresses of may be called from any thread. +unsafe impl Sync for Hmac {} -impl Hash { +impl Hmac { /// A pointer to the table's words, as the verified functions that take it read them. - pub const fn as_ptr(&'static self) -> *const [usize; 5] { + pub const fn as_ptr(&'static self) -> *const [usize; 6] { core::ptr::from_ref(self).cast() } } -/// A table of functions of the interface `Hmac` (`VG.FnTable`): the addresses of its 3 functions, then its 3 words of data. Only the statics of this module are of this type. +/// A table of functions of the interface `Hash` (`VG.FnTable`): the addresses of its 3 functions, then its 2 words of data. Only the statics of this module are of this type. #[repr(C)] -pub struct Hmac { +pub struct Hash { fns: [*const (); 3], - data: [usize; 3], + data: [usize; 2], } -// SAFETY: a `Hmac` is immutable, and the functions it holds the addresses of may be called from any thread. -unsafe impl Sync for Hmac {} +// SAFETY: a `Hash` is immutable, and the functions it holds the addresses of may be called from any thread. +unsafe impl Sync for Hash {} -impl Hmac { +impl Hash { /// A pointer to the table's words, as the verified functions that take it read them. - pub const fn as_ptr(&'static self) -> *const [usize; 6] { + pub const fn as_ptr(&'static self) -> *const [usize; 5] { core::ptr::from_ref(self).cast() } } @@ -53,38 +53,6 @@ impl MdHash { } } -#[rustfmt::skip] -mod vg_hash_md5; -pub use vg_hash_md5::VG_HASH_MD5; - -#[rustfmt::skip] -mod vg_hash_sha1; -pub use vg_hash_sha1::VG_HASH_SHA1; - -#[rustfmt::skip] -mod vg_hash_sha224; -pub use vg_hash_sha224::VG_HASH_SHA224; - -#[rustfmt::skip] -mod vg_hash_sha256; -pub use vg_hash_sha256::VG_HASH_SHA256; - -#[rustfmt::skip] -mod vg_hash_sha384; -pub use vg_hash_sha384::VG_HASH_SHA384; - -#[rustfmt::skip] -mod vg_hash_sha512; -pub use vg_hash_sha512::VG_HASH_SHA512; - -#[rustfmt::skip] -mod vg_hash_sha512_224; -pub use vg_hash_sha512_224::VG_HASH_SHA512_224; - -#[rustfmt::skip] -mod vg_hash_sha512_256; -pub use vg_hash_sha512_256::VG_HASH_SHA512_256; - #[rustfmt::skip] mod vg_hmac_md5; pub use vg_hmac_md5::VG_HMAC_MD5; @@ -117,6 +85,38 @@ pub use vg_hmac_sha512_224::VG_HMAC_SHA512_224; mod vg_hmac_sha512_256; pub use vg_hmac_sha512_256::VG_HMAC_SHA512_256; +#[rustfmt::skip] +mod vg_hash_md5; +pub use vg_hash_md5::VG_HASH_MD5; + +#[rustfmt::skip] +mod vg_hash_sha1; +pub use vg_hash_sha1::VG_HASH_SHA1; + +#[rustfmt::skip] +mod vg_hash_sha224; +pub use vg_hash_sha224::VG_HASH_SHA224; + +#[rustfmt::skip] +mod vg_hash_sha256; +pub use vg_hash_sha256::VG_HASH_SHA256; + +#[rustfmt::skip] +mod vg_hash_sha384; +pub use vg_hash_sha384::VG_HASH_SHA384; + +#[rustfmt::skip] +mod vg_hash_sha512; +pub use vg_hash_sha512::VG_HASH_SHA512; + +#[rustfmt::skip] +mod vg_hash_sha512_224; +pub use vg_hash_sha512_224::VG_HASH_SHA512_224; + +#[rustfmt::skip] +mod vg_hash_sha512_256; +pub use vg_hash_sha512_256::VG_HASH_SHA512_256; + #[rustfmt::skip] mod vg_md_hash_md5; pub use vg_md_hash_md5::VG_MD_HASH_MD5; diff --git a/crates/verified-garbage-asm-x86/src/dispatch/aes.rs b/crates/verified-garbage-asm-x86/src/dispatch/aes.rs index f18973ed6e7..fa98c2095c3 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/aes.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/aes.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Aes.expandKeyContract`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_expand_key_aesni` if the CPU supports its features (`VG_AES_EXPAND_KEY_AESNI_FEATURES`); or else `vg_aes_expand_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_expand_key_aesni` if the CPU supports its features (`VG_AES_EXPAND_KEY_AESNI_FEATURES`); or else `vg_aes_expand_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -56,7 +56,7 @@ pub unsafe fn vg_aes_expand_key_auto(_dit: verified_garbage_cpu::Dit<'_>, key: * /// /// Contract: `VG.Spec.Aes.encryptBlocksContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_encrypt_blocks_aesni` if the CPU supports its features (`VG_AES_ENCRYPT_BLOCKS_AESNI_FEATURES`); or else `vg_aes_encrypt_blocks`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_encrypt_blocks_aesni` if the CPU supports its features (`VG_AES_ENCRYPT_BLOCKS_AESNI_FEATURES`); or else `vg_aes_encrypt_blocks`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -107,7 +107,7 @@ pub unsafe fn vg_aes_encrypt_blocks_auto(_dit: verified_garbage_cpu::Dit<'_>, sc /// /// Contract: `VG.Spec.Aes.decryptBlocksContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_decrypt_blocks_aesni` if the CPU supports its features (`VG_AES_DECRYPT_BLOCKS_AESNI_FEATURES`); or else `vg_aes_decrypt_blocks`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_decrypt_blocks_aesni` if the CPU supports its features (`VG_AES_DECRYPT_BLOCKS_AESNI_FEATURES`); or else `vg_aes_decrypt_blocks`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/aes_cbc.rs b/crates/verified-garbage-asm-x86/src/dispatch/aes_cbc.rs index 65a601b53b0..646cdb1415c 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/aes_cbc.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/aes_cbc.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Cbc.aesEncryptContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cbc_encrypt_aesni` if the CPU supports its features (`VG_AES_CBC_ENCRYPT_AESNI_FEATURES`); or else `vg_aes_cbc_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cbc_encrypt_aesni` if the CPU supports its features (`VG_AES_CBC_ENCRYPT_AESNI_FEATURES`); or else `vg_aes_cbc_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -58,7 +58,7 @@ pub unsafe fn vg_aes_cbc_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, sched /// /// Contract: `VG.Spec.Cbc.aesDecryptContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cbc_decrypt_aesni` if the CPU supports its features (`VG_AES_CBC_DECRYPT_AESNI_FEATURES`); or else `vg_aes_cbc_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cbc_decrypt_aesni` if the CPU supports its features (`VG_AES_CBC_DECRYPT_AESNI_FEATURES`); or else `vg_aes_cbc_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/aes_ccm.rs b/crates/verified-garbage-asm-x86/src/dispatch/aes_ccm.rs index 03079e12187..fa822e87906 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/aes_ccm.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/aes_ccm.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ccm.sealContract`. Constant time: only the pointers, `rounds`, the lengths and `tag_len` may affect timing, not the key schedule, the nonce, the associated data or the payload. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ccm_seal_aesni` if the CPU supports its features (`VG_AES_CCM_SEAL_AESNI_FEATURES`); or else `vg_aes_ccm_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ccm_seal_aesni` if the CPU supports its features (`VG_AES_CCM_SEAL_AESNI_FEATURES`); or else `vg_aes_ccm_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -62,7 +62,7 @@ pub unsafe fn vg_aes_ccm_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule /// /// Contract: `VG.Spec.Ccm.openContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key schedule, the nonce, the associated data, the data or the tag. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ccm_open_aesni` if the CPU supports its features (`VG_AES_CCM_OPEN_AESNI_FEATURES`); or else `vg_aes_ccm_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ccm_open_aesni` if the CPU supports its features (`VG_AES_CCM_OPEN_AESNI_FEATURES`); or else `vg_aes_ccm_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/aes_cfb.rs b/crates/verified-garbage-asm-x86/src/dispatch/aes_cfb.rs index 2b2dcb4eb54..62888d2194f 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/aes_cfb.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/aes_cfb.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Cfb.aesEncryptContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cfb128_encrypt_aesni` if the CPU supports its features (`VG_AES_CFB128_ENCRYPT_AESNI_FEATURES`); or else `vg_aes_cfb128_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cfb128_encrypt_aesni` if the CPU supports its features (`VG_AES_CFB128_ENCRYPT_AESNI_FEATURES`); or else `vg_aes_cfb128_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -58,7 +58,7 @@ pub unsafe fn vg_aes_cfb128_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, sc /// /// Contract: `VG.Spec.Cfb.aesDecryptContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cfb128_decrypt_aesni` if the CPU supports its features (`VG_AES_CFB128_DECRYPT_AESNI_FEATURES`); or else `vg_aes_cfb128_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cfb128_decrypt_aesni` if the CPU supports its features (`VG_AES_CFB128_DECRYPT_AESNI_FEATURES`); or else `vg_aes_cfb128_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -110,7 +110,7 @@ pub unsafe fn vg_aes_cfb128_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, sc /// /// Contract: `VG.Spec.Cfb.aesEncryptFinalContract`. Constant time: only the pointers, `rounds` and `len` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cfb128_encrypt_final_aesni` if the CPU supports its features (`VG_AES_CFB128_ENCRYPT_FINAL_AESNI_FEATURES`); or else `vg_aes_cfb128_encrypt_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cfb128_encrypt_final_aesni` if the CPU supports its features (`VG_AES_CFB128_ENCRYPT_FINAL_AESNI_FEATURES`); or else `vg_aes_cfb128_encrypt_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -162,7 +162,7 @@ pub unsafe fn vg_aes_cfb128_encrypt_final_auto(_dit: verified_garbage_cpu::Dit<' /// /// Contract: `VG.Spec.Cfb.aesDecryptFinalContract`. Constant time: only the pointers, `rounds` and `len` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cfb128_decrypt_final_aesni` if the CPU supports its features (`VG_AES_CFB128_DECRYPT_FINAL_AESNI_FEATURES`); or else `vg_aes_cfb128_decrypt_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cfb128_decrypt_final_aesni` if the CPU supports its features (`VG_AES_CFB128_DECRYPT_FINAL_AESNI_FEATURES`); or else `vg_aes_cfb128_decrypt_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/aes_cfb8.rs b/crates/verified-garbage-asm-x86/src/dispatch/aes_cfb8.rs index 1d9c229c1b7..f991804f10a 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/aes_cfb8.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/aes_cfb8.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Cfb8.aesEncryptContract`. Constant time: only the pointers, `rounds` and `len` may affect timing, not the key schedule, the input block or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cfb8_encrypt_aesni` if the CPU supports its features (`VG_AES_CFB8_ENCRYPT_AESNI_FEATURES`); or else `vg_aes_cfb8_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cfb8_encrypt_aesni` if the CPU supports its features (`VG_AES_CFB8_ENCRYPT_AESNI_FEATURES`); or else `vg_aes_cfb8_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -58,7 +58,7 @@ pub unsafe fn vg_aes_cfb8_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, sche /// /// Contract: `VG.Spec.Cfb8.aesDecryptContract`. Constant time: only the pointers, `rounds` and `len` may affect timing, not the key schedule, the input block or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cfb8_decrypt_aesni` if the CPU supports its features (`VG_AES_CFB8_DECRYPT_AESNI_FEATURES`); or else `vg_aes_cfb8_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cfb8_decrypt_aesni` if the CPU supports its features (`VG_AES_CFB8_DECRYPT_AESNI_FEATURES`); or else `vg_aes_cfb8_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/aes_ctr.rs b/crates/verified-garbage-asm-x86/src/dispatch/aes_ctr.rs index 64b8142bc6b..ce469297672 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/aes_ctr.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/aes_ctr.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ctr.aesContract`. Constant time: only the pointers, `rounds`, `n` and the last four bytes of `*ctr` (the 32-bit counter, public in OpenSSL, BoringSSL and AWS-LC, which split the blocks where it wraps) may affect timing, not the key schedule, the rest of the counter block or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ctr_aesni` if the CPU supports its features (`VG_AES_CTR_AESNI_FEATURES`); or else `vg_aes_ctr`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ctr_aesni` if the CPU supports its features (`VG_AES_CTR_AESNI_FEATURES`); or else `vg_aes_ctr`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -58,7 +58,7 @@ pub unsafe fn vg_aes_ctr_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *co /// /// Contract: `VG.Spec.Ctr.aesFinalContract`. Constant time: only the pointers, `rounds`, `len` and the last four bytes of `*ctr` (the 32-bit counter, public in OpenSSL, BoringSSL and AWS-LC, which split the blocks where it wraps) may affect timing, not the key schedule, the rest of the counter block or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ctr_final_aesni` if the CPU supports its features (`VG_AES_CTR_FINAL_AESNI_FEATURES`); or else `vg_aes_ctr_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ctr_final_aesni` if the CPU supports its features (`VG_AES_CTR_FINAL_AESNI_FEATURES`); or else `vg_aes_ctr_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/aes_ecb.rs b/crates/verified-garbage-asm-x86/src/dispatch/aes_ecb.rs index 3f320b9587a..83e09c13f9e 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/aes_ecb.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/aes_ecb.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.AesEcb.encryptContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ecb_encrypt_aesni` if the CPU supports its features (`VG_AES_ECB_ENCRYPT_AESNI_FEATURES`); or else `vg_aes_ecb_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ecb_encrypt_aesni` if the CPU supports its features (`VG_AES_ECB_ENCRYPT_AESNI_FEATURES`); or else `vg_aes_ecb_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -56,7 +56,7 @@ pub unsafe fn vg_aes_ecb_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, sched /// /// Contract: `VG.Spec.AesEcb.decryptContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ecb_decrypt_aesni` if the CPU supports its features (`VG_AES_ECB_DECRYPT_AESNI_FEATURES`); or else `vg_aes_ecb_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ecb_decrypt_aesni` if the CPU supports its features (`VG_AES_ECB_DECRYPT_AESNI_FEATURES`); or else `vg_aes_ecb_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/aes_gcm_siv.rs b/crates/verified-garbage-asm-x86/src/dispatch/aes_gcm_siv.rs index c043b04aef1..8871f71e6e1 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/aes_gcm_siv.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/aes_gcm_siv.rs @@ -8,7 +8,7 @@ /// /// Contract: `VG.Spec.GcmSiv.sealContract`. Constant time: only the pointers, `rounds` and the lengths may affect timing, not the key schedule, the nonce, the additional data or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_siv_seal_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_siv_seal_aesni` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AESNI_FEATURES`); `vg_aes_gcm_siv_seal_pclmul` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_PCLMUL_FEATURES`); or else `vg_aes_gcm_siv_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_siv_seal_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_siv_seal_aesni` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AESNI_FEATURES`); `vg_aes_gcm_siv_seal_pclmul` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_PCLMUL_FEATURES`); or else `vg_aes_gcm_siv_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -74,7 +74,7 @@ pub unsafe fn vg_aes_gcm_siv_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, sche /// /// Contract: `VG.Spec.GcmSiv.openContract`. Constant time but for the result: only the pointers, `rounds`, the lengths and whether the function returns 1 or 0 may affect timing, not the key schedule, the nonce, the additional data, the data or the tag. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_siv_open_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_siv_open_aesni` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AESNI_FEATURES`); `vg_aes_gcm_siv_open_pclmul` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_PCLMUL_FEATURES`); or else `vg_aes_gcm_siv_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_siv_open_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_siv_open_aesni` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AESNI_FEATURES`); `vg_aes_gcm_siv_open_pclmul` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_PCLMUL_FEATURES`); or else `vg_aes_gcm_siv_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/aes_ocb.rs b/crates/verified-garbage-asm-x86/src/dispatch/aes_ocb.rs index 085f60592c4..5cdde96c243 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/aes_ocb.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/aes_ocb.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ocb.initContract`. The key context is `VG.Spec.Ocb.KeyRepr`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ocb_init_aesni` if the CPU supports its features (`VG_AES_OCB_INIT_AESNI_FEATURES`); or else `vg_aes_ocb_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ocb_init_aesni` if the CPU supports its features (`VG_AES_OCB_INIT_AESNI_FEATURES`); or else `vg_aes_ocb_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -57,7 +57,7 @@ pub unsafe fn vg_aes_ocb_init_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *co /// /// Contract: `VG.Spec.Ocb.sealContract`. Constant time: only the pointers, `rounds`, the lengths and `tag_len` may affect timing, not the key context, the nonce, the associated data or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ocb_seal_aesni` if the CPU supports its features (`VG_AES_OCB_SEAL_AESNI_FEATURES`); or else `vg_aes_ocb_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ocb_seal_aesni` if the CPU supports its features (`VG_AES_OCB_SEAL_AESNI_FEATURES`); or else `vg_aes_ocb_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -112,7 +112,7 @@ pub unsafe fn vg_aes_ocb_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *co /// /// Contract: `VG.Spec.Ocb.openContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the associated data, the data or the tag. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ocb_open_aesni` if the CPU supports its features (`VG_AES_OCB_OPEN_AESNI_FEATURES`); or else `vg_aes_ocb_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ocb_open_aesni` if the CPU supports its features (`VG_AES_OCB_OPEN_AESNI_FEATURES`); or else `vg_aes_ocb_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/aes_ofb.rs b/crates/verified-garbage-asm-x86/src/dispatch/aes_ofb.rs index 636dc6f8644..acc2667b898 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/aes_ofb.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/aes_ofb.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ofb.aesContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ofb_aesni` if the CPU supports its features (`VG_AES_OFB_AESNI_FEATURES`); or else `vg_aes_ofb`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ofb_aesni` if the CPU supports its features (`VG_AES_OFB_AESNI_FEATURES`); or else `vg_aes_ofb`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -58,7 +58,7 @@ pub unsafe fn vg_aes_ofb_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *co /// /// Contract: `VG.Spec.Ofb.aesFinalContract`. Constant time: only the pointers, `rounds` and `len` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ofb_final_aesni` if the CPU supports its features (`VG_AES_OFB_FINAL_AESNI_FEATURES`); or else `vg_aes_ofb_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ofb_final_aesni` if the CPU supports its features (`VG_AES_OFB_FINAL_AESNI_FEATURES`); or else `vg_aes_ofb_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/aes_siv.rs b/crates/verified-garbage-asm-x86/src/dispatch/aes_siv.rs index 4d914099081..c4939d1e0e2 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/aes_siv.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/aes_siv.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Siv.initContract`. The key context is `VG.Spec.Siv.KeyRepr`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_siv_init_aesni` if the CPU supports its features (`VG_AES_SIV_INIT_AESNI_FEATURES`); or else `vg_aes_siv_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_siv_init_aesni` if the CPU supports its features (`VG_AES_SIV_INIT_AESNI_FEATURES`); or else `vg_aes_siv_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -57,7 +57,7 @@ pub unsafe fn vg_aes_siv_init_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *co /// /// Contract: `VG.Spec.Siv.encryptContract`. Constant time: only the pointers, `rounds`, `ads_count`, `len` and where the components are (their addresses and lengths) may affect timing, not the key context, the associated data or the plaintext. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_siv_encrypt_aesni` if the CPU supports its features (`VG_AES_SIV_ENCRYPT_AESNI_FEATURES`); or else `vg_aes_siv_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_siv_encrypt_aesni` if the CPU supports its features (`VG_AES_SIV_ENCRYPT_AESNI_FEATURES`); or else `vg_aes_siv_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -110,7 +110,7 @@ pub unsafe fn vg_aes_siv_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: /// /// Contract: `VG.Spec.Siv.decryptContract`. Constant time but for the result: only the pointers, `rounds`, `ads_count`, `len`, where the components are (their addresses and lengths) and whether the function returns 1 or 0 may affect timing, not the key context, the associated data, the data or `V`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_siv_decrypt_aesni` if the CPU supports its features (`VG_AES_SIV_DECRYPT_AESNI_FEATURES`); or else `vg_aes_siv_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_siv_decrypt_aesni` if the CPU supports its features (`VG_AES_SIV_DECRYPT_AESNI_FEATURES`); or else `vg_aes_siv_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/aes_xts.rs b/crates/verified-garbage-asm-x86/src/dispatch/aes_xts.rs index 21231c6b06c..9d27b7f404f 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/aes_xts.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/aes_xts.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Xts.aesEncryptUnitContract`. Constant time: only the pointers, `rounds` and `len` may affect timing, not the key schedules, the tweak value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_xts_encrypt_unit_aesni` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_AESNI_FEATURES`); or else `vg_aes_xts_encrypt_unit`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_xts_encrypt_unit_aesni` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_AESNI_FEATURES`); or else `vg_aes_xts_encrypt_unit`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -60,7 +60,7 @@ pub unsafe fn vg_aes_xts_encrypt_unit_auto(_dit: verified_garbage_cpu::Dit<'_>, /// /// Contract: `VG.Spec.Xts.aesDecryptUnitContract`. Constant time: only the pointers, `rounds` and `len` may affect timing, not the key schedules, the tweak value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_xts_decrypt_unit_aesni` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_AESNI_FEATURES`); or else `vg_aes_xts_decrypt_unit`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_xts_decrypt_unit_aesni` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_AESNI_FEATURES`); or else `vg_aes_xts_decrypt_unit`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/chacha20.rs b/crates/verified-garbage-asm-x86/src/dispatch/chacha20.rs index c28cc429767..d5f29b5c880 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/chacha20.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/chacha20.rs @@ -22,11 +22,30 @@ pub unsafe fn vg_chacha20_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: unsafe { crate::chacha20::vg_chacha20_init(state, key, nonce) } } +/// Restarts a ChaCha20 keystream with the same key: makes the streaming state `*state` represent the whole keystream of its key for the 16-byte nonce `*nonce` (as `vg_chacha20_init` does), discarding what was left of the previous one. +/// +/// Contract: `VG.Spec.ChaCha20.setNonceContract`. Constant time: only the pointers may affect timing, not the key or the nonce. +/// +/// It runs `vg_chacha20_set_nonce`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 768 bytes. +/// * `nonce` must be valid for reads of 16 bytes. +/// * `state` must not overlap `nonce` or the arguments on the stack (distinct Rust objects never do). +/// * Neither `state` nor `nonce` may overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_chacha20_set_nonce_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 96], nonce: *const [u8; 16]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::chacha20::vg_chacha20_set_nonce(state, nonce) } +} + /// Applies a ChaCha20 keystream (RFC 8439 §2.4), encrypting or decrypting: if at least `len` bytes are left of the keystream that the streaming state `*state` represents, XORs the next `len` of them into the `len` bytes at `data`, keeps the rest in `*state`, and returns 1. Otherwise (the block counter would pass 2³² − 1) returns 0, and leaves the bytes at `data` and the keystream unchanged. /// /// Contract: `VG.Spec.ChaCha20.applyContract`. Constant time: only the pointers, `len` and the number of bytes of keystream left in `*state` (`VG.Spec.ChaCha20.leftAt`: 64 × (2³² − `c`) for the initial block counter `c`, less the bytes applied since) may affect timing, not the key, the nonce, the keystream or the data. The function may leak that number. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_chacha20_apply_ssse3` if the CPU supports its features (`VG_CHACHA20_APPLY_SSSE3_FEATURES`); or else `vg_chacha20_apply`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_chacha20_apply_ssse3` if the CPU supports its features (`VG_CHACHA20_APPLY_SSSE3_FEATURES`); or else `vg_chacha20_apply`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -71,22 +90,3 @@ pub unsafe fn vg_chacha20_apply_auto(_dit: verified_garbage_cpu::Dit<'_>, state: // either carries the caller's obligations, which are those of every implementation. unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, data, len) } } - -/// Restarts a ChaCha20 keystream with the same key: makes the streaming state `*state` represent the whole keystream of its key for the 16-byte nonce `*nonce` (as `vg_chacha20_init` does), discarding what was left of the previous one. -/// -/// Contract: `VG.Spec.ChaCha20.setNonceContract`. Constant time: only the pointers may affect timing, not the key or the nonce. -/// -/// It runs `vg_chacha20_set_nonce`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 768 bytes. -/// * `nonce` must be valid for reads of 16 bytes. -/// * `state` must not overlap `nonce` or the arguments on the stack (distinct Rust objects never do). -/// * Neither `state` nor `nonce` may overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_chacha20_set_nonce_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 96], nonce: *const [u8; 16]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::chacha20::vg_chacha20_set_nonce(state, nonce) } -} diff --git a/crates/verified-garbage-asm-x86/src/dispatch/chacha20poly1305.rs b/crates/verified-garbage-asm-x86/src/dispatch/chacha20poly1305.rs index 64a1ea6a421..29745fe67a5 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/chacha20poly1305.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/chacha20poly1305.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.ChaCha20Poly1305.sealContract`. Constant time: only the pointers and the lengths may affect timing, not the key, the nonce or the data. The block counter wraps around beyond 2³²-1 blocks of data (RFC 8439's `P_MAX`), which the caller must not exceed for the construction to be secure. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_chacha20_poly1305_seal_ssse3` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_SSSE3_FEATURES`); or else `vg_chacha20_poly1305_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_chacha20_poly1305_seal_ssse3` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_SSSE3_FEATURES`); or else `vg_chacha20_poly1305_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -59,7 +59,7 @@ pub unsafe fn vg_chacha20_poly1305_seal_auto(_dit: verified_garbage_cpu::Dit<'_> /// /// Contract: `VG.Spec.ChaCha20Poly1305.openZeroContract`. Constant time but for the result: only the pointers, the lengths and whether the function returns 1 or 0 may affect timing, not the key, the nonce, the additional data, the tag or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_chacha20_poly1305_open_ssse3` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_SSSE3_FEATURES`); or else `vg_chacha20_poly1305_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_chacha20_poly1305_open_ssse3` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_SSSE3_FEATURES`); or else `vg_chacha20_poly1305_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -112,7 +112,7 @@ pub unsafe fn vg_chacha20_poly1305_open_auto(_dit: verified_garbage_cpu::Dit<'_> /// /// Contract: `VG.Spec.ChaCha20Poly1305.openToContract`. Constant time but for the result: only the pointers, the lengths and whether the function returns 1 or 0 may affect timing, not the key, the nonce, the additional data, the tag or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_chacha20_poly1305_open_to_ssse3` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_TO_SSSE3_FEATURES`); or else `vg_chacha20_poly1305_open_to`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_chacha20_poly1305_open_to_ssse3` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_TO_SSSE3_FEATURES`); or else `vg_chacha20_poly1305_open_to`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -168,7 +168,7 @@ pub unsafe fn vg_chacha20_poly1305_open_to_auto(_dit: verified_garbage_cpu::Dit< /// /// Contract: `VG.Spec.ChaCha20Poly1305.sealGatherContract`. Constant time: only the pointers, the lengths, `src_count` and where the slices are (their addresses and lengths) may affect timing, not the key, the nonce, the additional data or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_chacha20_poly1305_seal_gather_ssse3` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_GATHER_SSSE3_FEATURES`); or else `vg_chacha20_poly1305_seal_gather`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_chacha20_poly1305_seal_gather_ssse3` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_GATHER_SSSE3_FEATURES`); or else `vg_chacha20_poly1305_seal_gather`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/cmac_aes.rs b/crates/verified-garbage-asm-x86/src/dispatch/cmac_aes.rs index d27f50d0f6e..0eef6444467 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/cmac_aes.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/cmac_aes.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Cmac.aesInitContract`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_cmac_aes_init_aesni` if the CPU supports its features (`VG_CMAC_AES_INIT_AESNI_FEATURES`); or else `vg_cmac_aes_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_cmac_aes_init_aesni` if the CPU supports its features (`VG_CMAC_AES_INIT_AESNI_FEATURES`); or else `vg_cmac_aes_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -55,7 +55,7 @@ pub unsafe fn vg_cmac_aes_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: /// /// Contract: `VG.Spec.Cmac.aesAbsorbContract`. Constant time: only the pointers, `rounds`, `count` and `len` may affect timing, not the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_cmac_aes_absorb_aesni` if the CPU supports its features (`VG_CMAC_AES_ABSORB_AESNI_FEATURES`); or else `vg_cmac_aes_absorb`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_cmac_aes_absorb_aesni` if the CPU supports its features (`VG_CMAC_AES_ABSORB_AESNI_FEATURES`); or else `vg_cmac_aes_absorb`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -104,7 +104,7 @@ pub unsafe fn vg_cmac_aes_absorb_auto(_dit: verified_garbage_cpu::Dit<'_>, state /// /// Contract: `VG.Spec.Cmac.aesFinishContract`. Constant time: only the pointers, `rounds` and `count` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_cmac_aes_finish_aesni` if the CPU supports its features (`VG_CMAC_AES_FINISH_AESNI_FEATURES`); or else `vg_cmac_aes_finish`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_cmac_aes_finish_aesni` if the CPU supports its features (`VG_CMAC_AES_FINISH_AESNI_FEATURES`); or else `vg_cmac_aes_finish`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/cmac_triple_des.rs b/crates/verified-garbage-asm-x86/src/dispatch/cmac_triple_des.rs index 623c4696145..5895501bb52 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/cmac_triple_des.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/cmac_triple_des.rs @@ -2,6 +2,26 @@ //! The dispatchers of the verified `cmac_triple_des` functions for `x86` //! (`crate::cmac_triple_des`, `VG.Rust.dispatcher`). +/// Starts a TDEA-CMAC computation (NIST SP 800-38B): makes the streaming state `*state` represent the empty message under the 16- or 24-byte TDEA key at `key` (FIPS 46-3; a 16-byte key is `K1 ‖ K2` with `K3 = K1`, parity bits are ignored). The state holds the key schedule (the three DES schedules), the subkeys `K1 ‖ K2` (§6.1), the chaining value and the message's last bytes, held back (`VG.Spec.Cmac.tdesRepr`). Continue with `vg_cmac_triple_des_absorb` and `vg_cmac_triple_des_finish`. +/// +/// Contract: `VG.Spec.Cmac.tdesStreamInitContract`. Constant time: only the pointers and `key_len` may affect timing, not the key. +/// +/// It runs `vg_cmac_triple_des_stream_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 416 bytes. +/// * `key` must be valid for reads of `key_len` bytes. +/// * `key_len` must be 16 or 24. +/// * `state` must not overlap `key` or the arguments on the stack (distinct Rust objects never do). +/// * Neither `state` nor `key` may overlap the return address on the stack or the 680 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_cmac_triple_des_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 52], key: *const u8, key_len: usize) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::cmac_triple_des::vg_cmac_triple_des_stream_init(state, key, key_len) } +} + /// Absorbs data into a TDEA-CMAC computation (NIST SP 800-38B §6.2): if the streaming state `*state` represents a message of `count` bytes under a TDEA key (as `vg_cmac_triple_des_stream_init` set it up), it then represents that message followed by the `len` bytes at `data`, under the same key, provided that the two together are shorter than 2⁶⁴ bytes. It chains (step 6) every block of the message but its last bytes `Mₙ*` (step 3), which it holds back in the state, since only `vg_cmac_triple_des_finish` knows they are the last. /// /// Contract: `VG.Spec.Cmac.tdesAbsorbContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. @@ -40,23 +60,3 @@ pub unsafe fn vg_cmac_triple_des_finish_auto(_dit: verified_garbage_cpu::Dit<'_> // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::cmac_triple_des::vg_cmac_triple_des_finish(state, count, out) } } - -/// Starts a TDEA-CMAC computation (NIST SP 800-38B): makes the streaming state `*state` represent the empty message under the 16- or 24-byte TDEA key at `key` (FIPS 46-3; a 16-byte key is `K1 ‖ K2` with `K3 = K1`, parity bits are ignored). The state holds the key schedule (the three DES schedules), the subkeys `K1 ‖ K2` (§6.1), the chaining value and the message's last bytes, held back (`VG.Spec.Cmac.tdesRepr`). Continue with `vg_cmac_triple_des_absorb` and `vg_cmac_triple_des_finish`. -/// -/// Contract: `VG.Spec.Cmac.tdesStreamInitContract`. Constant time: only the pointers and `key_len` may affect timing, not the key. -/// -/// It runs `vg_cmac_triple_des_stream_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 416 bytes. -/// * `key` must be valid for reads of `key_len` bytes. -/// * `key_len` must be 16 or 24. -/// * `state` must not overlap `key` or the arguments on the stack (distinct Rust objects never do). -/// * Neither `state` nor `key` may overlap the return address on the stack or the 680 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_cmac_triple_des_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 52], key: *const u8, key_len: usize) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::cmac_triple_des::vg_cmac_triple_des_stream_init(state, key, key_len) } -} diff --git a/crates/verified-garbage-asm-x86/src/dispatch/dsa.rs b/crates/verified-garbage-asm-x86/src/dispatch/dsa.rs index af92219c233..8c9385ba356 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/dsa.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/dsa.rs @@ -2,6 +2,32 @@ //! The dispatchers of the verified `dsa` functions for `x86` //! (`crate::dsa`, `VG.Rust.dispatcher`). +/// Generate a DSA private key from p||q||g and fresh independent random bytes. Uses at most 128 rejection-sampling draws, computes y=g^x mod p, and checks algebraic consistency, not primality. Returns 1 with p||q||g||y||x, 0 for invalid input/draw limit, or 2 for insufficient randomness. Failure zeros out; used counts consumed complete draws on every return. Timing may depend on public parameters, sampling decisions, consumption and success, not otherwise on x or randomness. Contract: `VG.Spec.Dsa.generateKeyContract`. +/// +/// It runs `vg_dsa_generate_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// The function may overwrite the arguments on the stack, as the calling convention lets it. +/// +/// # Safety +/// +/// * `out` must be valid for reads and writes of `out_len` bytes. +/// * `used` must be valid for reads and writes of 8 bytes. +/// * `params` must be valid for reads of `params_len` bytes. +/// * `rand` must be valid for reads of `rand_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). +/// * params_len must be 2*p_len + q_len; out_len must be 3*p_len + 2*q_len. +/// * scratch_len must be at least 64*p_len + 1024 words; destroy scratch after use. +/// * Caller must assure domain validity and supply fresh independent random bytes. +/// * `out`, `used` and `scratch` must not overlap each other, `params` or `rand` (distinct Rust objects never do). +/// * None of `out`, `used`, `params`, `rand` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 232 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_dsa_generate_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], params: *const u8, params_len: usize, p_len: usize, q_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::dsa::vg_dsa_generate_key(out, out_len, used, params, params_len, p_len, q_len, rand, rand_len, scratch, scratch_len) } +} + /// Derive a DSA private key from big-endian p||q||g and a supplied q_len-byte scalar x, as key generation does after sampling x: checks the domain and 0 < x < q, computes y = g^x mod p and checks the key. Returns 1 with p||q||g||y||x, or 0 and zero output. Checks algebraic consistency, not primality. x must be a fresh uniform scalar the caller sampled. Timing may depend on p, q, g and success, not otherwise on x. Contract: `VG.Spec.Dsa.deriveKeyContract`. /// /// It runs `vg_dsa_derive_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. @@ -26,37 +52,30 @@ pub unsafe fn vg_dsa_derive_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: * unsafe { crate::dsa::vg_dsa_derive_key(out, out_len, params, params_len, p_len, x, q_len, scratch, scratch_len) } } -/// Generate a DSA private key from p||q||g and fresh independent random bytes. Uses at most 128 rejection-sampling draws, computes y=g^x mod p, and checks algebraic consistency, not primality. Returns 1 with p||q||g||y||x, 0 for invalid input/draw limit, or 2 for insufficient randomness. Failure zeros out; used counts consumed complete draws on every return. Timing may depend on public parameters, sampling decisions, consumption and success, not otherwise on x or randomness. Contract: `VG.Spec.Dsa.generateKeyContract`. +/// Validate packed DSA p||q||g||y||x as signing checks it: domain consistency, including g^q mod p = 1, 1 < y < p, 0 < x < q and g^x mod p = y. Returns 1 exactly when validation succeeds, otherwise 0. Does not test primality. Timing may depend on p,q,g,y and the result, not otherwise on x. Scratch contents are unspecified on return and may hold secrets. Contract: `VG.Spec.Dsa.validatePrivateKeyContract`. /// -/// It runs `vg_dsa_generate_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_dsa_validate_private_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `out` must be valid for reads and writes of `out_len` bytes. -/// * `used` must be valid for reads and writes of 8 bytes. -/// * `params` must be valid for reads of `params_len` bytes. -/// * `rand` must be valid for reads of `rand_len` bytes. +/// * `key` must be valid for reads of `key_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. /// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). -/// * params_len must be 2*p_len + q_len; out_len must be 3*p_len + 2*q_len. -/// * scratch_len must be at least 64*p_len + 1024 words; destroy scratch after use. -/// * Caller must assure domain validity and supply fresh independent random bytes. -/// * `out`, `used` and `scratch` must not overlap each other, `params` or `rand` (distinct Rust objects never do). -/// * None of `out`, `used`, `params`, `rand` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 232 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * key_len must be 3*p_len + 2*q_len; scratch_len must be at least 16*p_len words; destroy scratch after use. +/// * `scratch` must not overlap `key` (distinct Rust objects never do). +/// * Neither `key` nor `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 108 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_dsa_generate_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], params: *const u8, params_len: usize, p_len: usize, q_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_dsa_validate_private_key_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *const u8, key_len: usize, p_len: usize, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::dsa::vg_dsa_generate_key(out, out_len, used, params, params_len, p_len, q_len, rand, rand_len, scratch, scratch_len) } + unsafe { crate::dsa::vg_dsa_validate_private_key(key, key_len, p_len, q_len, scratch, scratch_len) } } -/// Search for DSA domain parameters (FIPS 186-4 A.1.1.2 and A.2.1) over the random tape `rand`, for the modulus width in `state`: each seed, its `q`, the `p` candidates from it and the generator's `h` candidates are read from `rand` in turn, with the rejected seeds counted in `state`. Returns 1 and writes `p || q || g` to `out` once parameters are found; 3 if the search is exhausted, by too many rejected seeds or by one seed needing more than the entropy budget; or 0 if the tape ran out inside a seed. Writes the octets of `rand` it consumed to `used`, and the state for the next call to `state`, whose last word is, for 0, the length of the next tape, more than the octets of `rand` after the first `used` and at most the entropy budget: keep those octets, in order, and append fresh random octets up to that length. `out` is zeros but for 1. Accepted parameters pass probable-prime tests, not primality certification. -/// -/// Contract: `VG.Spec.Dsa.parameterSearchContract`. Timing may depend on everything, `rand` included: these are public domain parameters. +/// Randomized DSA signing of a prehashed digest, for an already validated packed private key (big-endian p||q||g||y||x; vg_dsa_validate_private_key validates it). Up to eight signing attempts, each with 128 nonce draws. Returns 1 with fixed-width r||s, 0 for retry exhaustion, or 2 for insufficient random bytes; out is zero on failure and used counts consumed complete draws on every return. Timing may depend on public p,q,g,y, sampling decisions, signing success bits and consumption, not otherwise on secrets. Contract: `VG.Spec.Dsa.randomSignPrevalidatedContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_dsa_parameter_search_sha1_shani_sha256_shani` if the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_FEATURES`); or else `vg_dsa_parameter_search`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs `vg_dsa_sign_random_prevalidated`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -64,50 +83,22 @@ pub unsafe fn vg_dsa_generate_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: /// /// * `out` must be valid for reads and writes of `out_len` bytes. /// * `used` must be valid for reads and writes of 8 bytes. +/// * `key` must be valid for reads of `key_len` bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. /// * `rand` must be valid for reads of `rand_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `state` must be valid for reads and writes of 32 bytes. -/// * `state` must come from `vg_dsa_parameter_search_init`, or from the last call of this function that returned 0. -/// * `out_len` must be `2 * state[1]` plus 20 if `state[1]` is 128, else plus 32. -/// * `rand_len` must be at most the entropy budget, `state[2]`. -/// * `scratch_len` must be at least `64 * state[1] + 1024` words. -/// * `rand` must hold independent uniform random octets. They are public under this contract and must never be used for private scalars or nonces. -/// * `out`, `used`, `scratch` and `state` must not overlap each other or `rand` (distinct Rust objects never do). -/// * None of `out`, `used`, `rand`, `scratch` and `state` may overlap the arguments on the stack, overlap the return address on the stack or the 216 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). +/// * key_len must be 3*p_len + 2*q_len; out_len must be 2*q_len. +/// * scratch_len must be at least 16*p_len + 64 words; destroy scratch after use. +/// * The exact key bytes must satisfy validatePrivateKeyBytes p_len q_len key = true. A successful vg_dsa_validate_private_key call establishes this only while the key bytes remain unchanged. Primality remains the caller's responsibility. +/// * Provide fresh independent random octets; never reuse a completed signing tape for another signature. +/// * `out`, `used` and `scratch` must not overlap each other, `key`, `digest` or `rand` (distinct Rust objects never do). +/// * None of `out`, `used`, `key`, `digest`, `rand` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 232 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_dsa_parameter_search_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32 { - /// The type of the implementations. - type Impl = unsafe extern "C" fn(out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32; - /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only - /// features the CPU has. - #[cold] - fn select() -> Impl { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_FEATURES) } - || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_FEATURES) - { - return crate::dsa::vg_dsa_parameter_search_sha1_shani_sha256_shani; - } - crate::dsa::vg_dsa_parameter_search - } - /// The first call: chooses the implementation, records it in `CHOSEN` for the calls - /// after it, and runs it. - #[cold] - unsafe extern "C" fn first(out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32 { - let f = select(); - CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); - // SAFETY: the caller's obligations, which are those of every implementation, `f` - // among them, and the CPU has the features `f` needs. - unsafe { f(out, out_len, used, rand, rand_len, scratch, scratch_len, state) } - } - /// The function to call: `first`, until it records the implementation it chose. - static CHOSEN: core::sync::atomic::AtomicPtr<()> = - core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); - // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, - // or an implementation `select` returned, which needs only features the CPU has. Calling - // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, used, rand, rand_len, scratch, scratch_len, state) } +pub unsafe fn vg_dsa_sign_random_prevalidated_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], key: *const u8, key_len: usize, p_len: usize, q_len: usize, digest: *const u8, digest_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::dsa::vg_dsa_sign_random_prevalidated(out, out_len, used, key, key_len, p_len, q_len, digest, digest_len, rand, rand_len, scratch, scratch_len) } } /// Validate packed DSA p||q||g||y, including domain consistency and both subgroup memberships. Returns 1 exactly when validation succeeds, otherwise 0. Does not test primality. The key is public; scratch contents are unspecified on return. Contract: `VG.Spec.Dsa.validatePublicKeyContract`. @@ -156,27 +147,6 @@ pub unsafe fn vg_dsa_verify_prevalidated_auto(_dit: verified_garbage_cpu::Dit<'_ unsafe { crate::dsa::vg_dsa_verify_prevalidated(key, key_len, p_len, q_len, signature, signature_len, digest, digest_len, scratch, scratch_len) } } -/// Validate packed DSA p||q||g||y||x as signing checks it: domain consistency, including g^q mod p = 1, 1 < y < p, 0 < x < q and g^x mod p = y. Returns 1 exactly when validation succeeds, otherwise 0. Does not test primality. Timing may depend on p,q,g,y and the result, not otherwise on x. Scratch contents are unspecified on return and may hold secrets. Contract: `VG.Spec.Dsa.validatePrivateKeyContract`. -/// -/// It runs `vg_dsa_validate_private_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// The function may overwrite the arguments on the stack, as the calling convention lets it. -/// -/// # Safety -/// -/// * `key` must be valid for reads of `key_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). -/// * key_len must be 3*p_len + 2*q_len; scratch_len must be at least 16*p_len words; destroy scratch after use. -/// * `scratch` must not overlap `key` (distinct Rust objects never do). -/// * Neither `key` nor `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 108 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_dsa_validate_private_key_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *const u8, key_len: usize, p_len: usize, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::dsa::vg_dsa_validate_private_key(key, key_len, p_len, q_len, scratch, scratch_len) } -} - /// Start a search for DSA domain parameters with a modulus of `p_len` octets (128, 256 or 384: 1024, 2048 or 3072 bits, with a subgroup of 160, 256 and 256 bits). Returns 1 and writes the search's four public words to `state`: the seeds it may still reject, `p_len`, its entropy budget, and the length of the first random tape to give `vg_dsa_parameter_search`. Returns 0 and writes zeros for any other `p_len`. /// /// Contract: `VG.Spec.Dsa.parameterInitContract`. @@ -195,9 +165,11 @@ pub unsafe fn vg_dsa_parameter_search_init_auto(_dit: verified_garbage_cpu::Dit< unsafe { crate::dsa::vg_dsa_parameter_search_init(state, p_len) } } -/// Randomized DSA signing of a prehashed digest, for an already validated packed private key (big-endian p||q||g||y||x; vg_dsa_validate_private_key validates it). Up to eight signing attempts, each with 128 nonce draws. Returns 1 with fixed-width r||s, 0 for retry exhaustion, or 2 for insufficient random bytes; out is zero on failure and used counts consumed complete draws on every return. Timing may depend on public p,q,g,y, sampling decisions, signing success bits and consumption, not otherwise on secrets. Contract: `VG.Spec.Dsa.randomSignPrevalidatedContract`. +/// Search for DSA domain parameters (FIPS 186-4 A.1.1.2 and A.2.1) over the random tape `rand`, for the modulus width in `state`: each seed, its `q`, the `p` candidates from it and the generator's `h` candidates are read from `rand` in turn, with the rejected seeds counted in `state`. Returns 1 and writes `p || q || g` to `out` once parameters are found; 3 if the search is exhausted, by too many rejected seeds or by one seed needing more than the entropy budget; or 0 if the tape ran out inside a seed. Writes the octets of `rand` it consumed to `used`, and the state for the next call to `state`, whose last word is, for 0, the length of the next tape, more than the octets of `rand` after the first `used` and at most the entropy budget: keep those octets, in order, and append fresh random octets up to that length. `out` is zeros but for 1. Accepted parameters pass probable-prime tests, not primality certification. /// -/// It runs `vg_dsa_sign_random_prevalidated`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// Contract: `VG.Spec.Dsa.parameterSearchContract`. Timing may depend on everything, `rand` included: these are public domain parameters. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_dsa_parameter_search_sha1_shani_sha256_shani` if the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_FEATURES`); or else `vg_dsa_parameter_search`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -205,20 +177,48 @@ pub unsafe fn vg_dsa_parameter_search_init_auto(_dit: verified_garbage_cpu::Dit< /// /// * `out` must be valid for reads and writes of `out_len` bytes. /// * `used` must be valid for reads and writes of 8 bytes. -/// * `key` must be valid for reads of `key_len` bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. /// * `rand` must be valid for reads of `rand_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). -/// * key_len must be 3*p_len + 2*q_len; out_len must be 2*q_len. -/// * scratch_len must be at least 16*p_len + 64 words; destroy scratch after use. -/// * The exact key bytes must satisfy validatePrivateKeyBytes p_len q_len key = true. A successful vg_dsa_validate_private_key call establishes this only while the key bytes remain unchanged. Primality remains the caller's responsibility. -/// * Provide fresh independent random octets; never reuse a completed signing tape for another signature. -/// * `out`, `used` and `scratch` must not overlap each other, `key`, `digest` or `rand` (distinct Rust objects never do). -/// * None of `out`, `used`, `key`, `digest`, `rand` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 232 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `state` must be valid for reads and writes of 32 bytes. +/// * `state` must come from `vg_dsa_parameter_search_init`, or from the last call of this function that returned 0. +/// * `out_len` must be `2 * state[1]` plus 20 if `state[1]` is 128, else plus 32. +/// * `rand_len` must be at most the entropy budget, `state[2]`. +/// * `scratch_len` must be at least `64 * state[1] + 1024` words. +/// * `rand` must hold independent uniform random octets. They are public under this contract and must never be used for private scalars or nonces. +/// * `out`, `used`, `scratch` and `state` must not overlap each other or `rand` (distinct Rust objects never do). +/// * None of `out`, `used`, `rand`, `scratch` and `state` may overlap the arguments on the stack, overlap the return address on the stack or the 216 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_dsa_sign_random_prevalidated_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], key: *const u8, key_len: usize, p_len: usize, q_len: usize, digest: *const u8, digest_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::dsa::vg_dsa_sign_random_prevalidated(out, out_len, used, key, key_len, p_len, q_len, digest, digest_len, rand, rand_len, scratch, scratch_len) } +pub unsafe fn vg_dsa_parameter_search_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32 { + /// The type of the implementations. + type Impl = unsafe extern "C" fn(out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32; + /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only + /// features the CPU has. + #[cold] + fn select() -> Impl { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_FEATURES) } + || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_FEATURES) + { + return crate::dsa::vg_dsa_parameter_search_sha1_shani_sha256_shani; + } + crate::dsa::vg_dsa_parameter_search + } + /// The first call: chooses the implementation, records it in `CHOSEN` for the calls + /// after it, and runs it. + #[cold] + unsafe extern "C" fn first(out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32 { + let f = select(); + CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); + // SAFETY: the caller's obligations, which are those of every implementation, `f` + // among them, and the CPU has the features `f` needs. + unsafe { f(out, out_len, used, rand, rand_len, scratch, scratch_len, state) } + } + /// The function to call: `first`, until it records the implementation it chose. + static CHOSEN: core::sync::atomic::AtomicPtr<()> = + core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); + // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, + // or an implementation `select` returned, which needs only features the CPU has. Calling + // either carries the caller's obligations, which are those of every implementation. + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, used, rand, rand_len, scratch, scratch_len, state) } } diff --git a/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_brainpoolp256r1.rs b/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_brainpoolp256r1.rs index f3e885db252..11940ca5bf9 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_brainpoolp256r1.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_brainpoolp256r1.rs @@ -2,54 +2,54 @@ //! The dispatchers of the verified `ecdsa_brainpoolp256r1` functions for `x86` //! (`crate::ecdsa_brainpoolp256r1`, `VG.Rust.dispatcher`). -/// Deterministic ECDSA signature generation over brainpoolP256r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). +/// ECDSA signature verification over brainpoolP256r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. /// -/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. /// -/// It runs `vg_ecdsa_brainpoolp256r1_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ecdsa_brainpoolp256r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 64 bytes. -/// * `d` must be valid for reads of 32 bytes. +/// * `public` must be valid for reads of 65 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 64 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. -/// * `digest_len` must be the digest length of the hash function `hmac` names. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). -/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the arguments on the stack, overlap the return address on the stack or the 588 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_brainpoolp256r1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { +pub unsafe fn vg_ecdsa_brainpoolp256r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_sign(out, d, digest, digest_len, scratch, hmac) } + unsafe { crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_verify(public, digest, digest_len, sig, scratch) } } -/// ECDSA signature verification over brainpoolP256r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// Deterministic ECDSA signature generation over brainpoolP256r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). /// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. /// -/// It runs `vg_ecdsa_brainpoolp256r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ecdsa_brainpoolp256r1_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `public` must be valid for reads of 65 bytes. +/// * `out` must be valid for reads and writes of 64 bytes. +/// * `d` must be valid for reads of 32 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 64 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. +/// * `digest_len` must be the digest length of the hash function `hmac` names. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). +/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). +/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the arguments on the stack, overlap the return address on the stack or the 588 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_brainpoolp256r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ecdsa_brainpoolp256r1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_verify(public, digest, digest_len, sig, scratch) } + unsafe { crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_sign(out, d, digest, digest_len, scratch, hmac) } } diff --git a/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_brainpoolp384r1.rs b/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_brainpoolp384r1.rs index 070f5b480bc..2eac0ef8614 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_brainpoolp384r1.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_brainpoolp384r1.rs @@ -2,54 +2,54 @@ //! The dispatchers of the verified `ecdsa_brainpoolp384r1` functions for `x86` //! (`crate::ecdsa_brainpoolp384r1`, `VG.Rust.dispatcher`). -/// Deterministic ECDSA signature generation over brainpoolP384r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (48 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (48 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). +/// ECDSA signature verification over brainpoolP384r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. /// -/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. /// -/// It runs `vg_ecdsa_brainpoolp384r1_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ecdsa_brainpoolp384r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 96 bytes. -/// * `d` must be valid for reads of 48 bytes. +/// * `public` must be valid for reads of 97 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 96 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. -/// * `digest_len` must be the digest length of the hash function `hmac` names. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). -/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the arguments on the stack, overlap the return address on the stack or the 588 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_brainpoolp384r1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { +pub unsafe fn vg_ecdsa_brainpoolp384r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_sign(out, d, digest, digest_len, scratch, hmac) } + unsafe { crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_verify(public, digest, digest_len, sig, scratch) } } -/// ECDSA signature verification over brainpoolP384r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// Deterministic ECDSA signature generation over brainpoolP384r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (48 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (48 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). /// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. /// -/// It runs `vg_ecdsa_brainpoolp384r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ecdsa_brainpoolp384r1_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `public` must be valid for reads of 97 bytes. +/// * `out` must be valid for reads and writes of 96 bytes. +/// * `d` must be valid for reads of 48 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 96 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. +/// * `digest_len` must be the digest length of the hash function `hmac` names. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). +/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). +/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the arguments on the stack, overlap the return address on the stack or the 588 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_brainpoolp384r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ecdsa_brainpoolp384r1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_verify(public, digest, digest_len, sig, scratch) } + unsafe { crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_sign(out, d, digest, digest_len, scratch, hmac) } } diff --git a/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_brainpoolp512r1.rs b/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_brainpoolp512r1.rs index c51cb512d86..6c6d4ae6b96 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_brainpoolp512r1.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_brainpoolp512r1.rs @@ -2,54 +2,54 @@ //! The dispatchers of the verified `ecdsa_brainpoolp512r1` functions for `x86` //! (`crate::ecdsa_brainpoolp512r1`, `VG.Rust.dispatcher`). -/// Deterministic ECDSA signature generation over brainpoolP512r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (64 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (64 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). +/// ECDSA signature verification over brainpoolP512r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 64 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 512 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 64 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. /// -/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. /// -/// It runs `vg_ecdsa_brainpoolp512r1_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ecdsa_brainpoolp512r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 128 bytes. -/// * `d` must be valid for reads of 64 bytes. +/// * `public` must be valid for reads of 129 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 128 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. -/// * `digest_len` must be the digest length of the hash function `hmac` names. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). -/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the arguments on the stack, overlap the return address on the stack or the 588 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 128 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_brainpoolp512r1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 128], d: *const [u8; 64], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { +pub unsafe fn vg_ecdsa_brainpoolp512r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 129], digest: *const u8, digest_len: usize, sig: *const [u8; 128], scratch: *mut [u64; 1024]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_sign(out, d, digest, digest_len, scratch, hmac) } + unsafe { crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_verify(public, digest, digest_len, sig, scratch) } } -/// ECDSA signature verification over brainpoolP512r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 64 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 512 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 64 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// Deterministic ECDSA signature generation over brainpoolP512r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (64 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (64 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). /// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. /// -/// It runs `vg_ecdsa_brainpoolp512r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ecdsa_brainpoolp512r1_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `public` must be valid for reads of 129 bytes. +/// * `out` must be valid for reads and writes of 128 bytes. +/// * `d` must be valid for reads of 64 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 128 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 128 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. +/// * `digest_len` must be the digest length of the hash function `hmac` names. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). +/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). +/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the arguments on the stack, overlap the return address on the stack or the 588 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_brainpoolp512r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 129], digest: *const u8, digest_len: usize, sig: *const [u8; 128], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ecdsa_brainpoolp512r1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 128], d: *const [u8; 64], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_verify(public, digest, digest_len, sig, scratch) } + unsafe { crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_sign(out, d, digest, digest_len, scratch, hmac) } } diff --git a/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_p192.rs b/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_p192.rs index 788eaefb3ed..e76f410b6ec 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_p192.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_p192.rs @@ -2,54 +2,54 @@ //! The dispatchers of the verified `ecdsa_p192` functions for `x86` //! (`crate::ecdsa_p192`, `VG.Rust.dispatcher`). -/// Deterministic ECDSA signature generation over P-192 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (24 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (24 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). +/// ECDSA signature verification over P-192 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 24 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 192 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 24 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. /// -/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. /// -/// It runs `vg_ecdsa_p192_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ecdsa_p192_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 48 bytes. -/// * `d` must be valid for reads of 24 bytes. +/// * `public` must be valid for reads of 49 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 48 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. -/// * `digest_len` must be the digest length of the hash function `hmac` names. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). -/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the arguments on the stack, overlap the return address on the stack or the 588 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p192_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 48], d: *const [u8; 24], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { +pub unsafe fn vg_ecdsa_p192_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 49], digest: *const u8, digest_len: usize, sig: *const [u8; 48], scratch: *mut [u64; 1024]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p192::vg_ecdsa_p192_sign(out, d, digest, digest_len, scratch, hmac) } + unsafe { crate::ecdsa_p192::vg_ecdsa_p192_verify(public, digest, digest_len, sig, scratch) } } -/// ECDSA signature verification over P-192 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 24 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 192 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 24 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// Deterministic ECDSA signature generation over P-192 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (24 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (24 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. /// -/// It runs `vg_ecdsa_p192_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ecdsa_p192_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `public` must be valid for reads of 49 bytes. +/// * `out` must be valid for reads and writes of 48 bytes. +/// * `d` must be valid for reads of 24 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 48 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. +/// * `digest_len` must be the digest length of the hash function `hmac` names. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). +/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). +/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the arguments on the stack, overlap the return address on the stack or the 588 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p192_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 49], digest: *const u8, digest_len: usize, sig: *const [u8; 48], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ecdsa_p192_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 48], d: *const [u8; 24], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p192::vg_ecdsa_p192_verify(public, digest, digest_len, sig, scratch) } + unsafe { crate::ecdsa_p192::vg_ecdsa_p192_sign(out, d, digest, digest_len, scratch, hmac) } } diff --git a/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_p224.rs b/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_p224.rs index e4092492a76..8f07ba7abdc 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_p224.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_p224.rs @@ -2,54 +2,54 @@ //! The dispatchers of the verified `ecdsa_p224` functions for `x86` //! (`crate::ecdsa_p224`, `VG.Rust.dispatcher`). -/// Deterministic ECDSA signature generation over P-224 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (28 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (28 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). +/// ECDSA signature verification over P-224 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 28 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 224 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 28 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. /// -/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. /// -/// It runs `vg_ecdsa_p224_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ecdsa_p224_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 56 bytes. -/// * `d` must be valid for reads of 28 bytes. +/// * `public` must be valid for reads of 57 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 56 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. -/// * `digest_len` must be the digest length of the hash function `hmac` names. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). -/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the arguments on the stack, overlap the return address on the stack or the 588 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p224_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], d: *const [u8; 28], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { +pub unsafe fn vg_ecdsa_p224_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 57], digest: *const u8, digest_len: usize, sig: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p224::vg_ecdsa_p224_sign(out, d, digest, digest_len, scratch, hmac) } + unsafe { crate::ecdsa_p224::vg_ecdsa_p224_verify(public, digest, digest_len, sig, scratch) } } -/// ECDSA signature verification over P-224 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 28 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 224 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 28 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// Deterministic ECDSA signature generation over P-224 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (28 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (28 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. /// -/// It runs `vg_ecdsa_p224_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ecdsa_p224_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `public` must be valid for reads of 57 bytes. +/// * `out` must be valid for reads and writes of 56 bytes. +/// * `d` must be valid for reads of 28 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 56 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. +/// * `digest_len` must be the digest length of the hash function `hmac` names. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). +/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). +/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the arguments on the stack, overlap the return address on the stack or the 588 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p224_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 57], digest: *const u8, digest_len: usize, sig: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ecdsa_p224_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], d: *const [u8; 28], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p224::vg_ecdsa_p224_verify(public, digest, digest_len, sig, scratch) } + unsafe { crate::ecdsa_p224::vg_ecdsa_p224_sign(out, d, digest, digest_len, scratch, hmac) } } diff --git a/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_p256.rs b/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_p256.rs index cba390193a5..e08bd307a17 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_p256.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_p256.rs @@ -2,54 +2,54 @@ //! The dispatchers of the verified `ecdsa_p256` functions for `x86` //! (`crate::ecdsa_p256`, `VG.Rust.dispatcher`). -/// Deterministic ECDSA signature generation over P-256 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). +/// ECDSA signature verification over P-256 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. /// -/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. /// -/// It runs `vg_ecdsa_p256_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ecdsa_p256_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 64 bytes. -/// * `d` must be valid for reads of 32 bytes. +/// * `public` must be valid for reads of 65 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 64 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. -/// * `digest_len` must be the digest length of the hash function `hmac` names. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). -/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the arguments on the stack, overlap the return address on the stack or the 588 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 128 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p256_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { +pub unsafe fn vg_ecdsa_p256_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p256::vg_ecdsa_p256_sign(out, d, digest, digest_len, scratch, hmac) } + unsafe { crate::ecdsa_p256::vg_ecdsa_p256_verify(public, digest, digest_len, sig, scratch) } } -/// ECDSA signature verification over P-256 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// Deterministic ECDSA signature generation over P-256 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. /// -/// It runs `vg_ecdsa_p256_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ecdsa_p256_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `public` must be valid for reads of 65 bytes. +/// * `out` must be valid for reads and writes of 64 bytes. +/// * `d` must be valid for reads of 32 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 64 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 128 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. +/// * `digest_len` must be the digest length of the hash function `hmac` names. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). +/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). +/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the arguments on the stack, overlap the return address on the stack or the 588 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p256_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ecdsa_p256_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p256::vg_ecdsa_p256_verify(public, digest, digest_len, sig, scratch) } + unsafe { crate::ecdsa_p256::vg_ecdsa_p256_sign(out, d, digest, digest_len, scratch, hmac) } } diff --git a/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_p384.rs b/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_p384.rs index 3b6d56188f0..97274e6bfb5 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_p384.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_p384.rs @@ -2,54 +2,54 @@ //! The dispatchers of the verified `ecdsa_p384` functions for `x86` //! (`crate::ecdsa_p384`, `VG.Rust.dispatcher`). -/// Deterministic ECDSA signature generation over P-384 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (48 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (48 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). +/// ECDSA signature verification over P-384 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. /// -/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. /// -/// It runs `vg_ecdsa_p384_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ecdsa_p384_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 96 bytes. -/// * `d` must be valid for reads of 48 bytes. +/// * `public` must be valid for reads of 97 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 96 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. -/// * `digest_len` must be the digest length of the hash function `hmac` names. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). -/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the arguments on the stack, overlap the return address on the stack or the 588 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p384_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { +pub unsafe fn vg_ecdsa_p384_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p384::vg_ecdsa_p384_sign(out, d, digest, digest_len, scratch, hmac) } + unsafe { crate::ecdsa_p384::vg_ecdsa_p384_verify(public, digest, digest_len, sig, scratch) } } -/// ECDSA signature verification over P-384 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// Deterministic ECDSA signature generation over P-384 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (48 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (48 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. /// -/// It runs `vg_ecdsa_p384_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ecdsa_p384_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `public` must be valid for reads of 97 bytes. +/// * `out` must be valid for reads and writes of 96 bytes. +/// * `d` must be valid for reads of 48 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 96 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. +/// * `digest_len` must be the digest length of the hash function `hmac` names. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). +/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). +/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the arguments on the stack, overlap the return address on the stack or the 588 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p384_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ecdsa_p384_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p384::vg_ecdsa_p384_verify(public, digest, digest_len, sig, scratch) } + unsafe { crate::ecdsa_p384::vg_ecdsa_p384_sign(out, d, digest, digest_len, scratch, hmac) } } diff --git a/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_p521.rs b/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_p521.rs index 890d8c00017..b30a0ce72a8 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_p521.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_p521.rs @@ -2,54 +2,54 @@ //! The dispatchers of the verified `ecdsa_p521` functions for `x86` //! (`crate::ecdsa_p521`, `VG.Rust.dispatcher`). -/// Deterministic ECDSA signature generation over P-521 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (66 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (66 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). +/// ECDSA signature verification over P-521 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 66 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 521 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 66 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. /// -/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. /// -/// It runs `vg_ecdsa_p521_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ecdsa_p521_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 132 bytes. -/// * `d` must be valid for reads of 66 bytes. +/// * `public` must be valid for reads of 133 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 132 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. -/// * `digest_len` must be the digest length of the hash function `hmac` names. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). -/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the arguments on the stack, overlap the return address on the stack or the 588 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p521_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 132], d: *const [u8; 66], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { +pub unsafe fn vg_ecdsa_p521_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 133], digest: *const u8, digest_len: usize, sig: *const [u8; 132], scratch: *mut [u64; 1024]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p521::vg_ecdsa_p521_sign(out, d, digest, digest_len, scratch, hmac) } + unsafe { crate::ecdsa_p521::vg_ecdsa_p521_verify(public, digest, digest_len, sig, scratch) } } -/// ECDSA signature verification over P-521 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 66 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 521 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 66 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// Deterministic ECDSA signature generation over P-521 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (66 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (66 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. /// -/// It runs `vg_ecdsa_p521_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ecdsa_p521_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `public` must be valid for reads of 133 bytes. +/// * `out` must be valid for reads and writes of 132 bytes. +/// * `d` must be valid for reads of 66 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 132 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. +/// * `digest_len` must be the digest length of the hash function `hmac` names. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). +/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). +/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the arguments on the stack, overlap the return address on the stack or the 588 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p521_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 133], digest: *const u8, digest_len: usize, sig: *const [u8; 132], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ecdsa_p521_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 132], d: *const [u8; 66], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p521::vg_ecdsa_p521_verify(public, digest, digest_len, sig, scratch) } + unsafe { crate::ecdsa_p521::vg_ecdsa_p521_sign(out, d, digest, digest_len, scratch, hmac) } } diff --git a/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_secp256k1.rs b/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_secp256k1.rs index 5124ce4a5e2..48c7694c55d 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_secp256k1.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/ecdsa_secp256k1.rs @@ -2,54 +2,54 @@ //! The dispatchers of the verified `ecdsa_secp256k1` functions for `x86` //! (`crate::ecdsa_secp256k1`, `VG.Rust.dispatcher`). -/// Deterministic ECDSA signature generation over secp256k1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). +/// ECDSA signature verification over secp256k1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. /// -/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. /// -/// It runs `vg_ecdsa_secp256k1_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ecdsa_secp256k1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 64 bytes. -/// * `d` must be valid for reads of 32 bytes. +/// * `public` must be valid for reads of 65 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 64 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. -/// * `digest_len` must be the digest length of the hash function `hmac` names. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). -/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the arguments on the stack, overlap the return address on the stack or the 588 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_secp256k1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { +pub unsafe fn vg_ecdsa_secp256k1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_sign(out, d, digest, digest_len, scratch, hmac) } + unsafe { crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_verify(public, digest, digest_len, sig, scratch) } } -/// ECDSA signature verification over secp256k1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// Deterministic ECDSA signature generation over secp256k1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. /// -/// It runs `vg_ecdsa_secp256k1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ecdsa_secp256k1_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `public` must be valid for reads of 65 bytes. +/// * `out` must be valid for reads and writes of 64 bytes. +/// * `d` must be valid for reads of 32 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 64 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. +/// * `digest_len` must be the digest length of the hash function `hmac` names. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). +/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). +/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the arguments on the stack, overlap the return address on the stack or the 588 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_secp256k1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ecdsa_secp256k1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_verify(public, digest, digest_len, sig, scratch) } + unsafe { crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_sign(out, d, digest, digest_len, scratch, hmac) } } diff --git a/crates/verified-garbage-asm-x86/src/dispatch/ed25519.rs b/crates/verified-garbage-asm-x86/src/dispatch/ed25519.rs index 79dfb892224..95665800223 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/ed25519.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/ed25519.rs @@ -2,28 +2,6 @@ //! The dispatchers of the verified `ed25519` functions for `x86` //! (`crate::ed25519`, `VG.Rust.dispatcher`). -/// Ed25519 verification (RFC 8032 §5.1.7): returns 1 if the 64-byte signature at `signature` verifies for the 32-byte public key at `pk` and the `len` bytes at `message`, and 0 otherwise. Uses pure Ed25519, with no context or prehash. Checks canonical point encodings, S < L, and `[S]B = R + [k]A` with the challenge k = SHA-512(R || A || M) reduced modulo L, as in RFC 8032 §6. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed25519.verifyContract`. Not constant time: timing may depend on the public key, message and signature. -/// -/// It runs `vg_ed25519_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// The function may overwrite the arguments on the stack, as the calling convention lets it. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 32 bytes. -/// * `message` must be valid for reads of `len` bytes. -/// * `signature` must be valid for reads of 64 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `scratch` must not overlap `pk`, `message` or `signature` (distinct Rust objects never do). -/// * None of `pk`, `message`, `signature` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 280 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ed25519_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 32], message: *const u8, len: usize, signature: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ed25519::vg_ed25519_verify(pk, message, len, signature, scratch) } -} - /// Ed25519 key-pair derivation (RFC 8032 §5.1.5): for the 32-byte private seed at `seed`, writes its 64-byte SHA-512 digest (§5.1.5, step 1) to `*expanded` and its 32-byte public key to `*out`, including pruning: the digest and public key that `vg_ed25519_sign_expanded` takes. Contract: `VG.Spec.Ed25519.keyPairContract`. Constant time: only pointers may affect timing. /// /// It runs `vg_ed25519_key_pair`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. @@ -72,3 +50,25 @@ pub unsafe fn vg_ed25519_sign_expanded_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ed25519::vg_ed25519_sign_expanded(out, expanded, pk, message, len, scratch) } } + +/// Ed25519 verification (RFC 8032 §5.1.7): returns 1 if the 64-byte signature at `signature` verifies for the 32-byte public key at `pk` and the `len` bytes at `message`, and 0 otherwise. Uses pure Ed25519, with no context or prehash. Checks canonical point encodings, S < L, and `[S]B = R + [k]A` with the challenge k = SHA-512(R || A || M) reduced modulo L, as in RFC 8032 §6. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed25519.verifyContract`. Not constant time: timing may depend on the public key, message and signature. +/// +/// It runs `vg_ed25519_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// The function may overwrite the arguments on the stack, as the calling convention lets it. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 32 bytes. +/// * `message` must be valid for reads of `len` bytes. +/// * `signature` must be valid for reads of 64 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `scratch` must not overlap `pk`, `message` or `signature` (distinct Rust objects never do). +/// * None of `pk`, `message`, `signature` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 280 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ed25519_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 32], message: *const u8, len: usize, signature: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ed25519::vg_ed25519_verify(pk, message, len, signature, scratch) } +} diff --git a/crates/verified-garbage-asm-x86/src/dispatch/ed448.rs b/crates/verified-garbage-asm-x86/src/dispatch/ed448.rs index 29a3405133a..04e34a7d3ba 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/ed448.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/ed448.rs @@ -2,29 +2,6 @@ //! The dispatchers of the verified `ed448` functions for `x86` //! (`crate::ed448`, `VG.Rust.dispatcher`). -/// Ed448 verification (RFC 8032 §5.2.7): returns 1 if the 114-byte signature at `signature` verifies for the 57-byte public key at `pk`, the `ctxlen` bytes of context at `context` and the `len` bytes at `message`, and 0 otherwise (always 0 for a context longer than 255 bytes). Uses Ed448 with a context, not Ed448ph (no prehash). Checks canonical point encodings, S < L, and the cofactored equation `[4][S]B = [4]R + [4][k]A` with the challenge k = SHAKE256(dom4(0, C) || R || A || M, 114) reduced modulo L. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed448.verifyContract`. Not constant time: timing may depend on the public key, context, message and signature. -/// -/// It runs `vg_ed448_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// The function may overwrite the arguments on the stack, as the calling convention lets it. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 57 bytes. -/// * `context` must be valid for reads of `ctxlen` bytes. -/// * `message` must be valid for reads of `len` bytes. -/// * `signature` must be valid for reads of 114 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `scratch` must not overlap `pk`, `context`, `message` or `signature` (distinct Rust objects never do). -/// * None of `pk`, `context`, `message`, `signature` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 280 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ed448_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, signature: *const [u8; 114], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ed448::vg_ed448_verify(pk, context, ctxlen, message, len, signature, scratch) } -} - /// Ed448 public-key derivation (RFC 8032 §5.2.5): writes the 57-byte public key to `*out`, from the 57-byte private key at `seed`, including SHAKE256 and pruning. Contract: `VG.Spec.Ed448.publicKeyContract`. Constant time: only pointers may affect timing. /// /// It runs `vg_ed448_public_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. @@ -73,3 +50,26 @@ pub unsafe fn vg_ed448_sign_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, out // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ed448::vg_ed448_sign_cached(out, seed, pk, context, ctxlen, message, len, scratch) } } + +/// Ed448 verification (RFC 8032 §5.2.7): returns 1 if the 114-byte signature at `signature` verifies for the 57-byte public key at `pk`, the `ctxlen` bytes of context at `context` and the `len` bytes at `message`, and 0 otherwise (always 0 for a context longer than 255 bytes). Uses Ed448 with a context, not Ed448ph (no prehash). Checks canonical point encodings, S < L, and the cofactored equation `[4][S]B = [4]R + [4][k]A` with the challenge k = SHAKE256(dom4(0, C) || R || A || M, 114) reduced modulo L. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed448.verifyContract`. Not constant time: timing may depend on the public key, context, message and signature. +/// +/// It runs `vg_ed448_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// The function may overwrite the arguments on the stack, as the calling convention lets it. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 57 bytes. +/// * `context` must be valid for reads of `ctxlen` bytes. +/// * `message` must be valid for reads of `len` bytes. +/// * `signature` must be valid for reads of 114 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `scratch` must not overlap `pk`, `context`, `message` or `signature` (distinct Rust objects never do). +/// * None of `pk`, `context`, `message`, `signature` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 280 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ed448_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, signature: *const [u8; 114], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ed448::vg_ed448_verify(pk, context, ctxlen, message, len, signature, scratch) } +} diff --git a/crates/verified-garbage-asm-x86/src/dispatch/ffdh.rs b/crates/verified-garbage-asm-x86/src/dispatch/ffdh.rs index 89ef4c9d345..79715c7af36 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/ffdh.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/ffdh.rs @@ -2,38 +2,6 @@ //! The dispatchers of the verified `ffdh` functions for `x86` //! (`crate::ffdh`, `VG.Rust.dispatcher`). -/// Finite-field Diffie–Hellman (SP 800-56A Rev. 3 §5.7.1.1, PKCS #3 §8). With `domain` as `vg_ffdh_check_domain` takes it, the private scalar x (`x_len` bytes) and the peer's public value y (`p_len` bytes), most significant first, computes `z = y^x mod p`; returns 0 and writes zeros if z is 0, 1 or `p − 1`, and otherwise returns 1 and writes z (`p_len` bytes, most significant first) to `out`. Applies no KDF. -/// -/// Contract: `VG.Spec.Ffdh.exchangeContract`. Timing may depend on the pointers, the lengths, the domain, y and the value returned, not otherwise on x; the exponentiation may run over all `x_len` bytes of x, leading zeros included. -/// -/// It runs `vg_ffdh_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// The function may overwrite the arguments on the stack, as the calling convention lets it. -/// -/// # Safety -/// -/// * `out` must be valid for reads and writes of `out_len` bytes. -/// * `domain` must be valid for reads of `domain_len` bytes. -/// * `x` must be valid for reads of `x_len` bytes. -/// * `peer` must be valid for reads of `peer_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `p_len` must be in 64..=1024. -/// * `domain_len` must be `2 * p_len` or `3 * p_len`. -/// * `out_len` and `peer_len` must be `p_len`. -/// * `x_len` must be in 1..=`p_len`. -/// * For the result to be specified, `domain` must be one that `vg_ffdh_check_domain` accepted (returned 1 for); otherwise it is unspecified. -/// * For the result to be specified, `peer` must also be a value that `vg_ffdh_check_public` accepted for this domain. -/// * `scratch_len` must be at least `16 * p_len + 32`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `domain`, `x` or `peer` (distinct Rust objects never do). -/// * None of `out`, `domain`, `x`, `peer` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 172 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ffdh_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, domain: *const u8, domain_len: usize, p_len: usize, x: *const u8, x_len: usize, peer: *const u8, peer_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ffdh::vg_ffdh_exchange(out, out_len, domain, domain_len, p_len, x, x_len, peer, peer_len, scratch, scratch_len) } -} - /// Checks imported finite-field Diffie–Hellman parameters (SP 800-56A Rev. 3 §5.5.2, without primality tests). `domain` is p, g and, if `domain_len` is `3 * p_len`, q, each `p_len` bytes, most significant first. Returns 1 if p has 512 to 8192 bits, its first byte is not zero and it is odd, `1 < g < p − 1`, and, with q, q is odd, `2 < q < p − 1`, q divides `p − 1` and `g^q mod p = 1`; otherwise 0. Does not test p or q for primality: the caller must establish their provenance. /// /// Contract: `VG.Spec.Ffdh.checkDomainContract`. Timing may depend on the pointers, the lengths and the domain. @@ -117,6 +85,38 @@ pub unsafe fn vg_ffdh_check_private_auto(_dit: verified_garbage_cpu::Dit<'_>, do unsafe { crate::ffdh::vg_ffdh_check_private(domain, domain_len, p_len, y, y_len, x, x_len, scratch, scratch_len) } } +/// Finite-field Diffie–Hellman (SP 800-56A Rev. 3 §5.7.1.1, PKCS #3 §8). With `domain` as `vg_ffdh_check_domain` takes it, the private scalar x (`x_len` bytes) and the peer's public value y (`p_len` bytes), most significant first, computes `z = y^x mod p`; returns 0 and writes zeros if z is 0, 1 or `p − 1`, and otherwise returns 1 and writes z (`p_len` bytes, most significant first) to `out`. Applies no KDF. +/// +/// Contract: `VG.Spec.Ffdh.exchangeContract`. Timing may depend on the pointers, the lengths, the domain, y and the value returned, not otherwise on x; the exponentiation may run over all `x_len` bytes of x, leading zeros included. +/// +/// It runs `vg_ffdh_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// The function may overwrite the arguments on the stack, as the calling convention lets it. +/// +/// # Safety +/// +/// * `out` must be valid for reads and writes of `out_len` bytes. +/// * `domain` must be valid for reads of `domain_len` bytes. +/// * `x` must be valid for reads of `x_len` bytes. +/// * `peer` must be valid for reads of `peer_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `p_len` must be in 64..=1024. +/// * `domain_len` must be `2 * p_len` or `3 * p_len`. +/// * `out_len` and `peer_len` must be `p_len`. +/// * `x_len` must be in 1..=`p_len`. +/// * For the result to be specified, `domain` must be one that `vg_ffdh_check_domain` accepted (returned 1 for); otherwise it is unspecified. +/// * For the result to be specified, `peer` must also be a value that `vg_ffdh_check_public` accepted for this domain. +/// * `scratch_len` must be at least `16 * p_len + 32`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `out` and `scratch` must not overlap each other, `domain`, `x` or `peer` (distinct Rust objects never do). +/// * None of `out`, `domain`, `x`, `peer` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 172 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ffdh_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, domain: *const u8, domain_len: usize, p_len: usize, x: *const u8, x_len: usize, peer: *const u8, peer_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ffdh::vg_ffdh_exchange(out, out_len, domain, domain_len, p_len, x, x_len, peer, peer_len, scratch, scratch_len) } +} + /// Derives the public value of an imported finite-field Diffie–Hellman private key (SP 800-56A Rev. 3 §5.6.2.1.2 for x's range, §5.6.1.1 for `y = g^x mod p`). With `domain` as `vg_ffdh_check_domain` takes it and x (`x_len` bytes, most significant first): returns 2 if x is not in `1..q` (`1..p − 1` without q); 1 if `y = g^x mod p` is a valid public value (as `vg_ffdh_check_public` checks it), writing y (`p_len` bytes, most significant first) to `y`; 3 otherwise. Writes zeros to `y` unless it returns 1. /// /// Contract: `VG.Spec.Ffdh.derivePublicContract`. Timing may depend on the pointers, the lengths, the domain and the value returned, not otherwise on x; the exponentiation may run over all `x_len` bytes of x, leading zeros included. @@ -163,6 +163,32 @@ pub unsafe fn vg_ffdh_key_generation_init_auto(_dit: verified_garbage_cpu::Dit<' unsafe { crate::ffdh::vg_ffdh_key_generation_init(state, p_len) } } +/// Initialize a reusable public fixed-base context for finite-field Diffie–Hellman key generation. The checked domain is p, g and optional q, each p_len bytes. Writes generationPrecompute: three public metadata words followed by RSA's modulus/R-squared values and 32 five-tooth comb entries. Returns 1. Word 0 is generationScalarBits, word 1 is its byte length and word 2 is the comb row count. The context has no pointers and is independent of the arithmetic backend. Contract: VG.Spec.Ffdh.generationPrecomputeContract. Timing may depend on the public domain, pointers and lengths. +/// +/// It runs `vg_ffdh_generate_key_precompute`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// The function may overwrite the arguments on the stack, as the calling convention lets it. +/// +/// # Safety +/// +/// * `ctx` must be valid for reads and writes of `8 * ctx_len` bytes. +/// * `domain` must be valid for reads of `domain_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `p_len` must be in 64..=1024. +/// * `domain_len` must be `2 * p_len` or `3 * p_len`. +/// * domain must be one that vg_ffdh_check_domain accepted. +/// * ctx_len must be generationPrecomputedWords(p_len), namely 3 + 34 * ceil(p_len / 8). +/// * `scratch_len` must be at least `16 * p_len + 32`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `ctx` and `scratch` must not overlap each other or `domain` (distinct Rust objects never do). +/// * None of `ctx`, `domain` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 172 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ffdh_generate_key_precompute_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *mut u64, ctx_len: usize, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ffdh::vg_ffdh_generate_key_precompute(ctx, ctx_len, domain, domain_len, p_len, scratch, scratch_len) } +} + /// One native finite-field Diffie–Hellman key-generation attempt using a reusable public five-tooth comb context. Takes p_len random bytes and implements the same vettedScalarOfOctets, keyStatus and output conventions as vg_ffdh_generate_key_vetted for checked parameters. The context's public maximum width fixes the schedule, including secret leading zeros. Contract: VG.Spec.Ffdh.cachedGenerateKeyContract. Timing may depend on the public domain, pointers, lengths and returned status, not otherwise on the random bytes or private scalar. /// /// It runs `vg_ffdh_generate_key_cached`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. @@ -193,56 +219,37 @@ pub unsafe fn vg_ffdh_generate_key_cached_auto(_dit: verified_garbage_cpu::Dit<' unsafe { crate::ffdh::vg_ffdh_generate_key_cached(x, x_len, y, y_len, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, ctx, ctx_len) } } -/// Run native finite-field Diffie–Hellman parameter generation, with RSA's trial-division sieve. The routine owns candidate retries, attempt limits, witness selection, primality rounds and entropy budgets. Initialize with `vg_ffdh_parameter_generation_init`. Returns 1 with the probable safe prime in `out`, 3 when a bound is exhausted, or 0 only when it needs more entropy. On 0, discard `used` bytes of `rand`, preserve its remaining suffix and extend that suffix to at least `state[3]` bytes before calling again. `state[1]` is the output width and `state[2]` the maximum tape length. The output is zero for 0 and 3. Accepted p and q pass the full 64/128 independent witness counts. Contract: `VG.Spec.Ffdh.sievedParameterSearchContract`. Timing may depend on the public tape and the first four controller words. +/// Generate a finite-field Diffie–Hellman key pair using a reusable public five-tooth comb context and the existing vettedKeySearch rejection policy. Returns 1 with a key pair, 3 when a budget is exhausted, or 0 to request entropy. On 0, discard used bytes, preserve the suffix and extend it to state[3] bytes before calling again. On 0 and 3 both outputs are zero. Entropy consumption, retry budgets and statuses are unchanged. Contract: VG.Spec.Ffdh.cachedKeySearchContract. Timing may depend on the public domain, pointers, lengths, controller policy and sequence of attempt statuses, not otherwise on private scalar bytes. /// -/// It runs `vg_ffdh_generate_parameters`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ffdh_generate_key_search_cached`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `out` must be valid for reads and writes of `out_len` bytes. +/// * `x` must be valid for reads and writes of `x_len` bytes. +/// * `y` must be valid for reads and writes of `y_len` bytes. /// * `used` must be valid for reads and writes of 8 bytes. +/// * `domain` must be valid for reads of `domain_len` bytes. /// * `rand` must be valid for reads of `rand_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. /// * `state` must be valid for reads and writes of 128 bytes. -/// * `state` must be initialized by `vg_ffdh_parameter_generation_init` for the supplied `bits` and `generator`, or be the state returned by this routine. -/// * `out_len` must equal `state[1]`, and `rand_len` must not exceed `state[2]`. -/// * `scratch_len` must be at least `16 * out_len + 32`. -/// * The last twelve controller words and the scratch contents are private workspace. -/// * `out`, `used`, `scratch` and `state` must not overlap each other or `rand` (distinct Rust objects never do). -/// * None of `out`, `used`, `rand`, `scratch` and `state` may overlap the arguments on the stack, overlap the return address on the stack or the 212 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ffdh_generate_parameters_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], bits: usize, generator: u32, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ffdh::vg_ffdh_generate_parameters(out, out_len, used, bits, generator, rand, rand_len, scratch, scratch_len, state) } -} - -/// Initialize a reusable public fixed-base context for finite-field Diffie–Hellman key generation. The checked domain is p, g and optional q, each p_len bytes. Writes generationPrecompute: three public metadata words followed by RSA's modulus/R-squared values and 32 five-tooth comb entries. Returns 1. Word 0 is generationScalarBits, word 1 is its byte length and word 2 is the comb row count. The context has no pointers and is independent of the arithmetic backend. Contract: VG.Spec.Ffdh.generationPrecomputeContract. Timing may depend on the public domain, pointers and lengths. -/// -/// It runs `vg_ffdh_generate_key_precompute`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// The function may overwrite the arguments on the stack, as the calling convention lets it. -/// -/// # Safety -/// -/// * `ctx` must be valid for reads and writes of `8 * ctx_len` bytes. -/// * `domain` must be valid for reads of `domain_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `ctx` must be valid for reads of `8 * ctx_len` bytes. /// * `p_len` must be in 64..=1024. /// * `domain_len` must be `2 * p_len` or `3 * p_len`. /// * domain must be one that vg_ffdh_check_domain accepted. -/// * ctx_len must be generationPrecomputedWords(p_len), namely 3 + 34 * ceil(p_len / 8). +/// * ctx_len must be generationPrecomputedWords(p_len). ctx must contain the unchanged words written by vg_ffdh_generate_key_precompute for these parameters. +/// * state must be initialized by vg_ffdh_key_generation_init for p_len, or be the state returned by this routine. +/// * x_len and y_len must be p_len. /// * `scratch_len` must be at least `16 * p_len + 32`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `ctx` and `scratch` must not overlap each other or `domain` (distinct Rust objects never do). -/// * None of `ctx`, `domain` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 172 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `x`, `y`, `used`, `scratch` and `state` must not overlap each other, `domain`, `rand` or `ctx` (distinct Rust objects never do). +/// * None of `x`, `y`, `used`, `domain`, `rand`, `scratch`, `state` and `ctx` may overlap the arguments on the stack, overlap the return address on the stack or the 228 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ffdh_generate_key_precompute_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *mut u64, ctx_len: usize, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_ffdh_generate_key_search_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, used: *mut [u64; 1], domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16], ctx: *const u64, ctx_len: usize) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ffdh::vg_ffdh_generate_key_precompute(ctx, ctx_len, domain, domain_len, p_len, scratch, scratch_len) } + unsafe { crate::ffdh::vg_ffdh_generate_key_search_cached(x, x_len, y, y_len, used, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, state, ctx, ctx_len) } } /// Initialize native finite-field Diffie–Hellman parameter generation. Validates 512..=8192 bits and generator 2 or 5. Returns 1 and initializes the controller's attempt limit, output width, entropy budget and first entropy request; returns 0 and clears those four words on invalid parameters. The remaining state words are private workspace. Contract: `VG.Spec.Ffdh.parameterInitContract`. @@ -261,35 +268,28 @@ pub unsafe fn vg_ffdh_parameter_generation_init_auto(_dit: verified_garbage_cpu: unsafe { crate::ffdh::vg_ffdh_parameter_generation_init(state, bits, generator) } } -/// Generate a finite-field Diffie–Hellman key pair using a reusable public five-tooth comb context and the existing vettedKeySearch rejection policy. Returns 1 with a key pair, 3 when a budget is exhausted, or 0 to request entropy. On 0, discard used bytes, preserve the suffix and extend it to state[3] bytes before calling again. On 0 and 3 both outputs are zero. Entropy consumption, retry budgets and statuses are unchanged. Contract: VG.Spec.Ffdh.cachedKeySearchContract. Timing may depend on the public domain, pointers, lengths, controller policy and sequence of attempt statuses, not otherwise on private scalar bytes. +/// Run native finite-field Diffie–Hellman parameter generation, with RSA's trial-division sieve. The routine owns candidate retries, attempt limits, witness selection, primality rounds and entropy budgets. Initialize with `vg_ffdh_parameter_generation_init`. Returns 1 with the probable safe prime in `out`, 3 when a bound is exhausted, or 0 only when it needs more entropy. On 0, discard `used` bytes of `rand`, preserve its remaining suffix and extend that suffix to at least `state[3]` bytes before calling again. `state[1]` is the output width and `state[2]` the maximum tape length. The output is zero for 0 and 3. Accepted p and q pass the full 64/128 independent witness counts. Contract: `VG.Spec.Ffdh.sievedParameterSearchContract`. Timing may depend on the public tape and the first four controller words. /// -/// It runs `vg_ffdh_generate_key_search_cached`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_ffdh_generate_parameters`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `x` must be valid for reads and writes of `x_len` bytes. -/// * `y` must be valid for reads and writes of `y_len` bytes. +/// * `out` must be valid for reads and writes of `out_len` bytes. /// * `used` must be valid for reads and writes of 8 bytes. -/// * `domain` must be valid for reads of `domain_len` bytes. /// * `rand` must be valid for reads of `rand_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. /// * `state` must be valid for reads and writes of 128 bytes. -/// * `ctx` must be valid for reads of `8 * ctx_len` bytes. -/// * `p_len` must be in 64..=1024. -/// * `domain_len` must be `2 * p_len` or `3 * p_len`. -/// * domain must be one that vg_ffdh_check_domain accepted. -/// * ctx_len must be generationPrecomputedWords(p_len). ctx must contain the unchanged words written by vg_ffdh_generate_key_precompute for these parameters. -/// * state must be initialized by vg_ffdh_key_generation_init for p_len, or be the state returned by this routine. -/// * x_len and y_len must be p_len. -/// * `scratch_len` must be at least `16 * p_len + 32`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `x`, `y`, `used`, `scratch` and `state` must not overlap each other, `domain`, `rand` or `ctx` (distinct Rust objects never do). -/// * None of `x`, `y`, `used`, `domain`, `rand`, `scratch`, `state` and `ctx` may overlap the arguments on the stack, overlap the return address on the stack or the 228 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `state` must be initialized by `vg_ffdh_parameter_generation_init` for the supplied `bits` and `generator`, or be the state returned by this routine. +/// * `out_len` must equal `state[1]`, and `rand_len` must not exceed `state[2]`. +/// * `scratch_len` must be at least `16 * out_len + 32`. +/// * The last twelve controller words and the scratch contents are private workspace. +/// * `out`, `used`, `scratch` and `state` must not overlap each other or `rand` (distinct Rust objects never do). +/// * None of `out`, `used`, `rand`, `scratch` and `state` may overlap the arguments on the stack, overlap the return address on the stack or the 212 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ffdh_generate_key_search_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, used: *mut [u64; 1], domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16], ctx: *const u64, ctx_len: usize) -> u32 { +pub unsafe fn vg_ffdh_generate_parameters_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], bits: usize, generator: u32, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ffdh::vg_ffdh_generate_key_search_cached(x, x_len, y, y_len, used, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, state, ctx, ctx_len) } + unsafe { crate::ffdh::vg_ffdh_generate_parameters(out, out_len, used, bits, generator, rand, rand_len, scratch, scratch_len, state) } } diff --git a/crates/verified-garbage-asm-x86/src/dispatch/fn_tables.rs b/crates/verified-garbage-asm-x86/src/dispatch/fn_tables.rs index bfe25a514c3..91e808152af 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/fn_tables.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/fn_tables.rs @@ -2,43 +2,91 @@ //! The dispatchers of the tables of functions for `x86` //! (`crate::fn_tables`, `VG.Rust.tableDispatcher`). -/// The table of functions `VG_HASH_MD5` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hash_md5_auto() -> &'static crate::fn_tables::Hash { - &crate::fn_tables::VG_HASH_MD5 -} - /// The table of functions `VG_HMAC_MD5` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] pub fn vg_hmac_md5_auto() -> &'static crate::fn_tables::Hmac { &crate::fn_tables::VG_HMAC_MD5 } -/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA1`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HASH_SHA1_SHANI` if the CPU supports its features (`VG_HASH_SHA1_SHANI_FEATURES`); or else `VG_HASH_SHA1`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA1`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HMAC_SHA1_SHANI` if the CPU supports its features (`VG_HMAC_SHA1_SHANI_FEATURES`); or else `VG_HMAC_SHA1`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] -pub fn vg_hash_sha1_auto() -> &'static crate::fn_tables::Hash { +pub fn vg_hmac_sha1_auto() -> &'static crate::fn_tables::Hmac { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HASH_SHA1_SHANI_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HASH_SHA1_SHANI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA1_SHANI_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA1_SHANI_FEATURES) { - return &crate::fn_tables::VG_HASH_SHA1_SHANI; + return &crate::fn_tables::VG_HMAC_SHA1_SHANI; } - &crate::fn_tables::VG_HASH_SHA1 + &crate::fn_tables::VG_HMAC_SHA1 } -/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA1`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HMAC_SHA1_SHANI` if the CPU supports its features (`VG_HMAC_SHA1_SHANI_FEATURES`); or else `VG_HMAC_SHA1`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HMAC_SHA224_SHANI` if the CPU supports its features (`VG_HMAC_SHA224_SHANI_FEATURES`); or else `VG_HMAC_SHA224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] -pub fn vg_hmac_sha1_auto() -> &'static crate::fn_tables::Hmac { +pub fn vg_hmac_sha224_auto() -> &'static crate::fn_tables::Hmac { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA1_SHANI_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA1_SHANI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA224_SHANI_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA224_SHANI_FEATURES) { - return &crate::fn_tables::VG_HMAC_SHA1_SHANI; + return &crate::fn_tables::VG_HMAC_SHA224_SHANI; } - &crate::fn_tables::VG_HMAC_SHA1 + &crate::fn_tables::VG_HMAC_SHA224 +} + +/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HMAC_SHA256_SHANI` if the CPU supports its features (`VG_HMAC_SHA256_SHANI_FEATURES`); or else `VG_HMAC_SHA256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +#[inline] +pub fn vg_hmac_sha256_auto() -> &'static crate::fn_tables::Hmac { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA256_SHANI_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA256_SHANI_FEATURES) + { + return &crate::fn_tables::VG_HMAC_SHA256_SHANI; + } + &crate::fn_tables::VG_HMAC_SHA256 +} + +/// The table of functions `VG_HMAC_SHA384` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha384_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA384 +} + +/// The table of functions `VG_HMAC_SHA512` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha512_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA512 +} + +/// The table of functions `VG_HMAC_SHA512_224` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha512_224_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA512_224 +} + +/// The table of functions `VG_HMAC_SHA512_256` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hmac_sha512_256_auto() -> &'static crate::fn_tables::Hmac { + &crate::fn_tables::VG_HMAC_SHA512_256 +} + +/// The table of functions `VG_HASH_MD5` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hash_md5_auto() -> &'static crate::fn_tables::Hash { + &crate::fn_tables::VG_HASH_MD5 +} + +/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA1`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HASH_SHA1_SHANI` if the CPU supports its features (`VG_HASH_SHA1_SHANI_FEATURES`); or else `VG_HASH_SHA1`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +#[inline] +pub fn vg_hash_sha1_auto() -> &'static crate::fn_tables::Hash { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HASH_SHA1_SHANI_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HASH_SHA1_SHANI_FEATURES) + { + return &crate::fn_tables::VG_HASH_SHA1_SHANI; + } + &crate::fn_tables::VG_HASH_SHA1 } -/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HASH_SHA224_SHANI` if the CPU supports its features (`VG_HASH_SHA224_SHANI_FEATURES`); or else `VG_HASH_SHA224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HASH_SHA224_SHANI` if the CPU supports its features (`VG_HASH_SHA224_SHANI_FEATURES`); or else `VG_HASH_SHA224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_hash_sha224_auto() -> &'static crate::fn_tables::Hash { let cpu = verified_garbage_cpu::detected(); @@ -50,7 +98,7 @@ pub fn vg_hash_sha224_auto() -> &'static crate::fn_tables::Hash { &crate::fn_tables::VG_HASH_SHA224 } -/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HASH_SHA256_SHANI` if the CPU supports its features (`VG_HASH_SHA256_SHANI_FEATURES`); or else `VG_HASH_SHA256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HASH_SHA256_SHANI` if the CPU supports its features (`VG_HASH_SHA256_SHANI_FEATURES`); or else `VG_HASH_SHA256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_hash_sha256_auto() -> &'static crate::fn_tables::Hash { let cpu = verified_garbage_cpu::detected(); @@ -74,40 +122,16 @@ pub fn vg_hash_sha512_auto() -> &'static crate::fn_tables::Hash { &crate::fn_tables::VG_HASH_SHA512 } -/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HMAC_SHA224_SHANI` if the CPU supports its features (`VG_HMAC_SHA224_SHANI_FEATURES`); or else `VG_HMAC_SHA224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. -#[inline] -pub fn vg_hmac_sha224_auto() -> &'static crate::fn_tables::Hmac { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA224_SHANI_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA224_SHANI_FEATURES) - { - return &crate::fn_tables::VG_HMAC_SHA224_SHANI; - } - &crate::fn_tables::VG_HMAC_SHA224 -} - -/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HMAC_SHA256_SHANI` if the CPU supports its features (`VG_HMAC_SHA256_SHANI_FEATURES`); or else `VG_HMAC_SHA256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. -#[inline] -pub fn vg_hmac_sha256_auto() -> &'static crate::fn_tables::Hmac { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA256_SHANI_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA256_SHANI_FEATURES) - { - return &crate::fn_tables::VG_HMAC_SHA256_SHANI; - } - &crate::fn_tables::VG_HMAC_SHA256 -} - -/// The table of functions `VG_HMAC_SHA384` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +/// The table of functions `VG_HASH_SHA512_224` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] -pub fn vg_hmac_sha384_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA384 +pub fn vg_hash_sha512_224_auto() -> &'static crate::fn_tables::Hash { + &crate::fn_tables::VG_HASH_SHA512_224 } -/// The table of functions `VG_HMAC_SHA512` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +/// The table of functions `VG_HASH_SHA512_256` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] -pub fn vg_hmac_sha512_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA512 +pub fn vg_hash_sha512_256_auto() -> &'static crate::fn_tables::Hash { + &crate::fn_tables::VG_HASH_SHA512_256 } /// The table of functions `VG_MD_HASH_MD5` of the interface `MdHash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). @@ -116,7 +140,7 @@ pub fn vg_md_hash_md5_auto() -> &'static crate::fn_tables::MdHash { &crate::fn_tables::VG_MD_HASH_MD5 } -/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA1`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_MD_HASH_SHA1_SHANI` if the CPU supports its features (`VG_MD_HASH_SHA1_SHANI_FEATURES`); or else `VG_MD_HASH_SHA1`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA1`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_MD_HASH_SHA1_SHANI` if the CPU supports its features (`VG_MD_HASH_SHA1_SHANI_FEATURES`); or else `VG_MD_HASH_SHA1`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_md_hash_sha1_auto() -> &'static crate::fn_tables::MdHash { let cpu = verified_garbage_cpu::detected(); @@ -128,7 +152,7 @@ pub fn vg_md_hash_sha1_auto() -> &'static crate::fn_tables::MdHash { &crate::fn_tables::VG_MD_HASH_SHA1 } -/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_MD_HASH_SHA224_SHANI` if the CPU supports its features (`VG_MD_HASH_SHA224_SHANI_FEATURES`); or else `VG_MD_HASH_SHA224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_MD_HASH_SHA224_SHANI` if the CPU supports its features (`VG_MD_HASH_SHA224_SHANI_FEATURES`); or else `VG_MD_HASH_SHA224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_md_hash_sha224_auto() -> &'static crate::fn_tables::MdHash { let cpu = verified_garbage_cpu::detected(); @@ -140,7 +164,7 @@ pub fn vg_md_hash_sha224_auto() -> &'static crate::fn_tables::MdHash { &crate::fn_tables::VG_MD_HASH_SHA224 } -/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_MD_HASH_SHA256_SHANI` if the CPU supports its features (`VG_MD_HASH_SHA256_SHANI_FEATURES`); or else `VG_MD_HASH_SHA256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_MD_HASH_SHA256_SHANI` if the CPU supports its features (`VG_MD_HASH_SHA256_SHANI_FEATURES`); or else `VG_MD_HASH_SHA256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_md_hash_sha256_auto() -> &'static crate::fn_tables::MdHash { let cpu = verified_garbage_cpu::detected(); @@ -164,30 +188,6 @@ pub fn vg_md_hash_sha512_auto() -> &'static crate::fn_tables::MdHash { &crate::fn_tables::VG_MD_HASH_SHA512 } -/// The table of functions `VG_HASH_SHA512_224` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hash_sha512_224_auto() -> &'static crate::fn_tables::Hash { - &crate::fn_tables::VG_HASH_SHA512_224 -} - -/// The table of functions `VG_HASH_SHA512_256` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hash_sha512_256_auto() -> &'static crate::fn_tables::Hash { - &crate::fn_tables::VG_HASH_SHA512_256 -} - -/// The table of functions `VG_HMAC_SHA512_224` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hmac_sha512_224_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA512_224 -} - -/// The table of functions `VG_HMAC_SHA512_256` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hmac_sha512_256_auto() -> &'static crate::fn_tables::Hmac { - &crate::fn_tables::VG_HMAC_SHA512_256 -} - /// The table of functions `VG_MD_HASH_SHA512_224` of the interface `MdHash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] pub fn vg_md_hash_sha512_224_auto() -> &'static crate::fn_tables::MdHash { diff --git a/crates/verified-garbage-asm-x86/src/dispatch/gcm.rs b/crates/verified-garbage-asm-x86/src/dispatch/gcm.rs index 2fa20b7fae1..b45b5518a60 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/gcm.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/gcm.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Gcm.initContract`. The key context is `VG.Spec.Gcm.KeyRepr`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_init_aesni` if the CPU supports its features (`VG_AES_GCM_INIT_AESNI_FEATURES`); or else `vg_aes_gcm_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_init_aesni` if the CPU supports its features (`VG_AES_GCM_INIT_AESNI_FEATURES`); or else `vg_aes_gcm_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -53,62 +53,60 @@ pub unsafe fn vg_aes_gcm_init_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *co unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(key, key_len, ctx) } } -/// AES-GCM authenticated encryption (NIST SP 800-38D §7.1, GCM-AE, with a 128-bit tag): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, encrypts the `len` bytes at `data` in place, under the IV the `nonce_len` bytes at `nonce`, and writes the tag of the ciphertext and the `aad_len` bytes of additional data at `aad` to `*tag`. A shorter tag is the first bytes of this one. +/// Encrypts the next piece of the text of an incremental AES-GCM encryption (NIST SP 800-38D §7.1): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and the ciphertext of exactly `text_len` bytes of plaintext so far, encrypts the `len` bytes at `data` in place, as the continuation of that plaintext, and the state then represents the message with them appended. /// -/// The function checks no length. GCM is secure only for a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of data and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must ensure; and a nonce must never be used twice with the same key. +/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.sealContract`. Constant time: only the pointers, `rounds` and the lengths may affect timing, not the key context, the nonce, the additional data or the data. +/// Contract: `VG.Spec.Gcm.streamEncryptContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_seal_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_seal_aesni` if the CPU supports its features (`VG_AES_GCM_SEAL_AESNI_FEATURES`); `vg_aes_gcm_seal_pclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_PCLMUL_FEATURES`); or else `vg_aes_gcm_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_stream_encrypt_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_stream_encrypt_aesni` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_AESNI_FEATURES`); `vg_aes_gcm_stream_encrypt_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `nonce` must be valid for reads of `nonce_len` bytes. -/// * `aad` must be valid for reads of `aad_len` bytes. +/// * `state` must be valid for reads and writes of 80 bytes. /// * `data` must be valid for reads and writes of `len` bytes. -/// * `tag` must be valid for reads and writes of 16 bytes. /// * `rounds` must be 10, 12 or 14. -/// * `data` and `tag` must not overlap each other, `ctx`, `nonce` or `aad` (distinct Rust objects never do). -/// * None of `ctx`, `nonce`, `aad`, `data` and `tag` may overlap the arguments on the stack, overlap the return address on the stack or the 2636 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the arguments on the stack, overlap the return address on the stack or the 2636 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *mut [u8; 16]) { +pub unsafe fn vg_aes_gcm_stream_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *mut [u8; 16]); + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_SEAL_AESNI_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_SEAL_AESNI_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_AESNI_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_AESNI_PCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_seal_aesni_pclmul; + return crate::gcm::vg_aes_gcm_stream_encrypt_aesni_pclmul; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_SEAL_AESNI_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_SEAL_AESNI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_AESNI_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_AESNI_FEATURES) { - return crate::gcm::vg_aes_gcm_seal_aesni; + return crate::gcm::vg_aes_gcm_stream_encrypt_aesni; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_SEAL_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_SEAL_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_seal_pclmul; + return crate::gcm::vg_aes_gcm_stream_encrypt_pclmul; } - crate::gcm::vg_aes_gcm_seal + crate::gcm::vg_aes_gcm_stream_encrypt } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *mut [u8; 16]) { + unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, rounds, nonce, nonce_len, aad, aad_len, data, len, tag) } + unsafe { f(ctx, rounds, state, aad_len, text_len, data, len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -116,65 +114,63 @@ pub unsafe fn vg_aes_gcm_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *co // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, nonce, nonce_len, aad, aad_len, data, len, tag) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, state, aad_len, text_len, data, len) } } -/// AES-GCM authenticated decryption (NIST SP 800-38D §7.2, GCM-AD): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds and the received tag the `tag_len` bytes at `tag`, returns 1 if `tag_len` is between 4 and 16 and the tag is the first `tag_len` bytes of that of the `len` bytes of ciphertext at `data` and the `aad_len` bytes of additional data at `aad`, under the IV the `nonce_len` bytes at `nonce`, having then decrypted the ciphertext in place; otherwise returns 0, and the bytes at `data` are unchanged. The tags are compared without a branch. +/// Decrypts the next piece of the text of an incremental AES-GCM decryption (NIST SP 800-38D §7.2): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext so far, decrypts the `len` bytes of ciphertext at `data` in place, as the continuation of that ciphertext, and the state then represents the message with them appended. The plaintext is not authenticated until `vg_aes_gcm_stream_verify` has returned 1. /// -/// The function checks no other length: GCM requires a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of ciphertext and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.openContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the additional data, the data or the tag. +/// Contract: `VG.Spec.Gcm.streamDecryptContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_open_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_open_aesni` if the CPU supports its features (`VG_AES_GCM_OPEN_AESNI_FEATURES`); `vg_aes_gcm_open_pclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_PCLMUL_FEATURES`); or else `vg_aes_gcm_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_stream_decrypt_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_stream_decrypt_aesni` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_AESNI_FEATURES`); `vg_aes_gcm_stream_decrypt_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `nonce` must be valid for reads of `nonce_len` bytes. -/// * `aad` must be valid for reads of `aad_len` bytes. +/// * `state` must be valid for reads and writes of 80 bytes. /// * `data` must be valid for reads and writes of `len` bytes. -/// * `tag` must be valid for reads of `tag_len` bytes. /// * `rounds` must be 10, 12 or 14. -/// * `data` must not overlap `ctx`, `nonce`, `aad` or `tag` (distinct Rust objects never do). -/// * None of `ctx`, `nonce`, `aad`, `data` and `tag` may overlap the arguments on the stack, overlap the return address on the stack or the 2640 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the arguments on the stack, overlap the return address on the stack or the 2636 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_open_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *const u8, tag_len: usize) -> u32 { +pub unsafe fn vg_aes_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *const u8, tag_len: usize) -> u32; + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_OPEN_AESNI_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_OPEN_AESNI_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_AESNI_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_AESNI_PCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_open_aesni_pclmul; + return crate::gcm::vg_aes_gcm_stream_decrypt_aesni_pclmul; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_OPEN_AESNI_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_OPEN_AESNI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_AESNI_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_AESNI_FEATURES) { - return crate::gcm::vg_aes_gcm_open_aesni; + return crate::gcm::vg_aes_gcm_stream_decrypt_aesni; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_OPEN_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_OPEN_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_open_pclmul; + return crate::gcm::vg_aes_gcm_stream_decrypt_pclmul; } - crate::gcm::vg_aes_gcm_open + crate::gcm::vg_aes_gcm_stream_decrypt } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *const u8, tag_len: usize) -> u32 { + unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, rounds, nonce, nonce_len, aad, aad_len, data, len, tag, tag_len) } + unsafe { f(ctx, rounds, state, aad_len, text_len, data, len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -182,67 +178,62 @@ pub unsafe fn vg_aes_gcm_open_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *co // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, nonce, nonce_len, aad, aad_len, data, len, tag, tag_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, state, aad_len, text_len, data, len) } } -/// AES-GCM authenticated decryption (NIST SP 800-38D §7.2, GCM-AD), out of place: with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds and the received tag the `tag_len` bytes at `tag`, returns 1 if `tag_len` is between 4 and 16 and the tag is the first `tag_len` bytes of that of the `len` bytes of ciphertext at `src` and the `aad_len` bytes of additional data at `aad`, under the IV the `nonce_len` bytes at `nonce`, having then written the plaintext to the `dst_len = len` bytes at `dst`; otherwise returns 0, having written zeros to them: `vg_aes_gcm_open` with the ciphertext read from `src` and the plaintext written to `dst`. `src` is not written, and `dst` is a buffer of its own: it overlaps no other buffer, `src` included. The tags are compared without a branch. -/// -/// The function checks no other length: GCM requires a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of ciphertext and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// Finishes an incremental AES-GCM encryption or decryption (NIST SP 800-38D §7.1 steps 5–6, §7.2 steps 6–7): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, writes its 128-bit tag to `*tag`. A shorter tag is the first bytes of this one; to check a received tag, use `vg_aes_gcm_stream_verify`. /// -/// Contract: `VG.Spec.Gcm.openToContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the additional data, the data or the tag. +/// Contract: `VG.Spec.Gcm.streamFinishContract`. Constant time: only the pointers, `rounds`, `aad_len` and `text_len` may affect timing, not the key context or the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_open_to_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_open_to_aesni` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_AESNI_FEATURES`); `vg_aes_gcm_open_to_pclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PCLMUL_FEATURES`); or else `vg_aes_gcm_open_to`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_stream_finish_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_stream_finish_aesni` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_AESNI_FEATURES`); `vg_aes_gcm_stream_finish_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_finish`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `nonce` must be valid for reads of `nonce_len` bytes. -/// * `aad` must be valid for reads of `aad_len` bytes. -/// * `src` must be valid for reads of `len` bytes. -/// * `dst` must be valid for reads and writes of `dst_len` bytes. -/// * `tag` must be valid for reads of `tag_len` bytes. +/// * `state` must be valid for reads and writes of 80 bytes. +/// * `tag` must be valid for reads and writes of 16 bytes. /// * `rounds` must be 10, 12 or 14. -/// * `dst_len` must equal `len`. -/// * `dst` must not overlap `ctx`, `nonce`, `aad`, `src` or `tag` (distinct Rust objects never do). -/// * None of `ctx`, `nonce`, `aad`, `src`, `dst` and `tag` may overlap the arguments on the stack, overlap the return address on the stack or the 2696 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * The contents of `state` on return are unspecified. +/// * `state` and `tag` must not overlap each other or `ctx` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `tag` may overlap the arguments on the stack, overlap the return address on the stack or the 2632 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_open_to_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const u8, len: usize, dst: *mut u8, dst_len: usize, tag: *const u8, tag_len: usize) -> u32 { +pub unsafe fn vg_aes_gcm_stream_finish_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *mut [u8; 16]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const u8, len: usize, dst: *mut u8, dst_len: usize, tag: *const u8, tag_len: usize) -> u32; + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *mut [u8; 16]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_OPEN_TO_AESNI_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_OPEN_TO_AESNI_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_FINISH_AESNI_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_FINISH_AESNI_PCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_open_to_aesni_pclmul; + return crate::gcm::vg_aes_gcm_stream_finish_aesni_pclmul; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_OPEN_TO_AESNI_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_OPEN_TO_AESNI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_FINISH_AESNI_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_FINISH_AESNI_FEATURES) { - return crate::gcm::vg_aes_gcm_open_to_aesni; + return crate::gcm::vg_aes_gcm_stream_finish_aesni; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_OPEN_TO_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_OPEN_TO_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_FINISH_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_FINISH_PCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_open_to_pclmul; + return crate::gcm::vg_aes_gcm_stream_finish_pclmul; } - crate::gcm::vg_aes_gcm_open_to + crate::gcm::vg_aes_gcm_stream_finish } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const u8, len: usize, dst: *mut u8, dst_len: usize, tag: *const u8, tag_len: usize) -> u32 { + unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *mut [u8; 16]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, rounds, nonce, nonce_len, aad, aad_len, src, len, dst, dst_len, tag, tag_len) } + unsafe { f(ctx, rounds, state, aad_len, text_len, tag) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -250,14 +241,14 @@ pub unsafe fn vg_aes_gcm_open_to_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, nonce, nonce_len, aad, aad_len, src, len, dst, dst_len, tag, tag_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, state, aad_len, text_len, tag) } } -/// Absorbs additional data into an incremental AES-GCM encryption or decryption: with the key context `*ctx`, if the streaming state `*state` represents a message with `aad_len` bytes (modulo 2⁶⁴) of additional data and no text yet, it then represents that message with the `len` bytes at `data` appended to its additional data. All of the additional data comes before the text (§7.1 step 5): after text, the state represents no message. +/// Finishes an incremental AES-GCM decryption and checks its tag (NIST SP 800-38D §7.2 steps 6–8): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds and the received tag the `tag_len` bytes at `tag`, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, returns 1 if `tag_len` is between 4 and 16 and the received tag is the first `tag_len` bytes of the message's 128-bit tag, and 0 otherwise. The tags are compared without a branch. /// -/// Contract: `VG.Spec.Gcm.streamAadContract`. Constant time: only the pointers, `aad_len` and `len` may affect timing, not the key context, the state or the data. +/// Contract: `VG.Spec.Gcm.streamVerifyContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `tag_len` may affect timing, not the key context, the state or the tags. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_stream_aad_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_AAD_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_aad`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_stream_verify_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_stream_verify_aesni` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_AESNI_FEATURES`); `vg_aes_gcm_stream_verify_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -265,35 +256,47 @@ pub unsafe fn vg_aes_gcm_open_to_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: /// /// * `ctx` must be valid for reads of 256 bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `state` must not overlap `ctx` or `data` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the arguments on the stack, overlap the return address on the stack or the 2616 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `tag` must be valid for reads of `tag_len` bytes. +/// * `rounds` must be 10, 12 or 14. +/// * The contents of `state` on return are unspecified. +/// * `state` must not overlap `ctx` or `tag` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `tag` may overlap the arguments on the stack, overlap the return address on the stack or the 2636 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { +pub unsafe fn vg_aes_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *const u8, tag_len: usize) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize); + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *const u8, tag_len: usize) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_AAD_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_AAD_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_AESNI_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_AESNI_PCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_aad_pclmul; + return crate::gcm::vg_aes_gcm_stream_verify_aesni_pclmul; } - crate::gcm::vg_aes_gcm_stream_aad + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_AESNI_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_AESNI_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_verify_aesni; + } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_PCLMUL_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_verify_pclmul; + } + crate::gcm::vg_aes_gcm_stream_verify } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { + unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *const u8, tag_len: usize) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, state, aad_len, data, len) } + unsafe { f(ctx, rounds, state, aad_len, text_len, tag, tag_len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -301,16 +304,16 @@ pub unsafe fn vg_aes_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ct // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, data, len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, state, aad_len, text_len, tag, tag_len) } } -/// AES-GCM authenticated encryption (NIST SP 800-38D §7.1, GCM-AE, with a 128-bit tag), out of place, of a plaintext in pieces: with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, encrypts the concatenation of the `src_count` slices that `src` lists (each an address and a length, in bytes), under the IV the `nonce_len` bytes at `nonce`, writes the ciphertext to the `len` bytes at `dst`, and writes the tag of the ciphertext and the `aad_len` bytes of additional data at `aad` to `*tag`: `vg_aes_gcm_seal` with the plaintext gathered from `src`. A shorter tag is the first bytes of this one. +/// AES-GCM authenticated encryption (NIST SP 800-38D §7.1, GCM-AE, with a 128-bit tag): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, encrypts the `len` bytes at `data` in place, under the IV the `nonce_len` bytes at `nonce`, and writes the tag of the ciphertext and the `aad_len` bytes of additional data at `aad` to `*tag`. A shorter tag is the first bytes of this one. /// /// The function checks no length. GCM is secure only for a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of data and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must ensure; and a nonce must never be used twice with the same key. /// -/// Contract: `VG.Spec.Gcm.sealGatherContract`. Constant time: only the pointers, `rounds`, the lengths, `src_count` and where the slices are (their addresses and lengths) may affect timing, not the key context, the nonce, the additional data or the data. +/// Contract: `VG.Spec.Gcm.sealContract`. Constant time: only the pointers, `rounds` and the lengths may affect timing, not the key context, the nonce, the additional data or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_seal_gather_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_seal_gather_aesni` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_AESNI_FEATURES`); `vg_aes_gcm_seal_gather_pclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PCLMUL_FEATURES`); or else `vg_aes_gcm_seal_gather`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_seal_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_seal_aesni` if the CPU supports its features (`VG_AES_GCM_SEAL_AESNI_FEATURES`); `vg_aes_gcm_seal_pclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_PCLMUL_FEATURES`); or else `vg_aes_gcm_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -319,49 +322,47 @@ pub unsafe fn vg_aes_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ct /// * `ctx` must be valid for reads of 256 bytes. /// * `nonce` must be valid for reads of `nonce_len` bytes. /// * `aad` must be valid for reads of `aad_len` bytes. -/// * `src` must be valid for reads of `2 * size_of::() * src_count` bytes, and each slice it lists for reads of its length in bytes. -/// * `dst` must be valid for reads and writes of `len` bytes. +/// * `data` must be valid for reads and writes of `len` bytes. /// * `tag` must be valid for reads and writes of 16 bytes. /// * `rounds` must be 10, 12 or 14. -/// * The slices `src` lists must be `len` bytes long in all. -/// * `dst` and `tag` must not overlap each other, `ctx`, `nonce`, `aad`, `src` or the slices `src` lists (distinct Rust objects never do). -/// * None of `ctx`, `nonce`, `aad`, `src`, the slices `src` lists, `dst` and `tag` may overlap the arguments on the stack, overlap the return address on the stack or the 2688 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `data` and `tag` must not overlap each other, `ctx`, `nonce` or `aad` (distinct Rust objects never do). +/// * None of `ctx`, `nonce`, `aad`, `data` and `tag` may overlap the arguments on the stack, overlap the return address on the stack or the 2636 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_seal_gather_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const [usize; 2], src_count: usize, dst: *mut u8, len: usize, tag: *mut [u8; 16]) { +pub unsafe fn vg_aes_gcm_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *mut [u8; 16]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const [usize; 2], src_count: usize, dst: *mut u8, len: usize, tag: *mut [u8; 16]); + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *mut [u8; 16]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_SEAL_GATHER_AESNI_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_SEAL_GATHER_AESNI_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_SEAL_AESNI_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_SEAL_AESNI_PCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_seal_gather_aesni_pclmul; + return crate::gcm::vg_aes_gcm_seal_aesni_pclmul; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_SEAL_GATHER_AESNI_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_SEAL_GATHER_AESNI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_SEAL_AESNI_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_SEAL_AESNI_FEATURES) { - return crate::gcm::vg_aes_gcm_seal_gather_aesni; + return crate::gcm::vg_aes_gcm_seal_aesni; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_SEAL_GATHER_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_SEAL_GATHER_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_SEAL_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_SEAL_PCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_seal_gather_pclmul; + return crate::gcm::vg_aes_gcm_seal_pclmul; } - crate::gcm::vg_aes_gcm_seal_gather + crate::gcm::vg_aes_gcm_seal } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const [usize; 2], src_count: usize, dst: *mut u8, len: usize, tag: *mut [u8; 16]) { + unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *mut [u8; 16]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, rounds, nonce, nonce_len, aad, aad_len, src, src_count, dst, len, tag) } + unsafe { f(ctx, rounds, nonce, nonce_len, aad, aad_len, data, len, tag) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -369,16 +370,16 @@ pub unsafe fn vg_aes_gcm_seal_gather_auto(_dit: verified_garbage_cpu::Dit<'_>, c // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, nonce, nonce_len, aad, aad_len, src, src_count, dst, len, tag) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, nonce, nonce_len, aad, aad_len, data, len, tag) } } -/// Starts an incremental AES-GCM encryption or decryption (NIST SP 800-38D §7): with the key context `*ctx` that `vg_aes_gcm_init` wrote, makes the streaming state `*state` represent the message with the IV the `nonce_len` bytes at `nonce`, and no additional data or text yet. Continue with `vg_aes_gcm_stream_aad`, then `vg_aes_gcm_stream_encrypt` or `vg_aes_gcm_stream_decrypt`, then `vg_aes_gcm_stream_finish` or `vg_aes_gcm_stream_verify`, with the same key context. +/// AES-GCM authenticated decryption (NIST SP 800-38D §7.2, GCM-AD): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds and the received tag the `tag_len` bytes at `tag`, returns 1 if `tag_len` is between 4 and 16 and the tag is the first `tag_len` bytes of that of the `len` bytes of ciphertext at `data` and the `aad_len` bytes of additional data at `aad`, under the IV the `nonce_len` bytes at `nonce`, having then decrypted the ciphertext in place; otherwise returns 0, and the bytes at `data` are unchanged. The tags are compared without a branch. /// -/// The function checks no length: GCM requires a nonce of 1 to `2^61 - 1` bytes (§5.2.1.1), which the caller must check. +/// The function checks no other length: GCM requires a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of ciphertext and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.streamInitContract`. The streaming state is `J₀`, the GHASH accumulator, a partial block, the next counter block and a keystream block (`VG.Spec.Gcm.StreamRepr`). Constant time: only the pointers and `nonce_len` may affect timing, not the key context or the nonce. +/// Contract: `VG.Spec.Gcm.openContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the additional data, the data or the tag. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_stream_init_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_INIT_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_open_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_open_aesni` if the CPU supports its features (`VG_AES_GCM_OPEN_AESNI_FEATURES`); `vg_aes_gcm_open_pclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_PCLMUL_FEATURES`); or else `vg_aes_gcm_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -386,35 +387,48 @@ pub unsafe fn vg_aes_gcm_seal_gather_auto(_dit: verified_garbage_cpu::Dit<'_>, c /// /// * `ctx` must be valid for reads of 256 bytes. /// * `nonce` must be valid for reads of `nonce_len` bytes. -/// * `state` must be valid for reads and writes of 80 bytes. -/// * `state` must not overlap `ctx` or `nonce` (distinct Rust objects never do). -/// * None of `ctx`, `nonce` and `state` may overlap the arguments on the stack, overlap the return address on the stack or the 2608 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `aad` must be valid for reads of `aad_len` bytes. +/// * `data` must be valid for reads and writes of `len` bytes. +/// * `tag` must be valid for reads of `tag_len` bytes. +/// * `rounds` must be 10, 12 or 14. +/// * `data` must not overlap `ctx`, `nonce`, `aad` or `tag` (distinct Rust objects never do). +/// * None of `ctx`, `nonce`, `aad`, `data` and `tag` may overlap the arguments on the stack, overlap the return address on the stack or the 2640 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { +pub unsafe fn vg_aes_gcm_open_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *const u8, tag_len: usize) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]); + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *const u8, tag_len: usize) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_INIT_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_INIT_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_OPEN_AESNI_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_OPEN_AESNI_PCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_init_pclmul; + return crate::gcm::vg_aes_gcm_open_aesni_pclmul; } - crate::gcm::vg_aes_gcm_stream_init + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_OPEN_AESNI_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_OPEN_AESNI_FEATURES) + { + return crate::gcm::vg_aes_gcm_open_aesni; + } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_OPEN_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_OPEN_PCLMUL_FEATURES) + { + return crate::gcm::vg_aes_gcm_open_pclmul; + } + crate::gcm::vg_aes_gcm_open } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { + unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *const u8, tag_len: usize) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, nonce, nonce_len, state) } + unsafe { f(ctx, rounds, nonce, nonce_len, aad, aad_len, data, len, tag, tag_len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -422,62 +436,67 @@ pub unsafe fn vg_aes_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, c // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, nonce, nonce_len, state) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, nonce, nonce_len, aad, aad_len, data, len, tag, tag_len) } } -/// Finishes an incremental AES-GCM encryption or decryption (NIST SP 800-38D §7.1 steps 5–6, §7.2 steps 6–7): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, writes its 128-bit tag to `*tag`. A shorter tag is the first bytes of this one; to check a received tag, use `vg_aes_gcm_stream_verify`. +/// AES-GCM authenticated encryption (NIST SP 800-38D §7.1, GCM-AE, with a 128-bit tag), out of place, of a plaintext in pieces: with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, encrypts the concatenation of the `src_count` slices that `src` lists (each an address and a length, in bytes), under the IV the `nonce_len` bytes at `nonce`, writes the ciphertext to the `len` bytes at `dst`, and writes the tag of the ciphertext and the `aad_len` bytes of additional data at `aad` to `*tag`: `vg_aes_gcm_seal` with the plaintext gathered from `src`. A shorter tag is the first bytes of this one. /// -/// Contract: `VG.Spec.Gcm.streamFinishContract`. Constant time: only the pointers, `rounds`, `aad_len` and `text_len` may affect timing, not the key context or the state. +/// The function checks no length. GCM is secure only for a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of data and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must ensure; and a nonce must never be used twice with the same key. +/// +/// Contract: `VG.Spec.Gcm.sealGatherContract`. Constant time: only the pointers, `rounds`, the lengths, `src_count` and where the slices are (their addresses and lengths) may affect timing, not the key context, the nonce, the additional data or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_stream_finish_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_stream_finish_aesni` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_AESNI_FEATURES`); `vg_aes_gcm_stream_finish_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_finish`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_seal_gather_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_seal_gather_aesni` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_AESNI_FEATURES`); `vg_aes_gcm_seal_gather_pclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PCLMUL_FEATURES`); or else `vg_aes_gcm_seal_gather`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `state` must be valid for reads and writes of 80 bytes. +/// * `nonce` must be valid for reads of `nonce_len` bytes. +/// * `aad` must be valid for reads of `aad_len` bytes. +/// * `src` must be valid for reads of `2 * size_of::() * src_count` bytes, and each slice it lists for reads of its length in bytes. +/// * `dst` must be valid for reads and writes of `len` bytes. /// * `tag` must be valid for reads and writes of 16 bytes. /// * `rounds` must be 10, 12 or 14. -/// * The contents of `state` on return are unspecified. -/// * `state` and `tag` must not overlap each other or `ctx` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `tag` may overlap the arguments on the stack, overlap the return address on the stack or the 2632 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * The slices `src` lists must be `len` bytes long in all. +/// * `dst` and `tag` must not overlap each other, `ctx`, `nonce`, `aad`, `src` or the slices `src` lists (distinct Rust objects never do). +/// * None of `ctx`, `nonce`, `aad`, `src`, the slices `src` lists, `dst` and `tag` may overlap the arguments on the stack, overlap the return address on the stack or the 2688 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_stream_finish_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *mut [u8; 16]) { +pub unsafe fn vg_aes_gcm_seal_gather_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const [usize; 2], src_count: usize, dst: *mut u8, len: usize, tag: *mut [u8; 16]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *mut [u8; 16]); + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const [usize; 2], src_count: usize, dst: *mut u8, len: usize, tag: *mut [u8; 16]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_FINISH_AESNI_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_FINISH_AESNI_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_SEAL_GATHER_AESNI_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_SEAL_GATHER_AESNI_PCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_finish_aesni_pclmul; + return crate::gcm::vg_aes_gcm_seal_gather_aesni_pclmul; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_FINISH_AESNI_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_FINISH_AESNI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_SEAL_GATHER_AESNI_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_SEAL_GATHER_AESNI_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_finish_aesni; + return crate::gcm::vg_aes_gcm_seal_gather_aesni; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_FINISH_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_FINISH_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_SEAL_GATHER_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_SEAL_GATHER_PCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_finish_pclmul; + return crate::gcm::vg_aes_gcm_seal_gather_pclmul; } - crate::gcm::vg_aes_gcm_stream_finish + crate::gcm::vg_aes_gcm_seal_gather } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *mut [u8; 16]) { + unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const [usize; 2], src_count: usize, dst: *mut u8, len: usize, tag: *mut [u8; 16]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, rounds, state, aad_len, text_len, tag) } + unsafe { f(ctx, rounds, nonce, nonce_len, aad, aad_len, src, src_count, dst, len, tag) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -485,62 +504,67 @@ pub unsafe fn vg_aes_gcm_stream_finish_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, state, aad_len, text_len, tag) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, nonce, nonce_len, aad, aad_len, src, src_count, dst, len, tag) } } -/// Finishes an incremental AES-GCM decryption and checks its tag (NIST SP 800-38D §7.2 steps 6–8): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds and the received tag the `tag_len` bytes at `tag`, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, returns 1 if `tag_len` is between 4 and 16 and the received tag is the first `tag_len` bytes of the message's 128-bit tag, and 0 otherwise. The tags are compared without a branch. +/// AES-GCM authenticated decryption (NIST SP 800-38D §7.2, GCM-AD), out of place: with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds and the received tag the `tag_len` bytes at `tag`, returns 1 if `tag_len` is between 4 and 16 and the tag is the first `tag_len` bytes of that of the `len` bytes of ciphertext at `src` and the `aad_len` bytes of additional data at `aad`, under the IV the `nonce_len` bytes at `nonce`, having then written the plaintext to the `dst_len = len` bytes at `dst`; otherwise returns 0, having written zeros to them: `vg_aes_gcm_open` with the ciphertext read from `src` and the plaintext written to `dst`. `src` is not written, and `dst` is a buffer of its own: it overlaps no other buffer, `src` included. The tags are compared without a branch. /// -/// Contract: `VG.Spec.Gcm.streamVerifyContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `tag_len` may affect timing, not the key context, the state or the tags. +/// The function checks no other length: GCM requires a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of ciphertext and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_stream_verify_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_stream_verify_aesni` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_AESNI_FEATURES`); `vg_aes_gcm_stream_verify_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// Contract: `VG.Spec.Gcm.openToContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the additional data, the data or the tag. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_open_to_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_open_to_aesni` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_AESNI_FEATURES`); `vg_aes_gcm_open_to_pclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PCLMUL_FEATURES`); or else `vg_aes_gcm_open_to`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `state` must be valid for reads and writes of 80 bytes. +/// * `nonce` must be valid for reads of `nonce_len` bytes. +/// * `aad` must be valid for reads of `aad_len` bytes. +/// * `src` must be valid for reads of `len` bytes. +/// * `dst` must be valid for reads and writes of `dst_len` bytes. /// * `tag` must be valid for reads of `tag_len` bytes. /// * `rounds` must be 10, 12 or 14. -/// * The contents of `state` on return are unspecified. -/// * `state` must not overlap `ctx` or `tag` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `tag` may overlap the arguments on the stack, overlap the return address on the stack or the 2636 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `dst_len` must equal `len`. +/// * `dst` must not overlap `ctx`, `nonce`, `aad`, `src` or `tag` (distinct Rust objects never do). +/// * None of `ctx`, `nonce`, `aad`, `src`, `dst` and `tag` may overlap the arguments on the stack, overlap the return address on the stack or the 2696 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *const u8, tag_len: usize) -> u32 { +pub unsafe fn vg_aes_gcm_open_to_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const u8, len: usize, dst: *mut u8, dst_len: usize, tag: *const u8, tag_len: usize) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *const u8, tag_len: usize) -> u32; + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const u8, len: usize, dst: *mut u8, dst_len: usize, tag: *const u8, tag_len: usize) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_AESNI_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_AESNI_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_OPEN_TO_AESNI_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_OPEN_TO_AESNI_PCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_verify_aesni_pclmul; + return crate::gcm::vg_aes_gcm_open_to_aesni_pclmul; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_AESNI_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_AESNI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_OPEN_TO_AESNI_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_OPEN_TO_AESNI_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_verify_aesni; + return crate::gcm::vg_aes_gcm_open_to_aesni; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_OPEN_TO_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_OPEN_TO_PCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_verify_pclmul; + return crate::gcm::vg_aes_gcm_open_to_pclmul; } - crate::gcm::vg_aes_gcm_stream_verify + crate::gcm::vg_aes_gcm_open_to } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *const u8, tag_len: usize) -> u32 { + unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const u8, len: usize, dst: *mut u8, dst_len: usize, tag: *const u8, tag_len: usize) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, rounds, state, aad_len, text_len, tag, tag_len) } + unsafe { f(ctx, rounds, nonce, nonce_len, aad, aad_len, src, len, dst, dst_len, tag, tag_len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -548,16 +572,14 @@ pub unsafe fn vg_aes_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, state, aad_len, text_len, tag, tag_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, nonce, nonce_len, aad, aad_len, src, len, dst, dst_len, tag, tag_len) } } -/// Encrypts the next piece of the text of an incremental AES-GCM encryption (NIST SP 800-38D §7.1): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and the ciphertext of exactly `text_len` bytes of plaintext so far, encrypts the `len` bytes at `data` in place, as the continuation of that plaintext, and the state then represents the message with them appended. -/// -/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// Absorbs additional data into an incremental AES-GCM encryption or decryption: with the key context `*ctx`, if the streaming state `*state` represents a message with `aad_len` bytes (modulo 2⁶⁴) of additional data and no text yet, it then represents that message with the `len` bytes at `data` appended to its additional data. All of the additional data comes before the text (§7.1 step 5): after text, the state represents no message. /// -/// Contract: `VG.Spec.Gcm.streamEncryptContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. +/// Contract: `VG.Spec.Gcm.streamAadContract`. Constant time: only the pointers, `aad_len` and `len` may affect timing, not the key context, the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_stream_encrypt_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_stream_encrypt_aesni` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_AESNI_FEATURES`); `vg_aes_gcm_stream_encrypt_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_stream_aad_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_AAD_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_aad`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -565,46 +587,35 @@ pub unsafe fn vg_aes_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, /// /// * `ctx` must be valid for reads of 256 bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `data` must be valid for reads and writes of `len` bytes. -/// * `rounds` must be 10, 12 or 14. -/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the arguments on the stack, overlap the return address on the stack or the 2636 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `state` must not overlap `ctx` or `data` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the arguments on the stack, overlap the return address on the stack or the 2616 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_stream_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { +pub unsafe fn vg_aes_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_AESNI_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_AESNI_PCLMUL_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_encrypt_aesni_pclmul; - } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_AESNI_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_AESNI_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_encrypt_aesni; - } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_AAD_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_AAD_PCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_encrypt_pclmul; + return crate::gcm::vg_aes_gcm_stream_aad_pclmul; } - crate::gcm::vg_aes_gcm_stream_encrypt + crate::gcm::vg_aes_gcm_stream_aad } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { + unsafe extern "C" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, rounds, state, aad_len, text_len, data, len) } + unsafe { f(ctx, state, aad_len, data, len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -612,63 +623,52 @@ pub unsafe fn vg_aes_gcm_stream_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_> // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, state, aad_len, text_len, data, len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, data, len) } } -/// Decrypts the next piece of the text of an incremental AES-GCM decryption (NIST SP 800-38D §7.2): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext so far, decrypts the `len` bytes of ciphertext at `data` in place, as the continuation of that ciphertext, and the state then represents the message with them appended. The plaintext is not authenticated until `vg_aes_gcm_stream_verify` has returned 1. +/// Starts an incremental AES-GCM encryption or decryption (NIST SP 800-38D §7): with the key context `*ctx` that `vg_aes_gcm_init` wrote, makes the streaming state `*state` represent the message with the IV the `nonce_len` bytes at `nonce`, and no additional data or text yet. Continue with `vg_aes_gcm_stream_aad`, then `vg_aes_gcm_stream_encrypt` or `vg_aes_gcm_stream_decrypt`, then `vg_aes_gcm_stream_finish` or `vg_aes_gcm_stream_verify`, with the same key context. /// -/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// The function checks no length: GCM requires a nonce of 1 to `2^61 - 1` bytes (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.streamDecryptContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. +/// Contract: `VG.Spec.Gcm.streamInitContract`. The streaming state is `J₀`, the GHASH accumulator, a partial block, the next counter block and a keystream block (`VG.Spec.Gcm.StreamRepr`). Constant time: only the pointers and `nonce_len` may affect timing, not the key context or the nonce. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_stream_decrypt_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_stream_decrypt_aesni` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_AESNI_FEATURES`); `vg_aes_gcm_stream_decrypt_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_stream_init_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_INIT_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. +/// * `nonce` must be valid for reads of `nonce_len` bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `data` must be valid for reads and writes of `len` bytes. -/// * `rounds` must be 10, 12 or 14. -/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the arguments on the stack, overlap the return address on the stack or the 2636 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `state` must not overlap `ctx` or `nonce` (distinct Rust objects never do). +/// * None of `ctx`, `nonce` and `state` may overlap the arguments on the stack, overlap the return address on the stack or the 2608 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { +pub unsafe fn vg_aes_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_AESNI_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_AESNI_PCLMUL_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_decrypt_aesni_pclmul; - } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_AESNI_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_AESNI_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_decrypt_aesni; - } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_INIT_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_INIT_PCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_decrypt_pclmul; + return crate::gcm::vg_aes_gcm_stream_init_pclmul; } - crate::gcm::vg_aes_gcm_stream_decrypt + crate::gcm::vg_aes_gcm_stream_init } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { + unsafe extern "C" fn first(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, rounds, state, aad_len, text_len, data, len) } + unsafe { f(ctx, nonce, nonce_len, state) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -676,5 +676,5 @@ pub unsafe fn vg_aes_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_> // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, state, aad_len, text_len, data, len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, nonce, nonce_len, state) } } diff --git a/crates/verified-garbage-asm-x86/src/dispatch/hmac_sha1.rs b/crates/verified-garbage-asm-x86/src/dispatch/hmac_sha1.rs index 8abfa8e0d18..1d3702cf1aa 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/hmac_sha1.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/hmac_sha1.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.initAnyKeyContract` of `VG.Spec.Hmac.sha1I`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha1_init_shani` if the CPU supports its features (`VG_HMAC_SHA1_INIT_SHANI_FEATURES`); or else `vg_hmac_sha1_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha1_init_shani` if the CPU supports its features (`VG_HMAC_SHA1_INIT_SHANI_FEATURES`); or else `vg_hmac_sha1_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -57,7 +57,7 @@ pub unsafe fn vg_hmac_sha1_init_auto(_dit: verified_garbage_cpu::Dit<'_>, inner: /// /// Contract: `VG.Spec.Hmac.Instance.finalizeContract` of `VG.Spec.Hmac.sha1I`. Constant time: only the pointers and `count` may affect timing, not the states. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha1_finalize_shani` if the CPU supports its features (`VG_HMAC_SHA1_FINALIZE_SHANI_FEATURES`); or else `vg_hmac_sha1_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha1_finalize_shani` if the CPU supports its features (`VG_HMAC_SHA1_FINALIZE_SHANI_FEATURES`); or else `vg_hmac_sha1_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/hmac_sha224.rs b/crates/verified-garbage-asm-x86/src/dispatch/hmac_sha224.rs index 7eff9125c81..fada3abab4e 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/hmac_sha224.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/hmac_sha224.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.initAnyKeyContract` of `VG.Spec.Hmac.sha224I`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha224_init_shani` if the CPU supports its features (`VG_HMAC_SHA224_INIT_SHANI_FEATURES`); or else `vg_hmac_sha224_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha224_init_shani` if the CPU supports its features (`VG_HMAC_SHA224_INIT_SHANI_FEATURES`); or else `vg_hmac_sha224_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -57,7 +57,7 @@ pub unsafe fn vg_hmac_sha224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, inne /// /// Contract: `VG.Spec.Hmac.Instance.finalizeContract` of `VG.Spec.Hmac.sha224I`. Constant time: only the pointers and `count` may affect timing, not the states. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha224_finalize_shani` if the CPU supports its features (`VG_HMAC_SHA224_FINALIZE_SHANI_FEATURES`); or else `vg_hmac_sha224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha224_finalize_shani` if the CPU supports its features (`VG_HMAC_SHA224_FINALIZE_SHANI_FEATURES`); or else `vg_hmac_sha224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/hmac_sha256.rs b/crates/verified-garbage-asm-x86/src/dispatch/hmac_sha256.rs index d4cbddc5085..1eee86a272c 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/hmac_sha256.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/hmac_sha256.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.initAnyKeyContract` of `VG.Spec.Hmac.sha256I`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha256_init_shani` if the CPU supports its features (`VG_HMAC_SHA256_INIT_SHANI_FEATURES`); or else `vg_hmac_sha256_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha256_init_shani` if the CPU supports its features (`VG_HMAC_SHA256_INIT_SHANI_FEATURES`); or else `vg_hmac_sha256_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -57,7 +57,7 @@ pub unsafe fn vg_hmac_sha256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, inne /// /// Contract: `VG.Spec.Hmac.Instance.finalizeContract` of `VG.Spec.Hmac.sha256I`. Constant time: only the pointers and `count` may affect timing, not the states. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha256_finalize_shani` if the CPU supports its features (`VG_HMAC_SHA256_FINALIZE_SHANI_FEATURES`); or else `vg_hmac_sha256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha256_finalize_shani` if the CPU supports its features (`VG_HMAC_SHA256_FINALIZE_SHANI_FEATURES`); or else `vg_hmac_sha256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/idea.rs b/crates/verified-garbage-asm-x86/src/dispatch/idea.rs index 912d0229e9c..10f67b59181 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/idea.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/idea.rs @@ -2,25 +2,6 @@ //! The dispatchers of the verified `idea` functions for `x86` //! (`crate::idea`, `VG.Rust.dispatcher`). -/// IDEA in ECB mode (Lai, 1992, §3.3; SP 800-38A §6.1) on `n` complete 8-byte blocks at `data`, in place, under the 52 subkeys at `schedule`: encryption under the subkeys `vg_idea_expand_key` writes, decryption under those `vg_idea_invert_key` writes. No padding is added or removed. For `n = 0`, no data is transformed. -/// -/// Contract: `VG.Spec.Idea.ecbContract`. Constant time: only pointers and `n` may affect timing, not the subkeys or data, including words or subkeys that are 0. -/// -/// It runs `vg_idea_ecb`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `schedule` must be valid for reads of 104 bytes. -/// * `data` must be valid for reads and writes of `8 * n` bytes. -/// * `data` must not overlap `schedule` or the arguments on the stack (distinct Rust objects never do). -/// * Neither `schedule` nor `data` may overlap the return address on the stack or the 152 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_idea_ecb_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *const [u8; 104], data: *mut [u8; 8], n: usize) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::idea::vg_idea_ecb(schedule, data, n) } -} - /// IDEA encryption key schedule (Lai, 1992, §3.3): writes the 52 encryption subkeys of the 16-byte key at `key` (as eight big-endian 16-bit words, rotated left by 25 bits after every eight subkeys) to `*schedule`, as little-endian 16-bit words. /// /// Contract: `VG.Spec.Idea.expandKeyContract`. Constant time: only pointers may affect timing, not the key. @@ -58,3 +39,22 @@ pub unsafe fn vg_idea_invert_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sched // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::idea::vg_idea_invert_key(schedule, inverse) } } + +/// IDEA in ECB mode (Lai, 1992, §3.3; SP 800-38A §6.1) on `n` complete 8-byte blocks at `data`, in place, under the 52 subkeys at `schedule`: encryption under the subkeys `vg_idea_expand_key` writes, decryption under those `vg_idea_invert_key` writes. No padding is added or removed. For `n = 0`, no data is transformed. +/// +/// Contract: `VG.Spec.Idea.ecbContract`. Constant time: only pointers and `n` may affect timing, not the subkeys or data, including words or subkeys that are 0. +/// +/// It runs `vg_idea_ecb`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `schedule` must be valid for reads of 104 bytes. +/// * `data` must be valid for reads and writes of `8 * n` bytes. +/// * `data` must not overlap `schedule` or the arguments on the stack (distinct Rust objects never do). +/// * Neither `schedule` nor `data` may overlap the return address on the stack or the 152 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_idea_ecb_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *const [u8; 104], data: *mut [u8; 8], n: usize) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::idea::vg_idea_ecb(schedule, data, n) } +} diff --git a/crates/verified-garbage-asm-x86/src/dispatch/mldsa44.rs b/crates/verified-garbage-asm-x86/src/dispatch/mldsa44.rs index 585aa6c9781..dd82f1ff76d 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/mldsa44.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/mldsa44.rs @@ -2,31 +2,29 @@ //! The dispatchers of the verified `mldsa44` functions for `x86` //! (`crate::mldsa44`, `VG.Rust.dispatcher`). -/// ML-DSA-44 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-44 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. +/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. /// -/// It runs `vg_mldsa44_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa44_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `sk` must be valid for reads of 2560 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `rnd` must be valid for reads of 32 bytes. -/// * `sig` must be valid for reads and writes of 2420 bytes. +/// * `seed` must be valid for reads of 32 bytes. +/// * `pk` must be valid for reads and writes of 1312 bytes. +/// * `sk` must be valid for reads and writes of 2560 bytes. /// * `scratch` must be valid for reads and writes of 77824 bytes. -/// * `sk` must have been written by `vg_mldsa44_keygen`. -/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. +/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { +pub unsafe fn vg_mldsa44_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1312], sk: *mut [u8; 2560], scratch: *mut [u64; 9728]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_sign(sk, mu, rnd, sig, scratch) } + unsafe { crate::mldsa44::vg_mldsa44_keygen(seed, pk, sk, scratch) } } /// ML-DSA-44 public-key hash: writes `tr = H(pk, 64)` (FIPS 204 Algorithm 8, line 6), SHAKE256 of the public key `*pk` to 64 bytes, to `*tr`, the digest `vg_mldsa44_verify_message_cached` takes. @@ -52,128 +50,106 @@ pub unsafe fn vg_mldsa44_pk_tr_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *co unsafe { crate::mldsa44::vg_mldsa44_pk_tr(pk, tr, scratch) } } -/// ML-DSA-44 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-44 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa44_verify_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. +/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. /// -/// It runs `vg_mldsa44_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa44_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `seed` must be valid for reads of 32 bytes. -/// * `pk` must be valid for reads and writes of 1312 bytes. -/// * `sk` must be valid for reads and writes of 2560 bytes. -/// * `scratch` must be valid for reads and writes of 77824 bytes. -/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. +/// * `pk` must be valid for reads of 1312 bytes. +/// * `prepared` must be valid for reads and writes of 5408 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). +/// * None of `pk`, `prepared` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1312], sk: *mut [u8; 2560], scratch: *mut [u64; 9728]) -> u32 { +pub unsafe fn vg_mldsa44_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], prepared: *mut [u8; 5408], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_keygen(seed, pk, sk, scratch) } + unsafe { crate::mldsa44::vg_mldsa44_prepare_verifying_key(pk, prepared, scratch) } } -/// ML-DSA-44 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// ML-DSA-44 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa44_sign_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. /// -/// It runs `vg_mldsa44_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa44_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `pk` must be valid for reads of 1312 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `sig` must be valid for reads of 2420 bytes. -/// * `scratch` must be valid for reads and writes of 77824 bytes. +/// * `sk` must be valid for reads of 2560 bytes. +/// * `prepared` must be valid for reads and writes of 14848 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). -/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). +/// * None of `sk`, `prepared` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], mu: *const [u8; 64], sig: *const [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { +pub unsafe fn vg_mldsa44_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], prepared: *mut [u8; 14848], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_verify(pk, mu, sig, scratch) } + unsafe { crate::mldsa44::vg_mldsa44_prepare_signing_key(sk, prepared, scratch) } } -/// ML-DSA-44 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. +/// ML-DSA-44 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. +/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. /// -/// It runs `vg_mldsa44_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa44_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `sk` must be valid for reads of 2560 bytes. -/// * `msg` must be valid for reads of `msg_len` bytes. -/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `mu` must be valid for reads of 64 bytes. /// * `rnd` must be valid for reads of 32 bytes. /// * `sig` must be valid for reads and writes of 2420 bytes. -/// * `scratch` must be valid for reads and writes of 78848 bytes. +/// * `scratch` must be valid for reads and writes of 77824 bytes. /// * `sk` must have been written by `vg_mldsa44_keygen`. /// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 176 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9856]) -> u32 { +pub unsafe fn vg_mldsa44_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { crate::mldsa44::vg_mldsa44_sign(sk, mu, rnd, sig, scratch) } } -/// ML-DSA-44 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa44_sign_message_prepared`. +/// ML-DSA-44 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. /// -/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. +/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. /// -/// It runs `vg_mldsa44_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa44_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `sk` must be valid for reads of 2560 bytes. -/// * `prepared` must be valid for reads and writes of 14848 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. -/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). -/// * None of `sk`, `prepared` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mldsa44_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], prepared: *mut [u8; 14848], scratch: *mut [u64; 128]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_prepare_signing_key(sk, prepared, scratch) } -} - -/// ML-DSA-44 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa44_verify_message_prepared`. -/// -/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. -/// -/// It runs `vg_mldsa44_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// The function may overwrite the arguments on the stack, as the calling convention lets it. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 1312 bytes. -/// * `prepared` must be valid for reads and writes of 5408 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `msg` must be valid for reads of `msg_len` bytes. +/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `rnd` must be valid for reads of 32 bytes. +/// * `sig` must be valid for reads and writes of 2420 bytes. +/// * `scratch` must be valid for reads and writes of 78848 bytes. +/// * `sk` must have been written by `vg_mldsa44_keygen`. +/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). -/// * None of `pk`, `prepared` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 176 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], prepared: *mut [u8; 5408], scratch: *mut [u64; 128]) { +pub unsafe fn vg_mldsa44_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9856]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa44::vg_mldsa44_prepare_verifying_key(pk, prepared, scratch) } + unsafe { crate::mldsa44::vg_mldsa44_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } /// ML-DSA-44 signing with a prepared private key: `vg_mldsa44_sign_message` (with its results, timing and leakage) of the private key that `*sk` begins with. @@ -205,6 +181,30 @@ pub unsafe fn vg_mldsa44_sign_message_prepared_auto(_dit: verified_garbage_cpu:: unsafe { crate::mldsa44::vg_mldsa44_sign_message_prepared(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } +/// ML-DSA-44 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// +/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// +/// It runs `vg_mldsa44_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// The function may overwrite the arguments on the stack, as the calling convention lets it. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 1312 bytes. +/// * `mu` must be valid for reads of 64 bytes. +/// * `sig` must be valid for reads of 2420 bytes. +/// * `scratch` must be valid for reads and writes of 77824 bytes. +/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). +/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). +/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mldsa44_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], mu: *const [u8; 64], sig: *const [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mldsa44::vg_mldsa44_verify(pk, mu, sig, scratch) } +} + /// ML-DSA-44 message verification with a cached public-key digest. Returns 1 for a valid signature, 0 for an invalid signature or exhausted sampling bound, and 2 if the context exceeds 255 bytes. It is `ML-DSA.Verify(pk, M, σ, ctx)` (FIPS 204 Algorithm 3) with `H(pk, 64)` read from `*tr`: with the public key `*pk`, the `msg_len` bytes of the message at `msg`, the `ctx_len` bytes of the context string at `ctx` and the signature `*sig`, it formats the message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 5), computes `μ = H(tr ‖ M′, 64)`, and verifies `*sig` on `μ` with `*pk`, `ML-DSA.Verify_internal(pk, M′, σ)` (Algorithm 8). /// /// Contract: `VG.Spec.MlDsa.verifyMessageCachedContract`. Timing may depend on the public key, message, context string and signature, and on argument addresses and lengths. diff --git a/crates/verified-garbage-asm-x86/src/dispatch/mldsa65.rs b/crates/verified-garbage-asm-x86/src/dispatch/mldsa65.rs index deb6cdf18df..3f8bcee71d7 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/mldsa65.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/mldsa65.rs @@ -2,31 +2,29 @@ //! The dispatchers of the verified `mldsa65` functions for `x86` //! (`crate::mldsa65`, `VG.Rust.dispatcher`). -/// ML-DSA-65 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-65 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. +/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. /// -/// It runs `vg_mldsa65_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa65_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `sk` must be valid for reads of 4032 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `rnd` must be valid for reads of 32 bytes. -/// * `sig` must be valid for reads and writes of 3309 bytes. +/// * `seed` must be valid for reads of 32 bytes. +/// * `pk` must be valid for reads and writes of 1952 bytes. +/// * `sk` must be valid for reads and writes of 4032 bytes. /// * `scratch` must be valid for reads and writes of 103424 bytes. -/// * `sk` must have been written by `vg_mldsa65_keygen`. -/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. +/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { +pub unsafe fn vg_mldsa65_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1952], sk: *mut [u8; 4032], scratch: *mut [u64; 12928]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_sign(sk, mu, rnd, sig, scratch) } + unsafe { crate::mldsa65::vg_mldsa65_keygen(seed, pk, sk, scratch) } } /// ML-DSA-65 public-key hash: writes `tr = H(pk, 64)` (FIPS 204 Algorithm 8, line 6), SHAKE256 of the public key `*pk` to 64 bytes, to `*tr`, the digest `vg_mldsa65_verify_message_cached` takes. @@ -52,128 +50,106 @@ pub unsafe fn vg_mldsa65_pk_tr_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *co unsafe { crate::mldsa65::vg_mldsa65_pk_tr(pk, tr, scratch) } } -/// ML-DSA-65 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-65 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa65_verify_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. +/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. /// -/// It runs `vg_mldsa65_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa65_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `seed` must be valid for reads of 32 bytes. -/// * `pk` must be valid for reads and writes of 1952 bytes. -/// * `sk` must be valid for reads and writes of 4032 bytes. -/// * `scratch` must be valid for reads and writes of 103424 bytes. -/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. +/// * `pk` must be valid for reads of 1952 bytes. +/// * `prepared` must be valid for reads and writes of 8096 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). +/// * None of `pk`, `prepared` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1952], sk: *mut [u8; 4032], scratch: *mut [u64; 12928]) -> u32 { +pub unsafe fn vg_mldsa65_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], prepared: *mut [u8; 8096], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_keygen(seed, pk, sk, scratch) } + unsafe { crate::mldsa65::vg_mldsa65_prepare_verifying_key(pk, prepared, scratch) } } -/// ML-DSA-65 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// ML-DSA-65 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa65_sign_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. /// -/// It runs `vg_mldsa65_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa65_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `pk` must be valid for reads of 1952 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `sig` must be valid for reads of 3309 bytes. -/// * `scratch` must be valid for reads and writes of 103424 bytes. +/// * `sk` must be valid for reads of 4032 bytes. +/// * `prepared` must be valid for reads and writes of 21440 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). -/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). +/// * None of `sk`, `prepared` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], mu: *const [u8; 64], sig: *const [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { +pub unsafe fn vg_mldsa65_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], prepared: *mut [u8; 21440], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_verify(pk, mu, sig, scratch) } + unsafe { crate::mldsa65::vg_mldsa65_prepare_signing_key(sk, prepared, scratch) } } -/// ML-DSA-65 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. +/// ML-DSA-65 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. +/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. /// -/// It runs `vg_mldsa65_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa65_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `sk` must be valid for reads of 4032 bytes. -/// * `msg` must be valid for reads of `msg_len` bytes. -/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `mu` must be valid for reads of 64 bytes. /// * `rnd` must be valid for reads of 32 bytes. /// * `sig` must be valid for reads and writes of 3309 bytes. -/// * `scratch` must be valid for reads and writes of 104448 bytes. +/// * `scratch` must be valid for reads and writes of 103424 bytes. /// * `sk` must have been written by `vg_mldsa65_keygen`. /// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 176 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 13056]) -> u32 { +pub unsafe fn vg_mldsa65_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { crate::mldsa65::vg_mldsa65_sign(sk, mu, rnd, sig, scratch) } } -/// ML-DSA-65 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa65_sign_message_prepared`. +/// ML-DSA-65 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. /// -/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. +/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. /// -/// It runs `vg_mldsa65_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa65_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `sk` must be valid for reads of 4032 bytes. -/// * `prepared` must be valid for reads and writes of 21440 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. -/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). -/// * None of `sk`, `prepared` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mldsa65_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], prepared: *mut [u8; 21440], scratch: *mut [u64; 128]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_prepare_signing_key(sk, prepared, scratch) } -} - -/// ML-DSA-65 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa65_verify_message_prepared`. -/// -/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. -/// -/// It runs `vg_mldsa65_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// The function may overwrite the arguments on the stack, as the calling convention lets it. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 1952 bytes. -/// * `prepared` must be valid for reads and writes of 8096 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `msg` must be valid for reads of `msg_len` bytes. +/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `rnd` must be valid for reads of 32 bytes. +/// * `sig` must be valid for reads and writes of 3309 bytes. +/// * `scratch` must be valid for reads and writes of 104448 bytes. +/// * `sk` must have been written by `vg_mldsa65_keygen`. +/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). -/// * None of `pk`, `prepared` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 176 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], prepared: *mut [u8; 8096], scratch: *mut [u64; 128]) { +pub unsafe fn vg_mldsa65_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 13056]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa65::vg_mldsa65_prepare_verifying_key(pk, prepared, scratch) } + unsafe { crate::mldsa65::vg_mldsa65_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } /// ML-DSA-65 signing with a prepared private key: `vg_mldsa65_sign_message` (with its results, timing and leakage) of the private key that `*sk` begins with. @@ -205,6 +181,30 @@ pub unsafe fn vg_mldsa65_sign_message_prepared_auto(_dit: verified_garbage_cpu:: unsafe { crate::mldsa65::vg_mldsa65_sign_message_prepared(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } +/// ML-DSA-65 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// +/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// +/// It runs `vg_mldsa65_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// The function may overwrite the arguments on the stack, as the calling convention lets it. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 1952 bytes. +/// * `mu` must be valid for reads of 64 bytes. +/// * `sig` must be valid for reads of 3309 bytes. +/// * `scratch` must be valid for reads and writes of 103424 bytes. +/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). +/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). +/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mldsa65_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], mu: *const [u8; 64], sig: *const [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mldsa65::vg_mldsa65_verify(pk, mu, sig, scratch) } +} + /// ML-DSA-65 message verification with a cached public-key digest. Returns 1 for a valid signature, 0 for an invalid signature or exhausted sampling bound, and 2 if the context exceeds 255 bytes. It is `ML-DSA.Verify(pk, M, σ, ctx)` (FIPS 204 Algorithm 3) with `H(pk, 64)` read from `*tr`: with the public key `*pk`, the `msg_len` bytes of the message at `msg`, the `ctx_len` bytes of the context string at `ctx` and the signature `*sig`, it formats the message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 5), computes `μ = H(tr ‖ M′, 64)`, and verifies `*sig` on `μ` with `*pk`, `ML-DSA.Verify_internal(pk, M′, σ)` (Algorithm 8). /// /// Contract: `VG.Spec.MlDsa.verifyMessageCachedContract`. Timing may depend on the public key, message, context string and signature, and on argument addresses and lengths. diff --git a/crates/verified-garbage-asm-x86/src/dispatch/mldsa87.rs b/crates/verified-garbage-asm-x86/src/dispatch/mldsa87.rs index 60d535127fb..13c9c2cd046 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/mldsa87.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/mldsa87.rs @@ -2,31 +2,29 @@ //! The dispatchers of the verified `mldsa87` functions for `x86` //! (`crate::mldsa87`, `VG.Rust.dispatcher`). -/// ML-DSA-87 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-87 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. +/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. /// -/// It runs `vg_mldsa87_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa87_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `sk` must be valid for reads of 4896 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `rnd` must be valid for reads of 32 bytes. -/// * `sig` must be valid for reads and writes of 4627 bytes. +/// * `seed` must be valid for reads of 32 bytes. +/// * `pk` must be valid for reads and writes of 2592 bytes. +/// * `sk` must be valid for reads and writes of 4896 bytes. /// * `scratch` must be valid for reads and writes of 144384 bytes. -/// * `sk` must have been written by `vg_mldsa87_keygen`. -/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. +/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { +pub unsafe fn vg_mldsa87_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 2592], sk: *mut [u8; 4896], scratch: *mut [u64; 18048]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_sign(sk, mu, rnd, sig, scratch) } + unsafe { crate::mldsa87::vg_mldsa87_keygen(seed, pk, sk, scratch) } } /// ML-DSA-87 public-key hash: writes `tr = H(pk, 64)` (FIPS 204 Algorithm 8, line 6), SHAKE256 of the public key `*pk` to 64 bytes, to `*tr`, the digest `vg_mldsa87_verify_message_cached` takes. @@ -52,128 +50,106 @@ pub unsafe fn vg_mldsa87_pk_tr_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *co unsafe { crate::mldsa87::vg_mldsa87_pk_tr(pk, tr, scratch) } } -/// ML-DSA-87 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-87 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa87_verify_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. +/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. /// -/// It runs `vg_mldsa87_keygen`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa87_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `seed` must be valid for reads of 32 bytes. -/// * `pk` must be valid for reads and writes of 2592 bytes. -/// * `sk` must be valid for reads and writes of 4896 bytes. -/// * `scratch` must be valid for reads and writes of 144384 bytes. -/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. +/// * `pk` must be valid for reads of 2592 bytes. +/// * `prepared` must be valid for reads and writes of 10784 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). +/// * None of `pk`, `prepared` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 2592], sk: *mut [u8; 4896], scratch: *mut [u64; 18048]) -> u32 { +pub unsafe fn vg_mldsa87_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], prepared: *mut [u8; 10784], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_keygen(seed, pk, sk, scratch) } + unsafe { crate::mldsa87::vg_mldsa87_prepare_verifying_key(pk, prepared, scratch) } } -/// ML-DSA-87 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// ML-DSA-87 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa87_sign_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. /// -/// It runs `vg_mldsa87_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa87_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `pk` must be valid for reads of 2592 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `sig` must be valid for reads of 4627 bytes. -/// * `scratch` must be valid for reads and writes of 144384 bytes. +/// * `sk` must be valid for reads of 4896 bytes. +/// * `prepared` must be valid for reads and writes of 28448 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). -/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). +/// * None of `sk`, `prepared` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], mu: *const [u8; 64], sig: *const [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { +pub unsafe fn vg_mldsa87_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], prepared: *mut [u8; 28448], scratch: *mut [u64; 128]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_verify(pk, mu, sig, scratch) } + unsafe { crate::mldsa87::vg_mldsa87_prepare_signing_key(sk, prepared, scratch) } } -/// ML-DSA-87 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. +/// ML-DSA-87 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. +/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. /// -/// It runs `vg_mldsa87_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa87_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `sk` must be valid for reads of 4896 bytes. -/// * `msg` must be valid for reads of `msg_len` bytes. -/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `mu` must be valid for reads of 64 bytes. /// * `rnd` must be valid for reads of 32 bytes. /// * `sig` must be valid for reads and writes of 4627 bytes. -/// * `scratch` must be valid for reads and writes of 145408 bytes. +/// * `scratch` must be valid for reads and writes of 144384 bytes. /// * `sk` must have been written by `vg_mldsa87_keygen`. /// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 176 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18176]) -> u32 { +pub unsafe fn vg_mldsa87_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { crate::mldsa87::vg_mldsa87_sign(sk, mu, rnd, sig, scratch) } } -/// ML-DSA-87 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa87_sign_message_prepared`. +/// ML-DSA-87 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. /// -/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. +/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. /// -/// It runs `vg_mldsa87_prepare_signing_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mldsa87_sign_message`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `sk` must be valid for reads of 4896 bytes. -/// * `prepared` must be valid for reads and writes of 28448 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. -/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). -/// * None of `sk`, `prepared` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mldsa87_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], prepared: *mut [u8; 28448], scratch: *mut [u64; 128]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_prepare_signing_key(sk, prepared, scratch) } -} - -/// ML-DSA-87 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa87_verify_message_prepared`. -/// -/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. -/// -/// It runs `vg_mldsa87_prepare_verifying_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// The function may overwrite the arguments on the stack, as the calling convention lets it. -/// -/// # Safety -/// -/// * `pk` must be valid for reads of 2592 bytes. -/// * `prepared` must be valid for reads and writes of 10784 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `msg` must be valid for reads of `msg_len` bytes. +/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `rnd` must be valid for reads of 32 bytes. +/// * `sig` must be valid for reads and writes of 4627 bytes. +/// * `scratch` must be valid for reads and writes of 145408 bytes. +/// * `sk` must have been written by `vg_mldsa87_keygen`. +/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). -/// * None of `pk`, `prepared` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 176 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], prepared: *mut [u8; 10784], scratch: *mut [u64; 128]) { +pub unsafe fn vg_mldsa87_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18176]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mldsa87::vg_mldsa87_prepare_verifying_key(pk, prepared, scratch) } + unsafe { crate::mldsa87::vg_mldsa87_sign_message(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } /// ML-DSA-87 signing with a prepared private key: `vg_mldsa87_sign_message` (with its results, timing and leakage) of the private key that `*sk` begins with. @@ -205,6 +181,30 @@ pub unsafe fn vg_mldsa87_sign_message_prepared_auto(_dit: verified_garbage_cpu:: unsafe { crate::mldsa87::vg_mldsa87_sign_message_prepared(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } +/// ML-DSA-87 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// +/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// +/// It runs `vg_mldsa87_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// The function may overwrite the arguments on the stack, as the calling convention lets it. +/// +/// # Safety +/// +/// * `pk` must be valid for reads of 2592 bytes. +/// * `mu` must be valid for reads of 64 bytes. +/// * `sig` must be valid for reads of 4627 bytes. +/// * `scratch` must be valid for reads and writes of 144384 bytes. +/// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). +/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). +/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mldsa87_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], mu: *const [u8; 64], sig: *const [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mldsa87::vg_mldsa87_verify(pk, mu, sig, scratch) } +} + /// ML-DSA-87 message verification with a cached public-key digest. Returns 1 for a valid signature, 0 for an invalid signature or exhausted sampling bound, and 2 if the context exceeds 255 bytes. It is `ML-DSA.Verify(pk, M, σ, ctx)` (FIPS 204 Algorithm 3) with `H(pk, 64)` read from `*tr`: with the public key `*pk`, the `msg_len` bytes of the message at `msg`, the `ctx_len` bytes of the context string at `ctx` and the signature `*sig`, it formats the message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 5), computes `μ = H(tr ‖ M′, 64)`, and verifies `*sig` on `μ` with `*pk`, `ML-DSA.Verify_internal(pk, M′, σ)` (Algorithm 8). /// /// Contract: `VG.Spec.MlDsa.verifyMessageCachedContract`. Timing may depend on the public key, message, context string and signature, and on argument addresses and lengths. diff --git a/crates/verified-garbage-asm-x86/src/dispatch/mlkem1024.rs b/crates/verified-garbage-asm-x86/src/dispatch/mlkem1024.rs index 175599d2fc4..59aab01e84d 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/mlkem1024.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/mlkem1024.rs @@ -2,6 +2,23 @@ //! The dispatchers of the verified `mlkem1024` functions for `x86` //! (`crate::mlkem1024`, `VG.Rust.dispatcher`). +/// The ML-KEM-1024 encapsulation key check (FIPS 203 §7.2): returns 1 if every 12-bit integer that the first 1536 bytes of `*ek` encode is less than `q` = 3329 (the modulus check), and 0 otherwise. An encapsulation key must pass it before it is given to `vg_mlkem1024_encaps`. +/// +/// Contract: `VG.Spec.MlKem1024.checkEkContract`. Constant time: only the pointer may affect timing. +/// +/// It runs `vg_mlkem1024_check_ek`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `ek` must be valid for reads of 1568 bytes. +/// * `ek` must not overlap the return address on the stack or the 16 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mlkem1024_check_ek_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1568]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mlkem1024::vg_mlkem1024_check_ek(ek) } +} + /// ML-KEM-1024 key generation from a seed, `ML-KEM.KeyGen_internal(d, z)` (FIPS 203 Algorithm 16): with `d` in bytes 0–31 of `*seed` and `z` in bytes 32–63, writes the encapsulation key to `*ek` and the decapsulation key to `*dk`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// /// Contract: `VG.Spec.MlKem1024.keyGenContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of the encapsulation key), but not on anything else of the seed or the keys. @@ -54,73 +71,56 @@ pub unsafe fn vg_mlkem1024_encaps_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: unsafe { crate::mlkem1024::vg_mlkem1024_encaps(ek, m, key, ct, scratch) } } -/// ML-KEM-1024 decapsulation, `ML-KEM.Decaps_internal(dk, c)` (FIPS 203 Algorithm 18): with the decapsulation key `*dk` and the ciphertext `*ct`, writes the shared secret key to `*key`, which is the implicit rejection key `J(z ‖ c)` if the ciphertext does not re-encrypt to itself. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-KEM-1024 encapsulation with given randomness and the hash of the key, `ML-KEM.Encaps_internal(ek, m)` (FIPS 203 Algorithm 17): with the encapsulation key `*ek`, its hash `H(ek)` in `*h` and the randomness `*m`, writes the shared secret key to `*key` and the ciphertext to `*ct`, as `vg_mlkem1024_encaps` does, using `*h` instead of hashing `*ek`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlKem1024.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 3072–3103 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. +/// Contract: `VG.Spec.MlKem1024.encapsHContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on its hash, on the randomness or on the outputs. /// -/// It runs `vg_mlkem1024_decaps`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mlkem1024_encaps_h`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `dk` must be valid for reads of 3168 bytes. -/// * `ct` must be valid for reads of 1568 bytes. +/// * `ek` must be valid for reads of 1568 bytes. +/// * `h` must be valid for reads of 32 bytes. +/// * `m` must be valid for reads of 32 bytes. /// * `key` must be valid for reads and writes of 32 bytes. +/// * `ct` must be valid for reads and writes of 1568 bytes. /// * `scratch` must be valid for reads and writes of 49152 bytes. -/// * `dk` must have been written by `vg_mlkem1024_keygen` (so that it passes the checks of FIPS 203 §7.3). +/// * `ek` must have passed `vg_mlkem1024_check_ek` (FIPS 203 §7.2). +/// * `h` must hold `H(ek)`, the SHA3-256 hash of the bytes of `*ek` (FIPS 203 §4.1). +/// * `m` must be fresh random bytes from an approved RBG (FIPS 203 §3.3). /// * `scratch` is working space: on return each of its bytes is unchanged or zero. -/// * `key` and `scratch` must not overlap each other, `dk` or `ct` (distinct Rust objects never do). -/// * None of `dk`, `ct`, `key` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 120 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mlkem1024_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: *const [u8; 3168], ct: *const [u8; 1568], key: *mut [u8; 32], scratch: *mut [u64; 6144]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mlkem1024::vg_mlkem1024_decaps(dk, ct, key, scratch) } -} - -/// The ML-KEM-1024 encapsulation key check (FIPS 203 §7.2): returns 1 if every 12-bit integer that the first 1536 bytes of `*ek` encode is less than `q` = 3329 (the modulus check), and 0 otherwise. An encapsulation key must pass it before it is given to `vg_mlkem1024_encaps`. -/// -/// Contract: `VG.Spec.MlKem1024.checkEkContract`. Constant time: only the pointer may affect timing. -/// -/// It runs `vg_mlkem1024_check_ek`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `ek` must be valid for reads of 1568 bytes. -/// * `ek` must not overlap the return address on the stack or the 16 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `key`, `ct` and `scratch` must not overlap each other, `ek`, `h` or `m` (distinct Rust objects never do). +/// * None of `ek`, `h`, `m`, `key`, `ct` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mlkem1024_check_ek_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1568]) -> u32 { +pub unsafe fn vg_mlkem1024_encaps_h_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1568], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1568], scratch: *mut [u64; 6144]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mlkem1024::vg_mlkem1024_check_ek(ek) } + unsafe { crate::mlkem1024::vg_mlkem1024_encaps_h(ek, h, m, key, ct, scratch) } } -/// ML-KEM-1024 encapsulation with given randomness and the hash of the key, `ML-KEM.Encaps_internal(ek, m)` (FIPS 203 Algorithm 17): with the encapsulation key `*ek`, its hash `H(ek)` in `*h` and the randomness `*m`, writes the shared secret key to `*key` and the ciphertext to `*ct`, as `vg_mlkem1024_encaps` does, using `*h` instead of hashing `*ek`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-KEM-1024 decapsulation, `ML-KEM.Decaps_internal(dk, c)` (FIPS 203 Algorithm 18): with the decapsulation key `*dk` and the ciphertext `*ct`, writes the shared secret key to `*key`, which is the implicit rejection key `J(z ‖ c)` if the ciphertext does not re-encrypt to itself. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlKem1024.encapsHContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on its hash, on the randomness or on the outputs. +/// Contract: `VG.Spec.MlKem1024.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 3072–3103 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. /// -/// It runs `vg_mlkem1024_encaps_h`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mlkem1024_decaps`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `ek` must be valid for reads of 1568 bytes. -/// * `h` must be valid for reads of 32 bytes. -/// * `m` must be valid for reads of 32 bytes. +/// * `dk` must be valid for reads of 3168 bytes. +/// * `ct` must be valid for reads of 1568 bytes. /// * `key` must be valid for reads and writes of 32 bytes. -/// * `ct` must be valid for reads and writes of 1568 bytes. /// * `scratch` must be valid for reads and writes of 49152 bytes. -/// * `ek` must have passed `vg_mlkem1024_check_ek` (FIPS 203 §7.2). -/// * `h` must hold `H(ek)`, the SHA3-256 hash of the bytes of `*ek` (FIPS 203 §4.1). -/// * `m` must be fresh random bytes from an approved RBG (FIPS 203 §3.3). +/// * `dk` must have been written by `vg_mlkem1024_keygen` (so that it passes the checks of FIPS 203 §7.3). /// * `scratch` is working space: on return each of its bytes is unchanged or zero. -/// * `key`, `ct` and `scratch` must not overlap each other, `ek`, `h` or `m` (distinct Rust objects never do). -/// * None of `ek`, `h`, `m`, `key`, `ct` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `key` and `scratch` must not overlap each other, `dk` or `ct` (distinct Rust objects never do). +/// * None of `dk`, `ct`, `key` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 120 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mlkem1024_encaps_h_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1568], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1568], scratch: *mut [u64; 6144]) -> u32 { +pub unsafe fn vg_mlkem1024_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: *const [u8; 3168], ct: *const [u8; 1568], key: *mut [u8; 32], scratch: *mut [u64; 6144]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mlkem1024::vg_mlkem1024_encaps_h(ek, h, m, key, ct, scratch) } + unsafe { crate::mlkem1024::vg_mlkem1024_decaps(dk, ct, key, scratch) } } diff --git a/crates/verified-garbage-asm-x86/src/dispatch/mlkem768.rs b/crates/verified-garbage-asm-x86/src/dispatch/mlkem768.rs index 866d8daaa99..ca44a2eda2a 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/mlkem768.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/mlkem768.rs @@ -2,6 +2,23 @@ //! The dispatchers of the verified `mlkem768` functions for `x86` //! (`crate::mlkem768`, `VG.Rust.dispatcher`). +/// The ML-KEM-768 encapsulation key check (FIPS 203 §7.2): returns 1 if every 12-bit integer that the first 1152 bytes of `*ek` encode is less than `q` = 3329 (the modulus check), and 0 otherwise. An encapsulation key must pass it before it is given to `vg_mlkem768_encaps`. +/// +/// Contract: `VG.Spec.MlKem.checkEkContract`. Constant time: only the pointer may affect timing. +/// +/// It runs `vg_mlkem768_check_ek`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `ek` must be valid for reads of 1184 bytes. +/// * `ek` must not overlap the return address on the stack or the 16 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mlkem768_check_ek_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1184]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::mlkem768::vg_mlkem768_check_ek(ek) } +} + /// ML-KEM-768 key generation from a seed, `ML-KEM.KeyGen_internal(d, z)` (FIPS 203 Algorithm 16): with `d` in bytes 0–31 of `*seed` and `z` in bytes 32–63, writes the encapsulation key to `*ek` and the decapsulation key to `*dk`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// /// Contract: `VG.Spec.MlKem.keyGenContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of the encapsulation key), but not on anything else of the seed or the keys. @@ -54,73 +71,56 @@ pub unsafe fn vg_mlkem768_encaps_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: * unsafe { crate::mlkem768::vg_mlkem768_encaps(ek, m, key, ct, scratch) } } -/// ML-KEM-768 decapsulation, `ML-KEM.Decaps_internal(dk, c)` (FIPS 203 Algorithm 18): with the decapsulation key `*dk` and the ciphertext `*ct`, writes the shared secret key to `*key`, which is the implicit rejection key `J(z ‖ c)` if the ciphertext does not re-encrypt to itself. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-KEM-768 encapsulation with given randomness and the hash of the key, `ML-KEM.Encaps_internal(ek, m)` (FIPS 203 Algorithm 17): with the encapsulation key `*ek`, its hash `H(ek)` in `*h` and the randomness `*m`, writes the shared secret key to `*key` and the ciphertext to `*ct`, as `vg_mlkem768_encaps` does, using `*h` instead of hashing `*ek`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlKem.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 2304–2335 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. +/// Contract: `VG.Spec.MlKem.encapsHContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on its hash, on the randomness or on the outputs. /// -/// It runs `vg_mlkem768_decaps`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mlkem768_encaps_h`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `dk` must be valid for reads of 2400 bytes. -/// * `ct` must be valid for reads of 1088 bytes. +/// * `ek` must be valid for reads of 1184 bytes. +/// * `h` must be valid for reads of 32 bytes. +/// * `m` must be valid for reads of 32 bytes. /// * `key` must be valid for reads and writes of 32 bytes. +/// * `ct` must be valid for reads and writes of 1088 bytes. /// * `scratch` must be valid for reads and writes of 32768 bytes. -/// * `dk` must have been written by `vg_mlkem768_keygen` (so that it passes the checks of FIPS 203 §7.3). +/// * `ek` must have passed `vg_mlkem768_check_ek` (FIPS 203 §7.2). +/// * `h` must hold `H(ek)`, the SHA3-256 hash of the bytes of `*ek` (FIPS 203 §4.1). +/// * `m` must be fresh random bytes from an approved RBG (FIPS 203 §3.3). /// * `scratch` is working space: on return each of its bytes is unchanged or zero. -/// * `key` and `scratch` must not overlap each other, `dk` or `ct` (distinct Rust objects never do). -/// * None of `dk`, `ct`, `key` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 120 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mlkem768_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: *const [u8; 2400], ct: *const [u8; 1088], key: *mut [u8; 32], scratch: *mut [u64; 4096]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mlkem768::vg_mlkem768_decaps(dk, ct, key, scratch) } -} - -/// The ML-KEM-768 encapsulation key check (FIPS 203 §7.2): returns 1 if every 12-bit integer that the first 1152 bytes of `*ek` encode is less than `q` = 3329 (the modulus check), and 0 otherwise. An encapsulation key must pass it before it is given to `vg_mlkem768_encaps`. -/// -/// Contract: `VG.Spec.MlKem.checkEkContract`. Constant time: only the pointer may affect timing. -/// -/// It runs `vg_mlkem768_check_ek`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `ek` must be valid for reads of 1184 bytes. -/// * `ek` must not overlap the return address on the stack or the 16 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `key`, `ct` and `scratch` must not overlap each other, `ek`, `h` or `m` (distinct Rust objects never do). +/// * None of `ek`, `h`, `m`, `key`, `ct` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mlkem768_check_ek_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1184]) -> u32 { +pub unsafe fn vg_mlkem768_encaps_h_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1184], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1088], scratch: *mut [u64; 4096]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mlkem768::vg_mlkem768_check_ek(ek) } + unsafe { crate::mlkem768::vg_mlkem768_encaps_h(ek, h, m, key, ct, scratch) } } -/// ML-KEM-768 encapsulation with given randomness and the hash of the key, `ML-KEM.Encaps_internal(ek, m)` (FIPS 203 Algorithm 17): with the encapsulation key `*ek`, its hash `H(ek)` in `*h` and the randomness `*m`, writes the shared secret key to `*key` and the ciphertext to `*ct`, as `vg_mlkem768_encaps` does, using `*h` instead of hashing `*ek`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-KEM-768 decapsulation, `ML-KEM.Decaps_internal(dk, c)` (FIPS 203 Algorithm 18): with the decapsulation key `*dk` and the ciphertext `*ct`, writes the shared secret key to `*key`, which is the implicit rejection key `J(z ‖ c)` if the ciphertext does not re-encrypt to itself. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlKem.encapsHContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on its hash, on the randomness or on the outputs. +/// Contract: `VG.Spec.MlKem.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 2304–2335 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. /// -/// It runs `vg_mlkem768_encaps_h`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_mlkem768_decaps`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `ek` must be valid for reads of 1184 bytes. -/// * `h` must be valid for reads of 32 bytes. -/// * `m` must be valid for reads of 32 bytes. +/// * `dk` must be valid for reads of 2400 bytes. +/// * `ct` must be valid for reads of 1088 bytes. /// * `key` must be valid for reads and writes of 32 bytes. -/// * `ct` must be valid for reads and writes of 1088 bytes. /// * `scratch` must be valid for reads and writes of 32768 bytes. -/// * `ek` must have passed `vg_mlkem768_check_ek` (FIPS 203 §7.2). -/// * `h` must hold `H(ek)`, the SHA3-256 hash of the bytes of `*ek` (FIPS 203 §4.1). -/// * `m` must be fresh random bytes from an approved RBG (FIPS 203 §3.3). +/// * `dk` must have been written by `vg_mlkem768_keygen` (so that it passes the checks of FIPS 203 §7.3). /// * `scratch` is working space: on return each of its bytes is unchanged or zero. -/// * `key`, `ct` and `scratch` must not overlap each other, `ek`, `h` or `m` (distinct Rust objects never do). -/// * None of `ek`, `h`, `m`, `key`, `ct` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `key` and `scratch` must not overlap each other, `dk` or `ct` (distinct Rust objects never do). +/// * None of `dk`, `ct`, `key` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 120 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mlkem768_encaps_h_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1184], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1088], scratch: *mut [u64; 4096]) -> u32 { +pub unsafe fn vg_mlkem768_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: *const [u8; 2400], ct: *const [u8; 1088], key: *mut [u8; 32], scratch: *mut [u64; 4096]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::mlkem768::vg_mlkem768_encaps_h(ek, h, m, key, ct, scratch) } + unsafe { crate::mlkem768::vg_mlkem768_decaps(dk, ct, key, scratch) } } diff --git a/crates/verified-garbage-asm-x86/src/dispatch/pbkdf2_sha1.rs b/crates/verified-garbage-asm-x86/src/dispatch/pbkdf2_sha1.rs index 96ee2befd05..e6c2e4ba7d9 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/pbkdf2_sha1.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/pbkdf2_sha1.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.pbkdf2Contract` of `VG.Spec.Hmac.sha1I`. Constant time: only the pointers, the lengths and `c` may affect timing, not the password, the salt or the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_pbkdf2_hmac_sha1_shani` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA1_SHANI_FEATURES`); or else `vg_pbkdf2_hmac_sha1`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_pbkdf2_hmac_sha1_shani` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA1_SHANI_FEATURES`); or else `vg_pbkdf2_hmac_sha1`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/pbkdf2_sha224.rs b/crates/verified-garbage-asm-x86/src/dispatch/pbkdf2_sha224.rs index 69a2923fb22..97a848de652 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/pbkdf2_sha224.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/pbkdf2_sha224.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.pbkdf2Contract` of `VG.Spec.Hmac.sha224I`. Constant time: only the pointers, the lengths and `c` may affect timing, not the password, the salt or the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_pbkdf2_hmac_sha224_shani` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA224_SHANI_FEATURES`); or else `vg_pbkdf2_hmac_sha224`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_pbkdf2_hmac_sha224_shani` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA224_SHANI_FEATURES`); or else `vg_pbkdf2_hmac_sha224`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/pbkdf2_sha256.rs b/crates/verified-garbage-asm-x86/src/dispatch/pbkdf2_sha256.rs index 34581c60d36..2b86cc686ff 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/pbkdf2_sha256.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/pbkdf2_sha256.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.pbkdf2Contract` of `VG.Spec.Hmac.sha256I`. Constant time: only the pointers, the lengths and `c` may affect timing, not the password, the salt or the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_pbkdf2_hmac_sha256_shani` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA256_SHANI_FEATURES`); or else `vg_pbkdf2_hmac_sha256`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_pbkdf2_hmac_sha256_shani` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA256_SHANI_FEATURES`); or else `vg_pbkdf2_hmac_sha256`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/rsa.rs b/crates/verified-garbage-asm-x86/src/dispatch/rsa.rs index 0b54f0648cd..96b4b99d069 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/rsa.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/rsa.rs @@ -2,6 +2,37 @@ //! The dispatchers of the verified `rsa` functions for `x86` //! (`crate::rsa`, `VG.Rust.dispatcher`). +/// The prime factors `p > q` of the modulus of an RSA private key `(n, e, d)` (SP 800-56B Rev. 2 §6.2.2's basic format), which bring it to the prime-factor format `(p, q, d)` that `vg_rsa_crt_values` takes, recovered by SP 800-56B Rev. 2 Appendix C.2's deterministic method. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes) and the private exponent `d` (`d_len` bytes), both most significant first, writes `p` and `q` (`n_len` bytes each, most significant first) and returns 1; or writes zeros to both and returns 0 if `n` is not such a modulus or the method fails. It finds the factors of a valid two-prime key whenever `K (p + q − 1) ≤ n` for `a = K φ(n)` (Appendix C.2's step 1): with the least private exponent, whenever the smaller prime exceeds `e²`, and in practice for every key whose primes are not tiny. +/// +/// Contract: `VG.Spec.Rsa.recoverPrimesDetContract`. Constant time but for the public key: timing may depend on the pointers, the lengths and the contents of `n` and `e`, not on `d`, the factors, or whether they were found. +/// +/// It runs `vg_rsa_recover_primes_det`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// The function may overwrite the arguments on the stack, as the calling convention lets it. +/// +/// # Safety +/// +/// * `p` must be valid for reads and writes of `p_len` bytes. +/// * `q` must be valid for reads and writes of `q_len` bytes. +/// * `n` must be valid for reads of `n_len` bytes. +/// * `e` must be valid for reads of `e_len` bytes. +/// * `d` must be valid for reads of `d_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `n_len` must be in 64..=1024. +/// * `p_len` and `q_len` must be `n_len`. +/// * `e_len` must be in 1..=8. +/// * `d_len` must be in 1..=`n_len`. +/// * `scratch_len` must be at least `16 * n_len + 32`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `p`, `q` and `scratch` must not overlap each other, `n`, `e` or `d` (distinct Rust objects never do). +/// * None of `p`, `q`, `n`, `e`, `d` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_rsa_recover_primes_det_auto(_dit: verified_garbage_cpu::Dit<'_>, p: *mut u8, p_len: usize, q: *mut u8, q_len: usize, n: *const u8, n_len: usize, e: *const u8, e_len: usize, d: *const u8, d_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::rsa::vg_rsa_recover_primes_det(p, p_len, q, q_len, n, n_len, e, e_len, d, d_len, scratch, scratch_len) } +} + /// Checks an RSA private key `(n, e, d, p, q, dP, dQ, qInv)` exactly as BoringSSL's `RSA_check_key` does. With the modulus `n` (`n_len` bytes), the public exponent `e` (`e_len` bytes), the private exponent `d` (`d_len` bytes), `p`, `dp` and `qinv` (`p_len` bytes) and `q` and `dq` (`q_len` bytes), all most significant first, returns 1 if `n` is odd, from 512 to 8192 bits and its first byte not zero, `e` is odd and from 3 to `2^33 - 1`, `d < n`, `p < n`, `q < n`, `p q = n`, `d e ≡ 1 (mod p - 1)`, `d e ≡ 1 (mod q - 1)`, `dP < p - 1`, `e dP ≡ 1 (mod p - 1)`, `dQ < q - 1`, `e dQ ≡ 1 (mod q - 1)`, `qInv < p` and `q qInv ≡ 1 (mod p)`; and 0 otherwise. Like `RSA_check_key`, it does not check that `p` and `q` are prime; if they are, `vg_rsa_private_checked` never returns its internal error for a key it accepts. /// /// Contract: `VG.Spec.Rsa.checkKeyContract`. Constant time but for the public key: timing may depend on the pointers, the lengths and the contents of `n` and `e`, not on the private key. @@ -94,34 +125,3 @@ pub unsafe fn vg_rsa_public_precompute_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::rsa::vg_rsa_public_precompute(pre, pre_len, n, n_len, scratch, scratch_len) } } - -/// The prime factors `p > q` of the modulus of an RSA private key `(n, e, d)` (SP 800-56B Rev. 2 §6.2.2's basic format), which bring it to the prime-factor format `(p, q, d)` that `vg_rsa_crt_values` takes, recovered by SP 800-56B Rev. 2 Appendix C.2's deterministic method. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes) and the private exponent `d` (`d_len` bytes), both most significant first, writes `p` and `q` (`n_len` bytes each, most significant first) and returns 1; or writes zeros to both and returns 0 if `n` is not such a modulus or the method fails. It finds the factors of a valid two-prime key whenever `K (p + q − 1) ≤ n` for `a = K φ(n)` (Appendix C.2's step 1): with the least private exponent, whenever the smaller prime exceeds `e²`, and in practice for every key whose primes are not tiny. -/// -/// Contract: `VG.Spec.Rsa.recoverPrimesDetContract`. Constant time but for the public key: timing may depend on the pointers, the lengths and the contents of `n` and `e`, not on `d`, the factors, or whether they were found. -/// -/// It runs `vg_rsa_recover_primes_det`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// The function may overwrite the arguments on the stack, as the calling convention lets it. -/// -/// # Safety -/// -/// * `p` must be valid for reads and writes of `p_len` bytes. -/// * `q` must be valid for reads and writes of `q_len` bytes. -/// * `n` must be valid for reads of `n_len` bytes. -/// * `e` must be valid for reads of `e_len` bytes. -/// * `d` must be valid for reads of `d_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `n_len` must be in 64..=1024. -/// * `p_len` and `q_len` must be `n_len`. -/// * `e_len` must be in 1..=8. -/// * `d_len` must be in 1..=`n_len`. -/// * `scratch_len` must be at least `16 * n_len + 32`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `p`, `q` and `scratch` must not overlap each other, `n`, `e` or `d` (distinct Rust objects never do). -/// * None of `p`, `q`, `n`, `e`, `d` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_rsa_recover_primes_det_auto(_dit: verified_garbage_cpu::Dit<'_>, p: *mut u8, p_len: usize, q: *mut u8, q_len: usize, n: *const u8, n_len: usize, e: *const u8, e_len: usize, d: *const u8, d_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa::vg_rsa_recover_primes_det(p, p_len, q, q_len, n, n_len, e, e_len, d, d_len, scratch, scratch_len) } -} diff --git a/crates/verified-garbage-asm-x86/src/dispatch/rsa_keygen.rs b/crates/verified-garbage-asm-x86/src/dispatch/rsa_keygen.rs index e7f0e82386b..ea75f9af38e 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/rsa_keygen.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/rsa_keygen.rs @@ -2,6 +2,25 @@ //! The dispatchers of the verified `rsa_keygen` functions for `x86` //! (`crate::rsa_keygen`, `VG.Rust.dispatcher`). +/// Start generating an RSA key, as BoringSSL's `RSA_generate_key_ex` (FIPS 186-5 Appendix A.1.3), with a modulus of `bits` bits rounded down to a multiple of 128, and the public exponent `e` (`e_len` octets, most significant first, with any number of leading zeros). Returns 0 if `e` is not odd from 3 to `2^32 - 1`, or 2 if `bits` is above 8192 or, rounded down, below 512, and writes zeros to `state`. Otherwise returns 1 and writes the search's seven public words to `state`: the rejected candidates the first prime's search allows, the primes' length in octets (`nlen / 16`), the entropy budget, the length of the first random tape to give `vg_rsa_generate`, the phase (0), the generations allowed (4), and the exponent's eight octets, most significant first. +/// +/// Contract: `VG.Spec.RsaKeyGen.keyInitContract`. Timing may depend on `bits` and `e`. +/// +/// It runs `vg_rsa_generate_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 56 bytes. +/// * `e` must be valid for reads of `e_len` bytes. +/// * `state` must not overlap `e` or the arguments on the stack (distinct Rust objects never do). +/// * Neither `state` nor `e` may overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_rsa_generate_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 7], bits: usize, e: *const u8, e_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::rsa_keygen::vg_rsa_generate_init(state, bits, e, e_len) } +} + /// Generate an RSA key, as BoringSSL's `RSA_generate_key_ex` (FIPS 186-5 Appendix A.1.3), over the random tape `rand`, from the state `state` that `vg_rsa_generate_init` started: the search for `p`, then for `q` not too close to `p`, each over candidates of `p_len` octets with their top two bits and bottom bit set, rejected by trial division, a common factor of `p − 1` and `e` or Miller–Rabin, up to BoringSSL's limit of rejected candidates; then the key, `d = e⁻¹ mod lcm(p − 1, q − 1)`, `dP`, `dQ` and `qInv`, with `p` the larger prime, checked as BoringSSL's `RSA_check_key` checks it, drawing both primes again if `d` is too small; a generation whose prime's search is exhausted is started again, up to four in all. Returns 1 and writes the key to `key` (`key_len` is `9 p_len`, for the primes' length `p_len = state[1]`): `n` and `d` (`2 p_len` octets each), then `p`, `q`, `dP`, `dQ` and `qInv` (`p_len` each), all most significant first; 2 if the key failed a check; 3 if every generation was exhausted; or 0 if the tape ran out inside a candidate. Writes the octets of `rand` it consumed to `used`, and the state for the next call to `state`, whose fourth word is, for 0, the length of the next tape, more than the octets of `rand` after the first `used` and at most the entropy budget: keep those octets, in order, and append fresh random octets up to that length. For 0, `key` holds the search's `p` once it is found, which the next call reads: pass it back unchanged. `key` is otherwise zeros but for 1. /// /// Contract: `VG.Spec.RsaKeyGen.keySearchContract`. Timing may depend on the pointers, the lengths, `state`, the status and consumption it returns, and, for each prime's search, its rejected candidates and the octets its accepted candidate's tests read, and each key's status. Not on the primes or the key. @@ -30,22 +49,3 @@ pub unsafe fn vg_rsa_generate_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *mu // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::rsa_keygen::vg_rsa_generate(key, key_len, used, rand, rand_len, scratch, scratch_len, state) } } - -/// Start generating an RSA key, as BoringSSL's `RSA_generate_key_ex` (FIPS 186-5 Appendix A.1.3), with a modulus of `bits` bits rounded down to a multiple of 128, and the public exponent `e` (`e_len` octets, most significant first, with any number of leading zeros). Returns 0 if `e` is not odd from 3 to `2^32 - 1`, or 2 if `bits` is above 8192 or, rounded down, below 512, and writes zeros to `state`. Otherwise returns 1 and writes the search's seven public words to `state`: the rejected candidates the first prime's search allows, the primes' length in octets (`nlen / 16`), the entropy budget, the length of the first random tape to give `vg_rsa_generate`, the phase (0), the generations allowed (4), and the exponent's eight octets, most significant first. -/// -/// Contract: `VG.Spec.RsaKeyGen.keyInitContract`. Timing may depend on `bits` and `e`. -/// -/// It runs `vg_rsa_generate_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 56 bytes. -/// * `e` must be valid for reads of `e_len` bytes. -/// * `state` must not overlap `e` or the arguments on the stack (distinct Rust objects never do). -/// * Neither `state` nor `e` may overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_rsa_generate_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 7], bits: usize, e: *const u8, e_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa_keygen::vg_rsa_generate_init(state, bits, e, e_len) } -} diff --git a/crates/verified-garbage-asm-x86/src/dispatch/rsa_oaep.rs b/crates/verified-garbage-asm-x86/src/dispatch/rsa_oaep.rs index 8c440447d06..a977066f5c9 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/rsa_oaep.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/rsa_oaep.rs @@ -2,87 +2,87 @@ //! The dispatchers of the verified `rsa_oaep` functions for `x86` //! (`crate::rsa_oaep`, `VG.Rust.dispatcher`). -/// RSAES-OAEP decryption (RFC 8017 §7.1.2) with the hash function of the table `hash` and MGF1 with that of the table `mgf1`, with the private key `(p, q, dP, dQ, qInv)` checked against the public exponent as `vg_rsa_private_checked` checks it. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero) and the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits), decrypts the ciphertext `ct` (`n_len` bytes, most significant first) with the label `label`: if RSADP's result passes the check against `e` and is a valid EME-OAEP encoding, writes the message to the start of `out`, zeros after it, and its length to `*msg_len`, and returns 1. Otherwise writes zeros to `out` and `*msg_len` and returns 0 for every failure of the key, the ciphertext (not below `n`) or its decoding, which are not told apart; or 2 (an internal error) if RSADP's result fails the check against `e`, which it does for no ciphertext if `vg_rsa_check_key` accepts the key and `p` and `q` are prime. +/// RSAES-OAEP encryption (RFC 8017 §7.1.1) with the hash function of the table `hash` and MGF1 with that of the table `mgf1`. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes, most significant first) and `pre`, the `pre_len` words written by `vg_rsa_public_precompute` for `n` (returning 1), writes the encryption of the message `msg` with the label `label` and the `seed_len`-byte seed `seed` (`n_len` bytes, most significant first) to `out` and returns 1; or writes zeros and returns 0 if `n` is not such a modulus or the message is longer than `n_len - 2 seed_len - 2` bytes. The seed must be fresh random bytes for each encryption. If `pre` does not hold those values, what it writes and returns is unspecified; memory safety and the timing guarantee still apply. /// -/// Contract: `VG.Spec.RsaOaep.decryptTablesContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, the contents of `n` and `e`, and which tables `hash` and `mgf1` are, not on the private key, the label, the ciphertext, whether its decoding is valid or the message's length. +/// Contract: `VG.Spec.RsaOaep.encryptTablesContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, the contents of `n`, `e` and `pre`, and which tables `hash` and `mgf1` are, not on the message, the label or the seed. /// -/// It runs `vg_rsa_oaep_decrypt`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_rsa_oaep_encrypt_precomputed`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `out` must be valid for reads and writes of `out_len` bytes. -/// * `msg_len` must be valid for reads and writes of 8 bytes. /// * `n` must be valid for reads of `n_len` bytes. /// * `e` must be valid for reads of `e_len` bytes. -/// * `p` must be valid for reads of `p_len` bytes. -/// * `q` must be valid for reads of `q_len` bytes. -/// * `dp` must be valid for reads of `dp_len` bytes. -/// * `dq` must be valid for reads of `dq_len` bytes. -/// * `qinv` must be valid for reads of `qinv_len` bytes. /// * `label` must be valid for reads of `label_len` bytes. -/// * `ct` must be valid for reads of `ct_len` bytes. +/// * `msg` must be valid for reads of `msg_len` bytes. +/// * `seed` must be valid for reads of `seed_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `pre` must be valid for reads of `8 * pre_len` bytes. /// * `hash` must be valid for reads of `5 * size_of::()` bytes. /// * `mgf1` must be valid for reads of `5 * size_of::()` bytes. /// * `n_len` must be in 64..=1024. -/// * `out_len` and `ct_len` must be `n_len`. +/// * `out_len` must be `n_len`. /// * `e_len` must be in 1..=`n_len`. -/// * `p_len` and `q_len` must be in 1..`n_len`. -/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. -/// * `scratch_len` must be at least `16 * n_len + 1056`. +/// * `scratch_len` must be at least `8 * ⌈n_len / 8⌉ + 1072`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `seed_len` must be the digest length of the hash function `hash` names. +/// * `pre_len` must be `2 * ⌈n_len / 8⌉`. +/// * For the result to be encryption's, `pre` must hold what `vg_rsa_public_precompute` wrote for `n` (returning 1). /// * `hash` names the hash function of the label and of the encoding: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the hash functions' tables of this crate, `fn_tables`, say which they name). /// * `mgf1` names MGF1's hash function: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the hash functions' tables of this crate, `fn_tables`, say which they name). -/// * `out`, `msg_len` and `scratch` must not overlap each other, `n`, `e`, `p`, `q`, `dp`, `dq`, `qinv`, `label`, `ct`, `hash` or `mgf1` (distinct Rust objects never do). -/// * None of `out`, `msg_len`, `n`, `e`, `p`, `q`, `dp`, `dq`, `qinv`, `label`, `ct`, `scratch`, `hash` and `mgf1` may overlap the arguments on the stack, overlap the return address on the stack or the 3608 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `n`, `e`, `label`, `msg`, `seed`, `pre`, `hash` or `mgf1` (distinct Rust objects never do). +/// * None of `out`, `n`, `e`, `label`, `msg`, `seed`, `scratch`, `pre`, `hash` and `mgf1` may overlap the arguments on the stack, overlap the return address on the stack or the 260 bytes of stack below it, or wrap around the end of the address space (no Rust object does). /// * `hash` must be the `as_ptr()` of one of the `Hash` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. /// * `mgf1` must be the `as_ptr()` of one of the `Hash` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_rsa_oaep_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, msg_len: *mut [u64; 1], n: *const u8, n_len: usize, e: *const u8, e_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, label: *const u8, label_len: usize, ct: *const u8, ct_len: usize, scratch: *mut u64, scratch_len: usize, hash: *const [usize; 5], mgf1: *const [usize; 5]) -> u32 { +pub unsafe fn vg_rsa_oaep_encrypt_precomputed_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, n: *const u8, n_len: usize, e: *const u8, e_len: usize, label: *const u8, label_len: usize, msg: *const u8, msg_len: usize, seed: *const u8, seed_len: usize, scratch: *mut u64, scratch_len: usize, pre: *const u64, pre_len: usize, hash: *const [usize; 5], mgf1: *const [usize; 5]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa_oaep::vg_rsa_oaep_decrypt(out, out_len, msg_len, n, n_len, e, e_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, label, label_len, ct, ct_len, scratch, scratch_len, hash, mgf1) } + unsafe { crate::rsa_oaep::vg_rsa_oaep_encrypt_precomputed(out, out_len, n, n_len, e, e_len, label, label_len, msg, msg_len, seed, seed_len, scratch, scratch_len, pre, pre_len, hash, mgf1) } } -/// RSAES-OAEP encryption (RFC 8017 §7.1.1) with the hash function of the table `hash` and MGF1 with that of the table `mgf1`. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes, most significant first) and `pre`, the `pre_len` words written by `vg_rsa_public_precompute` for `n` (returning 1), writes the encryption of the message `msg` with the label `label` and the `seed_len`-byte seed `seed` (`n_len` bytes, most significant first) to `out` and returns 1; or writes zeros and returns 0 if `n` is not such a modulus or the message is longer than `n_len - 2 seed_len - 2` bytes. The seed must be fresh random bytes for each encryption. If `pre` does not hold those values, what it writes and returns is unspecified; memory safety and the timing guarantee still apply. +/// RSAES-OAEP decryption (RFC 8017 §7.1.2) with the hash function of the table `hash` and MGF1 with that of the table `mgf1`, with the private key `(p, q, dP, dQ, qInv)` checked against the public exponent as `vg_rsa_private_checked` checks it. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero) and the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits), decrypts the ciphertext `ct` (`n_len` bytes, most significant first) with the label `label`: if RSADP's result passes the check against `e` and is a valid EME-OAEP encoding, writes the message to the start of `out`, zeros after it, and its length to `*msg_len`, and returns 1. Otherwise writes zeros to `out` and `*msg_len` and returns 0 for every failure of the key, the ciphertext (not below `n`) or its decoding, which are not told apart; or 2 (an internal error) if RSADP's result fails the check against `e`, which it does for no ciphertext if `vg_rsa_check_key` accepts the key and `p` and `q` are prime. /// -/// Contract: `VG.Spec.RsaOaep.encryptTablesContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, the contents of `n`, `e` and `pre`, and which tables `hash` and `mgf1` are, not on the message, the label or the seed. +/// Contract: `VG.Spec.RsaOaep.decryptTablesContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, the contents of `n` and `e`, and which tables `hash` and `mgf1` are, not on the private key, the label, the ciphertext, whether its decoding is valid or the message's length. /// -/// It runs `vg_rsa_oaep_encrypt_precomputed`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_rsa_oaep_decrypt`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `out` must be valid for reads and writes of `out_len` bytes. +/// * `msg_len` must be valid for reads and writes of 8 bytes. /// * `n` must be valid for reads of `n_len` bytes. /// * `e` must be valid for reads of `e_len` bytes. +/// * `p` must be valid for reads of `p_len` bytes. +/// * `q` must be valid for reads of `q_len` bytes. +/// * `dp` must be valid for reads of `dp_len` bytes. +/// * `dq` must be valid for reads of `dq_len` bytes. +/// * `qinv` must be valid for reads of `qinv_len` bytes. /// * `label` must be valid for reads of `label_len` bytes. -/// * `msg` must be valid for reads of `msg_len` bytes. -/// * `seed` must be valid for reads of `seed_len` bytes. +/// * `ct` must be valid for reads of `ct_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `pre` must be valid for reads of `8 * pre_len` bytes. /// * `hash` must be valid for reads of `5 * size_of::()` bytes. /// * `mgf1` must be valid for reads of `5 * size_of::()` bytes. /// * `n_len` must be in 64..=1024. -/// * `out_len` must be `n_len`. +/// * `out_len` and `ct_len` must be `n_len`. /// * `e_len` must be in 1..=`n_len`. -/// * `scratch_len` must be at least `8 * ⌈n_len / 8⌉ + 1072`. +/// * `p_len` and `q_len` must be in 1..`n_len`. +/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. +/// * `scratch_len` must be at least `16 * n_len + 1056`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `seed_len` must be the digest length of the hash function `hash` names. -/// * `pre_len` must be `2 * ⌈n_len / 8⌉`. -/// * For the result to be encryption's, `pre` must hold what `vg_rsa_public_precompute` wrote for `n` (returning 1). /// * `hash` names the hash function of the label and of the encoding: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the hash functions' tables of this crate, `fn_tables`, say which they name). /// * `mgf1` names MGF1's hash function: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the hash functions' tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `n`, `e`, `label`, `msg`, `seed`, `pre`, `hash` or `mgf1` (distinct Rust objects never do). -/// * None of `out`, `n`, `e`, `label`, `msg`, `seed`, `scratch`, `pre`, `hash` and `mgf1` may overlap the arguments on the stack, overlap the return address on the stack or the 260 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out`, `msg_len` and `scratch` must not overlap each other, `n`, `e`, `p`, `q`, `dp`, `dq`, `qinv`, `label`, `ct`, `hash` or `mgf1` (distinct Rust objects never do). +/// * None of `out`, `msg_len`, `n`, `e`, `p`, `q`, `dp`, `dq`, `qinv`, `label`, `ct`, `scratch`, `hash` and `mgf1` may overlap the arguments on the stack, overlap the return address on the stack or the 3608 bytes of stack below it, or wrap around the end of the address space (no Rust object does). /// * `hash` must be the `as_ptr()` of one of the `Hash` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. /// * `mgf1` must be the `as_ptr()` of one of the `Hash` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_rsa_oaep_encrypt_precomputed_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, n: *const u8, n_len: usize, e: *const u8, e_len: usize, label: *const u8, label_len: usize, msg: *const u8, msg_len: usize, seed: *const u8, seed_len: usize, scratch: *mut u64, scratch_len: usize, pre: *const u64, pre_len: usize, hash: *const [usize; 5], mgf1: *const [usize; 5]) -> u32 { +pub unsafe fn vg_rsa_oaep_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, msg_len: *mut [u64; 1], n: *const u8, n_len: usize, e: *const u8, e_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, label: *const u8, label_len: usize, ct: *const u8, ct_len: usize, scratch: *mut u64, scratch_len: usize, hash: *const [usize; 5], mgf1: *const [usize; 5]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa_oaep::vg_rsa_oaep_encrypt_precomputed(out, out_len, n, n_len, e, e_len, label, label_len, msg, msg_len, seed, seed_len, scratch, scratch_len, pre, pre_len, hash, mgf1) } + unsafe { crate::rsa_oaep::vg_rsa_oaep_decrypt(out, out_len, msg_len, n, n_len, e, e_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, label, label_len, ct, ct_len, scratch, scratch_len, hash, mgf1) } } diff --git a/crates/verified-garbage-asm-x86/src/dispatch/rsa_pkcs1_enc.rs b/crates/verified-garbage-asm-x86/src/dispatch/rsa_pkcs1_enc.rs index e9041bb7620..de075ae1bf7 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/rsa_pkcs1_enc.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/rsa_pkcs1_enc.rs @@ -2,11 +2,48 @@ //! The dispatchers of the verified `rsa_pkcs1_enc` functions for `x86` //! (`crate::rsa_pkcs1_enc`, `VG.Rust.dispatcher`). +/// RSAES-PKCS1-v1_5 encryption: RSAES-PKCS1-V1_5-ENCRYPT (RFC 8017 §7.2.1), with the padding string drawn from the random tape `rand` (`rand_len` bytes). With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits), and `pre`, the `pre_len` words `vg_rsa_public_precompute` wrote for `n`, takes as the padding string `PS` the first `n_len - msg_len - 3` nonzero bytes of `rand`, encrypts the encoded message `0x00 ‖ 0x02 ‖ PS ‖ 0x00 ‖ M`, for the message `M` (`msg_len` bytes), with RSAEP, writes the ciphertext (`n_len` bytes, most significant first) to `out` and returns 1; or writes zeros and returns 2 if `n` or `e` is not such a number. Writes to `tape[0]` the bytes of `rand` it consumed, up to the last byte of `PS`. If `rand` has fewer nonzero bytes, writes zeros to `out` and: if `rand_len` is the entropy budget, `n_len - msg_len + 125` bytes, returns 3 (which only a broken random number generator makes likely); otherwise consumes none of `rand`, writes to `tape[1]` the length of the next tape, more than `rand_len` and at most the budget, and returns 0: keep `rand`, append fresh random bytes up to that length, and call again. `tape[1]` is 0 but for 0. If `pre` does not hold the values for `n`, what it writes to `out` and returns once it has drawn `PS` is unspecified; memory safety, `tape` and the timing guarantee still apply. +/// +/// Contract: `VG.Spec.RsaPkcs1Enc.encryptRandomContract`. Constant time but for the public key and the tape's zero bytes: timing may depend on the pointers, the lengths, the contents of `n`, `e` and `pre`, and which bytes of `rand` are zero, not on the message or the other bytes of `rand`. +/// +/// It runs `vg_rsa_pkcs1_encrypt_random`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// The function may overwrite the arguments on the stack, as the calling convention lets it. +/// +/// # Safety +/// +/// * `out` must be valid for reads and writes of `out_len` bytes. +/// * `tape` must be valid for reads and writes of 16 bytes. +/// * `n` must be valid for reads of `n_len` bytes. +/// * `e` must be valid for reads of `e_len` bytes. +/// * `msg` must be valid for reads of `msg_len` bytes. +/// * `rand` must be valid for reads of `rand_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `pre` must be valid for reads of `8 * pre_len` bytes. +/// * `n_len` must be in 64..=1024. +/// * `out_len` must be `n_len`. +/// * `e_len` must be in 1..=`n_len`. +/// * `msg_len + 11` must be at most `n_len`. +/// * `rand_len` must be at most `n_len - msg_len + 125`. +/// * `pre_len` must be `2 * ⌈n_len / 8⌉`. +/// * For the result to be encryption's, `pre` must hold what `vg_rsa_public_precompute` wrote for `n` (returning 1). +/// * `rand` must hold independent uniform random bytes, fresh for this encryption, extended only as `tape[1]` asks; never reuse them for another encryption. +/// * `scratch_len` must be at least `8 * ⌈n_len / 8⌉ + 48`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `out`, `tape` and `scratch` must not overlap each other, `n`, `e`, `msg`, `rand` or `pre` (distinct Rust objects never do). +/// * None of `out`, `tape`, `n`, `e`, `msg`, `rand`, `scratch` and `pre` may overlap the arguments on the stack, overlap the return address on the stack or the 2296 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_rsa_pkcs1_encrypt_random_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, tape: *mut [u64; 2], n: *const u8, n_len: usize, e: *const u8, e_len: usize, msg: *const u8, msg_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, pre: *const u64, pre_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::rsa_pkcs1_enc::vg_rsa_pkcs1_encrypt_random(out, out_len, tape, n, n_len, e, e_len, msg, msg_len, rand, rand_len, scratch, scratch_len, pre, pre_len) } +} + /// RSAES-PKCS1-v1_5 decryption with implicit rejection: RSAES-PKCS1-V1_5-DECRYPT (RFC 8017 §7.2.2) as draft-irtf-cfrg-rsa-guidance-10 §7.2 implements it, with the private key `(n, e, d, p, q, dP, dQ, qInv)`. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero) and the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits), computes the encoded message `EM` of the ciphertext `input` (`n_len` bytes, most significant first) with RSADP as `vg_rsa_private_checked` does. If its padding is valid (`0x00 ‖ 0x02 ‖ PS ‖ 0x00 ‖ M`, `PS` at least 8 nonzero bytes), the message is `M`; otherwise it is a message derived from `d` and the ciphertext (the draft's §7.1 PRF keyed with HMAC-SHA-256 of the ciphertext under the SHA-256 digest of `d`), of at most `n_len - 11` bytes, which is the same every time the same key decrypts the same ciphertext. Writes the message at the end of `out` (`n_len` bytes), after zeros, and its length to `*msg_len`, and returns 1. Writes zeros to both and returns 0 if `n` or `e` is not such a number, the ciphertext is not below `n`, `p q ≠ n`, or `qInv ≥ p`; and returns 2 (an internal error) if RSADP's result fails its check against `e`, which is the case for no ciphertext if `vg_rsa_check_key` accepts the key and `p` and `q` are prime. An invalid padding is never an error. `d` is used as given, never recomputed. `p`, `dp` and `qinv` are `p_len` bytes, `q` and `dq` are `q_len` bytes, and `d` is `d_len` bytes, all most significant first. /// /// Contract: `VG.Spec.RsaPkcs1Enc.decryptContract`. Constant time but for the public key: timing may depend on the pointers, the lengths and the contents of `n` and `e`, not on the ciphertext, the private key, the validity of the padding, the length of the message or which message is returned. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_pkcs1_decrypt_sha256_shani` if the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_SHA256_SHANI_FEATURES`); or else `vg_rsa_pkcs1_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_pkcs1_decrypt_sha256_shani` if the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_SHA256_SHANI_FEATURES`); or else `vg_rsa_pkcs1_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -68,40 +105,3 @@ pub unsafe fn vg_rsa_pkcs1_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, out // either carries the caller's obligations, which are those of every implementation. unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, msg_len, n, n_len, e, e_len, d, d_len, input, input_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, scratch, scratch_len) } } - -/// RSAES-PKCS1-v1_5 encryption: RSAES-PKCS1-V1_5-ENCRYPT (RFC 8017 §7.2.1), with the padding string drawn from the random tape `rand` (`rand_len` bytes). With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits), and `pre`, the `pre_len` words `vg_rsa_public_precompute` wrote for `n`, takes as the padding string `PS` the first `n_len - msg_len - 3` nonzero bytes of `rand`, encrypts the encoded message `0x00 ‖ 0x02 ‖ PS ‖ 0x00 ‖ M`, for the message `M` (`msg_len` bytes), with RSAEP, writes the ciphertext (`n_len` bytes, most significant first) to `out` and returns 1; or writes zeros and returns 2 if `n` or `e` is not such a number. Writes to `tape[0]` the bytes of `rand` it consumed, up to the last byte of `PS`. If `rand` has fewer nonzero bytes, writes zeros to `out` and: if `rand_len` is the entropy budget, `n_len - msg_len + 125` bytes, returns 3 (which only a broken random number generator makes likely); otherwise consumes none of `rand`, writes to `tape[1]` the length of the next tape, more than `rand_len` and at most the budget, and returns 0: keep `rand`, append fresh random bytes up to that length, and call again. `tape[1]` is 0 but for 0. If `pre` does not hold the values for `n`, what it writes to `out` and returns once it has drawn `PS` is unspecified; memory safety, `tape` and the timing guarantee still apply. -/// -/// Contract: `VG.Spec.RsaPkcs1Enc.encryptRandomContract`. Constant time but for the public key and the tape's zero bytes: timing may depend on the pointers, the lengths, the contents of `n`, `e` and `pre`, and which bytes of `rand` are zero, not on the message or the other bytes of `rand`. -/// -/// It runs `vg_rsa_pkcs1_encrypt_random`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// The function may overwrite the arguments on the stack, as the calling convention lets it. -/// -/// # Safety -/// -/// * `out` must be valid for reads and writes of `out_len` bytes. -/// * `tape` must be valid for reads and writes of 16 bytes. -/// * `n` must be valid for reads of `n_len` bytes. -/// * `e` must be valid for reads of `e_len` bytes. -/// * `msg` must be valid for reads of `msg_len` bytes. -/// * `rand` must be valid for reads of `rand_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `pre` must be valid for reads of `8 * pre_len` bytes. -/// * `n_len` must be in 64..=1024. -/// * `out_len` must be `n_len`. -/// * `e_len` must be in 1..=`n_len`. -/// * `msg_len + 11` must be at most `n_len`. -/// * `rand_len` must be at most `n_len - msg_len + 125`. -/// * `pre_len` must be `2 * ⌈n_len / 8⌉`. -/// * For the result to be encryption's, `pre` must hold what `vg_rsa_public_precompute` wrote for `n` (returning 1). -/// * `rand` must hold independent uniform random bytes, fresh for this encryption, extended only as `tape[1]` asks; never reuse them for another encryption. -/// * `scratch_len` must be at least `8 * ⌈n_len / 8⌉ + 48`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out`, `tape` and `scratch` must not overlap each other, `n`, `e`, `msg`, `rand` or `pre` (distinct Rust objects never do). -/// * None of `out`, `tape`, `n`, `e`, `msg`, `rand`, `scratch` and `pre` may overlap the arguments on the stack, overlap the return address on the stack or the 2296 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_rsa_pkcs1_encrypt_random_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, tape: *mut [u64; 2], n: *const u8, n_len: usize, e: *const u8, e_len: usize, msg: *const u8, msg_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, pre: *const u64, pre_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa_pkcs1_enc::vg_rsa_pkcs1_encrypt_random(out, out_len, tape, n, n_len, e, e_len, msg, msg_len, rand, rand_len, scratch, scratch_len, pre, pre_len) } -} diff --git a/crates/verified-garbage-asm-x86/src/dispatch/rsa_pkcs1_sig.rs b/crates/verified-garbage-asm-x86/src/dispatch/rsa_pkcs1_sig.rs index 92287d74034..5e28bb21612 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/rsa_pkcs1_sig.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/rsa_pkcs1_sig.rs @@ -2,42 +2,6 @@ //! The dispatchers of the verified `rsa_pkcs1_sig` functions for `x86` //! (`crate::rsa_pkcs1_sig`, `VG.Rust.dispatcher`). -/// RSASSA-PKCS1-v1_5 signature generation (RFC 8017 §8.2.1) of a hash value, as BoringSSL's `RSA_sign` does it. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits) and the private key `(p, q, dP, dQ, qInv)` (as `vg_rsa_private_checked` takes it), encodes `digest` with EMSA-PKCS1-v1_5 (RFC 8017 §9.2: `0x00 0x01`, bytes `0xff`, `0x00` and the DER encoding of its `DigestInfo`) to `n_len` bytes and computes the signature from it as `vg_rsa_private_checked` does: writes it to `out` (`n_len` bytes, most significant first) and returns 1; writes zeros and returns 0 if `hash` names no hash function, `digest` is not as long as its values, `n_len` is less than the encoding's 11 bytes more than the `DigestInfo`, or `vg_rsa_private_checked` refuses the key; and writes zeros and returns 2 (an internal error) if the signature fails the check against `e`, which is the case for no hash value if `vg_rsa_check_key` accepts the key and `p` and `q` are prime. A signature it writes is one `vg_rsa_pkcs1_verify` accepts. `hash` names the hash function: 0 MD5, 1 SHA-1, 2 SHA-224, 3 SHA-256, 4 SHA-384, 5 SHA-512, 6 SHA-512/224, 7 SHA-512/256, 8 SHA3-224, 9 SHA3-256, 10 SHA3-384, 11 SHA3-512 (`VG.Spec.RsaPkcs1Sig.Hash.ofId`). -/// -/// Contract: `VG.Spec.RsaPkcs1Sig.signContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, `hash` and the contents of `n` and `e`, not on `digest` or the private key. -/// -/// It runs `vg_rsa_pkcs1_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// The function may overwrite the arguments on the stack, as the calling convention lets it. -/// -/// # Safety -/// -/// * `out` must be valid for reads and writes of `out_len` bytes. -/// * `n` must be valid for reads of `n_len` bytes. -/// * `e` must be valid for reads of `e_len` bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `p` must be valid for reads of `p_len` bytes. -/// * `q` must be valid for reads of `q_len` bytes. -/// * `dp` must be valid for reads of `dp_len` bytes. -/// * `dq` must be valid for reads of `dq_len` bytes. -/// * `qinv` must be valid for reads of `qinv_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `n_len` must be in 64..=1024. -/// * `out_len` must be `n_len`. -/// * `e_len` must be in 1..=`n_len`. -/// * `p_len` and `q_len` must be in 1..`n_len`. -/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. -/// * `scratch_len` must be at least `16 * n_len + 32`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `n`, `e`, `digest`, `p`, `q`, `dp`, `dq` or `qinv` (distinct Rust objects never do). -/// * None of `out`, `n`, `e`, `digest`, `p`, `q`, `dp`, `dq`, `qinv` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 4584 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_rsa_pkcs1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, n: *const u8, n_len: usize, e: *const u8, e_len: usize, hash: u32, digest: *const u8, digest_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa_pkcs1_sig::vg_rsa_pkcs1_sign(out, out_len, n, n_len, e, e_len, hash, digest, digest_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, scratch, scratch_len) } -} - /// RSASSA-PKCS1-v1_5 signature verification (RFC 8017 §8.2.2) of a hash value, as BoringSSL's `RSA_verify` does it. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero) and the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits), returns 1 if `sig` is `n_len` bytes, the integer `s` of `sig` (most significant byte first) is below `n`, and `s^e mod n` (as `n_len` bytes) is `0x00 0x01`, at least 8 bytes `0xff`, `0x00` and then exactly the DER encoding of the `DigestInfo` of `digest` (RFC 8017 §9.2 note 1), which must be as long as the hash function's values; and 0 otherwise, or if `n` or `e` is not such a number or `hash` names no hash function. That is, exactly when `s^e mod n` is EMSA-PKCS1-v1_5's encoding of `digest`: the signed `DigestInfo` is compared, never parsed. `hash` names the hash function: 0 MD5, 1 SHA-1, 2 SHA-224, 3 SHA-256, 4 SHA-384, 5 SHA-512, 6 SHA-512/224, 7 SHA-512/256, 8 SHA3-224, 9 SHA3-256, 10 SHA3-384, 11 SHA3-512 (`VG.Spec.RsaPkcs1Sig.Hash.ofId`). /// /// Contract: `VG.Spec.RsaPkcs1Sig.verifyContract`. Not constant time: timing may depend on the pointers, the lengths, `hash` and the contents of `n`, `e`, `digest` and `sig`. @@ -108,3 +72,39 @@ pub unsafe fn vg_rsa_pkcs1_recover_precomputed_auto(_dit: verified_garbage_cpu:: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::rsa_pkcs1_sig::vg_rsa_pkcs1_recover_precomputed(out, out_len, n, n_len, e, e_len, hash, sig, sig_len, scratch, scratch_len, pre, pre_len) } } + +/// RSASSA-PKCS1-v1_5 signature generation (RFC 8017 §8.2.1) of a hash value, as BoringSSL's `RSA_sign` does it. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits) and the private key `(p, q, dP, dQ, qInv)` (as `vg_rsa_private_checked` takes it), encodes `digest` with EMSA-PKCS1-v1_5 (RFC 8017 §9.2: `0x00 0x01`, bytes `0xff`, `0x00` and the DER encoding of its `DigestInfo`) to `n_len` bytes and computes the signature from it as `vg_rsa_private_checked` does: writes it to `out` (`n_len` bytes, most significant first) and returns 1; writes zeros and returns 0 if `hash` names no hash function, `digest` is not as long as its values, `n_len` is less than the encoding's 11 bytes more than the `DigestInfo`, or `vg_rsa_private_checked` refuses the key; and writes zeros and returns 2 (an internal error) if the signature fails the check against `e`, which is the case for no hash value if `vg_rsa_check_key` accepts the key and `p` and `q` are prime. A signature it writes is one `vg_rsa_pkcs1_verify` accepts. `hash` names the hash function: 0 MD5, 1 SHA-1, 2 SHA-224, 3 SHA-256, 4 SHA-384, 5 SHA-512, 6 SHA-512/224, 7 SHA-512/256, 8 SHA3-224, 9 SHA3-256, 10 SHA3-384, 11 SHA3-512 (`VG.Spec.RsaPkcs1Sig.Hash.ofId`). +/// +/// Contract: `VG.Spec.RsaPkcs1Sig.signContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, `hash` and the contents of `n` and `e`, not on `digest` or the private key. +/// +/// It runs `vg_rsa_pkcs1_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// The function may overwrite the arguments on the stack, as the calling convention lets it. +/// +/// # Safety +/// +/// * `out` must be valid for reads and writes of `out_len` bytes. +/// * `n` must be valid for reads of `n_len` bytes. +/// * `e` must be valid for reads of `e_len` bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `p` must be valid for reads of `p_len` bytes. +/// * `q` must be valid for reads of `q_len` bytes. +/// * `dp` must be valid for reads of `dp_len` bytes. +/// * `dq` must be valid for reads of `dq_len` bytes. +/// * `qinv` must be valid for reads of `qinv_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `n_len` must be in 64..=1024. +/// * `out_len` must be `n_len`. +/// * `e_len` must be in 1..=`n_len`. +/// * `p_len` and `q_len` must be in 1..`n_len`. +/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. +/// * `scratch_len` must be at least `16 * n_len + 32`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `out` and `scratch` must not overlap each other, `n`, `e`, `digest`, `p`, `q`, `dp`, `dq` or `qinv` (distinct Rust objects never do). +/// * None of `out`, `n`, `e`, `digest`, `p`, `q`, `dp`, `dq`, `qinv` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 4584 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_rsa_pkcs1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, n: *const u8, n_len: usize, e: *const u8, e_len: usize, hash: u32, digest: *const u8, digest_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::rsa_pkcs1_sig::vg_rsa_pkcs1_sign(out, out_len, n, n_len, e, e_len, hash, digest, digest_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, scratch, scratch_len) } +} diff --git a/crates/verified-garbage-asm-x86/src/dispatch/rsa_pss.rs b/crates/verified-garbage-asm-x86/src/dispatch/rsa_pss.rs index c567965c5ab..acffa37709b 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/rsa_pss.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/rsa_pss.rs @@ -2,85 +2,85 @@ //! The dispatchers of the verified `rsa_pss` functions for `x86` //! (`crate::rsa_pss`, `VG.Rust.dispatcher`). -/// RSASSA-PSS signature generation (RFC 8017 §8.1.1) with the hash function of the table `hash` and MGF1 with that of the table `mgf1`, of a digest of the former, with the private key `(p, q, dP, dQ, qInv)` checked against the public exponent as `vg_rsa_private_checked` checks it. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero) and the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits), encodes the `digest_len`-byte digest `digest` with the salt `salt` (EMSA-PSS, RFC 8017 §9.1.1) and computes RSASP1 of the encoding: if it passes the check against `e`, writes the signature (`n_len` bytes, most significant first) to `out` and returns 1. Writes zeros and returns 0 if the salt does not fit (`salt_len` more than `⌈(bits of n - 1) / 8⌉ - digest_len - 2`), or the key is refused as `vg_rsa_private_checked` refuses it; writes zeros and returns 2 (an internal error) if the result fails the check against `e`, which it does for no digest if `vg_rsa_check_key` accepts the key and `p` and `q` are prime. The salt must be fresh random bytes for each signature (or empty). +/// RSASSA-PSS signature verification (RFC 8017 §8.1.2) with the hash function of the table `hash` and MGF1 with that of the table `mgf1`, of a signature of a digest of the former. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits) and `pre`, the `pre_len` words written by `vg_rsa_public_precompute` for `n` (returning 1), returns 1 if `sig` (`n_len` bytes, most significant first) is an RSASSA-PSS signature of the `digest_len`-byte digest `digest` with a salt of `salt_len` bytes, or of any length if `any_salt_len` is not zero (BoringSSL's `RSA_PSS_SALTLEN_AUTO`); and 0 if it is not, or if `n` is not such a modulus, `e` is not such a number, or `sig` is not below `n`. If `pre` does not hold those values, the result is unspecified; memory safety and the timing guarantee still apply. /// -/// Contract: `VG.Spec.RsaPss.signTableContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, the contents of `n` and `e`, and which tables `hash` and `mgf1` are, not on the private key, the digest or the salt. +/// Contract: `VG.Spec.RsaPss.verifyPrecomputedTableContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, `salt_len`, `any_salt_len`, the contents of `n`, `e` and `pre`, and which tables `hash` and `mgf1` are, not on the digest, the signature or anything computed from them but the result. /// -/// It runs `vg_rsa_pss_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_rsa_pss_verify_precomputed`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// -/// * `out` must be valid for reads and writes of `out_len` bytes. /// * `n` must be valid for reads of `n_len` bytes. /// * `e` must be valid for reads of `e_len` bytes. -/// * `p` must be valid for reads of `p_len` bytes. -/// * `q` must be valid for reads of `q_len` bytes. -/// * `dp` must be valid for reads of `dp_len` bytes. -/// * `dq` must be valid for reads of `dq_len` bytes. -/// * `qinv` must be valid for reads of `qinv_len` bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `salt` must be valid for reads of `salt_len` bytes. +/// * `sig` must be valid for reads of `sig_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `pre` must be valid for reads of `8 * pre_len` bytes. /// * `hash` must be valid for reads of `11 * size_of::()` bytes. /// * `mgf1` must be valid for reads of `5 * size_of::()` bytes. /// * `n_len` must be in 64..=1024. -/// * `out_len` must be `n_len`. +/// * `sig_len` must be `n_len`. /// * `e_len` must be in 1..=`n_len`. -/// * `p_len` and `q_len` must be in 1..`n_len`. -/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. -/// * `digest_len` must be the digest length of the hash function `hash` names. -/// * `scratch_len` must be at least `16 * n_len + 1056`. +/// * `scratch_len` must be at least `8 * ⌈n_len / 8⌉ + 1072`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `digest_len` must be the digest length of the hash function `hash` names. +/// * `pre_len` must be `2 * ⌈n_len / 8⌉`. +/// * For the result to be signature verification's, `pre` must hold what `vg_rsa_public_precompute` wrote for `n` (returning 1). /// * `hash` names the hash function of the digest and of the encoding: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the hash functions' `MdHash` tables of this crate, `fn_tables`, say which they name). /// * `mgf1` names MGF1's hash function: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the hash functions' tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `n`, `e`, `p`, `q`, `dp`, `dq`, `qinv`, `digest`, `salt`, `hash` or `mgf1` (distinct Rust objects never do). -/// * None of `out`, `n`, `e`, `p`, `q`, `dp`, `dq`, `qinv`, `digest`, `salt`, `scratch`, `hash` and `mgf1` may overlap the arguments on the stack, overlap the return address on the stack or the 3608 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `scratch` must not overlap `n`, `e`, `digest`, `sig`, `pre`, `hash` or `mgf1` (distinct Rust objects never do). +/// * None of `n`, `e`, `digest`, `sig`, `scratch`, `pre`, `hash` and `mgf1` may overlap the arguments on the stack, overlap the return address on the stack or the 260 bytes of stack below it, or wrap around the end of the address space (no Rust object does). /// * `hash` must be the `as_ptr()` of one of the `MdHash` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. /// * `mgf1` must be the `as_ptr()` of one of the `Hash` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_rsa_pss_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, n: *const u8, n_len: usize, e: *const u8, e_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, digest: *const u8, digest_len: usize, salt: *const u8, salt_len: usize, scratch: *mut u64, scratch_len: usize, hash: *const [usize; 11], mgf1: *const [usize; 5]) -> u32 { +pub unsafe fn vg_rsa_pss_verify_precomputed_auto(_dit: verified_garbage_cpu::Dit<'_>, n: *const u8, n_len: usize, e: *const u8, e_len: usize, digest: *const u8, digest_len: usize, sig: *const u8, sig_len: usize, salt_len: usize, any_salt_len: u32, scratch: *mut u64, scratch_len: usize, pre: *const u64, pre_len: usize, hash: *const [usize; 11], mgf1: *const [usize; 5]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa_pss::vg_rsa_pss_sign(out, out_len, n, n_len, e, e_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, digest, digest_len, salt, salt_len, scratch, scratch_len, hash, mgf1) } + unsafe { crate::rsa_pss::vg_rsa_pss_verify_precomputed(n, n_len, e, e_len, digest, digest_len, sig, sig_len, salt_len, any_salt_len, scratch, scratch_len, pre, pre_len, hash, mgf1) } } -/// RSASSA-PSS signature verification (RFC 8017 §8.1.2) with the hash function of the table `hash` and MGF1 with that of the table `mgf1`, of a signature of a digest of the former. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits) and `pre`, the `pre_len` words written by `vg_rsa_public_precompute` for `n` (returning 1), returns 1 if `sig` (`n_len` bytes, most significant first) is an RSASSA-PSS signature of the `digest_len`-byte digest `digest` with a salt of `salt_len` bytes, or of any length if `any_salt_len` is not zero (BoringSSL's `RSA_PSS_SALTLEN_AUTO`); and 0 if it is not, or if `n` is not such a modulus, `e` is not such a number, or `sig` is not below `n`. If `pre` does not hold those values, the result is unspecified; memory safety and the timing guarantee still apply. +/// RSASSA-PSS signature generation (RFC 8017 §8.1.1) with the hash function of the table `hash` and MGF1 with that of the table `mgf1`, of a digest of the former, with the private key `(p, q, dP, dQ, qInv)` checked against the public exponent as `vg_rsa_private_checked` checks it. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero) and the public exponent `e` (`e_len` bytes, most significant first, odd, from 3 to `2^33 - 1`: BoringSSL's limits), encodes the `digest_len`-byte digest `digest` with the salt `salt` (EMSA-PSS, RFC 8017 §9.1.1) and computes RSASP1 of the encoding: if it passes the check against `e`, writes the signature (`n_len` bytes, most significant first) to `out` and returns 1. Writes zeros and returns 0 if the salt does not fit (`salt_len` more than `⌈(bits of n - 1) / 8⌉ - digest_len - 2`), or the key is refused as `vg_rsa_private_checked` refuses it; writes zeros and returns 2 (an internal error) if the result fails the check against `e`, which it does for no digest if `vg_rsa_check_key` accepts the key and `p` and `q` are prime. The salt must be fresh random bytes for each signature (or empty). /// -/// Contract: `VG.Spec.RsaPss.verifyPrecomputedTableContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, `salt_len`, `any_salt_len`, the contents of `n`, `e` and `pre`, and which tables `hash` and `mgf1` are, not on the digest, the signature or anything computed from them but the result. +/// Contract: `VG.Spec.RsaPss.signTableContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, the contents of `n` and `e`, and which tables `hash` and `mgf1` are, not on the private key, the digest or the salt. /// -/// It runs `vg_rsa_pss_verify_precomputed`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_rsa_pss_sign`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// +/// * `out` must be valid for reads and writes of `out_len` bytes. /// * `n` must be valid for reads of `n_len` bytes. /// * `e` must be valid for reads of `e_len` bytes. +/// * `p` must be valid for reads of `p_len` bytes. +/// * `q` must be valid for reads of `q_len` bytes. +/// * `dp` must be valid for reads of `dp_len` bytes. +/// * `dq` must be valid for reads of `dq_len` bytes. +/// * `qinv` must be valid for reads of `qinv_len` bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of `sig_len` bytes. +/// * `salt` must be valid for reads of `salt_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `pre` must be valid for reads of `8 * pre_len` bytes. /// * `hash` must be valid for reads of `11 * size_of::()` bytes. /// * `mgf1` must be valid for reads of `5 * size_of::()` bytes. /// * `n_len` must be in 64..=1024. -/// * `sig_len` must be `n_len`. +/// * `out_len` must be `n_len`. /// * `e_len` must be in 1..=`n_len`. -/// * `scratch_len` must be at least `8 * ⌈n_len / 8⌉ + 1072`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `p_len` and `q_len` must be in 1..`n_len`. +/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. /// * `digest_len` must be the digest length of the hash function `hash` names. -/// * `pre_len` must be `2 * ⌈n_len / 8⌉`. -/// * For the result to be signature verification's, `pre` must hold what `vg_rsa_public_precompute` wrote for `n` (returning 1). +/// * `scratch_len` must be at least `16 * n_len + 1056`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. /// * `hash` names the hash function of the digest and of the encoding: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the hash functions' `MdHash` tables of this crate, `fn_tables`, say which they name). /// * `mgf1` names MGF1's hash function: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the hash functions' tables of this crate, `fn_tables`, say which they name). -/// * `scratch` must not overlap `n`, `e`, `digest`, `sig`, `pre`, `hash` or `mgf1` (distinct Rust objects never do). -/// * None of `n`, `e`, `digest`, `sig`, `scratch`, `pre`, `hash` and `mgf1` may overlap the arguments on the stack, overlap the return address on the stack or the 260 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `n`, `e`, `p`, `q`, `dp`, `dq`, `qinv`, `digest`, `salt`, `hash` or `mgf1` (distinct Rust objects never do). +/// * None of `out`, `n`, `e`, `p`, `q`, `dp`, `dq`, `qinv`, `digest`, `salt`, `scratch`, `hash` and `mgf1` may overlap the arguments on the stack, overlap the return address on the stack or the 3608 bytes of stack below it, or wrap around the end of the address space (no Rust object does). /// * `hash` must be the `as_ptr()` of one of the `MdHash` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. /// * `mgf1` must be the `as_ptr()` of one of the `Hash` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_rsa_pss_verify_precomputed_auto(_dit: verified_garbage_cpu::Dit<'_>, n: *const u8, n_len: usize, e: *const u8, e_len: usize, digest: *const u8, digest_len: usize, sig: *const u8, sig_len: usize, salt_len: usize, any_salt_len: u32, scratch: *mut u64, scratch_len: usize, pre: *const u64, pre_len: usize, hash: *const [usize; 11], mgf1: *const [usize; 5]) -> u32 { +pub unsafe fn vg_rsa_pss_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, n: *const u8, n_len: usize, e: *const u8, e_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, digest: *const u8, digest_len: usize, salt: *const u8, salt_len: usize, scratch: *mut u64, scratch_len: usize, hash: *const [usize; 11], mgf1: *const [usize; 5]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa_pss::vg_rsa_pss_verify_precomputed(n, n_len, e, e_len, digest, digest_len, sig, sig_len, salt_len, any_salt_len, scratch, scratch_len, pre, pre_len, hash, mgf1) } + unsafe { crate::rsa_pss::vg_rsa_pss_sign(out, out_len, n, n_len, e, e_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, digest, digest_len, salt, salt_len, scratch, scratch_len, hash, mgf1) } } diff --git a/crates/verified-garbage-asm-x86/src/dispatch/scrypt.rs b/crates/verified-garbage-asm-x86/src/dispatch/scrypt.rs index e856fc6a132..ec0b6e3da9d 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/scrypt.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/scrypt.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Scrypt.scryptContract`. Not constant time in the indices: timing may depend on the pointers, the lengths, `r`, `N`, `p` and the indices `j` of step 3 of each scryptROMix, which are derived from the password and the salt and so leak information about them (as in every scrypt that indexes `V` directly), but on nothing else. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_scrypt_shani` if the CPU supports its features (`VG_SCRYPT_SHANI_FEATURES`); or else `vg_scrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_scrypt_shani` if the CPU supports its features (`VG_SCRYPT_SHANI_FEATURES`); or else `vg_scrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/sha1.rs b/crates/verified-garbage-asm-x86/src/dispatch/sha1.rs index a3ddaff61ff..aff361da003 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/sha1.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/sha1.rs @@ -2,29 +2,11 @@ //! The dispatchers of the verified `sha1` functions for `x86` //! (`crate::sha1`, `VG.Rust.dispatcher`). -/// Starts a SHA-1 computation: makes the streaming state `*state` represent the empty message. -/// -/// Contract: `VG.Spec.Sha1.initContract`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha1.Repr`). -/// -/// It runs `vg_sha1_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 84 bytes. -/// * `state` must not overlap the arguments on the stack (distinct Rust objects never do). -/// * `state` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_sha1_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 84]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha1::vg_sha1_init(state) } -} - /// Computes the SHA-1 digest of the `len` bytes at `data` (20 bytes, `VG.Spec.Sha1.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-1 computation with that message. /// /// Contract: `VG.Spec.Sha1.digestContract`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha1_digest_shani` if the CPU supports its features (`VG_SHA1_DIGEST_SHANI_FEATURES`); or else `vg_sha1_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha1_digest_shani` if the CPU supports its features (`VG_SHA1_DIGEST_SHANI_FEATURES`); or else `vg_sha1_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -68,11 +50,29 @@ pub unsafe fn vg_sha1_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *co unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } } +/// Starts a SHA-1 computation: makes the streaming state `*state` represent the empty message. +/// +/// Contract: `VG.Spec.Sha1.initContract`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha1.Repr`). +/// +/// It runs `vg_sha1_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 84 bytes. +/// * `state` must not overlap the arguments on the stack (distinct Rust objects never do). +/// * `state` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_sha1_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 84]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::sha1::vg_sha1_init(state) } +} + /// Absorbs data into a SHA-1 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), it then represents that message followed by the `len` bytes at `data`. /// /// Contract: `VG.Spec.Sha1.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha1_update_shani` if the CPU supports its features (`VG_SHA1_UPDATE_SHANI_FEATURES`); or else `vg_sha1_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha1_update_shani` if the CPU supports its features (`VG_SHA1_UPDATE_SHANI_FEATURES`); or else `vg_sha1_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -122,7 +122,7 @@ pub unsafe fn vg_sha1_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *m /// /// Contract: `VG.Spec.Sha1.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha1_finalize_shani` if the CPU supports its features (`VG_SHA1_FINALIZE_SHANI_FEATURES`); or else `vg_sha1_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha1_finalize_shani` if the CPU supports its features (`VG_SHA1_FINALIZE_SHANI_FEATURES`); or else `vg_sha1_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/sha256.rs b/crates/verified-garbage-asm-x86/src/dispatch/sha256.rs index 2e148f3826d..825130319eb 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/sha256.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/sha256.rs @@ -2,47 +2,11 @@ //! The dispatchers of the verified `sha256` functions for `x86` //! (`crate::sha256`, `VG.Rust.dispatcher`). -/// Starts a SHA-224 computation: makes the SHA-256 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-224 (`VG.Spec.Sha256.H0_224`). Continue with `vg_sha256_update` and `vg_sha256_finalize`, and take the first 28 bytes of the final hash value as the digest. -/// -/// Contract: `VG.Spec.Sha256.init224Contract`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha256.ReprFrom`). -/// -/// It runs `vg_sha224_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 96 bytes. -/// * `state` must not overlap the arguments on the stack (distinct Rust objects never do). -/// * `state` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_sha224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha256::vg_sha224_init(state) } -} - -/// Starts a SHA-256 computation: makes the streaming state `*state` represent the empty message. -/// -/// Contract: `VG.Spec.Sha256.initContract`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha256.Repr`). -/// -/// It runs `vg_sha256_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 96 bytes. -/// * `state` must not overlap the arguments on the stack (distinct Rust objects never do). -/// * `state` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_sha256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha256::vg_sha256_init(state) } -} - /// Computes the SHA-256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha256.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-256 computation with that message. /// /// Contract: `VG.Spec.Sha256.digestContract` for `VG.Spec.Sha256.hash`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha256_digest_shani` if the CPU supports its features (`VG_SHA256_DIGEST_SHANI_FEATURES`); or else `vg_sha256_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha256_digest_shani` if the CPU supports its features (`VG_SHA256_DIGEST_SHANI_FEATURES`); or else `vg_sha256_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -90,7 +54,7 @@ pub unsafe fn vg_sha256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: * /// /// Contract: `VG.Spec.Sha256.digestContract` for `VG.Spec.Sha256.sha224`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha224_digest_shani` if the CPU supports its features (`VG_SHA224_DIGEST_SHANI_FEATURES`); or else `vg_sha224_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha224_digest_shani` if the CPU supports its features (`VG_SHA224_DIGEST_SHANI_FEATURES`); or else `vg_sha224_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -134,11 +98,47 @@ pub unsafe fn vg_sha224_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: * unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } } +/// Starts a SHA-224 computation: makes the SHA-256 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-224 (`VG.Spec.Sha256.H0_224`). Continue with `vg_sha256_update` and `vg_sha256_finalize`, and take the first 28 bytes of the final hash value as the digest. +/// +/// Contract: `VG.Spec.Sha256.init224Contract`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha256.ReprFrom`). +/// +/// It runs `vg_sha224_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 96 bytes. +/// * `state` must not overlap the arguments on the stack (distinct Rust objects never do). +/// * `state` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_sha224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::sha256::vg_sha224_init(state) } +} + +/// Starts a SHA-256 computation: makes the streaming state `*state` represent the empty message. +/// +/// Contract: `VG.Spec.Sha256.initContract`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha256.Repr`). +/// +/// It runs `vg_sha256_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 96 bytes. +/// * `state` must not overlap the arguments on the stack (distinct Rust objects never do). +/// * `state` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_sha256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::sha256::vg_sha256_init(state) } +} + /// Absorbs data into a SHA-224 or SHA-256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), it then represents that message followed by the `len` bytes at `data`. /// /// Contract: `VG.Spec.Sha256.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha256_update_shani` if the CPU supports its features (`VG_SHA256_UPDATE_SHANI_FEATURES`); or else `vg_sha256_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha256_update_shani` if the CPU supports its features (`VG_SHA256_UPDATE_SHANI_FEATURES`); or else `vg_sha256_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -188,7 +188,7 @@ pub unsafe fn vg_sha256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: /// /// Contract: `VG.Spec.Sha256.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha256_finalize_shani` if the CPU supports its features (`VG_SHA256_FINALIZE_SHANI_FEATURES`); or else `vg_sha256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha256_finalize_shani` if the CPU supports its features (`VG_SHA256_FINALIZE_SHANI_FEATURES`); or else `vg_sha256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -239,7 +239,7 @@ pub unsafe fn vg_sha256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state /// /// Contract: `VG.Spec.Sha256.finalize224Contract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha224_finalize_shani` if the CPU supports its features (`VG_SHA224_FINALIZE_SHANI_FEATURES`); or else `vg_sha224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha224_finalize_shani` if the CPU supports its features (`VG_SHA224_FINALIZE_SHANI_FEATURES`); or else `vg_sha224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86/src/dispatch/sha3.rs b/crates/verified-garbage-asm-x86/src/dispatch/sha3.rs index c3c07e60aee..479c8d220de 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/sha3.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/sha3.rs @@ -26,35 +26,34 @@ pub unsafe fn vg_sha3_224_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state unsafe { crate::sha3::vg_sha3_224_update(state, pos, data, len, scratch) } } -/// Absorbs data into a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, 136), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 136`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Finishes a SHA3-224 computation: if the state `*state` represents a message whose length is `pos` modulo 144 (`VG.Spec.Sha3.Repr` with SHA3-224's rate, as `vg_sha3_224_update` leaves it), writes the SHA3-224 digest of that message (28 bytes, `VG.Spec.Sha3.sha3_224`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 136. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_224`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs `vg_sha3_256_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `scratch` must be valid for reads and writes of 640 bytes. -/// * `pos` must be less than 136. -/// * The contents of `scratch` on return are unspecified. -/// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). -/// * None of `state`, `data` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 40 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 28 bytes. +/// * `pos` must be less than 144. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the arguments on the stack, overlap the return address on the stack or the 680 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 28]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_256_update(state, pos, data, len, scratch) } + unsafe { crate::sha3::vg_sha3_224_finalize(state, pos, out) } } -/// Absorbs data into a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, 104), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 104`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Absorbs data into a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, 136), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 136`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 104. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 136. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_sha3_384_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_256_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -63,46 +62,45 @@ pub unsafe fn vg_sha3_256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state /// * `state` must be valid for reads and writes of 200 bytes. /// * `data` must be valid for reads of `len` bytes. /// * `scratch` must be valid for reads and writes of 640 bytes. -/// * `pos` must be less than 104. +/// * `pos` must be less than 136. /// * The contents of `scratch` on return are unspecified. /// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). /// * None of `state`, `data` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 40 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_384_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_384_update(state, pos, data, len, scratch) } + unsafe { crate::sha3::vg_sha3_256_update(state, pos, data, len, scratch) } } -/// Absorbs data into a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, 72), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 72`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Finishes a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, as `vg_sha3_256_update` leaves it), writes the SHA3-256 digest of that message (32 bytes, `VG.Spec.Sha3.sha3_256`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 72. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_256`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs `vg_sha3_512_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `scratch` must be valid for reads and writes of 640 bytes. -/// * `pos` must be less than 72. -/// * The contents of `scratch` on return are unspecified. -/// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). -/// * None of `state`, `data` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 40 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 32 bytes. +/// * `pos` must be less than 136. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the arguments on the stack, overlap the return address on the stack or the 680 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 32]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_512_update(state, pos, data, len, scratch) } + unsafe { crate::sha3::vg_sha3_256_finalize(state, pos, out) } } -/// Absorbs data into a SHAKE128 computation: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, 168), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 168`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Absorbs data into a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, 104), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 104`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 168. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 104. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_shake128_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_384_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -111,189 +109,191 @@ pub unsafe fn vg_sha3_512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state /// * `state` must be valid for reads and writes of 200 bytes. /// * `data` must be valid for reads of `len` bytes. /// * `scratch` must be valid for reads and writes of 640 bytes. -/// * `pos` must be less than 168. +/// * `pos` must be less than 104. /// * The contents of `scratch` on return are unspecified. /// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). /// * None of `state`, `data` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 40 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake128_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_384_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake128_update(state, pos, data, len, scratch) } + unsafe { crate::sha3::vg_sha3_384_update(state, pos, data, len, scratch) } } -/// Squeezes SHAKE128 output: if the state `*state`, from position `pos`, continues the output of SHAKE128 of a message (`VG.Spec.Sha3.shake128`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake128_finalize` leaves and position 0. +/// Finishes a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, as `vg_sha3_384_update` leaves it), writes the SHA3-384 digest of that message (48 bytes, `VG.Spec.Sha3.sha3_384`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_384`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs `vg_shake128_squeeze`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_384_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of `outlen` bytes. -/// * `pos` must be at most 168. +/// * `out` must be valid for reads and writes of 48 bytes. +/// * `pos` must be less than 104. +/// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the arguments on the stack, overlap the return address on the stack or the 680 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake128_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { +pub unsafe fn vg_sha3_384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 48]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake128_squeeze(state, pos, out, outlen) } + unsafe { crate::sha3::vg_sha3_384_finalize(state, pos, out) } } -/// Squeezes SHAKE256 output: if the state `*state`, from position `pos`, continues the output of SHAKE256 of a message (`VG.Spec.Sha3.shake256`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake256_finalize` leaves and position 0. +/// Absorbs data into a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, 72), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 72`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 72. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_shake256_squeeze`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_512_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of `outlen` bytes. -/// * `pos` must be at most 136. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the arguments on the stack, overlap the return address on the stack or the 680 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `scratch` must be valid for reads and writes of 640 bytes. +/// * `pos` must be less than 72. +/// * The contents of `scratch` on return are unspecified. +/// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). +/// * None of `state`, `data` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 40 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake256_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { +pub unsafe fn vg_sha3_512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake256_squeeze(state, pos, out, outlen) } + unsafe { crate::sha3::vg_sha3_512_update(state, pos, data, len, scratch) } } -/// Finishes a SHA3-224 computation: if the state `*state` represents a message whose length is `pos` modulo 144 (`VG.Spec.Sha3.Repr` with SHA3-224's rate, as `vg_sha3_224_update` leaves it), writes the SHA3-224 digest of that message (28 bytes, `VG.Spec.Sha3.sha3_224`) to `*out`. +/// Finishes a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, as `vg_sha3_512_update` leaves it), writes the SHA3-512 digest of that message (64 bytes, `VG.Spec.Sha3.sha3_512`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_224`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_512`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs `vg_sha3_224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_512_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of 28 bytes. -/// * `pos` must be less than 144. +/// * `out` must be valid for reads and writes of 64 bytes. +/// * `pos` must be less than 72. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the arguments on the stack, overlap the return address on the stack or the 680 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 28]) { +pub unsafe fn vg_sha3_512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 64]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_224_finalize(state, pos, out) } + unsafe { crate::sha3::vg_sha3_512_finalize(state, pos, out) } } -/// Finishes a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, as `vg_sha3_256_update` leaves it), writes the SHA3-256 digest of that message (32 bytes, `VG.Spec.Sha3.sha3_256`) to `*out`. +/// Absorbs data into a SHAKE128 computation: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, 168), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 168`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_256`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 168. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_sha3_256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake128_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of 32 bytes. -/// * `pos` must be less than 136. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the arguments on the stack, overlap the return address on the stack or the 680 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `scratch` must be valid for reads and writes of 640 bytes. +/// * `pos` must be less than 168. +/// * The contents of `scratch` on return are unspecified. +/// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). +/// * None of `state`, `data` and `scratch` may overlap the arguments on the stack, overlap the return address on the stack or the 40 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 32]) { +pub unsafe fn vg_shake128_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_256_finalize(state, pos, out) } + unsafe { crate::sha3::vg_shake128_update(state, pos, data, len, scratch) } } -/// Finishes a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, as `vg_sha3_384_update` leaves it), writes the SHA3-384 digest of that message (48 bytes, `VG.Spec.Sha3.sha3_384`) to `*out`. +/// Finishes absorbing a SHAKE128 message: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, as `vg_shake128_update` leaves it), it becomes a state that, squeezed by `vg_shake128_squeeze` from position 0, gives the output of SHAKE128 of that message (`VG.Spec.Sha3.shake128`) from its first byte on (`VG.Spec.Sha3.XofRepr`). /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_384`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointer and `pos` may affect timing, not the state. /// -/// It runs `vg_sha3_384_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake128_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of 48 bytes. -/// * `pos` must be less than 104. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the arguments on the stack, overlap the return address on the stack or the 680 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `pos` must be less than 168. +/// * `state` must not overlap the arguments on the stack, overlap the return address on the stack or the 676 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 48]) { +pub unsafe fn vg_shake128_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_384_finalize(state, pos, out) } + unsafe { crate::sha3::vg_shake128_finalize(state, pos) } } -/// Finishes a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, as `vg_sha3_512_update` leaves it), writes the SHA3-512 digest of that message (64 bytes, `VG.Spec.Sha3.sha3_512`) to `*out`. +/// Squeezes SHAKE128 output: if the state `*state`, from position `pos`, continues the output of SHAKE128 of a message (`VG.Spec.Sha3.shake128`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake128_finalize` leaves and position 0. /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_512`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. /// -/// It runs `vg_sha3_512_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake128_squeeze`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of 64 bytes. -/// * `pos` must be less than 72. -/// * The contents of `state` on return are unspecified. +/// * `out` must be valid for reads and writes of `outlen` bytes. +/// * `pos` must be at most 168. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the arguments on the stack, overlap the return address on the stack or the 680 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 64]) { +pub unsafe fn vg_shake128_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_512_finalize(state, pos, out) } + unsafe { crate::sha3::vg_shake128_squeeze(state, pos, out, outlen) } } -/// Finishes absorbing a SHAKE128 message: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, as `vg_shake128_update` leaves it), it becomes a state that, squeezed by `vg_shake128_squeeze` from position 0, gives the output of SHAKE128 of that message (`VG.Spec.Sha3.shake128`) from its first byte on (`VG.Spec.Sha3.XofRepr`). +/// Finishes absorbing a SHAKE256 message: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHAKE256's rate, as `vg_sha3_256_update` leaves it), it becomes a state that, squeezed by `vg_shake256_squeeze` from position 0, gives the output of SHAKE256 of that message (`VG.Spec.Sha3.shake256`) from its first byte on (`VG.Spec.Sha3.XofRepr`). /// -/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointer and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointer and `pos` may affect timing, not the state. /// -/// It runs `vg_shake128_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `pos` must be less than 168. +/// * `pos` must be less than 136. /// * `state` must not overlap the arguments on the stack, overlap the return address on the stack or the 676 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake128_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { +pub unsafe fn vg_shake256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake128_finalize(state, pos) } + unsafe { crate::sha3::vg_shake256_finalize(state, pos) } } -/// Finishes absorbing a SHAKE256 message: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHAKE256's rate, as `vg_sha3_256_update` leaves it), it becomes a state that, squeezed by `vg_shake256_squeeze` from position 0, gives the output of SHAKE256 of that message (`VG.Spec.Sha3.shake256`) from its first byte on (`VG.Spec.Sha3.XofRepr`). +/// Squeezes SHAKE256 output: if the state `*state`, from position `pos`, continues the output of SHAKE256 of a message (`VG.Spec.Sha3.shake256`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake256_finalize` leaves and position 0. /// -/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointer and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. /// -/// It runs `vg_shake256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake256_squeeze`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `pos` must be less than 136. -/// * `state` must not overlap the arguments on the stack, overlap the return address on the stack or the 676 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of `outlen` bytes. +/// * `pos` must be at most 136. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the arguments on the stack, overlap the return address on the stack or the 680 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { +pub unsafe fn vg_shake256_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake256_finalize(state, pos) } + unsafe { crate::sha3::vg_shake256_squeeze(state, pos, out, outlen) } } diff --git a/crates/verified-garbage-asm-x86/src/dispatch/sha512.rs b/crates/verified-garbage-asm-x86/src/dispatch/sha512.rs index 66dc2a5c73c..2c5f10ea8f5 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/sha512.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/sha512.rs @@ -38,217 +38,217 @@ pub unsafe fn vg_sha512_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *m unsafe { crate::sha512::vg_sha512_init(state) } } -/// Absorbs data into a SHA-384, SHA-512, SHA-512/224 or SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), it then represents that message followed by the `len` bytes at `data`. +/// Starts a SHA-512/224 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/224 (`VG.Spec.Sha512.H0_512_224`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. /// -/// Contract: `VG.Spec.Sha512.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_224`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). /// -/// It runs `vg_sha512_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_224_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `state` must not overlap `data` or the arguments on the stack (distinct Rust objects never do). -/// * Neither `state` nor `data` may overlap the return address on the stack or the 1424 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `state` must not overlap the arguments on the stack (distinct Rust objects never do). +/// * `state` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, data: *const u8, len: usize) { +pub unsafe fn vg_sha512_224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_update(state, count, data, len) } + unsafe { crate::sha512::vg_sha512_224_init(state) } } -/// Computes the SHA-384 digest of the `len` bytes at `data` (48 bytes, `VG.Spec.Sha512.sha384`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-384 computation with that message. +/// Starts a SHA-512/256 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/256 (`VG.Spec.Sha512.H0_512_256`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. /// -/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_256`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). /// -/// It runs `vg_sha384_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_256_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 48 bytes. -/// * `out` must not overlap `data` or the arguments on the stack (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the return address on the stack or the 612 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `state` must be valid for reads and writes of 192 bytes. +/// * `state` must not overlap the arguments on the stack (distinct Rust objects never do). +/// * `state` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha384_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 48]) { +pub unsafe fn vg_sha512_256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha384_digest(data, len, out) } + unsafe { crate::sha512::vg_sha512_256_init(state) } } -/// Computes the SHA-512 digest of the `len` bytes at `data` (64 bytes, `VG.Spec.Sha512.sha512`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512 computation with that message. +/// Absorbs data into a SHA-384, SHA-512, SHA-512/224 or SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), it then represents that message followed by the `len` bytes at `data`. /// -/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha512.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_sha512_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// +/// * `state` must be valid for reads and writes of 192 bytes. /// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 64 bytes. -/// * `out` must not overlap `data` or the arguments on the stack (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the return address on the stack or the 612 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `state` must not overlap `data` or the arguments on the stack (distinct Rust objects never do). +/// * Neither `state` nor `data` may overlap the return address on the stack or the 1424 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 64]) { +pub unsafe fn vg_sha512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, data: *const u8, len: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_digest(data, len, out) } + unsafe { crate::sha512::vg_sha512_update(state, count, data, len) } } -/// Starts a SHA-512/224 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/224 (`VG.Spec.Sha512.H0_512_224`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. +/// Finishes a SHA-384, SHA-512, SHA-512/224 or SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (64 bytes) to `*out`. The SHA-512 digest is all of it; the SHA-384, SHA-512/224 and SHA-512/256 digests are its first 48, 28 and 32 bytes. /// -/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_224`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). +/// Contract: `VG.Spec.Sha512.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha512_224_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `state` must not overlap the arguments on the stack (distinct Rust objects never do). -/// * `state` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 64 bytes. +/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other or the arguments on the stack (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the return address on the stack or the 1420 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { +pub unsafe fn vg_sha512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 64]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_224_init(state) } + unsafe { crate::sha512::vg_sha512_finalize(state, count, out) } } -/// Starts a SHA-512/256 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/256 (`VG.Spec.Sha512.H0_512_256`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. +/// Finishes a SHA-384 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-384 (as `vg_sha384_init` starts it), writes the SHA-384 digest of that message (48 bytes, `VG.Spec.Sha512.sha384`) to `*out`. /// -/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_256`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). +/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_384` and `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha512_256_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha384_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `state` must not overlap the arguments on the stack (distinct Rust objects never do). -/// * `state` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 48 bytes. +/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other or the arguments on the stack (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the return address on the stack or the 1420 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { +pub unsafe fn vg_sha384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 48]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_256_init(state) } + unsafe { crate::sha512::vg_sha384_finalize(state, count, out) } } -/// Finishes a SHA-384, SHA-512, SHA-512/224 or SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (64 bytes) to `*out`. The SHA-512 digest is all of it; the SHA-384, SHA-512/224 and SHA-512/256 digests are its first 48, 28 and 32 bytes. +/// Finishes a SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/256 (as `vg_sha512_256_init` starts it), writes the SHA-512/256 digest of that message (32 bytes, `VG.Spec.Sha512.sha512_256`) to `*out`. /// -/// Contract: `VG.Spec.Sha512.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_256` and `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha512_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 64 bytes. +/// * `out` must be valid for reads and writes of 32 bytes. /// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other or the arguments on the stack (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the return address on the stack or the 1420 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 64]) { +pub unsafe fn vg_sha512_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 32]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_finalize(state, count, out) } + unsafe { crate::sha512::vg_sha512_256_finalize(state, count, out) } } -/// Finishes a SHA-384 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-384 (as `vg_sha384_init` starts it), writes the SHA-384 digest of that message (48 bytes, `VG.Spec.Sha512.sha384`) to `*out`. +/// Finishes a SHA-512/224 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/224 (as `vg_sha512_224_init` starts it), writes the SHA-512/224 digest of that message (28 bytes, `VG.Spec.Sha512.sha512_224`) to `*out`. /// -/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_384` and `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_224` and `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs `vg_sha384_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 48 bytes. +/// * `out` must be valid for reads and writes of 28 bytes. /// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other or the arguments on the stack (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the return address on the stack or the 1420 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 48]) { +pub unsafe fn vg_sha512_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 28]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha384_finalize(state, count, out) } + unsafe { crate::sha512::vg_sha512_224_finalize(state, count, out) } } -/// Computes the SHA-512/224 digest of the `len` bytes at `data` (28 bytes, `VG.Spec.Sha512.sha512_224`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512/224 computation with that message. +/// Computes the SHA-384 digest of the `len` bytes at `data` (48 bytes, `VG.Spec.Sha512.sha384`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-384 computation with that message. /// -/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha512_224_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha384_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 28 bytes. +/// * `out` must be valid for reads and writes of 48 bytes. /// * `out` must not overlap `data` or the arguments on the stack (distinct Rust objects never do). /// * Neither `data` nor `out` may overlap the return address on the stack or the 612 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_224_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 28]) { +pub unsafe fn vg_sha384_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 48]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_224_digest(data, len, out) } + unsafe { crate::sha512::vg_sha384_digest(data, len, out) } } -/// Computes the SHA-512/256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha512.sha512_256`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512/256 computation with that message. +/// Computes the SHA-512 digest of the `len` bytes at `data` (64 bytes, `VG.Spec.Sha512.sha512`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512 computation with that message. /// -/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha512_256_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 32 bytes. +/// * `out` must be valid for reads and writes of 64 bytes. /// * `out` must not overlap `data` or the arguments on the stack (distinct Rust objects never do). /// * Neither `data` nor `out` may overlap the return address on the stack or the 612 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { +pub unsafe fn vg_sha512_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 64]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_256_digest(data, len, out) } + unsafe { crate::sha512::vg_sha512_digest(data, len, out) } } -/// Finishes a SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/256 (as `vg_sha512_256_init` starts it), writes the SHA-512/256 digest of that message (32 bytes, `VG.Spec.Sha512.sha512_256`) to `*out`. +/// Computes the SHA-512/224 digest of the `len` bytes at `data` (28 bytes, `VG.Spec.Sha512.sha512_224`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512/224 computation with that message. /// -/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_256` and `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha512_256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_224_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 32 bytes. -/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other or the arguments on the stack (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the return address on the stack or the 1420 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `out` must be valid for reads and writes of 28 bytes. +/// * `out` must not overlap `data` or the arguments on the stack (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the return address on the stack or the 612 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 32]) { +pub unsafe fn vg_sha512_224_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 28]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_256_finalize(state, count, out) } + unsafe { crate::sha512::vg_sha512_224_digest(data, len, out) } } -/// Finishes a SHA-512/224 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/224 (as `vg_sha512_224_init` starts it), writes the SHA-512/224 digest of that message (28 bytes, `VG.Spec.Sha512.sha512_224`) to `*out`. +/// Computes the SHA-512/256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha512.sha512_256`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512/256 computation with that message. /// -/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_224` and `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs `vg_sha512_224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha512_256_digest`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 28 bytes. -/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other or the arguments on the stack (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the return address on the stack or the 1420 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `out` must be valid for reads and writes of 32 bytes. +/// * `out` must not overlap `data` or the arguments on the stack (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the return address on the stack or the 612 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 28]) { +pub unsafe fn vg_sha512_256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_224_finalize(state, count, out) } + unsafe { crate::sha512::vg_sha512_256_digest(data, len, out) } } diff --git a/crates/verified-garbage-asm-x86/src/dispatch/sm4_gcm.rs b/crates/verified-garbage-asm-x86/src/dispatch/sm4_gcm.rs index e6897a585c5..356bb52e050 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/sm4_gcm.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/sm4_gcm.rs @@ -23,13 +23,13 @@ pub unsafe fn vg_sm4_gcm_init_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *co unsafe { crate::sm4_gcm::vg_sm4_gcm_init(key, ctx) } } -/// SM4-GCM authenticated encryption (NIST SP 800-38D §7.1, GCM-AE, with a 128-bit tag; RFC 8998 §3.5.1): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, encrypts the `len` bytes at `data` in place, under the IV the `nonce_len` bytes at `nonce`, and writes the tag of the ciphertext and the `aad_len` bytes of additional data at `aad` to `*tag`. A shorter tag is the first bytes of this one. +/// Starts an incremental SM4-GCM encryption or decryption (NIST SP 800-38D §7): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, makes the streaming state `*state` represent the message with the IV the `nonce_len` bytes at `nonce`, and no additional data or text yet. Continue with `vg_sm4_gcm_stream_aad`, then `vg_sm4_gcm_stream_encrypt` or `vg_sm4_gcm_stream_decrypt`, then `vg_sm4_gcm_stream_finish` or `vg_sm4_gcm_stream_verify`, with the same key context. /// -/// The function checks no length. GCM is secure only for a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of data and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must ensure; and a nonce must never be used twice with the same key. +/// The function checks no length: GCM requires a nonce of 1 to `2^61 - 1` bytes (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Sm4.Gcm.sealContract`. Constant time: only the pointers and the lengths may affect timing, not the key context, the nonce, the additional data or the data. +/// Contract: `VG.Spec.Gcm.streamInitContract` (AES-GCM's, which reads only the hash subkey of the key context). The streaming state is `J₀`, the GHASH accumulator, a partial block, the next counter block and a keystream block (`VG.Spec.Gcm.StreamRepr`). Constant time: only the pointers and `nonce_len` may affect timing, not the key context or the nonce. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_seal_pclmul` if the CPU supports its features (`VG_SM4_GCM_SEAL_PCLMUL_FEATURES`); or else `vg_sm4_gcm_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_stream_init_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_INIT_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -37,37 +37,35 @@ pub unsafe fn vg_sm4_gcm_init_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *co /// /// * `ctx` must be valid for reads of 256 bytes. /// * `nonce` must be valid for reads of `nonce_len` bytes. -/// * `aad` must be valid for reads of `aad_len` bytes. -/// * `data` must be valid for reads and writes of `len` bytes. -/// * `tag` must be valid for reads and writes of 16 bytes. -/// * `data` and `tag` must not overlap each other, `ctx`, `nonce` or `aad` (distinct Rust objects never do). -/// * None of `ctx`, `nonce`, `aad`, `data` and `tag` may overlap the arguments on the stack, overlap the return address on the stack or the 4136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `state` must be valid for reads and writes of 80 bytes. +/// * `state` must not overlap `ctx` or `nonce` (distinct Rust objects never do). +/// * None of `ctx`, `nonce` and `state` may overlap the arguments on the stack, overlap the return address on the stack or the 2608 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sm4_gcm_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *mut [u8; 16]) { +pub unsafe fn vg_sm4_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *mut [u8; 16]); + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_SEAL_PCLMUL_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_SEAL_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_INIT_PCLMUL_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_INIT_PCLMUL_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_seal_pclmul; + return crate::sm4_gcm::vg_sm4_gcm_stream_init_pclmul; } - crate::sm4_gcm::vg_sm4_gcm_seal + crate::sm4_gcm::vg_sm4_gcm_stream_init } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *mut [u8; 16]) { + unsafe extern "C" fn first(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, nonce, nonce_len, aad, aad_len, data, len, tag) } + unsafe { f(ctx, nonce, nonce_len, state) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -75,54 +73,50 @@ pub unsafe fn vg_sm4_gcm_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *co // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, nonce, nonce_len, aad, aad_len, data, len, tag) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, nonce, nonce_len, state) } } -/// SM4-GCM authenticated decryption (NIST SP 800-38D §7.2, GCM-AD; RFC 8998 §3.5.1): with the key context `*ctx` that `vg_sm4_gcm_init` wrote and the received tag the `tag_len` bytes at `tag`, returns 1 if `tag_len` is between 4 and 16 and the tag is the first `tag_len` bytes of that of the `len` bytes of ciphertext at `data` and the `aad_len` bytes of additional data at `aad`, under the IV the `nonce_len` bytes at `nonce`, having then decrypted the ciphertext in place; otherwise returns 0, and the bytes at `data` are unchanged. The tags are compared without a branch. -/// -/// The function checks no other length: GCM requires a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of ciphertext and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// Absorbs additional data into an incremental SM4-GCM encryption or decryption: with the key context `*ctx`, if the streaming state `*state` represents a message with `aad_len` bytes (modulo 2⁶⁴) of additional data and no text yet, it then represents that message with the `len` bytes at `data` appended to its additional data. All of the additional data comes before the text (§7.1 step 5): after text, the state represents no message. /// -/// Contract: `VG.Spec.Sm4.Gcm.openContract`. Constant time but for the result: only the pointers, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the additional data, the data or the tag. +/// Contract: `VG.Spec.Gcm.streamAadContract` (AES-GCM's, which reads only the hash subkey of the key context). Constant time: only the pointers, `aad_len` and `len` may affect timing, not the key context, the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_open_pclmul` if the CPU supports its features (`VG_SM4_GCM_OPEN_PCLMUL_FEATURES`); or else `vg_sm4_gcm_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_stream_aad_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_AAD_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_aad`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `nonce` must be valid for reads of `nonce_len` bytes. -/// * `aad` must be valid for reads of `aad_len` bytes. -/// * `data` must be valid for reads and writes of `len` bytes. -/// * `tag` must be valid for reads of `tag_len` bytes. -/// * `data` must not overlap `ctx`, `nonce`, `aad` or `tag` (distinct Rust objects never do). -/// * None of `ctx`, `nonce`, `aad`, `data` and `tag` may overlap the arguments on the stack, overlap the return address on the stack or the 4140 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `state` must be valid for reads and writes of 80 bytes. +/// * `data` must be valid for reads of `len` bytes. +/// * `state` must not overlap `ctx` or `data` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the arguments on the stack, overlap the return address on the stack or the 2616 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sm4_gcm_open_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *const u8, tag_len: usize) -> u32 { +pub unsafe fn vg_sm4_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *const u8, tag_len: usize) -> u32; + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_OPEN_PCLMUL_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_OPEN_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_AAD_PCLMUL_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_AAD_PCLMUL_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_open_pclmul; + return crate::sm4_gcm::vg_sm4_gcm_stream_aad_pclmul; } - crate::sm4_gcm::vg_sm4_gcm_open + crate::sm4_gcm::vg_sm4_gcm_stream_aad } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *const u8, tag_len: usize) -> u32 { + unsafe extern "C" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, nonce, nonce_len, aad, aad_len, data, len, tag, tag_len) } + unsafe { f(ctx, state, aad_len, data, len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -130,14 +124,16 @@ pub unsafe fn vg_sm4_gcm_open_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *co // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, nonce, nonce_len, aad, aad_len, data, len, tag, tag_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, data, len) } } -/// Absorbs additional data into an incremental SM4-GCM encryption or decryption: with the key context `*ctx`, if the streaming state `*state` represents a message with `aad_len` bytes (modulo 2⁶⁴) of additional data and no text yet, it then represents that message with the `len` bytes at `data` appended to its additional data. All of the additional data comes before the text (§7.1 step 5): after text, the state represents no message. +/// Encrypts the next piece of the text of an incremental SM4-GCM encryption (NIST SP 800-38D §7.1): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and the ciphertext of exactly `text_len` bytes of plaintext so far, encrypts the `len` bytes at `data` in place, as the continuation of that plaintext, and the state then represents the message with them appended. /// -/// Contract: `VG.Spec.Gcm.streamAadContract` (AES-GCM's, which reads only the hash subkey of the key context). Constant time: only the pointers, `aad_len` and `len` may affect timing, not the key context, the state or the data. +/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// +/// Contract: `VG.Spec.Sm4.Gcm.streamEncryptContract`. Constant time: only the pointers, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_stream_aad_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_AAD_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_aad`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_stream_encrypt_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_ENCRYPT_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -145,35 +141,35 @@ pub unsafe fn vg_sm4_gcm_open_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *co /// /// * `ctx` must be valid for reads of 256 bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `state` must not overlap `ctx` or `data` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the arguments on the stack, overlap the return address on the stack or the 2616 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads and writes of `len` bytes. +/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the arguments on the stack, overlap the return address on the stack or the 4136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sm4_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { +pub unsafe fn vg_sm4_gcm_stream_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize); + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_AAD_PCLMUL_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_AAD_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_ENCRYPT_PCLMUL_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_ENCRYPT_PCLMUL_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_stream_aad_pclmul; + return crate::sm4_gcm::vg_sm4_gcm_stream_encrypt_pclmul; } - crate::sm4_gcm::vg_sm4_gcm_stream_aad + crate::sm4_gcm::vg_sm4_gcm_stream_encrypt } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { + unsafe extern "C" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, state, aad_len, data, len) } + unsafe { f(ctx, state, aad_len, text_len, data, len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -181,52 +177,52 @@ pub unsafe fn vg_sm4_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ct // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, data, len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, text_len, data, len) } } -/// Starts an incremental SM4-GCM encryption or decryption (NIST SP 800-38D §7): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, makes the streaming state `*state` represent the message with the IV the `nonce_len` bytes at `nonce`, and no additional data or text yet. Continue with `vg_sm4_gcm_stream_aad`, then `vg_sm4_gcm_stream_encrypt` or `vg_sm4_gcm_stream_decrypt`, then `vg_sm4_gcm_stream_finish` or `vg_sm4_gcm_stream_verify`, with the same key context. +/// Decrypts the next piece of the text of an incremental SM4-GCM decryption (NIST SP 800-38D §7.2): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext so far, decrypts the `len` bytes of ciphertext at `data` in place, as the continuation of that ciphertext, and the state then represents the message with them appended. The plaintext is not authenticated until `vg_sm4_gcm_stream_verify` has returned 1. /// -/// The function checks no length: GCM requires a nonce of 1 to `2^61 - 1` bytes (§5.2.1.1), which the caller must check. +/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Gcm.streamInitContract` (AES-GCM's, which reads only the hash subkey of the key context). The streaming state is `J₀`, the GHASH accumulator, a partial block, the next counter block and a keystream block (`VG.Spec.Gcm.StreamRepr`). Constant time: only the pointers and `nonce_len` may affect timing, not the key context or the nonce. +/// Contract: `VG.Spec.Sm4.Gcm.streamDecryptContract`. Constant time: only the pointers, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_stream_init_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_INIT_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_stream_decrypt_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_DECRYPT_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `nonce` must be valid for reads of `nonce_len` bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `state` must not overlap `ctx` or `nonce` (distinct Rust objects never do). -/// * None of `ctx`, `nonce` and `state` may overlap the arguments on the stack, overlap the return address on the stack or the 2608 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads and writes of `len` bytes. +/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the arguments on the stack, overlap the return address on the stack or the 4136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sm4_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { +pub unsafe fn vg_sm4_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]); + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_INIT_PCLMUL_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_INIT_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_DECRYPT_PCLMUL_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_DECRYPT_PCLMUL_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_stream_init_pclmul; + return crate::sm4_gcm::vg_sm4_gcm_stream_decrypt_pclmul; } - crate::sm4_gcm::vg_sm4_gcm_stream_init + crate::sm4_gcm::vg_sm4_gcm_stream_decrypt } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { + unsafe extern "C" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, nonce, nonce_len, state) } + unsafe { f(ctx, state, aad_len, text_len, data, len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -234,14 +230,14 @@ pub unsafe fn vg_sm4_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, c // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, nonce, nonce_len, state) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, text_len, data, len) } } /// Finishes an incremental SM4-GCM encryption or decryption (NIST SP 800-38D §7.1 steps 5–6, §7.2 steps 6–7): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, writes its 128-bit tag to `*tag`. A shorter tag is the first bytes of this one; to check a received tag, use `vg_sm4_gcm_stream_verify`. /// /// Contract: `VG.Spec.Sm4.Gcm.streamFinishContract`. Constant time: only the pointers, `aad_len` and `text_len` may affect timing, not the key context or the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_stream_finish_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_FINISH_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_finish`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_stream_finish_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_FINISH_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_finish`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -293,7 +289,7 @@ pub unsafe fn vg_sm4_gcm_stream_finish_auto(_dit: verified_garbage_cpu::Dit<'_>, /// /// Contract: `VG.Spec.Sm4.Gcm.streamVerifyContract`. Constant time: only the pointers, `aad_len`, `text_len` and `tag_len` may affect timing, not the key context, the state or the tags. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_stream_verify_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_VERIFY_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_stream_verify_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_VERIFY_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// @@ -341,49 +337,51 @@ pub unsafe fn vg_sm4_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, text_len, tag, tag_len) } } -/// Encrypts the next piece of the text of an incremental SM4-GCM encryption (NIST SP 800-38D §7.1): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and the ciphertext of exactly `text_len` bytes of plaintext so far, encrypts the `len` bytes at `data` in place, as the continuation of that plaintext, and the state then represents the message with them appended. +/// SM4-GCM authenticated encryption (NIST SP 800-38D §7.1, GCM-AE, with a 128-bit tag; RFC 8998 §3.5.1): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, encrypts the `len` bytes at `data` in place, under the IV the `nonce_len` bytes at `nonce`, and writes the tag of the ciphertext and the `aad_len` bytes of additional data at `aad` to `*tag`. A shorter tag is the first bytes of this one. /// -/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// The function checks no length. GCM is secure only for a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of data and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must ensure; and a nonce must never be used twice with the same key. /// -/// Contract: `VG.Spec.Sm4.Gcm.streamEncryptContract`. Constant time: only the pointers, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. +/// Contract: `VG.Spec.Sm4.Gcm.sealContract`. Constant time: only the pointers and the lengths may affect timing, not the key context, the nonce, the additional data or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_stream_encrypt_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_ENCRYPT_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_seal_pclmul` if the CPU supports its features (`VG_SM4_GCM_SEAL_PCLMUL_FEATURES`); or else `vg_sm4_gcm_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `state` must be valid for reads and writes of 80 bytes. +/// * `nonce` must be valid for reads of `nonce_len` bytes. +/// * `aad` must be valid for reads of `aad_len` bytes. /// * `data` must be valid for reads and writes of `len` bytes. -/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the arguments on the stack, overlap the return address on the stack or the 4136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `tag` must be valid for reads and writes of 16 bytes. +/// * `data` and `tag` must not overlap each other, `ctx`, `nonce` or `aad` (distinct Rust objects never do). +/// * None of `ctx`, `nonce`, `aad`, `data` and `tag` may overlap the arguments on the stack, overlap the return address on the stack or the 4136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sm4_gcm_stream_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { +pub unsafe fn vg_sm4_gcm_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *mut [u8; 16]) { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *mut [u8; 16]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_ENCRYPT_PCLMUL_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_ENCRYPT_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_SEAL_PCLMUL_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_SEAL_PCLMUL_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_stream_encrypt_pclmul; + return crate::sm4_gcm::vg_sm4_gcm_seal_pclmul; } - crate::sm4_gcm::vg_sm4_gcm_stream_encrypt + crate::sm4_gcm::vg_sm4_gcm_seal } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { + unsafe extern "C" fn first(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *mut [u8; 16]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, state, aad_len, text_len, data, len) } + unsafe { f(ctx, nonce, nonce_len, aad, aad_len, data, len, tag) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -391,52 +389,54 @@ pub unsafe fn vg_sm4_gcm_stream_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_> // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, text_len, data, len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, nonce, nonce_len, aad, aad_len, data, len, tag) } } -/// Decrypts the next piece of the text of an incremental SM4-GCM decryption (NIST SP 800-38D §7.2): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext so far, decrypts the `len` bytes of ciphertext at `data` in place, as the continuation of that ciphertext, and the state then represents the message with them appended. The plaintext is not authenticated until `vg_sm4_gcm_stream_verify` has returned 1. +/// SM4-GCM authenticated decryption (NIST SP 800-38D §7.2, GCM-AD; RFC 8998 §3.5.1): with the key context `*ctx` that `vg_sm4_gcm_init` wrote and the received tag the `tag_len` bytes at `tag`, returns 1 if `tag_len` is between 4 and 16 and the tag is the first `tag_len` bytes of that of the `len` bytes of ciphertext at `data` and the `aad_len` bytes of additional data at `aad`, under the IV the `nonce_len` bytes at `nonce`, having then decrypted the ciphertext in place; otherwise returns 0, and the bytes at `data` are unchanged. The tags are compared without a branch. /// -/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// The function checks no other length: GCM requires a nonce of 1 to `2^61 - 1` bytes, at most `2^36 - 32` bytes of ciphertext and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// Contract: `VG.Spec.Sm4.Gcm.streamDecryptContract`. Constant time: only the pointers, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. +/// Contract: `VG.Spec.Sm4.Gcm.openContract`. Constant time but for the result: only the pointers, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the additional data, the data or the tag. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_stream_decrypt_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_DECRYPT_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_open_pclmul` if the CPU supports its features (`VG_SM4_GCM_OPEN_PCLMUL_FEATURES`); or else `vg_sm4_gcm_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// The function may overwrite the arguments on the stack, as the calling convention lets it. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `state` must be valid for reads and writes of 80 bytes. +/// * `nonce` must be valid for reads of `nonce_len` bytes. +/// * `aad` must be valid for reads of `aad_len` bytes. /// * `data` must be valid for reads and writes of `len` bytes. -/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the arguments on the stack, overlap the return address on the stack or the 4136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `tag` must be valid for reads of `tag_len` bytes. +/// * `data` must not overlap `ctx`, `nonce`, `aad` or `tag` (distinct Rust objects never do). +/// * None of `ctx`, `nonce`, `aad`, `data` and `tag` may overlap the arguments on the stack, overlap the return address on the stack or the 4140 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sm4_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { +pub unsafe fn vg_sm4_gcm_open_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *const u8, tag_len: usize) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); + type Impl = unsafe extern "C" fn(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *const u8, tag_len: usize) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_DECRYPT_PCLMUL_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_DECRYPT_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_OPEN_PCLMUL_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_OPEN_PCLMUL_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_stream_decrypt_pclmul; + return crate::sm4_gcm::vg_sm4_gcm_open_pclmul; } - crate::sm4_gcm::vg_sm4_gcm_stream_decrypt + crate::sm4_gcm::vg_sm4_gcm_open } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "C" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { + unsafe extern "C" fn first(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, data: *mut u8, len: usize, tag: *const u8, tag_len: usize) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, state, aad_len, text_len, data, len) } + unsafe { f(ctx, nonce, nonce_len, aad, aad_len, data, len, tag, tag_len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -444,5 +444,5 @@ pub unsafe fn vg_sm4_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_> // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, text_len, data, len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, nonce, nonce_len, aad, aad_len, data, len, tag, tag_len) } } diff --git a/crates/verified-garbage-asm-x86/src/dispatch/x25519.rs b/crates/verified-garbage-asm-x86/src/dispatch/x25519.rs index fd38ced1fe9..aab319607b5 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/x25519.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/x25519.rs @@ -2,45 +2,45 @@ //! The dispatchers of the verified `x25519` functions for `x86` //! (`crate::x25519`, `VG.Rust.dispatcher`). -/// Computes an X25519 public key (RFC 7748 §6.1): writes `X25519(k, 9)`, the X25519 function of the 32-byte scalar `k` at `scalar` and the base point's u-coordinate 9, to `*out`. The scalar is decoded (clamped). +/// Computes an X25519 shared secret (RFC 7748 §6.1): writes `X25519(k, u)` to `*out`, for the 32-byte scalar `k` at `scalar` and the 32-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped) and the most significant bit of `u` masked as RFC 7748 specifies (§5). /// -/// Contract: `VG.Spec.X25519.x25519BaseContract`. Constant time: only the pointers may affect timing, not the scalar. +/// Contract: `VG.Spec.X25519.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. /// -/// It runs `vg_x25519_base`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_x25519_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `out` must be valid for reads and writes of 32 bytes. /// * `scalar` must be valid for reads of 32 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * `point` must be valid for reads of 32 bytes. +/// * `scratch` must be valid for reads and writes of 4096 bytes. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `scalar` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `scalar` and `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `scalar`, `point` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `scalar`, `point` and `scratch` may overlap the return address on the stack or the 28 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x25519_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], scratch: *mut [u64; 1024]) { +pub unsafe fn vg_x25519_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], point: *const [u8; 32], scratch: *mut [u64; 512]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::x25519::vg_x25519_base(out, scalar, scratch) } + unsafe { crate::x25519::vg_x25519_exchange(out, scalar, point, scratch) } } -/// Computes an X25519 shared secret (RFC 7748 §6.1): writes `X25519(k, u)` to `*out`, for the 32-byte scalar `k` at `scalar` and the 32-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped) and the most significant bit of `u` masked as RFC 7748 specifies (§5). +/// Computes an X25519 public key (RFC 7748 §6.1): writes `X25519(k, 9)`, the X25519 function of the 32-byte scalar `k` at `scalar` and the base point's u-coordinate 9, to `*out`. The scalar is decoded (clamped). /// -/// Contract: `VG.Spec.X25519.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. +/// Contract: `VG.Spec.X25519.x25519BaseContract`. Constant time: only the pointers may affect timing, not the scalar. /// -/// It runs `vg_x25519_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_x25519_base`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `out` must be valid for reads and writes of 32 bytes. /// * `scalar` must be valid for reads of 32 bytes. -/// * `point` must be valid for reads of 32 bytes. -/// * `scratch` must be valid for reads and writes of 4096 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `scalar`, `point` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `scalar`, `point` and `scratch` may overlap the return address on the stack or the 28 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `scalar` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `scalar` and `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x25519_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], point: *const [u8; 32], scratch: *mut [u64; 512]) -> u32 { +pub unsafe fn vg_x25519_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], scratch: *mut [u64; 1024]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::x25519::vg_x25519_exchange(out, scalar, point, scratch) } + unsafe { crate::x25519::vg_x25519_base(out, scalar, scratch) } } diff --git a/crates/verified-garbage-asm-x86/src/dispatch/x448.rs b/crates/verified-garbage-asm-x86/src/dispatch/x448.rs index c7e58daec5d..0f92e5d0fbe 100644 --- a/crates/verified-garbage-asm-x86/src/dispatch/x448.rs +++ b/crates/verified-garbage-asm-x86/src/dispatch/x448.rs @@ -2,45 +2,45 @@ //! The dispatchers of the verified `x448` functions for `x86` //! (`crate::x448`, `VG.Rust.dispatcher`). -/// Computes an X448 public key (RFC 7748 §6.2): writes `X448(k, 5)`, the X448 function of the 56-byte scalar `k` at `scalar` and the base point's u-coordinate 5, to `*out`. The scalar is decoded (clamped). +/// Computes an X448 shared secret (RFC 7748 §6.2): writes `X448(k, u)` to `*out`, for the 56-byte scalar `k` at `scalar` and the 56-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped); all 448 bits of `u` are used and noncanonical coordinates are reduced modulo the field prime (§5). /// -/// Contract: `VG.Spec.X448.x448BaseContract`. Constant time: only the pointers may affect timing, not the scalar. +/// Contract: `VG.Spec.X448.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. /// -/// It runs `vg_x448_base`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_x448_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `out` must be valid for reads and writes of 56 bytes. /// * `scalar` must be valid for reads of 56 bytes. +/// * `point` must be valid for reads of 56 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `scalar` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `scalar` and `scratch` may overlap the return address on the stack or the 100 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `scalar`, `point` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `scalar`, `point` and `scratch` may overlap the return address on the stack or the 40 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x448_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], scratch: *mut [u64; 1024]) { +pub unsafe fn vg_x448_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], point: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::x448::vg_x448_base(out, scalar, scratch) } + unsafe { crate::x448::vg_x448_exchange(out, scalar, point, scratch) } } -/// Computes an X448 shared secret (RFC 7748 §6.2): writes `X448(k, u)` to `*out`, for the 56-byte scalar `k` at `scalar` and the 56-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped); all 448 bits of `u` are used and noncanonical coordinates are reduced modulo the field prime (§5). +/// Computes an X448 public key (RFC 7748 §6.2): writes `X448(k, 5)`, the X448 function of the 56-byte scalar `k` at `scalar` and the base point's u-coordinate 5, to `*out`. The scalar is decoded (clamped). /// -/// Contract: `VG.Spec.X448.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. +/// Contract: `VG.Spec.X448.x448BaseContract`. Constant time: only the pointers may affect timing, not the scalar. /// -/// It runs `vg_x448_exchange`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_x448_base`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `out` must be valid for reads and writes of 56 bytes. /// * `scalar` must be valid for reads of 56 bytes. -/// * `point` must be valid for reads of 56 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `scalar`, `point` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `scalar`, `point` and `scratch` may overlap the return address on the stack or the 40 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `scalar` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `scalar` and `scratch` may overlap the return address on the stack or the 100 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x448_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], point: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_x448_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], scratch: *mut [u64; 1024]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::x448::vg_x448_exchange(out, scalar, point, scratch) } + unsafe { crate::x448::vg_x448_base(out, scalar, scratch) } } diff --git a/crates/verified-garbage-asm-x86/src/fn_tables/mod.rs b/crates/verified-garbage-asm-x86/src/fn_tables/mod.rs index 97f9a4a0fe8..38ee82680af 100644 --- a/crates/verified-garbage-asm-x86/src/fn_tables/mod.rs +++ b/crates/verified-garbage-asm-x86/src/fn_tables/mod.rs @@ -2,36 +2,36 @@ //! The tables of functions of the verified functions for `x86` (`Artifact.tables`). #![allow(dead_code, unused_imports)] -/// A table of functions of the interface `Hash` (`VG.FnTable`): the addresses of its 3 functions, then its 2 words of data. Only the statics of this module are of this type. +/// A table of functions of the interface `Hmac` (`VG.FnTable`): the addresses of its 3 functions, then its 3 words of data. Only the statics of this module are of this type. #[repr(C)] -pub struct Hash { +pub struct Hmac { fns: [*const (); 3], - data: [usize; 2], + data: [usize; 3], } -// SAFETY: a `Hash` is immutable, and the functions it holds the addresses of may be called from any thread. -unsafe impl Sync for Hash {} +// SAFETY: a `Hmac` is immutable, and the functions it holds the addresses of may be called from any thread. +unsafe impl Sync for Hmac {} -impl Hash { +impl Hmac { /// A pointer to the table's words, as the verified functions that take it read them. - pub const fn as_ptr(&'static self) -> *const [usize; 5] { + pub const fn as_ptr(&'static self) -> *const [usize; 6] { core::ptr::from_ref(self).cast() } } -/// A table of functions of the interface `Hmac` (`VG.FnTable`): the addresses of its 3 functions, then its 3 words of data. Only the statics of this module are of this type. +/// A table of functions of the interface `Hash` (`VG.FnTable`): the addresses of its 3 functions, then its 2 words of data. Only the statics of this module are of this type. #[repr(C)] -pub struct Hmac { +pub struct Hash { fns: [*const (); 3], - data: [usize; 3], + data: [usize; 2], } -// SAFETY: a `Hmac` is immutable, and the functions it holds the addresses of may be called from any thread. -unsafe impl Sync for Hmac {} +// SAFETY: a `Hash` is immutable, and the functions it holds the addresses of may be called from any thread. +unsafe impl Sync for Hash {} -impl Hmac { +impl Hash { /// A pointer to the table's words, as the verified functions that take it read them. - pub const fn as_ptr(&'static self) -> *const [usize; 6] { + pub const fn as_ptr(&'static self) -> *const [usize; 5] { core::ptr::from_ref(self).cast() } } @@ -54,121 +54,121 @@ impl MdHash { } #[rustfmt::skip] -mod vg_hash_md5; -pub use vg_hash_md5::VG_HASH_MD5; +mod vg_hmac_md5; +pub use vg_hmac_md5::VG_HMAC_MD5; #[rustfmt::skip] -mod vg_hash_sha1; -pub use vg_hash_sha1::VG_HASH_SHA1; +mod vg_hmac_sha1_shani; +pub use vg_hmac_sha1_shani::{VG_HMAC_SHA1_SHANI, VG_HMAC_SHA1_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha1_shani; -pub use vg_hash_sha1_shani::{VG_HASH_SHA1_SHANI, VG_HASH_SHA1_SHANI_FEATURES}; +mod vg_hmac_sha1; +pub use vg_hmac_sha1::VG_HMAC_SHA1; #[rustfmt::skip] -mod vg_hash_sha224; -pub use vg_hash_sha224::VG_HASH_SHA224; +mod vg_hmac_sha224_shani; +pub use vg_hmac_sha224_shani::{VG_HMAC_SHA224_SHANI, VG_HMAC_SHA224_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha224_shani; -pub use vg_hash_sha224_shani::{VG_HASH_SHA224_SHANI, VG_HASH_SHA224_SHANI_FEATURES}; +mod vg_hmac_sha224; +pub use vg_hmac_sha224::VG_HMAC_SHA224; #[rustfmt::skip] -mod vg_hash_sha256; -pub use vg_hash_sha256::VG_HASH_SHA256; +mod vg_hmac_sha256_shani; +pub use vg_hmac_sha256_shani::{VG_HMAC_SHA256_SHANI, VG_HMAC_SHA256_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha256_shani; -pub use vg_hash_sha256_shani::{VG_HASH_SHA256_SHANI, VG_HASH_SHA256_SHANI_FEATURES}; +mod vg_hmac_sha256; +pub use vg_hmac_sha256::VG_HMAC_SHA256; #[rustfmt::skip] -mod vg_hash_sha384; -pub use vg_hash_sha384::VG_HASH_SHA384; +mod vg_hmac_sha384; +pub use vg_hmac_sha384::VG_HMAC_SHA384; #[rustfmt::skip] -mod vg_hash_sha512; -pub use vg_hash_sha512::VG_HASH_SHA512; +mod vg_hmac_sha512; +pub use vg_hmac_sha512::VG_HMAC_SHA512; #[rustfmt::skip] -mod vg_hash_sha512_224; -pub use vg_hash_sha512_224::VG_HASH_SHA512_224; +mod vg_hmac_sha512_224; +pub use vg_hmac_sha512_224::VG_HMAC_SHA512_224; #[rustfmt::skip] -mod vg_hash_sha512_256; -pub use vg_hash_sha512_256::VG_HASH_SHA512_256; +mod vg_hmac_sha512_256; +pub use vg_hmac_sha512_256::VG_HMAC_SHA512_256; #[rustfmt::skip] -mod vg_hmac_md5; -pub use vg_hmac_md5::VG_HMAC_MD5; +mod vg_hash_md5; +pub use vg_hash_md5::VG_HASH_MD5; #[rustfmt::skip] -mod vg_hmac_sha1; -pub use vg_hmac_sha1::VG_HMAC_SHA1; +mod vg_hash_sha1_shani; +pub use vg_hash_sha1_shani::{VG_HASH_SHA1_SHANI, VG_HASH_SHA1_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha1_shani; -pub use vg_hmac_sha1_shani::{VG_HMAC_SHA1_SHANI, VG_HMAC_SHA1_SHANI_FEATURES}; +mod vg_hash_sha1; +pub use vg_hash_sha1::VG_HASH_SHA1; #[rustfmt::skip] -mod vg_hmac_sha224; -pub use vg_hmac_sha224::VG_HMAC_SHA224; +mod vg_hash_sha224_shani; +pub use vg_hash_sha224_shani::{VG_HASH_SHA224_SHANI, VG_HASH_SHA224_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha224_shani; -pub use vg_hmac_sha224_shani::{VG_HMAC_SHA224_SHANI, VG_HMAC_SHA224_SHANI_FEATURES}; +mod vg_hash_sha224; +pub use vg_hash_sha224::VG_HASH_SHA224; #[rustfmt::skip] -mod vg_hmac_sha256; -pub use vg_hmac_sha256::VG_HMAC_SHA256; +mod vg_hash_sha256_shani; +pub use vg_hash_sha256_shani::{VG_HASH_SHA256_SHANI, VG_HASH_SHA256_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha256_shani; -pub use vg_hmac_sha256_shani::{VG_HMAC_SHA256_SHANI, VG_HMAC_SHA256_SHANI_FEATURES}; +mod vg_hash_sha256; +pub use vg_hash_sha256::VG_HASH_SHA256; #[rustfmt::skip] -mod vg_hmac_sha384; -pub use vg_hmac_sha384::VG_HMAC_SHA384; +mod vg_hash_sha384; +pub use vg_hash_sha384::VG_HASH_SHA384; #[rustfmt::skip] -mod vg_hmac_sha512; -pub use vg_hmac_sha512::VG_HMAC_SHA512; +mod vg_hash_sha512; +pub use vg_hash_sha512::VG_HASH_SHA512; #[rustfmt::skip] -mod vg_hmac_sha512_224; -pub use vg_hmac_sha512_224::VG_HMAC_SHA512_224; +mod vg_hash_sha512_224; +pub use vg_hash_sha512_224::VG_HASH_SHA512_224; #[rustfmt::skip] -mod vg_hmac_sha512_256; -pub use vg_hmac_sha512_256::VG_HMAC_SHA512_256; +mod vg_hash_sha512_256; +pub use vg_hash_sha512_256::VG_HASH_SHA512_256; #[rustfmt::skip] mod vg_md_hash_md5; pub use vg_md_hash_md5::VG_MD_HASH_MD5; -#[rustfmt::skip] -mod vg_md_hash_sha1; -pub use vg_md_hash_sha1::VG_MD_HASH_SHA1; - #[rustfmt::skip] mod vg_md_hash_sha1_shani; pub use vg_md_hash_sha1_shani::{VG_MD_HASH_SHA1_SHANI, VG_MD_HASH_SHA1_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_md_hash_sha224; -pub use vg_md_hash_sha224::VG_MD_HASH_SHA224; +mod vg_md_hash_sha1; +pub use vg_md_hash_sha1::VG_MD_HASH_SHA1; #[rustfmt::skip] mod vg_md_hash_sha224_shani; pub use vg_md_hash_sha224_shani::{VG_MD_HASH_SHA224_SHANI, VG_MD_HASH_SHA224_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_md_hash_sha256; -pub use vg_md_hash_sha256::VG_MD_HASH_SHA256; +mod vg_md_hash_sha224; +pub use vg_md_hash_sha224::VG_MD_HASH_SHA224; #[rustfmt::skip] mod vg_md_hash_sha256_shani; pub use vg_md_hash_sha256_shani::{VG_MD_HASH_SHA256_SHANI, VG_MD_HASH_SHA256_SHANI_FEATURES}; +#[rustfmt::skip] +mod vg_md_hash_sha256; +pub use vg_md_hash_sha256::VG_MD_HASH_SHA256; + #[rustfmt::skip] mod vg_md_hash_sha384; pub use vg_md_hash_sha384::VG_MD_HASH_SHA384; diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/aes.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/aes.rs index f4dcbee0081..a9766f8740f 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/aes.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/aes.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Aes.expandKeyContract`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_expand_key_aesni` if the CPU supports its features (`VG_AES_EXPAND_KEY_AESNI_FEATURES`); or else `vg_aes_expand_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_expand_key_aesni` if the CPU supports its features (`VG_AES_EXPAND_KEY_AESNI_FEATURES`); or else `vg_aes_expand_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -56,7 +56,7 @@ pub unsafe fn vg_aes_expand_key_auto(_dit: verified_garbage_cpu::Dit<'_>, key: * /// /// Contract: `VG.Spec.Aes.encryptBlocksContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_encrypt_blocks_vaes` if the CPU supports its features (`VG_AES_ENCRYPT_BLOCKS_VAES_FEATURES`); `vg_aes_encrypt_blocks_aesni` if the CPU supports its features (`VG_AES_ENCRYPT_BLOCKS_AESNI_FEATURES`); or else `vg_aes_encrypt_blocks`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_encrypt_blocks_vaes` if the CPU supports its features (`VG_AES_ENCRYPT_BLOCKS_VAES_FEATURES`); `vg_aes_encrypt_blocks_aesni` if the CPU supports its features (`VG_AES_ENCRYPT_BLOCKS_AESNI_FEATURES`); or else `vg_aes_encrypt_blocks`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -112,7 +112,7 @@ pub unsafe fn vg_aes_encrypt_blocks_auto(_dit: verified_garbage_cpu::Dit<'_>, sc /// /// Contract: `VG.Spec.Aes.decryptBlocksContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_decrypt_blocks_vaes` if the CPU supports its features (`VG_AES_DECRYPT_BLOCKS_VAES_FEATURES`); `vg_aes_decrypt_blocks_aesni` if the CPU supports its features (`VG_AES_DECRYPT_BLOCKS_AESNI_FEATURES`); or else `vg_aes_decrypt_blocks`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_decrypt_blocks_vaes` if the CPU supports its features (`VG_AES_DECRYPT_BLOCKS_VAES_FEATURES`); `vg_aes_decrypt_blocks_aesni` if the CPU supports its features (`VG_AES_DECRYPT_BLOCKS_AESNI_FEATURES`); or else `vg_aes_decrypt_blocks`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_cbc.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_cbc.rs index 651999ab236..831a07b6095 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_cbc.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_cbc.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Cbc.aesEncryptContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cbc_encrypt_aesni` if the CPU supports its features (`VG_AES_CBC_ENCRYPT_AESNI_FEATURES`); `vg_aes_cbc_encrypt_avx` if the CPU supports its features (`VG_AES_CBC_ENCRYPT_AVX_FEATURES`); or else `vg_aes_cbc_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cbc_encrypt_aesni` if the CPU supports its features (`VG_AES_CBC_ENCRYPT_AESNI_FEATURES`); `vg_aes_cbc_encrypt_avx` if the CPU supports its features (`VG_AES_CBC_ENCRYPT_AVX_FEATURES`); or else `vg_aes_cbc_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -63,7 +63,7 @@ pub unsafe fn vg_aes_cbc_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, sched /// /// Contract: `VG.Spec.Cbc.aesDecryptContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cbc_decrypt_vaes_avx512` if the CPU supports its features (`VG_AES_CBC_DECRYPT_VAES_AVX512_FEATURES`); `vg_aes_cbc_decrypt_vaes` if the CPU supports its features (`VG_AES_CBC_DECRYPT_VAES_FEATURES`); `vg_aes_cbc_decrypt_aesni_avx` if the CPU supports its features (`VG_AES_CBC_DECRYPT_AESNI_AVX_FEATURES`); `vg_aes_cbc_decrypt_aesni` if the CPU supports its features (`VG_AES_CBC_DECRYPT_AESNI_FEATURES`); `vg_aes_cbc_decrypt_avx` if the CPU supports its features (`VG_AES_CBC_DECRYPT_AVX_FEATURES`); or else `vg_aes_cbc_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cbc_decrypt_vaes_avx512` if the CPU supports its features (`VG_AES_CBC_DECRYPT_VAES_AVX512_FEATURES`); `vg_aes_cbc_decrypt_vaes` if the CPU supports its features (`VG_AES_CBC_DECRYPT_VAES_FEATURES`); `vg_aes_cbc_decrypt_aesni_avx` if the CPU supports its features (`VG_AES_CBC_DECRYPT_AESNI_AVX_FEATURES`); `vg_aes_cbc_decrypt_aesni` if the CPU supports its features (`VG_AES_CBC_DECRYPT_AESNI_FEATURES`); `vg_aes_cbc_decrypt_avx` if the CPU supports its features (`VG_AES_CBC_DECRYPT_AVX_FEATURES`); or else `vg_aes_cbc_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_ccm.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_ccm.rs index 2c9d044ec5e..4252f731380 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_ccm.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_ccm.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ccm.sealContract`. Constant time: only the pointers, `rounds`, the lengths and `tag_len` may affect timing, not the key schedule, the nonce, the associated data or the payload. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ccm_seal_aesni` if the CPU supports its features (`VG_AES_CCM_SEAL_AESNI_FEATURES`); or else `vg_aes_ccm_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ccm_seal_aesni` if the CPU supports its features (`VG_AES_CCM_SEAL_AESNI_FEATURES`); or else `vg_aes_ccm_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -60,7 +60,7 @@ pub unsafe fn vg_aes_ccm_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule /// /// Contract: `VG.Spec.Ccm.openContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key schedule, the nonce, the associated data, the data or the tag. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ccm_open_aesni` if the CPU supports its features (`VG_AES_CCM_OPEN_AESNI_FEATURES`); or else `vg_aes_ccm_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ccm_open_aesni` if the CPU supports its features (`VG_AES_CCM_OPEN_AESNI_FEATURES`); or else `vg_aes_ccm_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_cfb.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_cfb.rs index 71fc08f2873..12786e506e6 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_cfb.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_cfb.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Cfb.aesEncryptContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cfb128_encrypt_vaes` if the CPU supports its features (`VG_AES_CFB128_ENCRYPT_VAES_FEATURES`); `vg_aes_cfb128_encrypt_aesni` if the CPU supports its features (`VG_AES_CFB128_ENCRYPT_AESNI_FEATURES`); or else `vg_aes_cfb128_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cfb128_encrypt_vaes` if the CPU supports its features (`VG_AES_CFB128_ENCRYPT_VAES_FEATURES`); `vg_aes_cfb128_encrypt_aesni` if the CPU supports its features (`VG_AES_CFB128_ENCRYPT_AESNI_FEATURES`); or else `vg_aes_cfb128_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -63,7 +63,7 @@ pub unsafe fn vg_aes_cfb128_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, sc /// /// Contract: `VG.Spec.Cfb.aesDecryptContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cfb128_decrypt_vaes` if the CPU supports its features (`VG_AES_CFB128_DECRYPT_VAES_FEATURES`); `vg_aes_cfb128_decrypt_aesni` if the CPU supports its features (`VG_AES_CFB128_DECRYPT_AESNI_FEATURES`); or else `vg_aes_cfb128_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cfb128_decrypt_vaes` if the CPU supports its features (`VG_AES_CFB128_DECRYPT_VAES_FEATURES`); `vg_aes_cfb128_decrypt_aesni` if the CPU supports its features (`VG_AES_CFB128_DECRYPT_AESNI_FEATURES`); or else `vg_aes_cfb128_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -120,7 +120,7 @@ pub unsafe fn vg_aes_cfb128_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, sc /// /// Contract: `VG.Spec.Cfb.aesEncryptFinalContract`. Constant time: only the pointers, `rounds` and `len` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cfb128_encrypt_final_vaes` if the CPU supports its features (`VG_AES_CFB128_ENCRYPT_FINAL_VAES_FEATURES`); `vg_aes_cfb128_encrypt_final_aesni` if the CPU supports its features (`VG_AES_CFB128_ENCRYPT_FINAL_AESNI_FEATURES`); or else `vg_aes_cfb128_encrypt_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cfb128_encrypt_final_vaes` if the CPU supports its features (`VG_AES_CFB128_ENCRYPT_FINAL_VAES_FEATURES`); `vg_aes_cfb128_encrypt_final_aesni` if the CPU supports its features (`VG_AES_CFB128_ENCRYPT_FINAL_AESNI_FEATURES`); or else `vg_aes_cfb128_encrypt_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -177,7 +177,7 @@ pub unsafe fn vg_aes_cfb128_encrypt_final_auto(_dit: verified_garbage_cpu::Dit<' /// /// Contract: `VG.Spec.Cfb.aesDecryptFinalContract`. Constant time: only the pointers, `rounds` and `len` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cfb128_decrypt_final_vaes` if the CPU supports its features (`VG_AES_CFB128_DECRYPT_FINAL_VAES_FEATURES`); `vg_aes_cfb128_decrypt_final_aesni` if the CPU supports its features (`VG_AES_CFB128_DECRYPT_FINAL_AESNI_FEATURES`); or else `vg_aes_cfb128_decrypt_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cfb128_decrypt_final_vaes` if the CPU supports its features (`VG_AES_CFB128_DECRYPT_FINAL_VAES_FEATURES`); `vg_aes_cfb128_decrypt_final_aesni` if the CPU supports its features (`VG_AES_CFB128_DECRYPT_FINAL_AESNI_FEATURES`); or else `vg_aes_cfb128_decrypt_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_cfb8.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_cfb8.rs index 81b07d0bb17..e6a9ece738d 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_cfb8.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_cfb8.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Cfb8.aesEncryptContract`. Constant time: only the pointers, `rounds` and `len` may affect timing, not the key schedule, the input block or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cfb8_encrypt_vaes` if the CPU supports its features (`VG_AES_CFB8_ENCRYPT_VAES_FEATURES`); `vg_aes_cfb8_encrypt_aesni` if the CPU supports its features (`VG_AES_CFB8_ENCRYPT_AESNI_FEATURES`); or else `vg_aes_cfb8_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cfb8_encrypt_vaes` if the CPU supports its features (`VG_AES_CFB8_ENCRYPT_VAES_FEATURES`); `vg_aes_cfb8_encrypt_aesni` if the CPU supports its features (`VG_AES_CFB8_ENCRYPT_AESNI_FEATURES`); or else `vg_aes_cfb8_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -63,7 +63,7 @@ pub unsafe fn vg_aes_cfb8_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, sche /// /// Contract: `VG.Spec.Cfb8.aesDecryptContract`. Constant time: only the pointers, `rounds` and `len` may affect timing, not the key schedule, the input block or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_cfb8_decrypt_vaes` if the CPU supports its features (`VG_AES_CFB8_DECRYPT_VAES_FEATURES`); `vg_aes_cfb8_decrypt_aesni` if the CPU supports its features (`VG_AES_CFB8_DECRYPT_AESNI_FEATURES`); or else `vg_aes_cfb8_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_cfb8_decrypt_vaes` if the CPU supports its features (`VG_AES_CFB8_DECRYPT_VAES_FEATURES`); `vg_aes_cfb8_decrypt_aesni` if the CPU supports its features (`VG_AES_CFB8_DECRYPT_AESNI_FEATURES`); or else `vg_aes_cfb8_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_ctr.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_ctr.rs index e815538cd57..e2e7eb9510a 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_ctr.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_ctr.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ctr.aesContract`. Constant time: only the pointers, `rounds`, `n` and the last four bytes of `*ctr` (the 32-bit counter, public in OpenSSL, BoringSSL and AWS-LC, which split the blocks where it wraps) may affect timing, not the key schedule, the rest of the counter block or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ctr_vaes` if the CPU supports its features (`VG_AES_CTR_VAES_FEATURES`); `vg_aes_ctr_aesni` if the CPU supports its features (`VG_AES_CTR_AESNI_FEATURES`); or else `vg_aes_ctr`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ctr_vaes` if the CPU supports its features (`VG_AES_CTR_VAES_FEATURES`); `vg_aes_ctr_aesni` if the CPU supports its features (`VG_AES_CTR_AESNI_FEATURES`); or else `vg_aes_ctr`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -63,7 +63,7 @@ pub unsafe fn vg_aes_ctr_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *co /// /// Contract: `VG.Spec.Ctr.aesFinalContract`. Constant time: only the pointers, `rounds`, `len` and the last four bytes of `*ctr` (the 32-bit counter, public in OpenSSL, BoringSSL and AWS-LC, which split the blocks where it wraps) may affect timing, not the key schedule, the rest of the counter block or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ctr_final_vaes` if the CPU supports its features (`VG_AES_CTR_FINAL_VAES_FEATURES`); `vg_aes_ctr_final_aesni` if the CPU supports its features (`VG_AES_CTR_FINAL_AESNI_FEATURES`); or else `vg_aes_ctr_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ctr_final_vaes` if the CPU supports its features (`VG_AES_CTR_FINAL_VAES_FEATURES`); `vg_aes_ctr_final_aesni` if the CPU supports its features (`VG_AES_CTR_FINAL_AESNI_FEATURES`); or else `vg_aes_ctr_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_ecb.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_ecb.rs index 11f1610eeab..250c2c1cf7d 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_ecb.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_ecb.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.AesEcb.encryptContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ecb_encrypt_vaes` if the CPU supports its features (`VG_AES_ECB_ENCRYPT_VAES_FEATURES`); `vg_aes_ecb_encrypt_aesni` if the CPU supports its features (`VG_AES_ECB_ENCRYPT_AESNI_FEATURES`); `vg_aes_ecb_encrypt_avx` if the CPU supports its features (`VG_AES_ECB_ENCRYPT_AVX_FEATURES`); or else `vg_aes_ecb_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ecb_encrypt_vaes` if the CPU supports its features (`VG_AES_ECB_ENCRYPT_VAES_FEATURES`); `vg_aes_ecb_encrypt_aesni` if the CPU supports its features (`VG_AES_ECB_ENCRYPT_AESNI_FEATURES`); `vg_aes_ecb_encrypt_avx` if the CPU supports its features (`VG_AES_ECB_ENCRYPT_AVX_FEATURES`); or else `vg_aes_ecb_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -66,7 +66,7 @@ pub unsafe fn vg_aes_ecb_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, sched /// /// Contract: `VG.Spec.AesEcb.decryptContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ecb_decrypt_vaes` if the CPU supports its features (`VG_AES_ECB_DECRYPT_VAES_FEATURES`); `vg_aes_ecb_decrypt_aesni_avx` if the CPU supports its features (`VG_AES_ECB_DECRYPT_AESNI_AVX_FEATURES`); `vg_aes_ecb_decrypt_aesni` if the CPU supports its features (`VG_AES_ECB_DECRYPT_AESNI_FEATURES`); `vg_aes_ecb_decrypt_avx` if the CPU supports its features (`VG_AES_ECB_DECRYPT_AVX_FEATURES`); or else `vg_aes_ecb_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ecb_decrypt_vaes` if the CPU supports its features (`VG_AES_ECB_DECRYPT_VAES_FEATURES`); `vg_aes_ecb_decrypt_aesni_avx` if the CPU supports its features (`VG_AES_ECB_DECRYPT_AESNI_AVX_FEATURES`); `vg_aes_ecb_decrypt_aesni` if the CPU supports its features (`VG_AES_ECB_DECRYPT_AESNI_FEATURES`); `vg_aes_ecb_decrypt_avx` if the CPU supports its features (`VG_AES_ECB_DECRYPT_AVX_FEATURES`); or else `vg_aes_ecb_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_gcm_siv.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_gcm_siv.rs index 563e68f4629..b19dbc7188e 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_gcm_siv.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_gcm_siv.rs @@ -8,7 +8,7 @@ /// /// Contract: `VG.Spec.GcmSiv.sealContract`. Constant time: only the pointers, `rounds` and the lengths may affect timing, not the key schedule, the nonce, the additional data or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_siv_seal_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_gcm_siv_seal_vaes_pclmul_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_VAES_PCLMUL_AVX2_FEATURES`); `vg_aes_gcm_siv_seal_aesni_vpclmul_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AESNI_VPCLMUL_AVX2_FEATURES`); `vg_aes_gcm_siv_seal_aesni_pclmul_avx_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AESNI_PCLMUL_AVX_AVX2_FEATURES`); `vg_aes_gcm_siv_seal_aesni_pclmul_avx` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AESNI_PCLMUL_AVX_FEATURES`); `vg_aes_gcm_siv_seal_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_siv_seal_vaes_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_VAES_AVX2_FEATURES`); `vg_aes_gcm_siv_seal_aesni_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AESNI_AVX2_FEATURES`); `vg_aes_gcm_siv_seal_aesni` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AESNI_FEATURES`); `vg_aes_gcm_siv_seal_vpclmul_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_VPCLMUL_AVX2_FEATURES`); `vg_aes_gcm_siv_seal_pclmul_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_PCLMUL_AVX2_FEATURES`); `vg_aes_gcm_siv_seal_pclmul` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_PCLMUL_FEATURES`); `vg_aes_gcm_siv_seal_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AVX2_FEATURES`); or else `vg_aes_gcm_siv_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_siv_seal_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_gcm_siv_seal_vaes_pclmul_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_VAES_PCLMUL_AVX2_FEATURES`); `vg_aes_gcm_siv_seal_aesni_vpclmul_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AESNI_VPCLMUL_AVX2_FEATURES`); `vg_aes_gcm_siv_seal_aesni_pclmul_avx_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AESNI_PCLMUL_AVX_AVX2_FEATURES`); `vg_aes_gcm_siv_seal_aesni_pclmul_avx` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AESNI_PCLMUL_AVX_FEATURES`); `vg_aes_gcm_siv_seal_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_siv_seal_vaes_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_VAES_AVX2_FEATURES`); `vg_aes_gcm_siv_seal_aesni_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AESNI_AVX2_FEATURES`); `vg_aes_gcm_siv_seal_aesni` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AESNI_FEATURES`); `vg_aes_gcm_siv_seal_vpclmul_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_VPCLMUL_AVX2_FEATURES`); `vg_aes_gcm_siv_seal_pclmul_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_PCLMUL_AVX2_FEATURES`); `vg_aes_gcm_siv_seal_pclmul` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_PCLMUL_FEATURES`); `vg_aes_gcm_siv_seal_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_SEAL_AVX2_FEATURES`); or else `vg_aes_gcm_siv_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -122,7 +122,7 @@ pub unsafe fn vg_aes_gcm_siv_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, sche /// /// Contract: `VG.Spec.GcmSiv.openContract`. Constant time but for the result: only the pointers, `rounds`, the lengths and whether the function returns 1 or 0 may affect timing, not the key schedule, the nonce, the additional data, the data or the tag. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_siv_open_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_gcm_siv_open_vaes_pclmul_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_VAES_PCLMUL_AVX2_FEATURES`); `vg_aes_gcm_siv_open_aesni_vpclmul_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AESNI_VPCLMUL_AVX2_FEATURES`); `vg_aes_gcm_siv_open_aesni_pclmul_avx_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AESNI_PCLMUL_AVX_AVX2_FEATURES`); `vg_aes_gcm_siv_open_aesni_pclmul_avx` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AESNI_PCLMUL_AVX_FEATURES`); `vg_aes_gcm_siv_open_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_siv_open_vaes_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_VAES_AVX2_FEATURES`); `vg_aes_gcm_siv_open_aesni_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AESNI_AVX2_FEATURES`); `vg_aes_gcm_siv_open_aesni` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AESNI_FEATURES`); `vg_aes_gcm_siv_open_vpclmul_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_VPCLMUL_AVX2_FEATURES`); `vg_aes_gcm_siv_open_pclmul_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_PCLMUL_AVX2_FEATURES`); `vg_aes_gcm_siv_open_pclmul` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_PCLMUL_FEATURES`); `vg_aes_gcm_siv_open_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AVX2_FEATURES`); or else `vg_aes_gcm_siv_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_siv_open_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_gcm_siv_open_vaes_pclmul_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_VAES_PCLMUL_AVX2_FEATURES`); `vg_aes_gcm_siv_open_aesni_vpclmul_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AESNI_VPCLMUL_AVX2_FEATURES`); `vg_aes_gcm_siv_open_aesni_pclmul_avx_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AESNI_PCLMUL_AVX_AVX2_FEATURES`); `vg_aes_gcm_siv_open_aesni_pclmul_avx` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AESNI_PCLMUL_AVX_FEATURES`); `vg_aes_gcm_siv_open_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_siv_open_vaes_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_VAES_AVX2_FEATURES`); `vg_aes_gcm_siv_open_aesni_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AESNI_AVX2_FEATURES`); `vg_aes_gcm_siv_open_aesni` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AESNI_FEATURES`); `vg_aes_gcm_siv_open_vpclmul_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_VPCLMUL_AVX2_FEATURES`); `vg_aes_gcm_siv_open_pclmul_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_PCLMUL_AVX2_FEATURES`); `vg_aes_gcm_siv_open_pclmul` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_PCLMUL_FEATURES`); `vg_aes_gcm_siv_open_avx2` if the CPU supports its features (`VG_AES_GCM_SIV_OPEN_AVX2_FEATURES`); or else `vg_aes_gcm_siv_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_ocb.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_ocb.rs index 46e49de00f0..72c9f438756 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_ocb.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_ocb.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ocb.initContract`. The key context is `VG.Spec.Ocb.KeyRepr`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ocb_init_vaes` if the CPU supports its features (`VG_AES_OCB_INIT_VAES_FEATURES`); `vg_aes_ocb_init_aesni` if the CPU supports its features (`VG_AES_OCB_INIT_AESNI_FEATURES`); or else `vg_aes_ocb_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ocb_init_vaes` if the CPU supports its features (`VG_AES_OCB_INIT_VAES_FEATURES`); `vg_aes_ocb_init_aesni` if the CPU supports its features (`VG_AES_OCB_INIT_AESNI_FEATURES`); or else `vg_aes_ocb_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -60,7 +60,7 @@ pub unsafe fn vg_aes_ocb_init_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *co /// /// Contract: `VG.Spec.Ocb.sealContract`. Constant time: only the pointers, `rounds`, the lengths and `tag_len` may affect timing, not the key context, the nonce, the associated data or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ocb_seal_vaes` if the CPU supports its features (`VG_AES_OCB_SEAL_VAES_FEATURES`); `vg_aes_ocb_seal_aesni` if the CPU supports its features (`VG_AES_OCB_SEAL_AESNI_FEATURES`); or else `vg_aes_ocb_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ocb_seal_vaes` if the CPU supports its features (`VG_AES_OCB_SEAL_VAES_FEATURES`); `vg_aes_ocb_seal_aesni` if the CPU supports its features (`VG_AES_OCB_SEAL_AESNI_FEATURES`); or else `vg_aes_ocb_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -118,7 +118,7 @@ pub unsafe fn vg_aes_ocb_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *co /// /// Contract: `VG.Spec.Ocb.openContract`. Constant time but for the result: only the pointers, `rounds`, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the associated data, the data or the tag. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ocb_open_vaes` if the CPU supports its features (`VG_AES_OCB_OPEN_VAES_FEATURES`); `vg_aes_ocb_open_aesni` if the CPU supports its features (`VG_AES_OCB_OPEN_AESNI_FEATURES`); or else `vg_aes_ocb_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ocb_open_vaes` if the CPU supports its features (`VG_AES_OCB_OPEN_VAES_FEATURES`); `vg_aes_ocb_open_aesni` if the CPU supports its features (`VG_AES_OCB_OPEN_AESNI_FEATURES`); or else `vg_aes_ocb_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_ofb.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_ofb.rs index c6a86dda744..a5b38b149ea 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_ofb.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_ofb.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ofb.aesContract`. Constant time: only the pointers, `rounds` and `n` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ofb_vaes` if the CPU supports its features (`VG_AES_OFB_VAES_FEATURES`); `vg_aes_ofb_aesni` if the CPU supports its features (`VG_AES_OFB_AESNI_FEATURES`); or else `vg_aes_ofb`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ofb_vaes` if the CPU supports its features (`VG_AES_OFB_VAES_FEATURES`); `vg_aes_ofb_aesni` if the CPU supports its features (`VG_AES_OFB_AESNI_FEATURES`); or else `vg_aes_ofb`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -63,7 +63,7 @@ pub unsafe fn vg_aes_ofb_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *co /// /// Contract: `VG.Spec.Ofb.aesFinalContract`. Constant time: only the pointers, `rounds` and `len` may affect timing, not the key schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_ofb_final_vaes` if the CPU supports its features (`VG_AES_OFB_FINAL_VAES_FEATURES`); `vg_aes_ofb_final_aesni` if the CPU supports its features (`VG_AES_OFB_FINAL_AESNI_FEATURES`); or else `vg_aes_ofb_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_ofb_final_vaes` if the CPU supports its features (`VG_AES_OFB_FINAL_VAES_FEATURES`); `vg_aes_ofb_final_aesni` if the CPU supports its features (`VG_AES_OFB_FINAL_AESNI_FEATURES`); or else `vg_aes_ofb_final`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_siv.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_siv.rs index b71202fffd3..625cf5cc2c4 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_siv.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_siv.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Siv.initContract`. The key context is `VG.Spec.Siv.KeyRepr`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_siv_init_vaes` if the CPU supports its features (`VG_AES_SIV_INIT_VAES_FEATURES`); `vg_aes_siv_init_aesni` if the CPU supports its features (`VG_AES_SIV_INIT_AESNI_FEATURES`); or else `vg_aes_siv_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_siv_init_vaes` if the CPU supports its features (`VG_AES_SIV_INIT_VAES_FEATURES`); `vg_aes_siv_init_aesni` if the CPU supports its features (`VG_AES_SIV_INIT_AESNI_FEATURES`); or else `vg_aes_siv_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -60,7 +60,7 @@ pub unsafe fn vg_aes_siv_init_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *co /// /// Contract: `VG.Spec.Siv.encryptContract`. Constant time: only the pointers, `rounds`, `ads_count`, `len` and where the components are (their addresses and lengths) may affect timing, not the key context, the associated data or the plaintext. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_siv_encrypt_aesni` if the CPU supports its features (`VG_AES_SIV_ENCRYPT_AESNI_FEATURES`); or else `vg_aes_siv_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_siv_encrypt_aesni` if the CPU supports its features (`VG_AES_SIV_ENCRYPT_AESNI_FEATURES`); or else `vg_aes_siv_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -111,7 +111,7 @@ pub unsafe fn vg_aes_siv_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: /// /// Contract: `VG.Spec.Siv.decryptContract`. Constant time but for the result: only the pointers, `rounds`, `ads_count`, `len`, where the components are (their addresses and lengths) and whether the function returns 1 or 0 may affect timing, not the key context, the associated data, the data or `V`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_siv_decrypt_aesni` if the CPU supports its features (`VG_AES_SIV_DECRYPT_AESNI_FEATURES`); or else `vg_aes_siv_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_siv_decrypt_aesni` if the CPU supports its features (`VG_AES_SIV_DECRYPT_AESNI_FEATURES`); or else `vg_aes_siv_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_xts.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_xts.rs index 4e18ae9dda9..f9c4fb04f8a 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/aes_xts.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/aes_xts.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Xts.aesEncryptUnitContract`. Constant time: only the pointers, `rounds` and `len` may affect timing, not the key schedules, the tweak value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_xts_encrypt_unit_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_xts_encrypt_unit_vaes_vpclmul` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_VAES_VPCLMUL_FEATURES`); `vg_aes_xts_encrypt_unit_aesni_pclmul` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_AESNI_PCLMUL_FEATURES`); `vg_aes_xts_encrypt_unit_vaes` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_VAES_FEATURES`); `vg_aes_xts_encrypt_unit_aesni` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_AESNI_FEATURES`); or else `vg_aes_xts_encrypt_unit`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_xts_encrypt_unit_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_xts_encrypt_unit_vaes_vpclmul` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_VAES_VPCLMUL_FEATURES`); `vg_aes_xts_encrypt_unit_aesni_pclmul` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_AESNI_PCLMUL_FEATURES`); `vg_aes_xts_encrypt_unit_vaes` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_VAES_FEATURES`); `vg_aes_xts_encrypt_unit_aesni` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_AESNI_FEATURES`); or else `vg_aes_xts_encrypt_unit`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -80,7 +80,7 @@ pub unsafe fn vg_aes_xts_encrypt_unit_auto(_dit: verified_garbage_cpu::Dit<'_>, /// /// Contract: `VG.Spec.Xts.aesDecryptUnitContract`. Constant time: only the pointers, `rounds` and `len` may affect timing, not the key schedules, the tweak value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_xts_decrypt_unit_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_xts_decrypt_unit_vaes_vpclmul` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_VAES_VPCLMUL_FEATURES`); `vg_aes_xts_decrypt_unit_aesni_pclmul` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_AESNI_PCLMUL_FEATURES`); `vg_aes_xts_decrypt_unit_vaes` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_VAES_FEATURES`); `vg_aes_xts_decrypt_unit_aesni` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_AESNI_FEATURES`); or else `vg_aes_xts_decrypt_unit`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_xts_decrypt_unit_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_xts_decrypt_unit_vaes_vpclmul` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_VAES_VPCLMUL_FEATURES`); `vg_aes_xts_decrypt_unit_aesni_pclmul` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_AESNI_PCLMUL_FEATURES`); `vg_aes_xts_decrypt_unit_vaes` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_VAES_FEATURES`); `vg_aes_xts_decrypt_unit_aesni` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_AESNI_FEATURES`); or else `vg_aes_xts_decrypt_unit`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -154,7 +154,7 @@ pub unsafe fn vg_aes_xts_decrypt_unit_auto(_dit: verified_garbage_cpu::Dit<'_>, /// /// Contract: `VG.Spec.Xts.aesEncryptUnitToContract`. Constant time: only the pointers, `rounds`, `len` and `dst_len` may affect timing, not the key schedules, the tweak value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_xts_encrypt_unit_to_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_TO_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_xts_encrypt_unit_to_vaes_vpclmul` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_TO_VAES_VPCLMUL_FEATURES`); `vg_aes_xts_encrypt_unit_to_aesni_pclmul` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_TO_AESNI_PCLMUL_FEATURES`); `vg_aes_xts_encrypt_unit_to_vaes` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_TO_VAES_FEATURES`); `vg_aes_xts_encrypt_unit_to_aesni` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_TO_AESNI_FEATURES`); or else `vg_aes_xts_encrypt_unit_to`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_xts_encrypt_unit_to_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_TO_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_xts_encrypt_unit_to_vaes_vpclmul` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_TO_VAES_VPCLMUL_FEATURES`); `vg_aes_xts_encrypt_unit_to_aesni_pclmul` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_TO_AESNI_PCLMUL_FEATURES`); `vg_aes_xts_encrypt_unit_to_vaes` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_TO_VAES_FEATURES`); `vg_aes_xts_encrypt_unit_to_aesni` if the CPU supports its features (`VG_AES_XTS_ENCRYPT_UNIT_TO_AESNI_FEATURES`); or else `vg_aes_xts_encrypt_unit_to`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -230,7 +230,7 @@ pub unsafe fn vg_aes_xts_encrypt_unit_to_auto(_dit: verified_garbage_cpu::Dit<'_ /// /// Contract: `VG.Spec.Xts.aesDecryptUnitToContract`. Constant time: only the pointers, `rounds`, `len` and `dst_len` may affect timing, not the key schedules, the tweak value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_xts_decrypt_unit_to_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_TO_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_xts_decrypt_unit_to_vaes_vpclmul` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_TO_VAES_VPCLMUL_FEATURES`); `vg_aes_xts_decrypt_unit_to_aesni_pclmul` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_TO_AESNI_PCLMUL_FEATURES`); `vg_aes_xts_decrypt_unit_to_vaes` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_TO_VAES_FEATURES`); `vg_aes_xts_decrypt_unit_to_aesni` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_TO_AESNI_FEATURES`); or else `vg_aes_xts_decrypt_unit_to`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_xts_decrypt_unit_to_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_TO_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_xts_decrypt_unit_to_vaes_vpclmul` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_TO_VAES_VPCLMUL_FEATURES`); `vg_aes_xts_decrypt_unit_to_aesni_pclmul` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_TO_AESNI_PCLMUL_FEATURES`); `vg_aes_xts_decrypt_unit_to_vaes` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_TO_VAES_FEATURES`); `vg_aes_xts_decrypt_unit_to_aesni` if the CPU supports its features (`VG_AES_XTS_DECRYPT_UNIT_TO_AESNI_FEATURES`); or else `vg_aes_xts_decrypt_unit_to`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/argon2.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/argon2.rs index 502bdd17ac3..8b580b3f645 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/argon2.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/argon2.rs @@ -8,7 +8,7 @@ /// /// Contract: `VG.Spec.Argon2.deriveContract`. Argon2i is constant time in all input contents. Argon2d/id additionally permit timing to depend on the sequence of data-dependent reference block indices (`VG.Spec.Argon2.references`), as required by their addressing rules, but on nothing else secret. Pointers, lengths and numeric parameters are public. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_argon2_blake2b_avx512_g_avx512` if the CPU supports its features (`VG_ARGON2_BLAKE2B_AVX512_G_AVX512_FEATURES`); `vg_argon2_blake2b_avx2_g_avx512` if the CPU supports its features (`VG_ARGON2_BLAKE2B_AVX2_G_AVX512_FEATURES`); `vg_argon2_blake2b_avx2_g_avx2` if the CPU supports its features (`VG_ARGON2_BLAKE2B_AVX2_G_AVX2_FEATURES`); or else `vg_argon2`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_argon2_blake2b_avx512_g_avx512` if the CPU supports its features (`VG_ARGON2_BLAKE2B_AVX512_G_AVX512_FEATURES`); `vg_argon2_blake2b_avx2_g_avx512` if the CPU supports its features (`VG_ARGON2_BLAKE2B_AVX2_G_AVX512_FEATURES`); `vg_argon2_blake2b_avx2_g_avx2` if the CPU supports its features (`VG_ARGON2_BLAKE2B_AVX2_G_AVX2_FEATURES`); or else `vg_argon2`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/blake2b.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/blake2b.rs index d870deac944..e9c82976b35 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/blake2b.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/blake2b.rs @@ -26,7 +26,7 @@ pub unsafe fn vg_blake2b_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: * /// /// Contract: `VG.Spec.Blake2.updateBContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_blake2b_update_avx512` if the CPU supports its features (`VG_BLAKE2B_UPDATE_AVX512_FEATURES`); `vg_blake2b_update_avx2` if the CPU supports its features (`VG_BLAKE2B_UPDATE_AVX2_FEATURES`); or else `vg_blake2b_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_blake2b_update_avx512` if the CPU supports its features (`VG_BLAKE2B_UPDATE_AVX512_FEATURES`); `vg_blake2b_update_avx2` if the CPU supports its features (`VG_BLAKE2B_UPDATE_AVX2_FEATURES`); or else `vg_blake2b_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -80,7 +80,7 @@ pub unsafe fn vg_blake2b_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: /// /// Contract: `VG.Spec.Blake2.finalizeBContract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_blake2b_finalize_avx512` if the CPU supports its features (`VG_BLAKE2B_FINALIZE_AVX512_FEATURES`); `vg_blake2b_finalize_avx2` if the CPU supports its features (`VG_BLAKE2B_FINALIZE_AVX2_FEATURES`); or else `vg_blake2b_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_blake2b_finalize_avx512` if the CPU supports its features (`VG_BLAKE2B_FINALIZE_AVX512_FEATURES`); `vg_blake2b_finalize_avx2` if the CPU supports its features (`VG_BLAKE2B_FINALIZE_AVX2_FEATURES`); or else `vg_blake2b_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/blake2s.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/blake2s.rs index 1f7f9c78d42..a6e1a9f2df8 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/blake2s.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/blake2s.rs @@ -26,7 +26,7 @@ pub unsafe fn vg_blake2s_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: * /// /// Contract: `VG.Spec.Blake2.updateSContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_blake2s_update_avx` if the CPU supports its features (`VG_BLAKE2S_UPDATE_AVX_FEATURES`); or else `vg_blake2s_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_blake2s_update_avx` if the CPU supports its features (`VG_BLAKE2S_UPDATE_AVX_FEATURES`); or else `vg_blake2s_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -75,7 +75,7 @@ pub unsafe fn vg_blake2s_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: /// /// Contract: `VG.Spec.Blake2.finalizeSContract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_blake2s_finalize_avx` if the CPU supports its features (`VG_BLAKE2S_FINALIZE_AVX_FEATURES`); or else `vg_blake2s_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_blake2s_finalize_avx` if the CPU supports its features (`VG_BLAKE2S_FINALIZE_AVX_FEATURES`); or else `vg_blake2s_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/chacha20.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/chacha20.rs index deb6f2c4061..ab092045585 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/chacha20.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/chacha20.rs @@ -22,11 +22,30 @@ pub unsafe fn vg_chacha20_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: unsafe { crate::chacha20::vg_chacha20_init(state, key, nonce) } } +/// Restarts a ChaCha20 keystream with the same key: makes the streaming state `*state` represent the whole keystream of its key for the 16-byte nonce `*nonce` (as `vg_chacha20_init` does), discarding what was left of the previous one. +/// +/// Contract: `VG.Spec.ChaCha20.setNonceContract`. Constant time: only the pointers may affect timing, not the key or the nonce. +/// +/// It runs `vg_chacha20_set_nonce`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 768 bytes. +/// * `nonce` must be valid for reads of 16 bytes. +/// * `state` must not overlap `nonce` (distinct Rust objects never do). +/// * Neither `state` nor `nonce` may overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_chacha20_set_nonce_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 96], nonce: *const [u8; 16]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::chacha20::vg_chacha20_set_nonce(state, nonce) } +} + /// Applies a ChaCha20 keystream (RFC 8439 §2.4), encrypting or decrypting: if at least `len` bytes are left of the keystream that the streaming state `*state` represents, XORs the next `len` of them into the `len` bytes at `data`, keeps the rest in `*state`, and returns 1. Otherwise (the block counter would pass 2³² − 1) returns 0, and leaves the bytes at `data` and the keystream unchanged. /// /// Contract: `VG.Spec.ChaCha20.applyContract`. Constant time: only the pointers, `len` and the number of bytes of keystream left in `*state` (`VG.Spec.ChaCha20.leftAt`: 64 × (2³² − `c`) for the initial block counter `c`, less the bytes applied since) may affect timing, not the key, the nonce, the keystream or the data. The function may leak that number. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_chacha20_apply_avx512vl` if the CPU supports its features (`VG_CHACHA20_APPLY_AVX512VL_FEATURES`); `vg_chacha20_apply_avx512` if the CPU supports its features (`VG_CHACHA20_APPLY_AVX512_FEATURES`); `vg_chacha20_apply_avx2` if the CPU supports its features (`VG_CHACHA20_APPLY_AVX2_FEATURES`); `vg_chacha20_apply_ssse3` if the CPU supports its features (`VG_CHACHA20_APPLY_SSSE3_FEATURES`); or else `vg_chacha20_apply`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_chacha20_apply_avx512vl` if the CPU supports its features (`VG_CHACHA20_APPLY_AVX512VL_FEATURES`); `vg_chacha20_apply_avx512` if the CPU supports its features (`VG_CHACHA20_APPLY_AVX512_FEATURES`); `vg_chacha20_apply_avx2` if the CPU supports its features (`VG_CHACHA20_APPLY_AVX2_FEATURES`); `vg_chacha20_apply_ssse3` if the CPU supports its features (`VG_CHACHA20_APPLY_SSSE3_FEATURES`); or else `vg_chacha20_apply`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -84,22 +103,3 @@ pub unsafe fn vg_chacha20_apply_auto(_dit: verified_garbage_cpu::Dit<'_>, state: // either carries the caller's obligations, which are those of every implementation. unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, data, len) } } - -/// Restarts a ChaCha20 keystream with the same key: makes the streaming state `*state` represent the whole keystream of its key for the 16-byte nonce `*nonce` (as `vg_chacha20_init` does), discarding what was left of the previous one. -/// -/// Contract: `VG.Spec.ChaCha20.setNonceContract`. Constant time: only the pointers may affect timing, not the key or the nonce. -/// -/// It runs `vg_chacha20_set_nonce`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 768 bytes. -/// * `nonce` must be valid for reads of 16 bytes. -/// * `state` must not overlap `nonce` (distinct Rust objects never do). -/// * Neither `state` nor `nonce` may overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_chacha20_set_nonce_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 96], nonce: *const [u8; 16]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::chacha20::vg_chacha20_set_nonce(state, nonce) } -} diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/chacha20poly1305.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/chacha20poly1305.rs index 228dfdaccee..48329418469 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/chacha20poly1305.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/chacha20poly1305.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.ChaCha20Poly1305.sealContract`. Constant time: only the pointers and the lengths may affect timing, not the key, the nonce or the data. The block counter wraps around beyond 2³²-1 blocks of data (RFC 8439's `P_MAX`), which the caller must not exceed for the construction to be secure. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_chacha20_poly1305_seal_ifma` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_IFMA_FEATURES`); `vg_chacha20_poly1305_seal_avx512vl` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_AVX512VL_FEATURES`); `vg_chacha20_poly1305_seal_avx512` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_AVX512_FEATURES`); `vg_chacha20_poly1305_seal_avx2` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_AVX2_FEATURES`); `vg_chacha20_poly1305_seal_ssse3` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_SSSE3_FEATURES`); or else `vg_chacha20_poly1305_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_chacha20_poly1305_seal_ifma` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_IFMA_FEATURES`); `vg_chacha20_poly1305_seal_avx512vl` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_AVX512VL_FEATURES`); `vg_chacha20_poly1305_seal_avx512` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_AVX512_FEATURES`); `vg_chacha20_poly1305_seal_avx2` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_AVX2_FEATURES`); `vg_chacha20_poly1305_seal_ssse3` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_SSSE3_FEATURES`); or else `vg_chacha20_poly1305_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -77,7 +77,7 @@ pub unsafe fn vg_chacha20_poly1305_seal_auto(_dit: verified_garbage_cpu::Dit<'_> /// /// Contract: `VG.Spec.ChaCha20Poly1305.openZeroContract`. Constant time but for the result: only the pointers, the lengths and whether the function returns 1 or 0 may affect timing, not the key, the nonce, the additional data, the tag or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_chacha20_poly1305_open_ifma` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_IFMA_FEATURES`); `vg_chacha20_poly1305_open_avx512vl` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_AVX512VL_FEATURES`); `vg_chacha20_poly1305_open_avx512` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_AVX512_FEATURES`); `vg_chacha20_poly1305_open_avx2` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_AVX2_FEATURES`); `vg_chacha20_poly1305_open_ssse3` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_SSSE3_FEATURES`); or else `vg_chacha20_poly1305_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_chacha20_poly1305_open_ifma` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_IFMA_FEATURES`); `vg_chacha20_poly1305_open_avx512vl` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_AVX512VL_FEATURES`); `vg_chacha20_poly1305_open_avx512` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_AVX512_FEATURES`); `vg_chacha20_poly1305_open_avx2` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_AVX2_FEATURES`); `vg_chacha20_poly1305_open_ssse3` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_SSSE3_FEATURES`); or else `vg_chacha20_poly1305_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -148,7 +148,7 @@ pub unsafe fn vg_chacha20_poly1305_open_auto(_dit: verified_garbage_cpu::Dit<'_> /// /// Contract: `VG.Spec.ChaCha20Poly1305.openToContract`. Constant time but for the result: only the pointers, the lengths and whether the function returns 1 or 0 may affect timing, not the key, the nonce, the additional data, the tag or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_chacha20_poly1305_open_to_ifma` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_TO_IFMA_FEATURES`); `vg_chacha20_poly1305_open_to_avx512vl` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_TO_AVX512VL_FEATURES`); `vg_chacha20_poly1305_open_to_avx512` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_TO_AVX512_FEATURES`); `vg_chacha20_poly1305_open_to_avx2` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_TO_AVX2_FEATURES`); `vg_chacha20_poly1305_open_to_ssse3` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_TO_SSSE3_FEATURES`); or else `vg_chacha20_poly1305_open_to`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_chacha20_poly1305_open_to_ifma` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_TO_IFMA_FEATURES`); `vg_chacha20_poly1305_open_to_avx512vl` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_TO_AVX512VL_FEATURES`); `vg_chacha20_poly1305_open_to_avx512` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_TO_AVX512_FEATURES`); `vg_chacha20_poly1305_open_to_avx2` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_TO_AVX2_FEATURES`); `vg_chacha20_poly1305_open_to_ssse3` if the CPU supports its features (`VG_CHACHA20_POLY1305_OPEN_TO_SSSE3_FEATURES`); or else `vg_chacha20_poly1305_open_to`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -222,7 +222,7 @@ pub unsafe fn vg_chacha20_poly1305_open_to_auto(_dit: verified_garbage_cpu::Dit< /// /// Contract: `VG.Spec.ChaCha20Poly1305.sealGatherContract`. Constant time: only the pointers, the lengths, `src_count` and where the slices are (their addresses and lengths) may affect timing, not the key, the nonce, the additional data or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_chacha20_poly1305_seal_gather_ifma` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_GATHER_IFMA_FEATURES`); `vg_chacha20_poly1305_seal_gather_avx512vl` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_GATHER_AVX512VL_FEATURES`); `vg_chacha20_poly1305_seal_gather_avx512` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_GATHER_AVX512_FEATURES`); `vg_chacha20_poly1305_seal_gather_avx2` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_GATHER_AVX2_FEATURES`); `vg_chacha20_poly1305_seal_gather_ssse3` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_GATHER_SSSE3_FEATURES`); or else `vg_chacha20_poly1305_seal_gather`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_chacha20_poly1305_seal_gather_ifma` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_GATHER_IFMA_FEATURES`); `vg_chacha20_poly1305_seal_gather_avx512vl` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_GATHER_AVX512VL_FEATURES`); `vg_chacha20_poly1305_seal_gather_avx512` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_GATHER_AVX512_FEATURES`); `vg_chacha20_poly1305_seal_gather_avx2` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_GATHER_AVX2_FEATURES`); `vg_chacha20_poly1305_seal_gather_ssse3` if the CPU supports its features (`VG_CHACHA20_POLY1305_SEAL_GATHER_SSSE3_FEATURES`); or else `vg_chacha20_poly1305_seal_gather`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/cmac_aes.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/cmac_aes.rs index 12c7522b9b4..a5f8d06fbee 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/cmac_aes.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/cmac_aes.rs @@ -2,50 +2,45 @@ //! The dispatchers of the verified `cmac_aes` functions for `x86_64` //! (`crate::cmac_aes`, `VG.Rust.dispatcher`). -/// Starts an AES-CMAC computation (NIST SP 800-38B, RFC 4493): makes the streaming state `*state` represent the empty message under the AES key of `key_len` bytes at `key` (AES-128, AES-192 or AES-256). The state holds the key schedule of AES (FIPS 197) with `key_len / 4 + 6` rounds, the subkeys `K1 ‖ K2` (§6.1), the chaining value and the message's last bytes, held back (`VG.Spec.Cmac.Repr`). Continue with `vg_cmac_aes_absorb` and `vg_cmac_aes_finish`, passing them `key_len / 4 + 6` as `rounds`. +/// Absorbs data into an AES-CMAC computation (NIST SP 800-38B §6.2): if the streaming state `*state` represents a message of `count` bytes under an AES key with `rounds` rounds (as `vg_cmac_aes_init` set it up), it then represents that message followed by the `len` bytes at `data`, under the same key, provided that the two together are shorter than 2⁶⁴ bytes. It chains (step 6) every block of the message but its last bytes `Mₙ*` (step 3), which it holds back in the state, since only `vg_cmac_aes_finish` knows they are the last. /// -/// Contract: `VG.Spec.Cmac.aesInitContract`. Constant time: only the pointers and `key_len` may affect timing, not the key. +/// Contract: `VG.Spec.Cmac.aesAbsorbContract`. Constant time: only the pointers, `rounds`, `count` and `len` may affect timing, not the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_cmac_aes_init_vaes` if the CPU supports its features (`VG_CMAC_AES_INIT_VAES_FEATURES`); `vg_cmac_aes_init_aesni` if the CPU supports its features (`VG_CMAC_AES_INIT_AESNI_FEATURES`); or else `vg_cmac_aes_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_cmac_aes_absorb_aesni` if the CPU supports its features (`VG_CMAC_AES_ABSORB_AESNI_FEATURES`); or else `vg_cmac_aes_absorb`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 304 bytes. -/// * `key` must be valid for reads of `key_len` bytes. -/// * `key_len` must be 16, 24 or 32. -/// * `state` must not overlap `key` (distinct Rust objects never do). -/// * Neither `state` nor `key` may overlap the return address on the stack or the 2328 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `rounds` must be 10, 12 or 14. +/// * `state` must not overlap `data` (distinct Rust objects never do). +/// * Neither `state` nor `data` may overlap the return address on the stack or the 2328 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_cmac_aes_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 38], key: *const u8, key_len: usize) { +pub unsafe fn vg_cmac_aes_absorb_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 38], rounds: usize, count: u64, data: *const u8, len: usize) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(state: *mut [u64; 38], key: *const u8, key_len: usize); + type Impl = unsafe extern "sysv64" fn(state: *mut [u64; 38], rounds: usize, count: u64, data: *const u8, len: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::cmac_aes::VG_CMAC_AES_INIT_VAES_FEATURES) } - || cpu.contains(crate::cmac_aes::VG_CMAC_AES_INIT_VAES_FEATURES) - { - return crate::cmac_aes::vg_cmac_aes_init_vaes; - } - if const { verified_garbage_cpu::target_contains(crate::cmac_aes::VG_CMAC_AES_INIT_AESNI_FEATURES) } - || cpu.contains(crate::cmac_aes::VG_CMAC_AES_INIT_AESNI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::cmac_aes::VG_CMAC_AES_ABSORB_AESNI_FEATURES) } + || cpu.contains(crate::cmac_aes::VG_CMAC_AES_ABSORB_AESNI_FEATURES) { - return crate::cmac_aes::vg_cmac_aes_init_aesni; + return crate::cmac_aes::vg_cmac_aes_absorb_aesni; } - crate::cmac_aes::vg_cmac_aes_init + crate::cmac_aes::vg_cmac_aes_absorb } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(state: *mut [u64; 38], key: *const u8, key_len: usize) { + unsafe extern "sysv64" fn first(state: *mut [u64; 38], rounds: usize, count: u64, data: *const u8, len: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, key, key_len) } + unsafe { f(state, rounds, count, data, len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -53,48 +48,53 @@ pub unsafe fn vg_cmac_aes_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, key, key_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, rounds, count, data, len) } } -/// Absorbs data into an AES-CMAC computation (NIST SP 800-38B §6.2): if the streaming state `*state` represents a message of `count` bytes under an AES key with `rounds` rounds (as `vg_cmac_aes_init` set it up), it then represents that message followed by the `len` bytes at `data`, under the same key, provided that the two together are shorter than 2⁶⁴ bytes. It chains (step 6) every block of the message but its last bytes `Mₙ*` (step 3), which it holds back in the state, since only `vg_cmac_aes_finish` knows they are the last. +/// Starts an AES-CMAC computation (NIST SP 800-38B, RFC 4493): makes the streaming state `*state` represent the empty message under the AES key of `key_len` bytes at `key` (AES-128, AES-192 or AES-256). The state holds the key schedule of AES (FIPS 197) with `key_len / 4 + 6` rounds, the subkeys `K1 ‖ K2` (§6.1), the chaining value and the message's last bytes, held back (`VG.Spec.Cmac.Repr`). Continue with `vg_cmac_aes_absorb` and `vg_cmac_aes_finish`, passing them `key_len / 4 + 6` as `rounds`. /// -/// Contract: `VG.Spec.Cmac.aesAbsorbContract`. Constant time: only the pointers, `rounds`, `count` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Cmac.aesInitContract`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_cmac_aes_absorb_aesni` if the CPU supports its features (`VG_CMAC_AES_ABSORB_AESNI_FEATURES`); or else `vg_cmac_aes_absorb`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_cmac_aes_init_vaes` if the CPU supports its features (`VG_CMAC_AES_INIT_VAES_FEATURES`); `vg_cmac_aes_init_aesni` if the CPU supports its features (`VG_CMAC_AES_INIT_AESNI_FEATURES`); or else `vg_cmac_aes_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 304 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `rounds` must be 10, 12 or 14. -/// * `state` must not overlap `data` (distinct Rust objects never do). -/// * Neither `state` nor `data` may overlap the return address on the stack or the 2328 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `key` must be valid for reads of `key_len` bytes. +/// * `key_len` must be 16, 24 or 32. +/// * `state` must not overlap `key` (distinct Rust objects never do). +/// * Neither `state` nor `key` may overlap the return address on the stack or the 2328 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_cmac_aes_absorb_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 38], rounds: usize, count: u64, data: *const u8, len: usize) { +pub unsafe fn vg_cmac_aes_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 38], key: *const u8, key_len: usize) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(state: *mut [u64; 38], rounds: usize, count: u64, data: *const u8, len: usize); + type Impl = unsafe extern "sysv64" fn(state: *mut [u64; 38], key: *const u8, key_len: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::cmac_aes::VG_CMAC_AES_ABSORB_AESNI_FEATURES) } - || cpu.contains(crate::cmac_aes::VG_CMAC_AES_ABSORB_AESNI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::cmac_aes::VG_CMAC_AES_INIT_VAES_FEATURES) } + || cpu.contains(crate::cmac_aes::VG_CMAC_AES_INIT_VAES_FEATURES) { - return crate::cmac_aes::vg_cmac_aes_absorb_aesni; + return crate::cmac_aes::vg_cmac_aes_init_vaes; } - crate::cmac_aes::vg_cmac_aes_absorb + if const { verified_garbage_cpu::target_contains(crate::cmac_aes::VG_CMAC_AES_INIT_AESNI_FEATURES) } + || cpu.contains(crate::cmac_aes::VG_CMAC_AES_INIT_AESNI_FEATURES) + { + return crate::cmac_aes::vg_cmac_aes_init_aesni; + } + crate::cmac_aes::vg_cmac_aes_init } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(state: *mut [u64; 38], rounds: usize, count: u64, data: *const u8, len: usize) { + unsafe extern "sysv64" fn first(state: *mut [u64; 38], key: *const u8, key_len: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, rounds, count, data, len) } + unsafe { f(state, key, key_len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -102,14 +102,14 @@ pub unsafe fn vg_cmac_aes_absorb_auto(_dit: verified_garbage_cpu::Dit<'_>, state // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, rounds, count, data, len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, key, key_len) } } /// Finishes an AES-CMAC computation (NIST SP 800-38B §6.2, with `Tlen = 128`): if the streaming state `*state` represents a message of `count` bytes, shorter than 2⁶⁴ bytes, under an AES key with `rounds` rounds (as `vg_cmac_aes_init` and `vg_cmac_aes_absorb` set it up), writes the MAC of that message under that key to `*out`: `Cₙ = CIPH_K(Cₙ₋₁ ⊕ Mₙ)`, where `Mₙ = K1 ⊕ Mₙ*` if the message's last bytes `Mₙ*` are a complete block, and `Mₙ = K2 ⊕ (Mₙ* ‖ 10ʲ)` otherwise (step 4). The caller truncates it (step 7) and compares it (§6.3). /// /// Contract: `VG.Spec.Cmac.aesFinishContract`. Constant time: only the pointers, `rounds` and `count` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_cmac_aes_finish_vaes` if the CPU supports its features (`VG_CMAC_AES_FINISH_VAES_FEATURES`); `vg_cmac_aes_finish_aesni` if the CPU supports its features (`VG_CMAC_AES_FINISH_AESNI_FEATURES`); or else `vg_cmac_aes_finish`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_cmac_aes_finish_vaes` if the CPU supports its features (`VG_CMAC_AES_FINISH_VAES_FEATURES`); `vg_cmac_aes_finish_aesni` if the CPU supports its features (`VG_CMAC_AES_FINISH_AESNI_FEATURES`); or else `vg_cmac_aes_finish`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/cmac_triple_des.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/cmac_triple_des.rs index 0fda412f698..e8c108f2561 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/cmac_triple_des.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/cmac_triple_des.rs @@ -2,6 +2,26 @@ //! The dispatchers of the verified `cmac_triple_des` functions for `x86_64` //! (`crate::cmac_triple_des`, `VG.Rust.dispatcher`). +/// Starts a TDEA-CMAC computation (NIST SP 800-38B): makes the streaming state `*state` represent the empty message under the 16- or 24-byte TDEA key at `key` (FIPS 46-3; a 16-byte key is `K1 ‖ K2` with `K3 = K1`, parity bits are ignored). The state holds the key schedule (the three DES schedules), the subkeys `K1 ‖ K2` (§6.1), the chaining value and the message's last bytes, held back (`VG.Spec.Cmac.tdesRepr`). Continue with `vg_cmac_triple_des_absorb` and `vg_cmac_triple_des_finish`. +/// +/// Contract: `VG.Spec.Cmac.tdesStreamInitContract`. Constant time: only the pointers and `key_len` may affect timing, not the key. +/// +/// It runs `vg_cmac_triple_des_stream_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 416 bytes. +/// * `key` must be valid for reads of `key_len` bytes. +/// * `key_len` must be 16 or 24. +/// * `state` must not overlap `key` (distinct Rust objects never do). +/// * Neither `state` nor `key` may overlap the return address on the stack or the 648 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_cmac_triple_des_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 52], key: *const u8, key_len: usize) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::cmac_triple_des::vg_cmac_triple_des_stream_init(state, key, key_len) } +} + /// Absorbs data into a TDEA-CMAC computation (NIST SP 800-38B §6.2): if the streaming state `*state` represents a message of `count` bytes under a TDEA key (as `vg_cmac_triple_des_stream_init` set it up), it then represents that message followed by the `len` bytes at `data`, under the same key, provided that the two together are shorter than 2⁶⁴ bytes. It chains (step 6) every block of the message but its last bytes `Mₙ*` (step 3), which it holds back in the state, since only `vg_cmac_triple_des_finish` knows they are the last. /// /// Contract: `VG.Spec.Cmac.tdesAbsorbContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. @@ -40,23 +60,3 @@ pub unsafe fn vg_cmac_triple_des_finish_auto(_dit: verified_garbage_cpu::Dit<'_> // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::cmac_triple_des::vg_cmac_triple_des_finish(state, count, out) } } - -/// Starts a TDEA-CMAC computation (NIST SP 800-38B): makes the streaming state `*state` represent the empty message under the 16- or 24-byte TDEA key at `key` (FIPS 46-3; a 16-byte key is `K1 ‖ K2` with `K3 = K1`, parity bits are ignored). The state holds the key schedule (the three DES schedules), the subkeys `K1 ‖ K2` (§6.1), the chaining value and the message's last bytes, held back (`VG.Spec.Cmac.tdesRepr`). Continue with `vg_cmac_triple_des_absorb` and `vg_cmac_triple_des_finish`. -/// -/// Contract: `VG.Spec.Cmac.tdesStreamInitContract`. Constant time: only the pointers and `key_len` may affect timing, not the key. -/// -/// It runs `vg_cmac_triple_des_stream_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 416 bytes. -/// * `key` must be valid for reads of `key_len` bytes. -/// * `key_len` must be 16 or 24. -/// * `state` must not overlap `key` (distinct Rust objects never do). -/// * Neither `state` nor `key` may overlap the return address on the stack or the 648 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_cmac_triple_des_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 52], key: *const u8, key_len: usize) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::cmac_triple_des::vg_cmac_triple_des_stream_init(state, key, key_len) } -} diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/dsa.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/dsa.rs index 71e830ceec2..21f0203003e 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/dsa.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/dsa.rs @@ -2,100 +2,68 @@ //! The dispatchers of the verified `dsa` functions for `x86_64` //! (`crate::dsa`, `VG.Rust.dispatcher`). -/// Derive a DSA private key from big-endian p||q||g and a supplied q_len-byte scalar x, as key generation does after sampling x: checks the domain and 0 < x < q, computes y = g^x mod p and checks the key. Returns 1 with p||q||g||y||x, or 0 and zero output. Checks algebraic consistency, not primality. x must be a fresh uniform scalar the caller sampled. Timing may depend on p, q, g and success, not otherwise on x. Contract: `VG.Spec.Dsa.deriveKeyContract`. +/// Start a search for DSA domain parameters with a modulus of `p_len` octets (128, 256 or 384: 1024, 2048 or 3072 bits, with a subgroup of 160, 256 and 256 bits). Returns 1 and writes the search's four public words to `state`: the seeds it may still reject, `p_len`, its entropy budget, and the length of the first random tape to give `vg_dsa_parameter_search`. Returns 0 and writes zeros for any other `p_len`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_dsa_derive_key_adx` if the CPU supports its features (`VG_DSA_DERIVE_KEY_ADX_FEATURES`); or else `vg_dsa_derive_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// Contract: `VG.Spec.Dsa.parameterInitContract`. +/// +/// It runs `vg_dsa_parameter_search_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// -/// * `out` must be valid for reads and writes of `out_len` bytes. -/// * `params` must be valid for reads of `params_len` bytes. -/// * `x` must be valid for reads of `q_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). -/// * params_len must be 2*p_len + q_len; out_len must be 3*p_len + 2*q_len. -/// * scratch_len must be at least 16*p_len words. Scratch may contain secrets on return. -/// * `out` and `scratch` must not overlap each other, `params`, `x` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `params`, `x` and `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `state` must be valid for reads and writes of 32 bytes. +/// * `state` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_dsa_derive_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, params: *const u8, params_len: usize, p_len: usize, x: *const u8, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(out: *mut u8, out_len: usize, params: *const u8, params_len: usize, p_len: usize, x: *const u8, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; - /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only - /// features the CPU has. - #[cold] - fn select() -> Impl { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_DERIVE_KEY_ADX_FEATURES) } - || cpu.contains(crate::dsa::VG_DSA_DERIVE_KEY_ADX_FEATURES) - { - return crate::dsa::vg_dsa_derive_key_adx; - } - crate::dsa::vg_dsa_derive_key - } - /// The first call: chooses the implementation, records it in `CHOSEN` for the calls - /// after it, and runs it. - #[cold] - unsafe extern "sysv64" fn first(out: *mut u8, out_len: usize, params: *const u8, params_len: usize, p_len: usize, x: *const u8, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - let f = select(); - CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); - // SAFETY: the caller's obligations, which are those of every implementation, `f` - // among them, and the CPU has the features `f` needs. - unsafe { f(out, out_len, params, params_len, p_len, x, q_len, scratch, scratch_len) } - } - /// The function to call: `first`, until it records the implementation it chose. - static CHOSEN: core::sync::atomic::AtomicPtr<()> = - core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); - // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, - // or an implementation `select` returned, which needs only features the CPU has. Calling - // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, params, params_len, p_len, x, q_len, scratch, scratch_len) } +pub unsafe fn vg_dsa_parameter_search_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 4], p_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::dsa::vg_dsa_parameter_search_init(state, p_len) } } -/// Generate a DSA private key from p||q||g and fresh independent random bytes. Uses at most 128 rejection-sampling draws, computes y=g^x mod p, and checks algebraic consistency, not primality. Returns 1 with p||q||g||y||x, 0 for invalid input/draw limit, or 2 for insufficient randomness. Failure zeros out; used counts consumed complete draws on every return. Timing may depend on public parameters, sampling decisions, consumption and success, not otherwise on x or randomness. Contract: `VG.Spec.Dsa.generateKeyContract`. +/// Randomized DSA signing of a prehashed digest, for an already validated packed private key (big-endian p||q||g||y||x; vg_dsa_validate_private_key validates it). Up to eight signing attempts, each with 128 nonce draws. Returns 1 with fixed-width r||s, 0 for retry exhaustion, or 2 for insufficient random bytes; out is zero on failure and used counts consumed complete draws on every return. Timing may depend on public p,q,g,y, sampling decisions, signing success bits and consumption, not otherwise on secrets. Contract: `VG.Spec.Dsa.randomSignPrevalidatedContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_dsa_generate_key_adx` if the CPU supports its features (`VG_DSA_GENERATE_KEY_ADX_FEATURES`); or else `vg_dsa_generate_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_dsa_sign_random_prevalidated_adx` if the CPU supports its features (`VG_DSA_SIGN_RANDOM_PREVALIDATED_ADX_FEATURES`); or else `vg_dsa_sign_random_prevalidated`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `out` must be valid for reads and writes of `out_len` bytes. /// * `used` must be valid for reads and writes of 8 bytes. -/// * `params` must be valid for reads of `params_len` bytes. +/// * `key` must be valid for reads of `key_len` bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. /// * `rand` must be valid for reads of `rand_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. /// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). -/// * params_len must be 2*p_len + q_len; out_len must be 3*p_len + 2*q_len. -/// * scratch_len must be at least 64*p_len + 1024 words; destroy scratch after use. -/// * Caller must assure domain validity and supply fresh independent random bytes. -/// * `out`, `used` and `scratch` must not overlap each other, `params`, `rand` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `used`, `params`, `rand` and `scratch` may overlap the return address on the stack or the 264 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * key_len must be 3*p_len + 2*q_len; out_len must be 2*q_len. +/// * scratch_len must be at least 16*p_len + 64 words; destroy scratch after use. +/// * The exact key bytes must satisfy validatePrivateKeyBytes p_len q_len key = true. A successful vg_dsa_validate_private_key call establishes this only while the key bytes remain unchanged. Primality remains the caller's responsibility. +/// * Provide fresh independent random octets; never reuse a completed signing tape for another signature. +/// * `out`, `used` and `scratch` must not overlap each other, `key`, `digest`, `rand` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `used`, `key`, `digest`, `rand` and `scratch` may overlap the return address on the stack or the 264 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_dsa_generate_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], params: *const u8, params_len: usize, p_len: usize, q_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_dsa_sign_random_prevalidated_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], key: *const u8, key_len: usize, p_len: usize, q_len: usize, digest: *const u8, digest_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(out: *mut u8, out_len: usize, used: *mut [u64; 1], params: *const u8, params_len: usize, p_len: usize, q_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; + type Impl = unsafe extern "sysv64" fn(out: *mut u8, out_len: usize, used: *mut [u64; 1], key: *const u8, key_len: usize, p_len: usize, q_len: usize, digest: *const u8, digest_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_GENERATE_KEY_ADX_FEATURES) } - || cpu.contains(crate::dsa::VG_DSA_GENERATE_KEY_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_SIGN_RANDOM_PREVALIDATED_ADX_FEATURES) } + || cpu.contains(crate::dsa::VG_DSA_SIGN_RANDOM_PREVALIDATED_ADX_FEATURES) { - return crate::dsa::vg_dsa_generate_key_adx; + return crate::dsa::vg_dsa_sign_random_prevalidated_adx; } - crate::dsa::vg_dsa_generate_key + crate::dsa::vg_dsa_sign_random_prevalidated } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(out: *mut u8, out_len: usize, used: *mut [u64; 1], params: *const u8, params_len: usize, p_len: usize, q_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + unsafe extern "sysv64" fn first(out: *mut u8, out_len: usize, used: *mut [u64; 1], key: *const u8, key_len: usize, p_len: usize, q_len: usize, digest: *const u8, digest_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, out_len, used, params, params_len, p_len, q_len, rand, rand_len, scratch, scratch_len) } + unsafe { f(out, out_len, used, key, key_len, p_len, q_len, digest, digest_len, rand, rand_len, scratch, scratch_len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -103,90 +71,47 @@ pub unsafe fn vg_dsa_generate_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, used, params, params_len, p_len, q_len, rand, rand_len, scratch, scratch_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, used, key, key_len, p_len, q_len, digest, digest_len, rand, rand_len, scratch, scratch_len) } } -/// Search for DSA domain parameters (FIPS 186-4 A.1.1.2 and A.2.1) over the random tape `rand`, for the modulus width in `state`: each seed, its `q`, the `p` candidates from it and the generator's `h` candidates are read from `rand` in turn, with the rejected seeds counted in `state`. Returns 1 and writes `p || q || g` to `out` once parameters are found; 3 if the search is exhausted, by too many rejected seeds or by one seed needing more than the entropy budget; or 0 if the tape ran out inside a seed. Writes the octets of `rand` it consumed to `used`, and the state for the next call to `state`, whose last word is, for 0, the length of the next tape, more than the octets of `rand` after the first `used` and at most the entropy budget: keep those octets, in order, and append fresh random octets up to that length. `out` is zeros but for 1. Accepted parameters pass probable-prime tests, not primality certification. -/// -/// Contract: `VG.Spec.Dsa.parameterSearchContract`. Timing may depend on everything, `rand` included: these are public domain parameters. +/// Validate packed DSA p||q||g||y||x as signing checks it: domain consistency, including g^q mod p = 1, 1 < y < p, 0 < x < q and g^x mod p = y. Returns 1 exactly when validation succeeds, otherwise 0. Does not test primality. Timing may depend on p,q,g,y and the result, not otherwise on x. Scratch contents are unspecified on return and may hold secrets. Contract: `VG.Spec.Dsa.validatePrivateKeyContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_dsa_parameter_search_sha1_shani_sha256_shani_mr_ifma` if the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_MR_IFMA_FEATURES`); `vg_dsa_parameter_search_sha1_shani_sha256_shani_mr_adx` if the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_MR_ADX_FEATURES`); `vg_dsa_parameter_search_sha1_shani_sha256_shani` if the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_FEATURES`); `vg_dsa_parameter_search_sha1_avx_bmi_sha256_avx2_mr_ifma` if the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_SHA1_AVX_BMI_SHA256_AVX2_MR_IFMA_FEATURES`); `vg_dsa_parameter_search_sha1_avx_bmi_sha256_avx2_mr_adx` if the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_SHA1_AVX_BMI_SHA256_AVX2_MR_ADX_FEATURES`); `vg_dsa_parameter_search_sha1_avx_bmi_sha256_avx2` if the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_SHA1_AVX_BMI_SHA256_AVX2_FEATURES`); `vg_dsa_parameter_search_mr_ifma` if the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_MR_IFMA_FEATURES`); `vg_dsa_parameter_search_mr_adx` if the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_MR_ADX_FEATURES`); or else `vg_dsa_parameter_search`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_dsa_validate_private_key_adx` if the CPU supports its features (`VG_DSA_VALIDATE_PRIVATE_KEY_ADX_FEATURES`); or else `vg_dsa_validate_private_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of `out_len` bytes. -/// * `used` must be valid for reads and writes of 8 bytes. -/// * `rand` must be valid for reads of `rand_len` bytes. +/// * `key` must be valid for reads of `key_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `state` must be valid for reads and writes of 32 bytes. -/// * `state` must come from `vg_dsa_parameter_search_init`, or from the last call of this function that returned 0. -/// * `out_len` must be `2 * state[1]` plus 20 if `state[1]` is 128, else plus 32. -/// * `rand_len` must be at most the entropy budget, `state[2]`. -/// * `scratch_len` must be at least `64 * state[1] + 1024` words. -/// * `rand` must hold independent uniform random octets. They are public under this contract and must never be used for private scalars or nonces. -/// * `out`, `used`, `scratch` and `state` must not overlap each other, `rand` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `used`, `rand`, `scratch` and `state` may overlap the return address on the stack or the 632 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). +/// * key_len must be 3*p_len + 2*q_len; scratch_len must be at least 16*p_len words; destroy scratch after use. +/// * `scratch` must not overlap `key` (distinct Rust objects never do). +/// * Neither `key` nor `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_dsa_parameter_search_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32 { +pub unsafe fn vg_dsa_validate_private_key_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *const u8, key_len: usize, p_len: usize, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32; + type Impl = unsafe extern "sysv64" fn(key: *const u8, key_len: usize, p_len: usize, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_MR_IFMA_FEATURES) } - || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_MR_IFMA_FEATURES) - { - return crate::dsa::vg_dsa_parameter_search_sha1_shani_sha256_shani_mr_ifma; - } - if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_MR_ADX_FEATURES) } - || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_MR_ADX_FEATURES) - { - return crate::dsa::vg_dsa_parameter_search_sha1_shani_sha256_shani_mr_adx; - } - if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_FEATURES) } - || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_FEATURES) - { - return crate::dsa::vg_dsa_parameter_search_sha1_shani_sha256_shani; - } - if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_AVX_BMI_SHA256_AVX2_MR_IFMA_FEATURES) } - || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_AVX_BMI_SHA256_AVX2_MR_IFMA_FEATURES) - { - return crate::dsa::vg_dsa_parameter_search_sha1_avx_bmi_sha256_avx2_mr_ifma; - } - if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_AVX_BMI_SHA256_AVX2_MR_ADX_FEATURES) } - || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_AVX_BMI_SHA256_AVX2_MR_ADX_FEATURES) - { - return crate::dsa::vg_dsa_parameter_search_sha1_avx_bmi_sha256_avx2_mr_adx; - } - if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_AVX_BMI_SHA256_AVX2_FEATURES) } - || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_AVX_BMI_SHA256_AVX2_FEATURES) - { - return crate::dsa::vg_dsa_parameter_search_sha1_avx_bmi_sha256_avx2; - } - if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_MR_IFMA_FEATURES) } - || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_MR_IFMA_FEATURES) - { - return crate::dsa::vg_dsa_parameter_search_mr_ifma; - } - if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_MR_ADX_FEATURES) } - || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_MR_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_VALIDATE_PRIVATE_KEY_ADX_FEATURES) } + || cpu.contains(crate::dsa::VG_DSA_VALIDATE_PRIVATE_KEY_ADX_FEATURES) { - return crate::dsa::vg_dsa_parameter_search_mr_adx; + return crate::dsa::vg_dsa_validate_private_key_adx; } - crate::dsa::vg_dsa_parameter_search + crate::dsa::vg_dsa_validate_private_key } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32 { + unsafe extern "sysv64" fn first(key: *const u8, key_len: usize, p_len: usize, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, out_len, used, rand, rand_len, scratch, scratch_len, state) } + unsafe { f(key, key_len, p_len, q_len, scratch, scratch_len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -194,12 +119,12 @@ pub unsafe fn vg_dsa_parameter_search_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, used, rand, rand_len, scratch, scratch_len, state) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(key, key_len, p_len, q_len, scratch, scratch_len) } } /// Validate packed DSA p||q||g||y, including domain consistency and both subgroup memberships. Returns 1 exactly when validation succeeds, otherwise 0. Does not test primality. The key is public; scratch contents are unspecified on return. Contract: `VG.Spec.Dsa.validatePublicKeyContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_dsa_validate_public_key_adx` if the CPU supports its features (`VG_DSA_VALIDATE_PUBLIC_KEY_ADX_FEATURES`); or else `vg_dsa_validate_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_dsa_validate_public_key_adx` if the CPU supports its features (`VG_DSA_VALIDATE_PUBLIC_KEY_ADX_FEATURES`); or else `vg_dsa_validate_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -247,7 +172,7 @@ pub unsafe fn vg_dsa_validate_public_key_auto(_dit: verified_garbage_cpu::Dit<'_ /// DSA verification of a prehashed digest, for an already validated packed public key (big-endian p||q||g||y with widths p_len, q_len, p_len, p_len; vg_dsa_validate_public_key validates it). The signature is r||s with q_len bytes each. Returns 1 if verification succeeds, otherwise 0: the result of fully checked verification (`VG.Spec.Dsa.verifyContract`), whose checks of the signature scalars and digest handling it keeps. Timing may depend on p,q,g,y and the result, not otherwise on signature or digest. Contract: `VG.Spec.Dsa.verifyPrevalidatedContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_dsa_verify_prevalidated_adx` if the CPU supports its features (`VG_DSA_VERIFY_PREVALIDATED_ADX_FEATURES`); or else `vg_dsa_verify_prevalidated`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_dsa_verify_prevalidated_adx` if the CPU supports its features (`VG_DSA_VERIFY_PREVALIDATED_ADX_FEATURES`); or else `vg_dsa_verify_prevalidated`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -297,44 +222,49 @@ pub unsafe fn vg_dsa_verify_prevalidated_auto(_dit: verified_garbage_cpu::Dit<'_ unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(key, key_len, p_len, q_len, signature, signature_len, digest, digest_len, scratch, scratch_len) } } -/// Validate packed DSA p||q||g||y||x as signing checks it: domain consistency, including g^q mod p = 1, 1 < y < p, 0 < x < q and g^x mod p = y. Returns 1 exactly when validation succeeds, otherwise 0. Does not test primality. Timing may depend on p,q,g,y and the result, not otherwise on x. Scratch contents are unspecified on return and may hold secrets. Contract: `VG.Spec.Dsa.validatePrivateKeyContract`. +/// Generate a DSA private key from p||q||g and fresh independent random bytes. Uses at most 128 rejection-sampling draws, computes y=g^x mod p, and checks algebraic consistency, not primality. Returns 1 with p||q||g||y||x, 0 for invalid input/draw limit, or 2 for insufficient randomness. Failure zeros out; used counts consumed complete draws on every return. Timing may depend on public parameters, sampling decisions, consumption and success, not otherwise on x or randomness. Contract: `VG.Spec.Dsa.generateKeyContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_dsa_validate_private_key_adx` if the CPU supports its features (`VG_DSA_VALIDATE_PRIVATE_KEY_ADX_FEATURES`); or else `vg_dsa_validate_private_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_dsa_generate_key_adx` if the CPU supports its features (`VG_DSA_GENERATE_KEY_ADX_FEATURES`); or else `vg_dsa_generate_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `key` must be valid for reads of `key_len` bytes. +/// * `out` must be valid for reads and writes of `out_len` bytes. +/// * `used` must be valid for reads and writes of 8 bytes. +/// * `params` must be valid for reads of `params_len` bytes. +/// * `rand` must be valid for reads of `rand_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. /// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). -/// * key_len must be 3*p_len + 2*q_len; scratch_len must be at least 16*p_len words; destroy scratch after use. -/// * `scratch` must not overlap `key` (distinct Rust objects never do). -/// * Neither `key` nor `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * params_len must be 2*p_len + q_len; out_len must be 3*p_len + 2*q_len. +/// * scratch_len must be at least 64*p_len + 1024 words; destroy scratch after use. +/// * Caller must assure domain validity and supply fresh independent random bytes. +/// * `out`, `used` and `scratch` must not overlap each other, `params`, `rand` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `used`, `params`, `rand` and `scratch` may overlap the return address on the stack or the 264 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_dsa_validate_private_key_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *const u8, key_len: usize, p_len: usize, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_dsa_generate_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], params: *const u8, params_len: usize, p_len: usize, q_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(key: *const u8, key_len: usize, p_len: usize, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; + type Impl = unsafe extern "sysv64" fn(out: *mut u8, out_len: usize, used: *mut [u64; 1], params: *const u8, params_len: usize, p_len: usize, q_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_VALIDATE_PRIVATE_KEY_ADX_FEATURES) } - || cpu.contains(crate::dsa::VG_DSA_VALIDATE_PRIVATE_KEY_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_GENERATE_KEY_ADX_FEATURES) } + || cpu.contains(crate::dsa::VG_DSA_GENERATE_KEY_ADX_FEATURES) { - return crate::dsa::vg_dsa_validate_private_key_adx; + return crate::dsa::vg_dsa_generate_key_adx; } - crate::dsa::vg_dsa_validate_private_key + crate::dsa::vg_dsa_generate_key } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(key: *const u8, key_len: usize, p_len: usize, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + unsafe extern "sysv64" fn first(out: *mut u8, out_len: usize, used: *mut [u64; 1], params: *const u8, params_len: usize, p_len: usize, q_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(key, key_len, p_len, q_len, scratch, scratch_len) } + unsafe { f(out, out_len, used, params, params_len, p_len, q_len, rand, rand_len, scratch, scratch_len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -342,71 +272,141 @@ pub unsafe fn vg_dsa_validate_private_key_auto(_dit: verified_garbage_cpu::Dit<' // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(key, key_len, p_len, q_len, scratch, scratch_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, used, params, params_len, p_len, q_len, rand, rand_len, scratch, scratch_len) } } -/// Start a search for DSA domain parameters with a modulus of `p_len` octets (128, 256 or 384: 1024, 2048 or 3072 bits, with a subgroup of 160, 256 and 256 bits). Returns 1 and writes the search's four public words to `state`: the seeds it may still reject, `p_len`, its entropy budget, and the length of the first random tape to give `vg_dsa_parameter_search`. Returns 0 and writes zeros for any other `p_len`. -/// -/// Contract: `VG.Spec.Dsa.parameterInitContract`. +/// Derive a DSA private key from big-endian p||q||g and a supplied q_len-byte scalar x, as key generation does after sampling x: checks the domain and 0 < x < q, computes y = g^x mod p and checks the key. Returns 1 with p||q||g||y||x, or 0 and zero output. Checks algebraic consistency, not primality. x must be a fresh uniform scalar the caller sampled. Timing may depend on p, q, g and success, not otherwise on x. Contract: `VG.Spec.Dsa.deriveKeyContract`. /// -/// It runs `vg_dsa_parameter_search_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_dsa_derive_key_adx` if the CPU supports its features (`VG_DSA_DERIVE_KEY_ADX_FEATURES`); or else `vg_dsa_derive_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 32 bytes. -/// * `state` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of `out_len` bytes. +/// * `params` must be valid for reads of `params_len` bytes. +/// * `x` must be valid for reads of `q_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). +/// * params_len must be 2*p_len + q_len; out_len must be 3*p_len + 2*q_len. +/// * scratch_len must be at least 16*p_len words. Scratch may contain secrets on return. +/// * `out` and `scratch` must not overlap each other, `params`, `x` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `params`, `x` and `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_dsa_parameter_search_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 4], p_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::dsa::vg_dsa_parameter_search_init(state, p_len) } +pub unsafe fn vg_dsa_derive_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, params: *const u8, params_len: usize, p_len: usize, x: *const u8, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + /// The type of the implementations. + type Impl = unsafe extern "sysv64" fn(out: *mut u8, out_len: usize, params: *const u8, params_len: usize, p_len: usize, x: *const u8, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; + /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only + /// features the CPU has. + #[cold] + fn select() -> Impl { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_DERIVE_KEY_ADX_FEATURES) } + || cpu.contains(crate::dsa::VG_DSA_DERIVE_KEY_ADX_FEATURES) + { + return crate::dsa::vg_dsa_derive_key_adx; + } + crate::dsa::vg_dsa_derive_key + } + /// The first call: chooses the implementation, records it in `CHOSEN` for the calls + /// after it, and runs it. + #[cold] + unsafe extern "sysv64" fn first(out: *mut u8, out_len: usize, params: *const u8, params_len: usize, p_len: usize, x: *const u8, q_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + let f = select(); + CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); + // SAFETY: the caller's obligations, which are those of every implementation, `f` + // among them, and the CPU has the features `f` needs. + unsafe { f(out, out_len, params, params_len, p_len, x, q_len, scratch, scratch_len) } + } + /// The function to call: `first`, until it records the implementation it chose. + static CHOSEN: core::sync::atomic::AtomicPtr<()> = + core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); + // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, + // or an implementation `select` returned, which needs only features the CPU has. Calling + // either carries the caller's obligations, which are those of every implementation. + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, params, params_len, p_len, x, q_len, scratch, scratch_len) } } -/// Randomized DSA signing of a prehashed digest, for an already validated packed private key (big-endian p||q||g||y||x; vg_dsa_validate_private_key validates it). Up to eight signing attempts, each with 128 nonce draws. Returns 1 with fixed-width r||s, 0 for retry exhaustion, or 2 for insufficient random bytes; out is zero on failure and used counts consumed complete draws on every return. Timing may depend on public p,q,g,y, sampling decisions, signing success bits and consumption, not otherwise on secrets. Contract: `VG.Spec.Dsa.randomSignPrevalidatedContract`. +/// Search for DSA domain parameters (FIPS 186-4 A.1.1.2 and A.2.1) over the random tape `rand`, for the modulus width in `state`: each seed, its `q`, the `p` candidates from it and the generator's `h` candidates are read from `rand` in turn, with the rejected seeds counted in `state`. Returns 1 and writes `p || q || g` to `out` once parameters are found; 3 if the search is exhausted, by too many rejected seeds or by one seed needing more than the entropy budget; or 0 if the tape ran out inside a seed. Writes the octets of `rand` it consumed to `used`, and the state for the next call to `state`, whose last word is, for 0, the length of the next tape, more than the octets of `rand` after the first `used` and at most the entropy budget: keep those octets, in order, and append fresh random octets up to that length. `out` is zeros but for 1. Accepted parameters pass probable-prime tests, not primality certification. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_dsa_sign_random_prevalidated_adx` if the CPU supports its features (`VG_DSA_SIGN_RANDOM_PREVALIDATED_ADX_FEATURES`); or else `vg_dsa_sign_random_prevalidated`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// Contract: `VG.Spec.Dsa.parameterSearchContract`. Timing may depend on everything, `rand` included: these are public domain parameters. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_dsa_parameter_search_sha1_shani_sha256_shani_mr_ifma` if the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_MR_IFMA_FEATURES`); `vg_dsa_parameter_search_sha1_shani_sha256_shani_mr_adx` if the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_MR_ADX_FEATURES`); `vg_dsa_parameter_search_sha1_shani_sha256_shani` if the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_FEATURES`); `vg_dsa_parameter_search_sha1_avx_bmi_sha256_avx2_mr_ifma` if the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_SHA1_AVX_BMI_SHA256_AVX2_MR_IFMA_FEATURES`); `vg_dsa_parameter_search_sha1_avx_bmi_sha256_avx2_mr_adx` if the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_SHA1_AVX_BMI_SHA256_AVX2_MR_ADX_FEATURES`); `vg_dsa_parameter_search_sha1_avx_bmi_sha256_avx2` if the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_SHA1_AVX_BMI_SHA256_AVX2_FEATURES`); `vg_dsa_parameter_search_mr_ifma` if the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_MR_IFMA_FEATURES`); `vg_dsa_parameter_search_mr_adx` if the CPU supports its features (`VG_DSA_PARAMETER_SEARCH_MR_ADX_FEATURES`); or else `vg_dsa_parameter_search`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `out` must be valid for reads and writes of `out_len` bytes. /// * `used` must be valid for reads and writes of 8 bytes. -/// * `key` must be valid for reads of `key_len` bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. /// * `rand` must be valid for reads of `rand_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * (p_len, q_len) must be (128,20), (256,20), (256,28), (256,32), or (384,32). -/// * key_len must be 3*p_len + 2*q_len; out_len must be 2*q_len. -/// * scratch_len must be at least 16*p_len + 64 words; destroy scratch after use. -/// * The exact key bytes must satisfy validatePrivateKeyBytes p_len q_len key = true. A successful vg_dsa_validate_private_key call establishes this only while the key bytes remain unchanged. Primality remains the caller's responsibility. -/// * Provide fresh independent random octets; never reuse a completed signing tape for another signature. -/// * `out`, `used` and `scratch` must not overlap each other, `key`, `digest`, `rand` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `used`, `key`, `digest`, `rand` and `scratch` may overlap the return address on the stack or the 264 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `state` must be valid for reads and writes of 32 bytes. +/// * `state` must come from `vg_dsa_parameter_search_init`, or from the last call of this function that returned 0. +/// * `out_len` must be `2 * state[1]` plus 20 if `state[1]` is 128, else plus 32. +/// * `rand_len` must be at most the entropy budget, `state[2]`. +/// * `scratch_len` must be at least `64 * state[1] + 1024` words. +/// * `rand` must hold independent uniform random octets. They are public under this contract and must never be used for private scalars or nonces. +/// * `out`, `used`, `scratch` and `state` must not overlap each other, `rand` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `used`, `rand`, `scratch` and `state` may overlap the return address on the stack or the 632 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_dsa_sign_random_prevalidated_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], key: *const u8, key_len: usize, p_len: usize, q_len: usize, digest: *const u8, digest_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_dsa_parameter_search_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(out: *mut u8, out_len: usize, used: *mut [u64; 1], key: *const u8, key_len: usize, p_len: usize, q_len: usize, digest: *const u8, digest_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; + type Impl = unsafe extern "sysv64" fn(out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_SIGN_RANDOM_PREVALIDATED_ADX_FEATURES) } - || cpu.contains(crate::dsa::VG_DSA_SIGN_RANDOM_PREVALIDATED_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_MR_IFMA_FEATURES) } + || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_MR_IFMA_FEATURES) { - return crate::dsa::vg_dsa_sign_random_prevalidated_adx; + return crate::dsa::vg_dsa_parameter_search_sha1_shani_sha256_shani_mr_ifma; } - crate::dsa::vg_dsa_sign_random_prevalidated + if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_MR_ADX_FEATURES) } + || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_MR_ADX_FEATURES) + { + return crate::dsa::vg_dsa_parameter_search_sha1_shani_sha256_shani_mr_adx; + } + if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_FEATURES) } + || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_SHANI_SHA256_SHANI_FEATURES) + { + return crate::dsa::vg_dsa_parameter_search_sha1_shani_sha256_shani; + } + if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_AVX_BMI_SHA256_AVX2_MR_IFMA_FEATURES) } + || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_AVX_BMI_SHA256_AVX2_MR_IFMA_FEATURES) + { + return crate::dsa::vg_dsa_parameter_search_sha1_avx_bmi_sha256_avx2_mr_ifma; + } + if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_AVX_BMI_SHA256_AVX2_MR_ADX_FEATURES) } + || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_AVX_BMI_SHA256_AVX2_MR_ADX_FEATURES) + { + return crate::dsa::vg_dsa_parameter_search_sha1_avx_bmi_sha256_avx2_mr_adx; + } + if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_AVX_BMI_SHA256_AVX2_FEATURES) } + || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_SHA1_AVX_BMI_SHA256_AVX2_FEATURES) + { + return crate::dsa::vg_dsa_parameter_search_sha1_avx_bmi_sha256_avx2; + } + if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_MR_IFMA_FEATURES) } + || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_MR_IFMA_FEATURES) + { + return crate::dsa::vg_dsa_parameter_search_mr_ifma; + } + if const { verified_garbage_cpu::target_contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_MR_ADX_FEATURES) } + || cpu.contains(crate::dsa::VG_DSA_PARAMETER_SEARCH_MR_ADX_FEATURES) + { + return crate::dsa::vg_dsa_parameter_search_mr_adx; + } + crate::dsa::vg_dsa_parameter_search } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(out: *mut u8, out_len: usize, used: *mut [u64; 1], key: *const u8, key_len: usize, p_len: usize, q_len: usize, digest: *const u8, digest_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + unsafe extern "sysv64" fn first(out: *mut u8, out_len: usize, used: *mut [u64; 1], rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 4]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, out_len, used, key, key_len, p_len, q_len, digest, digest_len, rand, rand_len, scratch, scratch_len) } + unsafe { f(out, out_len, used, rand, rand_len, scratch, scratch_len, state) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -414,5 +414,5 @@ pub unsafe fn vg_dsa_sign_random_prevalidated_auto(_dit: verified_garbage_cpu::D // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, used, key, key_len, p_len, q_len, digest, digest_len, rand, rand_len, scratch, scratch_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, used, rand, rand_len, scratch, scratch_len, state) } } diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p256.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p256.rs index 73bd19eb01d..ca7649ec3b1 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p256.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p256.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.publicKeyContract`. Constant time: only the pointers may affect timing, not the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_p256_public_key_adx` if the CPU supports its features (`VG_EC_P256_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p256_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_p256_public_key_adx` if the CPU supports its features (`VG_EC_P256_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p256_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p256_compressed.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p256_compressed.rs index cef926f7a9f..afd7398e4a6 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p256_compressed.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p256_compressed.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.compressPublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_p256_compress_public_key_adx` if the CPU supports its features (`VG_EC_P256_COMPRESS_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p256_compress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_p256_compress_public_key_adx` if the CPU supports its features (`VG_EC_P256_COMPRESS_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p256_compress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -56,7 +56,7 @@ pub unsafe fn vg_ec_p256_compress_public_key_auto(_dit: verified_garbage_cpu::Di /// /// Contract: `VG.Spec.EcKey.Instance.decompressPublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_p256_decompress_public_key_adx` if the CPU supports its features (`VG_EC_P256_DECOMPRESS_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p256_decompress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_p256_decompress_public_key_adx` if the CPU supports its features (`VG_EC_P256_DECOMPRESS_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p256_decompress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p256_validate.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p256_validate.rs index 11db1c11d9a..c028eef174d 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p256_validate.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p256_validate.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.validatePublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_p256_validate_public_key_adx` if the CPU supports its features (`VG_EC_P256_VALIDATE_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p256_validate_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_p256_validate_public_key_adx` if the CPU supports its features (`VG_EC_P256_VALIDATE_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p256_validate_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p384.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p384.rs index c0ed14aa24f..0d5d94755ca 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p384.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p384.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.publicKeyContract`. Constant time: only the pointers may affect timing, not the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_p384_public_key_adx` if the CPU supports its features (`VG_EC_P384_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p384_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_p384_public_key_adx` if the CPU supports its features (`VG_EC_P384_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p384_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p384_compressed.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p384_compressed.rs index 9badc097db5..c4e76cf52f1 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p384_compressed.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p384_compressed.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.compressPublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_p384_compress_public_key_adx` if the CPU supports its features (`VG_EC_P384_COMPRESS_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p384_compress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_p384_compress_public_key_adx` if the CPU supports its features (`VG_EC_P384_COMPRESS_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p384_compress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -56,7 +56,7 @@ pub unsafe fn vg_ec_p384_compress_public_key_auto(_dit: verified_garbage_cpu::Di /// /// Contract: `VG.Spec.EcKey.Instance.decompressPublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_p384_decompress_public_key_adx` if the CPU supports its features (`VG_EC_P384_DECOMPRESS_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p384_decompress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_p384_decompress_public_key_adx` if the CPU supports its features (`VG_EC_P384_DECOMPRESS_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p384_decompress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p384_validate.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p384_validate.rs index c0233ffeafa..09eafed0cec 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p384_validate.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p384_validate.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.validatePublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_p384_validate_public_key_adx` if the CPU supports its features (`VG_EC_P384_VALIDATE_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p384_validate_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_p384_validate_public_key_adx` if the CPU supports its features (`VG_EC_P384_VALIDATE_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p384_validate_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p521.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p521.rs index 5da64726153..c2e394a78f6 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p521.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p521.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.publicKeyContract`. Constant time: only the pointers may affect timing, not the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_p521_public_key_adx` if the CPU supports its features (`VG_EC_P521_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p521_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_p521_public_key_adx` if the CPU supports its features (`VG_EC_P521_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p521_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p521_compressed.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p521_compressed.rs index 6f70ac2caf8..bfd81a532e5 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p521_compressed.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p521_compressed.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.compressPublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_p521_compress_public_key_adx` if the CPU supports its features (`VG_EC_P521_COMPRESS_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p521_compress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_p521_compress_public_key_adx` if the CPU supports its features (`VG_EC_P521_COMPRESS_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p521_compress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -56,7 +56,7 @@ pub unsafe fn vg_ec_p521_compress_public_key_auto(_dit: verified_garbage_cpu::Di /// /// Contract: `VG.Spec.EcKey.Instance.decompressPublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_p521_decompress_public_key_adx` if the CPU supports its features (`VG_EC_P521_DECOMPRESS_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p521_decompress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_p521_decompress_public_key_adx` if the CPU supports its features (`VG_EC_P521_DECOMPRESS_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p521_decompress_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p521_validate.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p521_validate.rs index c83c3be5265..e8cf95eb86c 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p521_validate.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ec_p521_validate.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.EcKey.Instance.validatePublicKeyContract`. The input is public: timing may depend on the pointers and the contents of `public`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ec_p521_validate_public_key_adx` if the CPU supports its features (`VG_EC_P521_VALIDATE_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p521_validate_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ec_p521_validate_public_key_adx` if the CPU supports its features (`VG_EC_P521_VALIDATE_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ec_p521_validate_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdh_p256.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdh_p256.rs index c7ba04ea543..46bc373158f 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdh_p256.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdh_p256.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ecdh.Instance.exchangeContract`. Constant time but for the peer's public key: timing may depend on the pointers and the contents of `peer`, not on the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdh_p256_adx` if the CPU supports its features (`VG_ECDH_P256_ADX_FEATURES`); or else `vg_ecdh_p256`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdh_p256_adx` if the CPU supports its features (`VG_ECDH_P256_ADX_FEATURES`); or else `vg_ecdh_p256`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdh_p384.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdh_p384.rs index a3f35384953..3a14a8118f2 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdh_p384.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdh_p384.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ecdh.Instance.exchangeContract`. Constant time but for the peer's public key: timing may depend on the pointers and the contents of `peer`, not on the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdh_p384_adx` if the CPU supports its features (`VG_ECDH_P384_ADX_FEATURES`); or else `vg_ecdh_p384`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdh_p384_adx` if the CPU supports its features (`VG_ECDH_P384_ADX_FEATURES`); or else `vg_ecdh_p384`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdh_p521.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdh_p521.rs index 6a6b6204a9b..12c8353c775 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdh_p521.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdh_p521.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Ecdh.Instance.exchangeContract`. Constant time but for the peer's public key: timing may depend on the pointers and the contents of `peer`, not on the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdh_p521_adx` if the CPU supports its features (`VG_ECDH_P521_ADX_FEATURES`); or else `vg_ecdh_p521`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdh_p521_adx` if the CPU supports its features (`VG_ECDH_P521_ADX_FEATURES`); or else `vg_ecdh_p521`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_brainpoolp256r1.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_brainpoolp256r1.rs index aee62163442..25a56000a28 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_brainpoolp256r1.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_brainpoolp256r1.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_brainpoolp256r1` functions for `x86_64` //! (`crate::ecdsa_brainpoolp256r1`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over brainpoolP256r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_brainpoolp256r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 65 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 64 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_brainpoolp256r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over brainpoolP256r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_brainpoolp256r1_sign_auto(_dit: verified_garbage_cpu::Dit // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over brainpoolP256r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_brainpoolp256r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 65 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 64 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_brainpoolp256r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_brainpoolp256r1::vg_ecdsa_brainpoolp256r1_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_brainpoolp384r1.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_brainpoolp384r1.rs index dd4fdda5c97..64e71ed28e1 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_brainpoolp384r1.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_brainpoolp384r1.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_brainpoolp384r1` functions for `x86_64` //! (`crate::ecdsa_brainpoolp384r1`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over brainpoolP384r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_brainpoolp384r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 97 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 96 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_brainpoolp384r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over brainpoolP384r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (48 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (48 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_brainpoolp384r1_sign_auto(_dit: verified_garbage_cpu::Dit // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over brainpoolP384r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_brainpoolp384r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 97 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 96 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_brainpoolp384r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_brainpoolp384r1::vg_ecdsa_brainpoolp384r1_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_brainpoolp512r1.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_brainpoolp512r1.rs index 2ba7f1ccadb..768308c1304 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_brainpoolp512r1.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_brainpoolp512r1.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_brainpoolp512r1` functions for `x86_64` //! (`crate::ecdsa_brainpoolp512r1`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over brainpoolP512r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 64 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 512 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 64 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_brainpoolp512r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 129 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 128 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_brainpoolp512r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 129], digest: *const u8, digest_len: usize, sig: *const [u8; 128], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over brainpoolP512r1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (64 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (64 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 128 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_brainpoolp512r1_sign_auto(_dit: verified_garbage_cpu::Dit // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over brainpoolP512r1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 64 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 512 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 64 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_brainpoolp512r1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 129 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 128 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_brainpoolp512r1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 129], digest: *const u8, digest_len: usize, sig: *const [u8; 128], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_brainpoolp512r1::vg_ecdsa_brainpoolp512r1_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_p192.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_p192.rs index 3ace0e0a533..d7b844fe83f 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_p192.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_p192.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_p192` functions for `x86_64` //! (`crate::ecdsa_p192`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over P-192 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 24 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 192 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 24 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_p192_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 49 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 48 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_p192_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 49], digest: *const u8, digest_len: usize, sig: *const [u8; 48], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_p192::vg_ecdsa_p192_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over P-192 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (24 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (24 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_p192_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_p192::vg_ecdsa_p192_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over P-192 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 24 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 192 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 24 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_p192_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 49 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 48 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_p192_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 49], digest: *const u8, digest_len: usize, sig: *const [u8; 48], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p192::vg_ecdsa_p192_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_p224.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_p224.rs index 9daa56bf41e..12bfaaa8891 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_p224.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_p224.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_p224` functions for `x86_64` //! (`crate::ecdsa_p224`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over P-224 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 28 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 224 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 28 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_p224_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 57 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 56 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_p224_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 57], digest: *const u8, digest_len: usize, sig: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_p224::vg_ecdsa_p224_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over P-224 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (28 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (28 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_p224_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_p224::vg_ecdsa_p224_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over P-224 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 28 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 224 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 28 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_p224_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 57 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 56 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_p224_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 57], digest: *const u8, digest_len: usize, sig: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_p224::vg_ecdsa_p224_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_p256.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_p256.rs index d11c03d9630..f464b1a5eb5 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_p256.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_p256.rs @@ -2,51 +2,47 @@ //! The dispatchers of the verified `ecdsa_p256` functions for `x86_64` //! (`crate::ecdsa_p256`, `VG.Rust.dispatcher`). -/// Deterministic ECDSA signature generation over P-256 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). +/// ECDSA signature verification over P-256 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. /// -/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdsa_p256_sign_adx` if the CPU supports its features (`VG_ECDSA_P256_SIGN_ADX_FEATURES`); or else `vg_ecdsa_p256_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdsa_p256_verify_adx` if the CPU supports its features (`VG_ECDSA_P256_VERIFY_ADX_FEATURES`); or else `vg_ecdsa_p256_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 64 bytes. -/// * `d` must be valid for reads of 32 bytes. +/// * `public` must be valid for reads of 65 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 64 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. -/// * `digest_len` must be the digest length of the hash function `hmac` names. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). -/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the return address on the stack or the 632 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p256_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { +pub unsafe fn vg_ecdsa_p256_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32; + type Impl = unsafe extern "sysv64" fn(public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::ecdsa_p256::VG_ECDSA_P256_SIGN_ADX_FEATURES) } - || cpu.contains(crate::ecdsa_p256::VG_ECDSA_P256_SIGN_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ecdsa_p256::VG_ECDSA_P256_VERIFY_ADX_FEATURES) } + || cpu.contains(crate::ecdsa_p256::VG_ECDSA_P256_VERIFY_ADX_FEATURES) { - return crate::ecdsa_p256::vg_ecdsa_p256_sign_adx; + return crate::ecdsa_p256::vg_ecdsa_p256_verify_adx; } - crate::ecdsa_p256::vg_ecdsa_p256_sign + crate::ecdsa_p256::vg_ecdsa_p256_verify } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { + unsafe extern "sysv64" fn first(public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, d, digest, digest_len, scratch, hmac) } + unsafe { f(public, digest, digest_len, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -54,50 +50,54 @@ pub unsafe fn vg_ecdsa_p256_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, d, digest, digest_len, scratch, hmac) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(public, digest, digest_len, sig, scratch) } } -/// ECDSA signature verification over P-256 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// Deterministic ECDSA signature generation over P-256 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdsa_p256_verify_adx` if the CPU supports its features (`VG_ECDSA_P256_VERIFY_ADX_FEATURES`); or else `vg_ecdsa_p256_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdsa_p256_sign_adx` if the CPU supports its features (`VG_ECDSA_P256_SIGN_ADX_FEATURES`); or else `vg_ecdsa_p256_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `public` must be valid for reads of 65 bytes. +/// * `out` must be valid for reads and writes of 64 bytes. +/// * `d` must be valid for reads of 32 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 64 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. +/// * `digest_len` must be the digest length of the hash function `hmac` names. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). +/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). +/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the return address on the stack or the 632 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p256_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ecdsa_p256_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32; + type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::ecdsa_p256::VG_ECDSA_P256_VERIFY_ADX_FEATURES) } - || cpu.contains(crate::ecdsa_p256::VG_ECDSA_P256_VERIFY_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ecdsa_p256::VG_ECDSA_P256_SIGN_ADX_FEATURES) } + || cpu.contains(crate::ecdsa_p256::VG_ECDSA_P256_SIGN_ADX_FEATURES) { - return crate::ecdsa_p256::vg_ecdsa_p256_verify_adx; + return crate::ecdsa_p256::vg_ecdsa_p256_sign_adx; } - crate::ecdsa_p256::vg_ecdsa_p256_verify + crate::ecdsa_p256::vg_ecdsa_p256_sign } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { + unsafe extern "sysv64" fn first(out: *mut [u8; 64], d: *const [u8; 32], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(public, digest, digest_len, sig, scratch) } + unsafe { f(out, d, digest, digest_len, scratch, hmac) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -105,5 +105,5 @@ pub unsafe fn vg_ecdsa_p256_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pub // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(public, digest, digest_len, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, d, digest, digest_len, scratch, hmac) } } diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_p384.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_p384.rs index 431a71ba005..42254516e28 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_p384.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_p384.rs @@ -2,51 +2,47 @@ //! The dispatchers of the verified `ecdsa_p384` functions for `x86_64` //! (`crate::ecdsa_p384`, `VG.Rust.dispatcher`). -/// Deterministic ECDSA signature generation over P-384 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (48 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (48 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). +/// ECDSA signature verification over P-384 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. /// -/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdsa_p384_sign_adx` if the CPU supports its features (`VG_ECDSA_P384_SIGN_ADX_FEATURES`); or else `vg_ecdsa_p384_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdsa_p384_verify_adx` if the CPU supports its features (`VG_ECDSA_P384_VERIFY_ADX_FEATURES`); or else `vg_ecdsa_p384_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 96 bytes. -/// * `d` must be valid for reads of 48 bytes. +/// * `public` must be valid for reads of 97 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 96 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. -/// * `digest_len` must be the digest length of the hash function `hmac` names. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). -/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the return address on the stack or the 632 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p384_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { +pub unsafe fn vg_ecdsa_p384_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32; + type Impl = unsafe extern "sysv64" fn(public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::ecdsa_p384::VG_ECDSA_P384_SIGN_ADX_FEATURES) } - || cpu.contains(crate::ecdsa_p384::VG_ECDSA_P384_SIGN_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ecdsa_p384::VG_ECDSA_P384_VERIFY_ADX_FEATURES) } + || cpu.contains(crate::ecdsa_p384::VG_ECDSA_P384_VERIFY_ADX_FEATURES) { - return crate::ecdsa_p384::vg_ecdsa_p384_sign_adx; + return crate::ecdsa_p384::vg_ecdsa_p384_verify_adx; } - crate::ecdsa_p384::vg_ecdsa_p384_sign + crate::ecdsa_p384::vg_ecdsa_p384_verify } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { + unsafe extern "sysv64" fn first(public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, d, digest, digest_len, scratch, hmac) } + unsafe { f(public, digest, digest_len, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -54,50 +50,54 @@ pub unsafe fn vg_ecdsa_p384_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, d, digest, digest_len, scratch, hmac) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(public, digest, digest_len, sig, scratch) } } -/// ECDSA signature verification over P-384 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 48 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 384 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 48 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// Deterministic ECDSA signature generation over P-384 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (48 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (48 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdsa_p384_verify_adx` if the CPU supports its features (`VG_ECDSA_P384_VERIFY_ADX_FEATURES`); or else `vg_ecdsa_p384_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdsa_p384_sign_adx` if the CPU supports its features (`VG_ECDSA_P384_SIGN_ADX_FEATURES`); or else `vg_ecdsa_p384_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `public` must be valid for reads of 97 bytes. +/// * `out` must be valid for reads and writes of 96 bytes. +/// * `d` must be valid for reads of 48 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 96 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. +/// * `digest_len` must be the digest length of the hash function `hmac` names. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). +/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). +/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the return address on the stack or the 632 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p384_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ecdsa_p384_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32; + type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::ecdsa_p384::VG_ECDSA_P384_VERIFY_ADX_FEATURES) } - || cpu.contains(crate::ecdsa_p384::VG_ECDSA_P384_VERIFY_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ecdsa_p384::VG_ECDSA_P384_SIGN_ADX_FEATURES) } + || cpu.contains(crate::ecdsa_p384::VG_ECDSA_P384_SIGN_ADX_FEATURES) { - return crate::ecdsa_p384::vg_ecdsa_p384_verify_adx; + return crate::ecdsa_p384::vg_ecdsa_p384_sign_adx; } - crate::ecdsa_p384::vg_ecdsa_p384_verify + crate::ecdsa_p384::vg_ecdsa_p384_sign } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(public: *const [u8; 97], digest: *const u8, digest_len: usize, sig: *const [u8; 96], scratch: *mut [u64; 1024]) -> u32 { + unsafe extern "sysv64" fn first(out: *mut [u8; 96], d: *const [u8; 48], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(public, digest, digest_len, sig, scratch) } + unsafe { f(out, d, digest, digest_len, scratch, hmac) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -105,5 +105,5 @@ pub unsafe fn vg_ecdsa_p384_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pub // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(public, digest, digest_len, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, d, digest, digest_len, scratch, hmac) } } diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_p521.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_p521.rs index ee8e7065fe7..c84b7fcf512 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_p521.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_p521.rs @@ -2,51 +2,47 @@ //! The dispatchers of the verified `ecdsa_p521` functions for `x86_64` //! (`crate::ecdsa_p521`, `VG.Rust.dispatcher`). -/// Deterministic ECDSA signature generation over P-521 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (66 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (66 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). +/// ECDSA signature verification over P-521 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 66 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 521 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 66 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. /// -/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdsa_p521_sign_adx` if the CPU supports its features (`VG_ECDSA_P521_SIGN_ADX_FEATURES`); or else `vg_ecdsa_p521_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdsa_p521_verify_adx` if the CPU supports its features (`VG_ECDSA_P521_VERIFY_ADX_FEATURES`); or else `vg_ecdsa_p521_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 132 bytes. -/// * `d` must be valid for reads of 66 bytes. +/// * `public` must be valid for reads of 133 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 132 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. -/// * `digest_len` must be the digest length of the hash function `hmac` names. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). -/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). -/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the return address on the stack or the 632 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p521_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 132], d: *const [u8; 66], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { +pub unsafe fn vg_ecdsa_p521_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 133], digest: *const u8, digest_len: usize, sig: *const [u8; 132], scratch: *mut [u64; 1024]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 132], d: *const [u8; 66], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32; + type Impl = unsafe extern "sysv64" fn(public: *const [u8; 133], digest: *const u8, digest_len: usize, sig: *const [u8; 132], scratch: *mut [u64; 1024]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::ecdsa_p521::VG_ECDSA_P521_SIGN_ADX_FEATURES) } - || cpu.contains(crate::ecdsa_p521::VG_ECDSA_P521_SIGN_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ecdsa_p521::VG_ECDSA_P521_VERIFY_ADX_FEATURES) } + || cpu.contains(crate::ecdsa_p521::VG_ECDSA_P521_VERIFY_ADX_FEATURES) { - return crate::ecdsa_p521::vg_ecdsa_p521_sign_adx; + return crate::ecdsa_p521::vg_ecdsa_p521_verify_adx; } - crate::ecdsa_p521::vg_ecdsa_p521_sign + crate::ecdsa_p521::vg_ecdsa_p521_verify } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(out: *mut [u8; 132], d: *const [u8; 66], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { + unsafe extern "sysv64" fn first(public: *const [u8; 133], digest: *const u8, digest_len: usize, sig: *const [u8; 132], scratch: *mut [u64; 1024]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, d, digest, digest_len, scratch, hmac) } + unsafe { f(public, digest, digest_len, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -54,50 +50,54 @@ pub unsafe fn vg_ecdsa_p521_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, d, digest, digest_len, scratch, hmac) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(public, digest, digest_len, sig, scratch) } } -/// ECDSA signature verification over P-521 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 66 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 521 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 66 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// Deterministic ECDSA signature generation over P-521 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (66 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (66 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ecdsa_p521_verify_adx` if the CPU supports its features (`VG_ECDSA_P521_VERIFY_ADX_FEATURES`); or else `vg_ecdsa_p521_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ecdsa_p521_sign_adx` if the CPU supports its features (`VG_ECDSA_P521_SIGN_ADX_FEATURES`); or else `vg_ecdsa_p521_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `public` must be valid for reads of 133 bytes. +/// * `out` must be valid for reads and writes of 132 bytes. +/// * `d` must be valid for reads of 66 bytes. /// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 132 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be valid for reads of `6 * size_of::()` bytes. +/// * `digest_len` must be the digest length of the hash function `hmac` names. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `hmac` is a table of HMAC over the hash function of the digest, which RFC 6979 uses: the one of `VG.Spec.Mgf1.hashes` at the index in its first word of data (the HMAC tables of this crate, `fn_tables`, say which they name). +/// * `out` and `scratch` must not overlap each other, `d`, `digest` or `hmac` (distinct Rust objects never do). +/// * None of `out`, `d`, `digest`, `scratch` and `hmac` may overlap the return address on the stack or the 632 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `hmac` must be the `as_ptr()` of one of the `Hmac` tables of functions of this crate (`fn_tables`), and the CPU must support the features its constant `
_FEATURES` gives, if it has one. #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ecdsa_p521_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 133], digest: *const u8, digest_len: usize, sig: *const [u8; 132], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ecdsa_p521_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 132], d: *const [u8; 66], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(public: *const [u8; 133], digest: *const u8, digest_len: usize, sig: *const [u8; 132], scratch: *mut [u64; 1024]) -> u32; + type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 132], d: *const [u8; 66], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::ecdsa_p521::VG_ECDSA_P521_VERIFY_ADX_FEATURES) } - || cpu.contains(crate::ecdsa_p521::VG_ECDSA_P521_VERIFY_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ecdsa_p521::VG_ECDSA_P521_SIGN_ADX_FEATURES) } + || cpu.contains(crate::ecdsa_p521::VG_ECDSA_P521_SIGN_ADX_FEATURES) { - return crate::ecdsa_p521::vg_ecdsa_p521_verify_adx; + return crate::ecdsa_p521::vg_ecdsa_p521_sign_adx; } - crate::ecdsa_p521::vg_ecdsa_p521_verify + crate::ecdsa_p521::vg_ecdsa_p521_sign } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(public: *const [u8; 133], digest: *const u8, digest_len: usize, sig: *const [u8; 132], scratch: *mut [u64; 1024]) -> u32 { + unsafe extern "sysv64" fn first(out: *mut [u8; 132], d: *const [u8; 66], digest: *const u8, digest_len: usize, scratch: *mut [u64; 1024], hmac: *const [usize; 6]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(public, digest, digest_len, sig, scratch) } + unsafe { f(out, d, digest, digest_len, scratch, hmac) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -105,5 +105,5 @@ pub unsafe fn vg_ecdsa_p521_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pub // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(public, digest, digest_len, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, d, digest, digest_len, scratch, hmac) } } diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_secp256k1.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_secp256k1.rs index 2ec88cfa656..15a385960c3 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_secp256k1.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ecdsa_secp256k1.rs @@ -2,6 +2,28 @@ //! The dispatchers of the verified `ecdsa_secp256k1` functions for `x86_64` //! (`crate::ecdsa_secp256k1`, `VG.Rust.dispatcher`). +/// ECDSA signature verification over secp256k1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. +/// +/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. +/// +/// It runs `vg_ecdsa_secp256k1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `public` must be valid for reads of 65 bytes. +/// * `digest` must be valid for reads of `digest_len` bytes. +/// * `sig` must be valid for reads of 64 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. +/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). +/// * None of `public`, `digest`, `sig` and `scratch` may overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ecdsa_secp256k1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_verify(public, digest, digest_len, sig, scratch) } +} + /// Deterministic ECDSA signature generation over secp256k1 with HMAC over the hash function of the table `hmac` (RFC 6979 §3.2; FIPS 186-5 §6.4.1): with the private key `d` (32 bytes, most significant first), signs the hash at `digest` (`digest_len` bytes, the hash function's output), deriving the per-message secret number `k` from the key and the hash. Returns 1 and writes `r` then `s` (32 bytes each, most significant first) to `*out`; or returns 0 and writes zeros to `*out` if `d` is not in `[1, n-1]`, or if none of the first 8 candidates for `k` is suitable (which does not happen in practice). /// /// Contract: `VG.Spec.Ecdsa.Rfc6979.TableInstance.signContract`. Constant time but for the number of candidates for `k` tried (almost always 1) and which table `hmac` is: timing may depend on the pointers, that number and the table, not otherwise on the key or the hash. @@ -27,25 +49,3 @@ pub unsafe fn vg_ecdsa_secp256k1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_sign(out, d, digest, digest_len, scratch, hmac) } } - -/// ECDSA signature verification over secp256k1 (FIPS 186-5 §6.4.2): whether the signature at `sig` (`r` then `s`, 32 bytes each, most significant first) of the hash at `digest` (`digest_len` bytes, the hash function's output, of any length; `e` is its leftmost 256 bits, or all of it if it is shorter) verifies with the public key at `public`, which must be a valid public key in the uncompressed form of SEC 1 §2.3.3 (`04`, then `x` and `y` in 32 bytes each, most significant first, both below `p`, on the curve; SP 800-56A §5.6.2.3.3). Returns 1 if it does, and 0 if it does not or the public key is not valid. -/// -/// Contract: `VG.Spec.Ecdsa.Instance.verifyHashContract`. Every input is public: timing may depend on the pointers, `digest_len` and the contents of `public`, `digest` and `sig`. -/// -/// It runs `vg_ecdsa_secp256k1_verify`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `public` must be valid for reads of 65 bytes. -/// * `digest` must be valid for reads of `digest_len` bytes. -/// * `sig` must be valid for reads of 64 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The contents of `scratch` on return are unspecified; the caller may reuse or destroy them. -/// * `scratch` must not overlap `public`, `digest` or `sig` (distinct Rust objects never do). -/// * None of `public`, `digest`, `sig` and `scratch` may overlap the return address on the stack or the 96 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ecdsa_secp256k1_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, public: *const [u8; 65], digest: *const u8, digest_len: usize, sig: *const [u8; 64], scratch: *mut [u64; 1024]) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ecdsa_secp256k1::vg_ecdsa_secp256k1_verify(public, digest, digest_len, sig, scratch) } -} diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ed25519.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ed25519.rs index cb4a7a9cdba..48097d79b65 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ed25519.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ed25519.rs @@ -4,7 +4,7 @@ /// Ed25519 verification (RFC 8032 §5.1.7): returns 1 if the 64-byte signature at `signature` verifies for the 32-byte public key at `pk` and the `len` bytes at `message`, and 0 otherwise. Uses pure Ed25519, with no context or prehash. Checks canonical point encodings, S < L, and `[S]B = R + [k]A` with the challenge k = SHA-512(R || A || M) reduced modulo L, as in RFC 8032 §6. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed25519.verifyContract`. Not constant time: timing may depend on the public key, message and signature. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ed25519_verify_shani_ifma` if the CPU supports its features (`VG_ED25519_VERIFY_SHANI_IFMA_FEATURES`); `vg_ed25519_verify_shani_adx` if the CPU supports its features (`VG_ED25519_VERIFY_SHANI_ADX_FEATURES`); `vg_ed25519_verify_avx2_ifma` if the CPU supports its features (`VG_ED25519_VERIFY_AVX2_IFMA_FEATURES`); `vg_ed25519_verify_avx2_adx` if the CPU supports its features (`VG_ED25519_VERIFY_AVX2_ADX_FEATURES`); `vg_ed25519_verify_ifma` if the CPU supports its features (`VG_ED25519_VERIFY_IFMA_FEATURES`); `vg_ed25519_verify_shani` if the CPU supports its features (`VG_ED25519_VERIFY_SHANI_FEATURES`); `vg_ed25519_verify_avx2` if the CPU supports its features (`VG_ED25519_VERIFY_AVX2_FEATURES`); `vg_ed25519_verify_adx` if the CPU supports its features (`VG_ED25519_VERIFY_ADX_FEATURES`); or else `vg_ed25519_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ed25519_verify_shani_ifma` if the CPU supports its features (`VG_ED25519_VERIFY_SHANI_IFMA_FEATURES`); `vg_ed25519_verify_shani_adx` if the CPU supports its features (`VG_ED25519_VERIFY_SHANI_ADX_FEATURES`); `vg_ed25519_verify_shani` if the CPU supports its features (`VG_ED25519_VERIFY_SHANI_FEATURES`); `vg_ed25519_verify_avx2_ifma` if the CPU supports its features (`VG_ED25519_VERIFY_AVX2_IFMA_FEATURES`); `vg_ed25519_verify_avx2_adx` if the CPU supports its features (`VG_ED25519_VERIFY_AVX2_ADX_FEATURES`); `vg_ed25519_verify_avx2` if the CPU supports its features (`VG_ED25519_VERIFY_AVX2_FEATURES`); `vg_ed25519_verify_ifma` if the CPU supports its features (`VG_ED25519_VERIFY_IFMA_FEATURES`); `vg_ed25519_verify_adx` if the CPU supports its features (`VG_ED25519_VERIFY_ADX_FEATURES`); or else `vg_ed25519_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -35,6 +35,11 @@ pub unsafe fn vg_ed25519_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *c { return crate::ed25519::vg_ed25519_verify_shani_adx; } + if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_VERIFY_SHANI_FEATURES) } + || cpu.contains(crate::ed25519::VG_ED25519_VERIFY_SHANI_FEATURES) + { + return crate::ed25519::vg_ed25519_verify_shani; + } if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_VERIFY_AVX2_IFMA_FEATURES) } || cpu.contains(crate::ed25519::VG_ED25519_VERIFY_AVX2_IFMA_FEATURES) { @@ -45,21 +50,16 @@ pub unsafe fn vg_ed25519_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *c { return crate::ed25519::vg_ed25519_verify_avx2_adx; } - if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_VERIFY_IFMA_FEATURES) } - || cpu.contains(crate::ed25519::VG_ED25519_VERIFY_IFMA_FEATURES) - { - return crate::ed25519::vg_ed25519_verify_ifma; - } - if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_VERIFY_SHANI_FEATURES) } - || cpu.contains(crate::ed25519::VG_ED25519_VERIFY_SHANI_FEATURES) - { - return crate::ed25519::vg_ed25519_verify_shani; - } if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_VERIFY_AVX2_FEATURES) } || cpu.contains(crate::ed25519::VG_ED25519_VERIFY_AVX2_FEATURES) { return crate::ed25519::vg_ed25519_verify_avx2; } + if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_VERIFY_IFMA_FEATURES) } + || cpu.contains(crate::ed25519::VG_ED25519_VERIFY_IFMA_FEATURES) + { + return crate::ed25519::vg_ed25519_verify_ifma; + } if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_VERIFY_ADX_FEATURES) } || cpu.contains(crate::ed25519::VG_ED25519_VERIFY_ADX_FEATURES) { @@ -86,82 +86,83 @@ pub unsafe fn vg_ed25519_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *c unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, message, len, signature, scratch) } } -/// Ed25519 key-pair derivation (RFC 8032 §5.1.5): for the 32-byte private seed at `seed`, writes its 64-byte SHA-512 digest (§5.1.5, step 1) to `*expanded` and its 32-byte public key to `*out`, including pruning: the digest and public key that `vg_ed25519_sign_expanded` takes. Contract: `VG.Spec.Ed25519.keyPairContract`. Constant time: only pointers may affect timing. +/// Deterministic Ed25519 signing (RFC 8032 §5.1.6): writes the 64-byte signature to `*out`, for the `len` bytes at `message` and the 32-byte private seed whose SHA-512 digest (§5.1.6, step 1) is cached at `expanded`, using its cached public key at `pk`. Includes the remaining hashing, pruning and scalar/group operations. Uses pure Ed25519, with no context or prehash. Contract: `VG.Spec.Ed25519.signExpandedContract`. Constant time: only pointers and the message length may affect timing, not any buffer contents. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ed25519_key_pair_shani_ifma` if the CPU supports its features (`VG_ED25519_KEY_PAIR_SHANI_IFMA_FEATURES`); `vg_ed25519_key_pair_shani_adx` if the CPU supports its features (`VG_ED25519_KEY_PAIR_SHANI_ADX_FEATURES`); `vg_ed25519_key_pair_avx2_ifma` if the CPU supports its features (`VG_ED25519_KEY_PAIR_AVX2_IFMA_FEATURES`); `vg_ed25519_key_pair_avx2_adx` if the CPU supports its features (`VG_ED25519_KEY_PAIR_AVX2_ADX_FEATURES`); `vg_ed25519_key_pair_ifma` if the CPU supports its features (`VG_ED25519_KEY_PAIR_IFMA_FEATURES`); `vg_ed25519_key_pair_shani` if the CPU supports its features (`VG_ED25519_KEY_PAIR_SHANI_FEATURES`); `vg_ed25519_key_pair_avx2` if the CPU supports its features (`VG_ED25519_KEY_PAIR_AVX2_FEATURES`); `vg_ed25519_key_pair_adx` if the CPU supports its features (`VG_ED25519_KEY_PAIR_ADX_FEATURES`); or else `vg_ed25519_key_pair`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ed25519_sign_expanded_shani_ifma` if the CPU supports its features (`VG_ED25519_SIGN_EXPANDED_SHANI_IFMA_FEATURES`); `vg_ed25519_sign_expanded_shani_adx` if the CPU supports its features (`VG_ED25519_SIGN_EXPANDED_SHANI_ADX_FEATURES`); `vg_ed25519_sign_expanded_shani` if the CPU supports its features (`VG_ED25519_SIGN_EXPANDED_SHANI_FEATURES`); `vg_ed25519_sign_expanded_avx2_ifma` if the CPU supports its features (`VG_ED25519_SIGN_EXPANDED_AVX2_IFMA_FEATURES`); `vg_ed25519_sign_expanded_avx2_adx` if the CPU supports its features (`VG_ED25519_SIGN_EXPANDED_AVX2_ADX_FEATURES`); `vg_ed25519_sign_expanded_avx2` if the CPU supports its features (`VG_ED25519_SIGN_EXPANDED_AVX2_FEATURES`); `vg_ed25519_sign_expanded_ifma` if the CPU supports its features (`VG_ED25519_SIGN_EXPANDED_IFMA_FEATURES`); `vg_ed25519_sign_expanded_adx` if the CPU supports its features (`VG_ED25519_SIGN_EXPANDED_ADX_FEATURES`); or else `vg_ed25519_sign_expanded`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 32 bytes. -/// * `expanded` must be valid for reads and writes of 64 bytes. -/// * `seed` must be valid for reads of 32 bytes. +/// * `out` must be valid for reads and writes of 64 bytes. +/// * `expanded` must be valid for reads of 64 bytes. +/// * `pk` must be valid for reads of 32 bytes. +/// * `message` must be valid for reads of `len` bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The 64 bytes at `expanded` must equal the SHA-512 digest (`VG.Spec.Sha512.sha512`) of a 32-byte seed, and the 32 bytes at `pk` the public key derived from that seed by RFC 8032 §5.1.5 (`VG.Spec.Ed25519.publicKey`). /// * The seed must originate from a cryptographically secure random generator. -/// * The 64 bytes written to `expanded` are secret: the caller must destroy them after use. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out`, `expanded` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `out`, `expanded`, `seed` and `scratch` may overlap the return address on the stack or the 1632 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `expanded`, `pk` or `message` (distinct Rust objects never do). +/// * None of `out`, `expanded`, `pk`, `message` and `scratch` may overlap the return address on the stack or the 264 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ed25519_key_pair_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], expanded: *mut [u8; 64], seed: *const [u8; 32], scratch: *mut [u64; 1024]) { +pub unsafe fn vg_ed25519_sign_expanded_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 64], expanded: *const [u8; 64], pk: *const [u8; 32], message: *const u8, len: usize, scratch: *mut [u64; 1024]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 32], expanded: *mut [u8; 64], seed: *const [u8; 32], scratch: *mut [u64; 1024]); + type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 64], expanded: *const [u8; 64], pk: *const [u8; 32], message: *const u8, len: usize, scratch: *mut [u64; 1024]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_KEY_PAIR_SHANI_IFMA_FEATURES) } - || cpu.contains(crate::ed25519::VG_ED25519_KEY_PAIR_SHANI_IFMA_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_SHANI_IFMA_FEATURES) } + || cpu.contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_SHANI_IFMA_FEATURES) { - return crate::ed25519::vg_ed25519_key_pair_shani_ifma; + return crate::ed25519::vg_ed25519_sign_expanded_shani_ifma; } - if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_KEY_PAIR_SHANI_ADX_FEATURES) } - || cpu.contains(crate::ed25519::VG_ED25519_KEY_PAIR_SHANI_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_SHANI_ADX_FEATURES) } + || cpu.contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_SHANI_ADX_FEATURES) { - return crate::ed25519::vg_ed25519_key_pair_shani_adx; + return crate::ed25519::vg_ed25519_sign_expanded_shani_adx; } - if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_KEY_PAIR_AVX2_IFMA_FEATURES) } - || cpu.contains(crate::ed25519::VG_ED25519_KEY_PAIR_AVX2_IFMA_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_SHANI_FEATURES) } + || cpu.contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_SHANI_FEATURES) { - return crate::ed25519::vg_ed25519_key_pair_avx2_ifma; + return crate::ed25519::vg_ed25519_sign_expanded_shani; } - if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_KEY_PAIR_AVX2_ADX_FEATURES) } - || cpu.contains(crate::ed25519::VG_ED25519_KEY_PAIR_AVX2_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_AVX2_IFMA_FEATURES) } + || cpu.contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_AVX2_IFMA_FEATURES) { - return crate::ed25519::vg_ed25519_key_pair_avx2_adx; + return crate::ed25519::vg_ed25519_sign_expanded_avx2_ifma; } - if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_KEY_PAIR_IFMA_FEATURES) } - || cpu.contains(crate::ed25519::VG_ED25519_KEY_PAIR_IFMA_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_AVX2_ADX_FEATURES) } + || cpu.contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_AVX2_ADX_FEATURES) { - return crate::ed25519::vg_ed25519_key_pair_ifma; + return crate::ed25519::vg_ed25519_sign_expanded_avx2_adx; } - if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_KEY_PAIR_SHANI_FEATURES) } - || cpu.contains(crate::ed25519::VG_ED25519_KEY_PAIR_SHANI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_AVX2_FEATURES) } + || cpu.contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_AVX2_FEATURES) { - return crate::ed25519::vg_ed25519_key_pair_shani; + return crate::ed25519::vg_ed25519_sign_expanded_avx2; } - if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_KEY_PAIR_AVX2_FEATURES) } - || cpu.contains(crate::ed25519::VG_ED25519_KEY_PAIR_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_IFMA_FEATURES) } + || cpu.contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_IFMA_FEATURES) { - return crate::ed25519::vg_ed25519_key_pair_avx2; + return crate::ed25519::vg_ed25519_sign_expanded_ifma; } - if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_KEY_PAIR_ADX_FEATURES) } - || cpu.contains(crate::ed25519::VG_ED25519_KEY_PAIR_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_ADX_FEATURES) } + || cpu.contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_ADX_FEATURES) { - return crate::ed25519::vg_ed25519_key_pair_adx; + return crate::ed25519::vg_ed25519_sign_expanded_adx; } - crate::ed25519::vg_ed25519_key_pair + crate::ed25519::vg_ed25519_sign_expanded } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(out: *mut [u8; 32], expanded: *mut [u8; 64], seed: *const [u8; 32], scratch: *mut [u64; 1024]) { + unsafe extern "sysv64" fn first(out: *mut [u8; 64], expanded: *const [u8; 64], pk: *const [u8; 32], message: *const u8, len: usize, scratch: *mut [u64; 1024]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, expanded, seed, scratch) } + unsafe { f(out, expanded, pk, message, len, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -169,86 +170,85 @@ pub unsafe fn vg_ed25519_key_pair_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, expanded, seed, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, expanded, pk, message, len, scratch) } } -/// Deterministic Ed25519 signing (RFC 8032 §5.1.6): writes the 64-byte signature to `*out`, for the `len` bytes at `message` and the 32-byte private seed whose SHA-512 digest (§5.1.6, step 1) is cached at `expanded`, using its cached public key at `pk`. Includes the remaining hashing, pruning and scalar/group operations. Uses pure Ed25519, with no context or prehash. Contract: `VG.Spec.Ed25519.signExpandedContract`. Constant time: only pointers and the message length may affect timing, not any buffer contents. +/// Ed25519 key-pair derivation (RFC 8032 §5.1.5): for the 32-byte private seed at `seed`, writes its 64-byte SHA-512 digest (§5.1.5, step 1) to `*expanded` and its 32-byte public key to `*out`, including pruning: the digest and public key that `vg_ed25519_sign_expanded` takes. Contract: `VG.Spec.Ed25519.keyPairContract`. Constant time: only pointers may affect timing. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ed25519_sign_expanded_shani_ifma` if the CPU supports its features (`VG_ED25519_SIGN_EXPANDED_SHANI_IFMA_FEATURES`); `vg_ed25519_sign_expanded_shani_adx` if the CPU supports its features (`VG_ED25519_SIGN_EXPANDED_SHANI_ADX_FEATURES`); `vg_ed25519_sign_expanded_avx2_ifma` if the CPU supports its features (`VG_ED25519_SIGN_EXPANDED_AVX2_IFMA_FEATURES`); `vg_ed25519_sign_expanded_avx2_adx` if the CPU supports its features (`VG_ED25519_SIGN_EXPANDED_AVX2_ADX_FEATURES`); `vg_ed25519_sign_expanded_ifma` if the CPU supports its features (`VG_ED25519_SIGN_EXPANDED_IFMA_FEATURES`); `vg_ed25519_sign_expanded_shani` if the CPU supports its features (`VG_ED25519_SIGN_EXPANDED_SHANI_FEATURES`); `vg_ed25519_sign_expanded_avx2` if the CPU supports its features (`VG_ED25519_SIGN_EXPANDED_AVX2_FEATURES`); `vg_ed25519_sign_expanded_adx` if the CPU supports its features (`VG_ED25519_SIGN_EXPANDED_ADX_FEATURES`); or else `vg_ed25519_sign_expanded`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ed25519_key_pair_shani_ifma` if the CPU supports its features (`VG_ED25519_KEY_PAIR_SHANI_IFMA_FEATURES`); `vg_ed25519_key_pair_shani_adx` if the CPU supports its features (`VG_ED25519_KEY_PAIR_SHANI_ADX_FEATURES`); `vg_ed25519_key_pair_shani` if the CPU supports its features (`VG_ED25519_KEY_PAIR_SHANI_FEATURES`); `vg_ed25519_key_pair_avx2_ifma` if the CPU supports its features (`VG_ED25519_KEY_PAIR_AVX2_IFMA_FEATURES`); `vg_ed25519_key_pair_avx2_adx` if the CPU supports its features (`VG_ED25519_KEY_PAIR_AVX2_ADX_FEATURES`); `vg_ed25519_key_pair_avx2` if the CPU supports its features (`VG_ED25519_KEY_PAIR_AVX2_FEATURES`); `vg_ed25519_key_pair_ifma` if the CPU supports its features (`VG_ED25519_KEY_PAIR_IFMA_FEATURES`); `vg_ed25519_key_pair_adx` if the CPU supports its features (`VG_ED25519_KEY_PAIR_ADX_FEATURES`); or else `vg_ed25519_key_pair`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 64 bytes. -/// * `expanded` must be valid for reads of 64 bytes. -/// * `pk` must be valid for reads of 32 bytes. -/// * `message` must be valid for reads of `len` bytes. +/// * `out` must be valid for reads and writes of 32 bytes. +/// * `expanded` must be valid for reads and writes of 64 bytes. +/// * `seed` must be valid for reads of 32 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The 64 bytes at `expanded` must equal the SHA-512 digest (`VG.Spec.Sha512.sha512`) of a 32-byte seed, and the 32 bytes at `pk` the public key derived from that seed by RFC 8032 §5.1.5 (`VG.Spec.Ed25519.publicKey`). /// * The seed must originate from a cryptographically secure random generator. +/// * The 64 bytes written to `expanded` are secret: the caller must destroy them after use. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `expanded`, `pk` or `message` (distinct Rust objects never do). -/// * None of `out`, `expanded`, `pk`, `message` and `scratch` may overlap the return address on the stack or the 264 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out`, `expanded` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `out`, `expanded`, `seed` and `scratch` may overlap the return address on the stack or the 1632 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ed25519_sign_expanded_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 64], expanded: *const [u8; 64], pk: *const [u8; 32], message: *const u8, len: usize, scratch: *mut [u64; 1024]) { +pub unsafe fn vg_ed25519_key_pair_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], expanded: *mut [u8; 64], seed: *const [u8; 32], scratch: *mut [u64; 1024]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 64], expanded: *const [u8; 64], pk: *const [u8; 32], message: *const u8, len: usize, scratch: *mut [u64; 1024]); + type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 32], expanded: *mut [u8; 64], seed: *const [u8; 32], scratch: *mut [u64; 1024]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_SHANI_IFMA_FEATURES) } - || cpu.contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_SHANI_IFMA_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_KEY_PAIR_SHANI_IFMA_FEATURES) } + || cpu.contains(crate::ed25519::VG_ED25519_KEY_PAIR_SHANI_IFMA_FEATURES) { - return crate::ed25519::vg_ed25519_sign_expanded_shani_ifma; + return crate::ed25519::vg_ed25519_key_pair_shani_ifma; } - if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_SHANI_ADX_FEATURES) } - || cpu.contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_SHANI_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_KEY_PAIR_SHANI_ADX_FEATURES) } + || cpu.contains(crate::ed25519::VG_ED25519_KEY_PAIR_SHANI_ADX_FEATURES) { - return crate::ed25519::vg_ed25519_sign_expanded_shani_adx; + return crate::ed25519::vg_ed25519_key_pair_shani_adx; } - if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_AVX2_IFMA_FEATURES) } - || cpu.contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_AVX2_IFMA_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_KEY_PAIR_SHANI_FEATURES) } + || cpu.contains(crate::ed25519::VG_ED25519_KEY_PAIR_SHANI_FEATURES) { - return crate::ed25519::vg_ed25519_sign_expanded_avx2_ifma; + return crate::ed25519::vg_ed25519_key_pair_shani; } - if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_AVX2_ADX_FEATURES) } - || cpu.contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_AVX2_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_KEY_PAIR_AVX2_IFMA_FEATURES) } + || cpu.contains(crate::ed25519::VG_ED25519_KEY_PAIR_AVX2_IFMA_FEATURES) { - return crate::ed25519::vg_ed25519_sign_expanded_avx2_adx; + return crate::ed25519::vg_ed25519_key_pair_avx2_ifma; } - if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_IFMA_FEATURES) } - || cpu.contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_IFMA_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_KEY_PAIR_AVX2_ADX_FEATURES) } + || cpu.contains(crate::ed25519::VG_ED25519_KEY_PAIR_AVX2_ADX_FEATURES) { - return crate::ed25519::vg_ed25519_sign_expanded_ifma; + return crate::ed25519::vg_ed25519_key_pair_avx2_adx; } - if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_SHANI_FEATURES) } - || cpu.contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_SHANI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_KEY_PAIR_AVX2_FEATURES) } + || cpu.contains(crate::ed25519::VG_ED25519_KEY_PAIR_AVX2_FEATURES) { - return crate::ed25519::vg_ed25519_sign_expanded_shani; + return crate::ed25519::vg_ed25519_key_pair_avx2; } - if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_AVX2_FEATURES) } - || cpu.contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_KEY_PAIR_IFMA_FEATURES) } + || cpu.contains(crate::ed25519::VG_ED25519_KEY_PAIR_IFMA_FEATURES) { - return crate::ed25519::vg_ed25519_sign_expanded_avx2; + return crate::ed25519::vg_ed25519_key_pair_ifma; } - if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_ADX_FEATURES) } - || cpu.contains(crate::ed25519::VG_ED25519_SIGN_EXPANDED_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed25519::VG_ED25519_KEY_PAIR_ADX_FEATURES) } + || cpu.contains(crate::ed25519::VG_ED25519_KEY_PAIR_ADX_FEATURES) { - return crate::ed25519::vg_ed25519_sign_expanded_adx; + return crate::ed25519::vg_ed25519_key_pair_adx; } - crate::ed25519::vg_ed25519_sign_expanded + crate::ed25519::vg_ed25519_key_pair } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(out: *mut [u8; 64], expanded: *const [u8; 64], pk: *const [u8; 32], message: *const u8, len: usize, scratch: *mut [u64; 1024]) { + unsafe extern "sysv64" fn first(out: *mut [u8; 32], expanded: *mut [u8; 64], seed: *const [u8; 32], scratch: *mut [u64; 1024]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, expanded, pk, message, len, scratch) } + unsafe { f(out, expanded, seed, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -256,5 +256,5 @@ pub unsafe fn vg_ed25519_sign_expanded_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, expanded, pk, message, len, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, expanded, seed, scratch) } } diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ed448.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ed448.rs index 2870428575d..60c66a2d9a4 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ed448.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ed448.rs @@ -2,46 +2,45 @@ //! The dispatchers of the verified `ed448` functions for `x86_64` //! (`crate::ed448`, `VG.Rust.dispatcher`). -/// Ed448 verification (RFC 8032 §5.2.7): returns 1 if the 114-byte signature at `signature` verifies for the 57-byte public key at `pk`, the `ctxlen` bytes of context at `context` and the `len` bytes at `message`, and 0 otherwise (always 0 for a context longer than 255 bytes). Uses Ed448 with a context, not Ed448ph (no prehash). Checks canonical point encodings, S < L, and the cofactored equation `[4][S]B = [4]R + [4][k]A` with the challenge k = SHAKE256(dom4(0, C) || R || A || M, 114) reduced modulo L. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed448.verifyContract`. Not constant time: timing may depend on the public key, context, message and signature. +/// Ed448 public-key derivation (RFC 8032 §5.2.5): writes the 57-byte public key to `*out`, from the 57-byte private key at `seed`, including SHAKE256 and pruning. Contract: `VG.Spec.Ed448.publicKeyContract`. Constant time: only pointers may affect timing. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ed448_verify_adx` if the CPU supports its features (`VG_ED448_VERIFY_ADX_FEATURES`); or else `vg_ed448_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ed448_public_key_adx` if the CPU supports its features (`VG_ED448_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ed448_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `pk` must be valid for reads of 57 bytes. -/// * `context` must be valid for reads of `ctxlen` bytes. -/// * `message` must be valid for reads of `len` bytes. -/// * `signature` must be valid for reads of 114 bytes. +/// * `out` must be valid for reads and writes of 57 bytes. +/// * `seed` must be valid for reads of 57 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * `seed` must originate from a cryptographically secure random generator. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `scratch` must not overlap `pk`, `context`, `message`, `signature` or the arguments on the stack (distinct Rust objects never do). -/// * None of `pk`, `context`, `message`, `signature` and `scratch` may overlap the return address on the stack or the 272 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `out`, `seed` and `scratch` may overlap the return address on the stack or the 104 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ed448_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, signature: *const [u8; 114], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_ed448_public_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 57], seed: *const [u8; 57], scratch: *mut [u64; 1024]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, signature: *const [u8; 114], scratch: *mut [u64; 1024]) -> u32; + type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 57], seed: *const [u8; 57], scratch: *mut [u64; 1024]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::ed448::VG_ED448_VERIFY_ADX_FEATURES) } - || cpu.contains(crate::ed448::VG_ED448_VERIFY_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed448::VG_ED448_PUBLIC_KEY_ADX_FEATURES) } + || cpu.contains(crate::ed448::VG_ED448_PUBLIC_KEY_ADX_FEATURES) { - return crate::ed448::vg_ed448_verify_adx; + return crate::ed448::vg_ed448_public_key_adx; } - crate::ed448::vg_ed448_verify + crate::ed448::vg_ed448_public_key } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, signature: *const [u8; 114], scratch: *mut [u64; 1024]) -> u32 { + unsafe extern "sysv64" fn first(out: *mut [u8; 57], seed: *const [u8; 57], scratch: *mut [u64; 1024]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(pk, context, ctxlen, message, len, signature, scratch) } + unsafe { f(out, seed, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -49,48 +48,53 @@ pub unsafe fn vg_ed448_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *con // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, context, ctxlen, message, len, signature, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, seed, scratch) } } -/// Ed448 public-key derivation (RFC 8032 §5.2.5): writes the 57-byte public key to `*out`, from the 57-byte private key at `seed`, including SHAKE256 and pruning. Contract: `VG.Spec.Ed448.publicKeyContract`. Constant time: only pointers may affect timing. +/// Ed448 signing (RFC 8032 §5.2.6): writes the 114-byte signature to `*out`, for the `len` bytes at `message`, the `ctxlen` bytes of context at `context` and the 57-byte private key at `seed`, using its cached public key at `pk`. Includes all hashing, pruning and scalar/group operations. Uses Ed448 with a context, not Ed448ph (no prehash); the context is empty for plain Ed448. Contract: `VG.Spec.Ed448.signCachedContract`. Constant time: only pointers and the context and message lengths may affect timing, not any buffer contents. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ed448_public_key_adx` if the CPU supports its features (`VG_ED448_PUBLIC_KEY_ADX_FEATURES`); or else `vg_ed448_public_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ed448_sign_cached_adx` if the CPU supports its features (`VG_ED448_SIGN_CACHED_ADX_FEATURES`); or else `vg_ed448_sign_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 57 bytes. +/// * `out` must be valid for reads and writes of 114 bytes. /// * `seed` must be valid for reads of 57 bytes. +/// * `pk` must be valid for reads of 57 bytes. +/// * `context` must be valid for reads of `ctxlen` bytes. +/// * `message` must be valid for reads of `len` bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * The 57 bytes at `pk` must equal the public key derived from the 57 bytes at `seed` by RFC 8032 §5.2.5 (`VG.Spec.Ed448.publicKey`). +/// * `ctxlen` must be at most 255. /// * `seed` must originate from a cryptographically secure random generator. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `out`, `seed` and `scratch` may overlap the return address on the stack or the 104 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `seed`, `pk`, `context`, `message` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `seed`, `pk`, `context`, `message` and `scratch` may overlap the return address on the stack or the 464 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ed448_public_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 57], seed: *const [u8; 57], scratch: *mut [u64; 1024]) { +pub unsafe fn vg_ed448_sign_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 114], seed: *const [u8; 57], pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, scratch: *mut [u64; 1024]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 57], seed: *const [u8; 57], scratch: *mut [u64; 1024]); + type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 114], seed: *const [u8; 57], pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, scratch: *mut [u64; 1024]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::ed448::VG_ED448_PUBLIC_KEY_ADX_FEATURES) } - || cpu.contains(crate::ed448::VG_ED448_PUBLIC_KEY_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed448::VG_ED448_SIGN_CACHED_ADX_FEATURES) } + || cpu.contains(crate::ed448::VG_ED448_SIGN_CACHED_ADX_FEATURES) { - return crate::ed448::vg_ed448_public_key_adx; + return crate::ed448::vg_ed448_sign_cached_adx; } - crate::ed448::vg_ed448_public_key + crate::ed448::vg_ed448_sign_cached } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(out: *mut [u8; 57], seed: *const [u8; 57], scratch: *mut [u64; 1024]) { + unsafe extern "sysv64" fn first(out: *mut [u8; 114], seed: *const [u8; 57], pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, scratch: *mut [u64; 1024]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, seed, scratch) } + unsafe { f(out, seed, pk, context, ctxlen, message, len, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -98,53 +102,49 @@ pub unsafe fn vg_ed448_public_key_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, seed, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, seed, pk, context, ctxlen, message, len, scratch) } } -/// Ed448 signing (RFC 8032 §5.2.6): writes the 114-byte signature to `*out`, for the `len` bytes at `message`, the `ctxlen` bytes of context at `context` and the 57-byte private key at `seed`, using its cached public key at `pk`. Includes all hashing, pruning and scalar/group operations. Uses Ed448 with a context, not Ed448ph (no prehash); the context is empty for plain Ed448. Contract: `VG.Spec.Ed448.signCachedContract`. Constant time: only pointers and the context and message lengths may affect timing, not any buffer contents. +/// Ed448 verification (RFC 8032 §5.2.7): returns 1 if the 114-byte signature at `signature` verifies for the 57-byte public key at `pk`, the `ctxlen` bytes of context at `context` and the `len` bytes at `message`, and 0 otherwise (always 0 for a context longer than 255 bytes). Uses Ed448 with a context, not Ed448ph (no prehash). Checks canonical point encodings, S < L, and the cofactored equation `[4][S]B = [4]R + [4][k]A` with the challenge k = SHAKE256(dom4(0, C) || R || A || M, 114) reduced modulo L. No additional subgroup or small-order check is imposed. Contract: `VG.Spec.Ed448.verifyContract`. Not constant time: timing may depend on the public key, context, message and signature. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ed448_sign_cached_adx` if the CPU supports its features (`VG_ED448_SIGN_CACHED_ADX_FEATURES`); or else `vg_ed448_sign_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ed448_verify_adx` if the CPU supports its features (`VG_ED448_VERIFY_ADX_FEATURES`); or else `vg_ed448_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of 114 bytes. -/// * `seed` must be valid for reads of 57 bytes. /// * `pk` must be valid for reads of 57 bytes. /// * `context` must be valid for reads of `ctxlen` bytes. /// * `message` must be valid for reads of `len` bytes. +/// * `signature` must be valid for reads of 114 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. -/// * The 57 bytes at `pk` must equal the public key derived from the 57 bytes at `seed` by RFC 8032 §5.2.5 (`VG.Spec.Ed448.publicKey`). -/// * `ctxlen` must be at most 255. -/// * `seed` must originate from a cryptographically secure random generator. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `seed`, `pk`, `context`, `message` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `seed`, `pk`, `context`, `message` and `scratch` may overlap the return address on the stack or the 464 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `scratch` must not overlap `pk`, `context`, `message`, `signature` or the arguments on the stack (distinct Rust objects never do). +/// * None of `pk`, `context`, `message`, `signature` and `scratch` may overlap the return address on the stack or the 272 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ed448_sign_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 114], seed: *const [u8; 57], pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, scratch: *mut [u64; 1024]) { +pub unsafe fn vg_ed448_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, signature: *const [u8; 114], scratch: *mut [u64; 1024]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 114], seed: *const [u8; 57], pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, scratch: *mut [u64; 1024]); + type Impl = unsafe extern "sysv64" fn(pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, signature: *const [u8; 114], scratch: *mut [u64; 1024]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::ed448::VG_ED448_SIGN_CACHED_ADX_FEATURES) } - || cpu.contains(crate::ed448::VG_ED448_SIGN_CACHED_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ed448::VG_ED448_VERIFY_ADX_FEATURES) } + || cpu.contains(crate::ed448::VG_ED448_VERIFY_ADX_FEATURES) { - return crate::ed448::vg_ed448_sign_cached_adx; + return crate::ed448::vg_ed448_verify_adx; } - crate::ed448::vg_ed448_sign_cached + crate::ed448::vg_ed448_verify } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(out: *mut [u8; 114], seed: *const [u8; 57], pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, scratch: *mut [u64; 1024]) { + unsafe extern "sysv64" fn first(pk: *const [u8; 57], context: *const u8, ctxlen: usize, message: *const u8, len: usize, signature: *const [u8; 114], scratch: *mut [u64; 1024]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, seed, pk, context, ctxlen, message, len, scratch) } + unsafe { f(pk, context, ctxlen, message, len, signature, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -152,5 +152,5 @@ pub unsafe fn vg_ed448_sign_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, out // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, seed, pk, context, ctxlen, message, len, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, context, ctxlen, message, len, signature, scratch) } } diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/ffdh.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/ffdh.rs index a173ca87bdf..0ae2e48603d 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/ffdh.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/ffdh.rs @@ -2,65 +2,166 @@ //! The dispatchers of the verified `ffdh` functions for `x86_64` //! (`crate::ffdh`, `VG.Rust.dispatcher`). -/// Finite-field Diffie–Hellman (SP 800-56A Rev. 3 §5.7.1.1, PKCS #3 §8). With `domain` as `vg_ffdh_check_domain` takes it, the private scalar x (`x_len` bytes) and the peer's public value y (`p_len` bytes), most significant first, computes `z = y^x mod p`; returns 0 and writes zeros if z is 0, 1 or `p − 1`, and otherwise returns 1 and writes z (`p_len` bytes, most significant first) to `out`. Applies no KDF. -/// -/// Contract: `VG.Spec.Ffdh.exchangeContract`. Timing may depend on the pointers, the lengths, the domain, y and the value returned, not otherwise on x; the exponentiation may run over all `x_len` bytes of x, leading zeros included. +/// Initialize a reusable public fixed-base context for finite-field Diffie–Hellman key generation. The checked domain is p, g and optional q, each p_len bytes. Writes generationPrecompute: three public metadata words followed by RSA's modulus/R-squared values and 32 five-tooth comb entries. Returns 1. Word 0 is generationScalarBits, word 1 is its byte length and word 2 is the comb row count. The context has no pointers and is independent of the arithmetic backend. Contract: VG.Spec.Ffdh.generationPrecomputeContract. Timing may depend on the public domain, pointers and lengths. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ffdh_exchange_mr_adx_exp_ifma` if the CPU supports its features (`VG_FFDH_EXCHANGE_MR_ADX_EXP_IFMA_FEATURES`); `vg_ffdh_exchange_exp_ifma` if the CPU supports its features (`VG_FFDH_EXCHANGE_EXP_IFMA_FEATURES`); `vg_ffdh_exchange_mr_adx` if the CPU supports its features (`VG_FFDH_EXCHANGE_MR_ADX_FEATURES`); or else `vg_ffdh_exchange`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ffdh_generate_key_precompute_adx` if the CPU supports its features (`VG_FFDH_GENERATE_KEY_PRECOMPUTE_ADX_FEATURES`); or else `vg_ffdh_generate_key_precompute`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `out` must be valid for reads and writes of `out_len` bytes. +/// * `ctx` must be valid for reads and writes of `8 * ctx_len` bytes. /// * `domain` must be valid for reads of `domain_len` bytes. -/// * `x` must be valid for reads of `x_len` bytes. -/// * `peer` must be valid for reads of `peer_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. /// * `p_len` must be in 64..=1024. /// * `domain_len` must be `2 * p_len` or `3 * p_len`. -/// * `out_len` and `peer_len` must be `p_len`. -/// * `x_len` must be in 1..=`p_len`. -/// * For the result to be specified, `domain` must be one that `vg_ffdh_check_domain` accepted (returned 1 for); otherwise it is unspecified. -/// * For the result to be specified, `peer` must also be a value that `vg_ffdh_check_public` accepted for this domain. +/// * domain must be one that vg_ffdh_check_domain accepted. +/// * ctx_len must be generationPrecomputedWords(p_len), namely 3 + 34 * ceil(p_len / 8). /// * `scratch_len` must be at least `16 * p_len + 32`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `domain`, `x`, `peer` or the arguments on the stack (distinct Rust objects never do). -/// * None of `out`, `domain`, `x`, `peer` and `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `ctx` and `scratch` must not overlap each other, `domain` or the arguments on the stack (distinct Rust objects never do). +/// * None of `ctx`, `domain` and `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ffdh_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, domain: *const u8, domain_len: usize, p_len: usize, x: *const u8, x_len: usize, peer: *const u8, peer_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_ffdh_generate_key_precompute_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *mut u64, ctx_len: usize, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(out: *mut u8, out_len: usize, domain: *const u8, domain_len: usize, p_len: usize, x: *const u8, x_len: usize, peer: *const u8, peer_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; + type Impl = unsafe extern "sysv64" fn(ctx: *mut u64, ctx_len: usize, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_EXCHANGE_MR_ADX_EXP_IFMA_FEATURES) } - || cpu.contains(crate::ffdh::VG_FFDH_EXCHANGE_MR_ADX_EXP_IFMA_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_GENERATE_KEY_PRECOMPUTE_ADX_FEATURES) } + || cpu.contains(crate::ffdh::VG_FFDH_GENERATE_KEY_PRECOMPUTE_ADX_FEATURES) { - return crate::ffdh::vg_ffdh_exchange_mr_adx_exp_ifma; + return crate::ffdh::vg_ffdh_generate_key_precompute_adx; } - if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_EXCHANGE_EXP_IFMA_FEATURES) } - || cpu.contains(crate::ffdh::VG_FFDH_EXCHANGE_EXP_IFMA_FEATURES) + crate::ffdh::vg_ffdh_generate_key_precompute + } + /// The first call: chooses the implementation, records it in `CHOSEN` for the calls + /// after it, and runs it. + #[cold] + unsafe extern "sysv64" fn first(ctx: *mut u64, ctx_len: usize, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + let f = select(); + CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); + // SAFETY: the caller's obligations, which are those of every implementation, `f` + // among them, and the CPU has the features `f` needs. + unsafe { f(ctx, ctx_len, domain, domain_len, p_len, scratch, scratch_len) } + } + /// The function to call: `first`, until it records the implementation it chose. + static CHOSEN: core::sync::atomic::AtomicPtr<()> = + core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); + // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, + // or an implementation `select` returned, which needs only features the CPU has. Calling + // either carries the caller's obligations, which are those of every implementation. + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, ctx_len, domain, domain_len, p_len, scratch, scratch_len) } +} + +/// One native finite-field Diffie–Hellman key-generation attempt using a reusable public five-tooth comb context. Takes p_len random bytes and implements the same vettedScalarOfOctets, keyStatus and output conventions as vg_ffdh_generate_key_vetted for checked parameters. The context's public maximum width fixes the schedule, including secret leading zeros. Contract: VG.Spec.Ffdh.cachedGenerateKeyContract. Timing may depend on the public domain, pointers, lengths and returned status, not otherwise on the random bytes or private scalar. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ffdh_generate_key_cached_adx` if the CPU supports its features (`VG_FFDH_GENERATE_KEY_CACHED_ADX_FEATURES`); or else `vg_ffdh_generate_key_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// +/// # Safety +/// +/// * `x` must be valid for reads and writes of `x_len` bytes. +/// * `y` must be valid for reads and writes of `y_len` bytes. +/// * `domain` must be valid for reads of `domain_len` bytes. +/// * `rand` must be valid for reads of `rand_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `ctx` must be valid for reads of `8 * ctx_len` bytes. +/// * `p_len` must be in 64..=1024. +/// * `domain_len` must be `2 * p_len` or `3 * p_len`. +/// * domain must be one that vg_ffdh_check_domain accepted. +/// * ctx_len must be generationPrecomputedWords(p_len). ctx must contain the unchanged words written by vg_ffdh_generate_key_precompute for these parameters. +/// * x_len, y_len and rand_len must be p_len. +/// * `scratch_len` must be at least `16 * p_len + 32`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `x`, `y` and `scratch` must not overlap each other, `domain`, `rand`, `ctx` or the arguments on the stack (distinct Rust objects never do). +/// * None of `x`, `y`, `domain`, `rand`, `scratch` and `ctx` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ffdh_generate_key_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, ctx: *const u64, ctx_len: usize) -> u32 { + /// The type of the implementations. + type Impl = unsafe extern "sysv64" fn(x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, ctx: *const u64, ctx_len: usize) -> u32; + /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only + /// features the CPU has. + #[cold] + fn select() -> Impl { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_GENERATE_KEY_CACHED_ADX_FEATURES) } + || cpu.contains(crate::ffdh::VG_FFDH_GENERATE_KEY_CACHED_ADX_FEATURES) { - return crate::ffdh::vg_ffdh_exchange_exp_ifma; + return crate::ffdh::vg_ffdh_generate_key_cached_adx; } - if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_EXCHANGE_MR_ADX_FEATURES) } - || cpu.contains(crate::ffdh::VG_FFDH_EXCHANGE_MR_ADX_FEATURES) + crate::ffdh::vg_ffdh_generate_key_cached + } + /// The first call: chooses the implementation, records it in `CHOSEN` for the calls + /// after it, and runs it. + #[cold] + unsafe extern "sysv64" fn first(x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, ctx: *const u64, ctx_len: usize) -> u32 { + let f = select(); + CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); + // SAFETY: the caller's obligations, which are those of every implementation, `f` + // among them, and the CPU has the features `f` needs. + unsafe { f(x, x_len, y, y_len, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, ctx, ctx_len) } + } + /// The function to call: `first`, until it records the implementation it chose. + static CHOSEN: core::sync::atomic::AtomicPtr<()> = + core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); + // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, + // or an implementation `select` returned, which needs only features the CPU has. Calling + // either carries the caller's obligations, which are those of every implementation. + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(x, x_len, y, y_len, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, ctx, ctx_len) } +} + +/// Generate a finite-field Diffie–Hellman key pair using a reusable public five-tooth comb context and the existing vettedKeySearch rejection policy. Returns 1 with a key pair, 3 when a budget is exhausted, or 0 to request entropy. On 0, discard used bytes, preserve the suffix and extend it to state[3] bytes before calling again. On 0 and 3 both outputs are zero. Entropy consumption, retry budgets and statuses are unchanged. Contract: VG.Spec.Ffdh.cachedKeySearchContract. Timing may depend on the public domain, pointers, lengths, controller policy and sequence of attempt statuses, not otherwise on private scalar bytes. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ffdh_generate_key_search_cached_adx` if the CPU supports its features (`VG_FFDH_GENERATE_KEY_SEARCH_CACHED_ADX_FEATURES`); or else `vg_ffdh_generate_key_search_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// +/// # Safety +/// +/// * `x` must be valid for reads and writes of `x_len` bytes. +/// * `y` must be valid for reads and writes of `y_len` bytes. +/// * `used` must be valid for reads and writes of 8 bytes. +/// * `domain` must be valid for reads of `domain_len` bytes. +/// * `rand` must be valid for reads of `rand_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `state` must be valid for reads and writes of 128 bytes. +/// * `ctx` must be valid for reads of `8 * ctx_len` bytes. +/// * `p_len` must be in 64..=1024. +/// * `domain_len` must be `2 * p_len` or `3 * p_len`. +/// * domain must be one that vg_ffdh_check_domain accepted. +/// * ctx_len must be generationPrecomputedWords(p_len). ctx must contain the unchanged words written by vg_ffdh_generate_key_precompute for these parameters. +/// * state must be initialized by vg_ffdh_key_generation_init for p_len, or be the state returned by this routine. +/// * x_len and y_len must be p_len. +/// * `scratch_len` must be at least `16 * p_len + 32`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `x`, `y`, `used`, `scratch` and `state` must not overlap each other, `domain`, `rand`, `ctx` or the arguments on the stack (distinct Rust objects never do). +/// * None of `x`, `y`, `used`, `domain`, `rand`, `scratch`, `state` and `ctx` may overlap the return address on the stack or the 72 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ffdh_generate_key_search_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, used: *mut [u64; 1], domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16], ctx: *const u64, ctx_len: usize) -> u32 { + /// The type of the implementations. + type Impl = unsafe extern "sysv64" fn(x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, used: *mut [u64; 1], domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16], ctx: *const u64, ctx_len: usize) -> u32; + /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only + /// features the CPU has. + #[cold] + fn select() -> Impl { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_GENERATE_KEY_SEARCH_CACHED_ADX_FEATURES) } + || cpu.contains(crate::ffdh::VG_FFDH_GENERATE_KEY_SEARCH_CACHED_ADX_FEATURES) { - return crate::ffdh::vg_ffdh_exchange_mr_adx; + return crate::ffdh::vg_ffdh_generate_key_search_cached_adx; } - crate::ffdh::vg_ffdh_exchange + crate::ffdh::vg_ffdh_generate_key_search_cached } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(out: *mut u8, out_len: usize, domain: *const u8, domain_len: usize, p_len: usize, x: *const u8, x_len: usize, peer: *const u8, peer_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + unsafe extern "sysv64" fn first(x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, used: *mut [u64; 1], domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16], ctx: *const u64, ctx_len: usize) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, out_len, domain, domain_len, p_len, x, x_len, peer, peer_len, scratch, scratch_len) } + unsafe { f(x, x_len, y, y_len, used, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, state, ctx, ctx_len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -68,14 +169,44 @@ pub unsafe fn vg_ffdh_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *m // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, domain, domain_len, p_len, x, x_len, peer, peer_len, scratch, scratch_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(x, x_len, y, y_len, used, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, state, ctx, ctx_len) } +} + +/// Initialize native finite-field Diffie–Hellman parameter generation. Validates 512..=8192 bits and generator 2 or 5. Returns 1 and initializes the controller's attempt limit, output width, entropy budget and first entropy request; returns 0 and clears those four words on invalid parameters. The remaining state words are private workspace. Contract: `VG.Spec.Ffdh.parameterInitContract`. +/// +/// It runs `vg_ffdh_parameter_generation_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 128 bytes. +/// * `state` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ffdh_parameter_generation_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 16], bits: usize, generator: u32) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ffdh::vg_ffdh_parameter_generation_init(state, bits, generator) } +} + +/// Initialize native finite-field Diffie–Hellman key generation. Returns 1 for a domain width in 64..=1024 bytes, initializing the rejection budgets and first entropy request; returns 0 and clears the first four state words otherwise. The remaining state words are private workspace. Contract: `VG.Spec.Ffdh.keyInitContract`. +/// +/// It runs `vg_ffdh_key_generation_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 128 bytes. +/// * `state` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_ffdh_key_generation_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 16], p_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::ffdh::vg_ffdh_key_generation_init(state, p_len) } } /// Checks an imported finite-field Diffie–Hellman public value (SP 800-56A Rev. 3 §5.6.2.3.1, and §5.6.2.3.2 with q). With `domain` as `vg_ffdh_check_domain` takes it and y (`p_len` bytes, most significant first), returns 1 if `1 < y < p − 1` and, with q, `y^q mod p = 1`; otherwise 0. /// /// Contract: `VG.Spec.Ffdh.checkPublicContract`. Timing may depend on the pointers, the lengths, the domain and y. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ffdh_check_public_mr_adx_exp_ifma` if the CPU supports its features (`VG_FFDH_CHECK_PUBLIC_MR_ADX_EXP_IFMA_FEATURES`); `vg_ffdh_check_public_exp_ifma` if the CPU supports its features (`VG_FFDH_CHECK_PUBLIC_EXP_IFMA_FEATURES`); `vg_ffdh_check_public_mr_adx` if the CPU supports its features (`VG_FFDH_CHECK_PUBLIC_MR_ADX_FEATURES`); or else `vg_ffdh_check_public`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ffdh_check_public_mr_adx_exp_ifma` if the CPU supports its features (`VG_FFDH_CHECK_PUBLIC_MR_ADX_EXP_IFMA_FEATURES`); `vg_ffdh_check_public_exp_ifma` if the CPU supports its features (`VG_FFDH_CHECK_PUBLIC_EXP_IFMA_FEATURES`); `vg_ffdh_check_public_mr_adx` if the CPU supports its features (`VG_FFDH_CHECK_PUBLIC_MR_ADX_FEATURES`); or else `vg_ffdh_check_public`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -136,58 +267,65 @@ pub unsafe fn vg_ffdh_check_public_auto(_dit: verified_garbage_cpu::Dit<'_>, dom unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(domain, domain_len, p_len, y, y_len, scratch, scratch_len) } } -/// Checks imported finite-field Diffie–Hellman parameters (SP 800-56A Rev. 3 §5.5.2, without primality tests). `domain` is p, g and, if `domain_len` is `3 * p_len`, q, each `p_len` bytes, most significant first. Returns 1 if p has 512 to 8192 bits, its first byte is not zero and it is odd, `1 < g < p − 1`, and, with q, q is odd, `2 < q < p − 1`, q divides `p − 1` and `g^q mod p = 1`; otherwise 0. Does not test p or q for primality: the caller must establish their provenance. +/// Finite-field Diffie–Hellman (SP 800-56A Rev. 3 §5.7.1.1, PKCS #3 §8). With `domain` as `vg_ffdh_check_domain` takes it, the private scalar x (`x_len` bytes) and the peer's public value y (`p_len` bytes), most significant first, computes `z = y^x mod p`; returns 0 and writes zeros if z is 0, 1 or `p − 1`, and otherwise returns 1 and writes z (`p_len` bytes, most significant first) to `out`. Applies no KDF. /// -/// Contract: `VG.Spec.Ffdh.checkDomainContract`. Timing may depend on the pointers, the lengths and the domain. +/// Contract: `VG.Spec.Ffdh.exchangeContract`. Timing may depend on the pointers, the lengths, the domain, y and the value returned, not otherwise on x; the exponentiation may run over all `x_len` bytes of x, leading zeros included. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ffdh_check_domain_mr_adx_exp_ifma` if the CPU supports its features (`VG_FFDH_CHECK_DOMAIN_MR_ADX_EXP_IFMA_FEATURES`); `vg_ffdh_check_domain_exp_ifma` if the CPU supports its features (`VG_FFDH_CHECK_DOMAIN_EXP_IFMA_FEATURES`); `vg_ffdh_check_domain_mr_adx` if the CPU supports its features (`VG_FFDH_CHECK_DOMAIN_MR_ADX_FEATURES`); or else `vg_ffdh_check_domain`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ffdh_exchange_mr_adx_exp_ifma` if the CPU supports its features (`VG_FFDH_EXCHANGE_MR_ADX_EXP_IFMA_FEATURES`); `vg_ffdh_exchange_exp_ifma` if the CPU supports its features (`VG_FFDH_EXCHANGE_EXP_IFMA_FEATURES`); `vg_ffdh_exchange_mr_adx` if the CPU supports its features (`VG_FFDH_EXCHANGE_MR_ADX_FEATURES`); or else `vg_ffdh_exchange`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// +/// * `out` must be valid for reads and writes of `out_len` bytes. /// * `domain` must be valid for reads of `domain_len` bytes. +/// * `x` must be valid for reads of `x_len` bytes. +/// * `peer` must be valid for reads of `peer_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. /// * `p_len` must be in 64..=1024. /// * `domain_len` must be `2 * p_len` or `3 * p_len`. +/// * `out_len` and `peer_len` must be `p_len`. +/// * `x_len` must be in 1..=`p_len`. +/// * For the result to be specified, `domain` must be one that `vg_ffdh_check_domain` accepted (returned 1 for); otherwise it is unspecified. +/// * For the result to be specified, `peer` must also be a value that `vg_ffdh_check_public` accepted for this domain. /// * `scratch_len` must be at least `16 * p_len + 32`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `scratch` must not overlap `domain` (distinct Rust objects never do). -/// * Neither `domain` nor `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `domain`, `x`, `peer` or the arguments on the stack (distinct Rust objects never do). +/// * None of `out`, `domain`, `x`, `peer` and `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ffdh_check_domain_auto(_dit: verified_garbage_cpu::Dit<'_>, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { +pub unsafe fn vg_ffdh_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut u8, out_len: usize, domain: *const u8, domain_len: usize, p_len: usize, x: *const u8, x_len: usize, peer: *const u8, peer_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; + type Impl = unsafe extern "sysv64" fn(out: *mut u8, out_len: usize, domain: *const u8, domain_len: usize, p_len: usize, x: *const u8, x_len: usize, peer: *const u8, peer_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_CHECK_DOMAIN_MR_ADX_EXP_IFMA_FEATURES) } - || cpu.contains(crate::ffdh::VG_FFDH_CHECK_DOMAIN_MR_ADX_EXP_IFMA_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_EXCHANGE_MR_ADX_EXP_IFMA_FEATURES) } + || cpu.contains(crate::ffdh::VG_FFDH_EXCHANGE_MR_ADX_EXP_IFMA_FEATURES) { - return crate::ffdh::vg_ffdh_check_domain_mr_adx_exp_ifma; + return crate::ffdh::vg_ffdh_exchange_mr_adx_exp_ifma; } - if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_CHECK_DOMAIN_EXP_IFMA_FEATURES) } - || cpu.contains(crate::ffdh::VG_FFDH_CHECK_DOMAIN_EXP_IFMA_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_EXCHANGE_EXP_IFMA_FEATURES) } + || cpu.contains(crate::ffdh::VG_FFDH_EXCHANGE_EXP_IFMA_FEATURES) { - return crate::ffdh::vg_ffdh_check_domain_exp_ifma; + return crate::ffdh::vg_ffdh_exchange_exp_ifma; } - if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_CHECK_DOMAIN_MR_ADX_FEATURES) } - || cpu.contains(crate::ffdh::VG_FFDH_CHECK_DOMAIN_MR_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_EXCHANGE_MR_ADX_FEATURES) } + || cpu.contains(crate::ffdh::VG_FFDH_EXCHANGE_MR_ADX_FEATURES) { - return crate::ffdh::vg_ffdh_check_domain_mr_adx; + return crate::ffdh::vg_ffdh_exchange_mr_adx; } - crate::ffdh::vg_ffdh_check_domain + crate::ffdh::vg_ffdh_exchange } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + unsafe extern "sysv64" fn first(out: *mut u8, out_len: usize, domain: *const u8, domain_len: usize, p_len: usize, x: *const u8, x_len: usize, peer: *const u8, peer_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(domain, domain_len, p_len, scratch, scratch_len) } + unsafe { f(out, out_len, domain, domain_len, p_len, x, x_len, peer, peer_len, scratch, scratch_len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -195,14 +333,14 @@ pub unsafe fn vg_ffdh_check_domain_auto(_dit: verified_garbage_cpu::Dit<'_>, dom // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(domain, domain_len, p_len, scratch, scratch_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, domain, domain_len, p_len, x, x_len, peer, peer_len, scratch, scratch_len) } } /// Checks an imported finite-field Diffie–Hellman key pair (SP 800-56A Rev. 3 §5.6.2.1.2 and §5.6.2.1.4). With `domain` as `vg_ffdh_check_domain` takes it, y (`p_len` bytes) and x (`x_len` bytes), most significant first: returns 2 if x is not in `1..q` (`1..p − 1` without q); 1 if y is a valid public value (as `vg_ffdh_check_public` checks it) and `y = g^x mod p`; 0 otherwise. /// /// Contract: `VG.Spec.Ffdh.checkPrivateContract`. Timing may depend on the pointers, the lengths, the domain, y and the value returned, not otherwise on x; the exponentiation may run over all `x_len` bytes of x, leading zeros included. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ffdh_check_private_mr_adx_exp_ifma` if the CPU supports its features (`VG_FFDH_CHECK_PRIVATE_MR_ADX_EXP_IFMA_FEATURES`); `vg_ffdh_check_private_exp_ifma` if the CPU supports its features (`VG_FFDH_CHECK_PRIVATE_EXP_IFMA_FEATURES`); `vg_ffdh_check_private_mr_adx` if the CPU supports its features (`VG_FFDH_CHECK_PRIVATE_MR_ADX_FEATURES`); or else `vg_ffdh_check_private`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ffdh_check_private_mr_adx_exp_ifma` if the CPU supports its features (`VG_FFDH_CHECK_PRIVATE_MR_ADX_EXP_IFMA_FEATURES`); `vg_ffdh_check_private_exp_ifma` if the CPU supports its features (`VG_FFDH_CHECK_PRIVATE_EXP_IFMA_FEATURES`); `vg_ffdh_check_private_mr_adx` if the CPU supports its features (`VG_FFDH_CHECK_PRIVATE_MR_ADX_FEATURES`); or else `vg_ffdh_check_private`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -269,7 +407,7 @@ pub unsafe fn vg_ffdh_check_private_auto(_dit: verified_garbage_cpu::Dit<'_>, do /// /// Contract: `VG.Spec.Ffdh.derivePublicContract`. Timing may depend on the pointers, the lengths, the domain and the value returned, not otherwise on x; the exponentiation may run over all `x_len` bytes of x, leading zeros included. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ffdh_derive_public_mr_adx_exp_ifma` if the CPU supports its features (`VG_FFDH_DERIVE_PUBLIC_MR_ADX_EXP_IFMA_FEATURES`); `vg_ffdh_derive_public_exp_ifma` if the CPU supports its features (`VG_FFDH_DERIVE_PUBLIC_EXP_IFMA_FEATURES`); `vg_ffdh_derive_public_mr_adx` if the CPU supports its features (`VG_FFDH_DERIVE_PUBLIC_MR_ADX_FEATURES`); or else `vg_ffdh_derive_public`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ffdh_derive_public_mr_adx_exp_ifma` if the CPU supports its features (`VG_FFDH_DERIVE_PUBLIC_MR_ADX_EXP_IFMA_FEATURES`); `vg_ffdh_derive_public_exp_ifma` if the CPU supports its features (`VG_FFDH_DERIVE_PUBLIC_EXP_IFMA_FEATURES`); `vg_ffdh_derive_public_mr_adx` if the CPU supports its features (`VG_FFDH_DERIVE_PUBLIC_MR_ADX_FEATURES`); or else `vg_ffdh_derive_public`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -332,53 +470,58 @@ pub unsafe fn vg_ffdh_derive_public_auto(_dit: verified_garbage_cpu::Dit<'_>, y: unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(y, y_len, domain, domain_len, p_len, x, x_len, scratch, scratch_len) } } -/// One native finite-field Diffie–Hellman key-generation attempt using a reusable public five-tooth comb context. Takes p_len random bytes and implements the same vettedScalarOfOctets, keyStatus and output conventions as vg_ffdh_generate_key_vetted for checked parameters. The context's public maximum width fixes the schedule, including secret leading zeros. Contract: VG.Spec.Ffdh.cachedGenerateKeyContract. Timing may depend on the public domain, pointers, lengths and returned status, not otherwise on the random bytes or private scalar. +/// Checks imported finite-field Diffie–Hellman parameters (SP 800-56A Rev. 3 §5.5.2, without primality tests). `domain` is p, g and, if `domain_len` is `3 * p_len`, q, each `p_len` bytes, most significant first. Returns 1 if p has 512 to 8192 bits, its first byte is not zero and it is odd, `1 < g < p − 1`, and, with q, q is odd, `2 < q < p − 1`, q divides `p − 1` and `g^q mod p = 1`; otherwise 0. Does not test p or q for primality: the caller must establish their provenance. +/// +/// Contract: `VG.Spec.Ffdh.checkDomainContract`. Timing may depend on the pointers, the lengths and the domain. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ffdh_generate_key_cached_adx` if the CPU supports its features (`VG_FFDH_GENERATE_KEY_CACHED_ADX_FEATURES`); or else `vg_ffdh_generate_key_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ffdh_check_domain_mr_adx_exp_ifma` if the CPU supports its features (`VG_FFDH_CHECK_DOMAIN_MR_ADX_EXP_IFMA_FEATURES`); `vg_ffdh_check_domain_exp_ifma` if the CPU supports its features (`VG_FFDH_CHECK_DOMAIN_EXP_IFMA_FEATURES`); `vg_ffdh_check_domain_mr_adx` if the CPU supports its features (`VG_FFDH_CHECK_DOMAIN_MR_ADX_FEATURES`); or else `vg_ffdh_check_domain`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `x` must be valid for reads and writes of `x_len` bytes. -/// * `y` must be valid for reads and writes of `y_len` bytes. /// * `domain` must be valid for reads of `domain_len` bytes. -/// * `rand` must be valid for reads of `rand_len` bytes. /// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `ctx` must be valid for reads of `8 * ctx_len` bytes. /// * `p_len` must be in 64..=1024. /// * `domain_len` must be `2 * p_len` or `3 * p_len`. -/// * domain must be one that vg_ffdh_check_domain accepted. -/// * ctx_len must be generationPrecomputedWords(p_len). ctx must contain the unchanged words written by vg_ffdh_generate_key_precompute for these parameters. -/// * x_len, y_len and rand_len must be p_len. /// * `scratch_len` must be at least `16 * p_len + 32`. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `x`, `y` and `scratch` must not overlap each other, `domain`, `rand`, `ctx` or the arguments on the stack (distinct Rust objects never do). -/// * None of `x`, `y`, `domain`, `rand`, `scratch` and `ctx` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `scratch` must not overlap `domain` (distinct Rust objects never do). +/// * Neither `domain` nor `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_ffdh_generate_key_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, ctx: *const u64, ctx_len: usize) -> u32 { +pub unsafe fn vg_ffdh_check_domain_auto(_dit: verified_garbage_cpu::Dit<'_>, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, ctx: *const u64, ctx_len: usize) -> u32; + type Impl = unsafe extern "sysv64" fn(domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_GENERATE_KEY_CACHED_ADX_FEATURES) } - || cpu.contains(crate::ffdh::VG_FFDH_GENERATE_KEY_CACHED_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_CHECK_DOMAIN_MR_ADX_EXP_IFMA_FEATURES) } + || cpu.contains(crate::ffdh::VG_FFDH_CHECK_DOMAIN_MR_ADX_EXP_IFMA_FEATURES) { - return crate::ffdh::vg_ffdh_generate_key_cached_adx; + return crate::ffdh::vg_ffdh_check_domain_mr_adx_exp_ifma; } - crate::ffdh::vg_ffdh_generate_key_cached + if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_CHECK_DOMAIN_EXP_IFMA_FEATURES) } + || cpu.contains(crate::ffdh::VG_FFDH_CHECK_DOMAIN_EXP_IFMA_FEATURES) + { + return crate::ffdh::vg_ffdh_check_domain_exp_ifma; + } + if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_CHECK_DOMAIN_MR_ADX_FEATURES) } + || cpu.contains(crate::ffdh::VG_FFDH_CHECK_DOMAIN_MR_ADX_FEATURES) + { + return crate::ffdh::vg_ffdh_check_domain_mr_adx; + } + crate::ffdh::vg_ffdh_check_domain } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, ctx: *const u64, ctx_len: usize) -> u32 { + unsafe extern "sysv64" fn first(domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(x, x_len, y, y_len, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, ctx, ctx_len) } + unsafe { f(domain, domain_len, p_len, scratch, scratch_len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -386,27 +529,12 @@ pub unsafe fn vg_ffdh_generate_key_cached_auto(_dit: verified_garbage_cpu::Dit<' // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(x, x_len, y, y_len, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, ctx, ctx_len) } -} - -/// Initialize native finite-field Diffie–Hellman key generation. Returns 1 for a domain width in 64..=1024 bytes, initializing the rejection budgets and first entropy request; returns 0 and clears the first four state words otherwise. The remaining state words are private workspace. Contract: `VG.Spec.Ffdh.keyInitContract`. -/// -/// It runs `vg_ffdh_key_generation_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 128 bytes. -/// * `state` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ffdh_key_generation_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 16], p_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ffdh::vg_ffdh_key_generation_init(state, p_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(domain, domain_len, p_len, scratch, scratch_len) } } /// Run native finite-field Diffie–Hellman parameter generation, with RSA's trial-division sieve. The routine owns candidate retries, attempt limits, witness selection, primality rounds and entropy budgets. Initialize with `vg_ffdh_parameter_generation_init`. Returns 1 with the probable safe prime in `out`, 3 when a bound is exhausted, or 0 only when it needs more entropy. On 0, discard `used` bytes of `rand`, preserve its remaining suffix and extend that suffix to at least `state[3]` bytes before calling again. `state[1]` is the output width and `state[2]` the maximum tape length. The output is zero for 0 and 3. Accepted p and q pass the full 64/128 independent witness counts. Contract: `VG.Spec.Ffdh.sievedParameterSearchContract`. Timing may depend on the public tape and the first four controller words. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ffdh_generate_parameters_ifma` if the CPU supports its features (`VG_FFDH_GENERATE_PARAMETERS_IFMA_FEATURES`); `vg_ffdh_generate_parameters_adx` if the CPU supports its features (`VG_FFDH_GENERATE_PARAMETERS_ADX_FEATURES`); or else `vg_ffdh_generate_parameters`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_ffdh_generate_parameters_ifma` if the CPU supports its features (`VG_FFDH_GENERATE_PARAMETERS_IFMA_FEATURES`); `vg_ffdh_generate_parameters_adx` if the CPU supports its features (`VG_FFDH_GENERATE_PARAMETERS_ADX_FEATURES`); or else `vg_ffdh_generate_parameters`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -461,131 +589,3 @@ pub unsafe fn vg_ffdh_generate_parameters_auto(_dit: verified_garbage_cpu::Dit<' // either carries the caller's obligations, which are those of every implementation. unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, out_len, used, bits, generator, rand, rand_len, scratch, scratch_len, state) } } - -/// Initialize a reusable public fixed-base context for finite-field Diffie–Hellman key generation. The checked domain is p, g and optional q, each p_len bytes. Writes generationPrecompute: three public metadata words followed by RSA's modulus/R-squared values and 32 five-tooth comb entries. Returns 1. Word 0 is generationScalarBits, word 1 is its byte length and word 2 is the comb row count. The context has no pointers and is independent of the arithmetic backend. Contract: VG.Spec.Ffdh.generationPrecomputeContract. Timing may depend on the public domain, pointers and lengths. -/// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ffdh_generate_key_precompute_adx` if the CPU supports its features (`VG_FFDH_GENERATE_KEY_PRECOMPUTE_ADX_FEATURES`); or else `vg_ffdh_generate_key_precompute`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. -/// -/// # Safety -/// -/// * `ctx` must be valid for reads and writes of `8 * ctx_len` bytes. -/// * `domain` must be valid for reads of `domain_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `p_len` must be in 64..=1024. -/// * `domain_len` must be `2 * p_len` or `3 * p_len`. -/// * domain must be one that vg_ffdh_check_domain accepted. -/// * ctx_len must be generationPrecomputedWords(p_len), namely 3 + 34 * ceil(p_len / 8). -/// * `scratch_len` must be at least `16 * p_len + 32`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `ctx` and `scratch` must not overlap each other, `domain` or the arguments on the stack (distinct Rust objects never do). -/// * None of `ctx`, `domain` and `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ffdh_generate_key_precompute_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *mut u64, ctx_len: usize, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(ctx: *mut u64, ctx_len: usize, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32; - /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only - /// features the CPU has. - #[cold] - fn select() -> Impl { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_GENERATE_KEY_PRECOMPUTE_ADX_FEATURES) } - || cpu.contains(crate::ffdh::VG_FFDH_GENERATE_KEY_PRECOMPUTE_ADX_FEATURES) - { - return crate::ffdh::vg_ffdh_generate_key_precompute_adx; - } - crate::ffdh::vg_ffdh_generate_key_precompute - } - /// The first call: chooses the implementation, records it in `CHOSEN` for the calls - /// after it, and runs it. - #[cold] - unsafe extern "sysv64" fn first(ctx: *mut u64, ctx_len: usize, domain: *const u8, domain_len: usize, p_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - let f = select(); - CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); - // SAFETY: the caller's obligations, which are those of every implementation, `f` - // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, ctx_len, domain, domain_len, p_len, scratch, scratch_len) } - } - /// The function to call: `first`, until it records the implementation it chose. - static CHOSEN: core::sync::atomic::AtomicPtr<()> = - core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); - // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, - // or an implementation `select` returned, which needs only features the CPU has. Calling - // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, ctx_len, domain, domain_len, p_len, scratch, scratch_len) } -} - -/// Initialize native finite-field Diffie–Hellman parameter generation. Validates 512..=8192 bits and generator 2 or 5. Returns 1 and initializes the controller's attempt limit, output width, entropy budget and first entropy request; returns 0 and clears those four words on invalid parameters. The remaining state words are private workspace. Contract: `VG.Spec.Ffdh.parameterInitContract`. -/// -/// It runs `vg_ffdh_parameter_generation_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 128 bytes. -/// * `state` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ffdh_parameter_generation_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 16], bits: usize, generator: u32) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::ffdh::vg_ffdh_parameter_generation_init(state, bits, generator) } -} - -/// Generate a finite-field Diffie–Hellman key pair using a reusable public five-tooth comb context and the existing vettedKeySearch rejection policy. Returns 1 with a key pair, 3 when a budget is exhausted, or 0 to request entropy. On 0, discard used bytes, preserve the suffix and extend it to state[3] bytes before calling again. On 0 and 3 both outputs are zero. Entropy consumption, retry budgets and statuses are unchanged. Contract: VG.Spec.Ffdh.cachedKeySearchContract. Timing may depend on the public domain, pointers, lengths, controller policy and sequence of attempt statuses, not otherwise on private scalar bytes. -/// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_ffdh_generate_key_search_cached_adx` if the CPU supports its features (`VG_FFDH_GENERATE_KEY_SEARCH_CACHED_ADX_FEATURES`); or else `vg_ffdh_generate_key_search_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. -/// -/// # Safety -/// -/// * `x` must be valid for reads and writes of `x_len` bytes. -/// * `y` must be valid for reads and writes of `y_len` bytes. -/// * `used` must be valid for reads and writes of 8 bytes. -/// * `domain` must be valid for reads of `domain_len` bytes. -/// * `rand` must be valid for reads of `rand_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `state` must be valid for reads and writes of 128 bytes. -/// * `ctx` must be valid for reads of `8 * ctx_len` bytes. -/// * `p_len` must be in 64..=1024. -/// * `domain_len` must be `2 * p_len` or `3 * p_len`. -/// * domain must be one that vg_ffdh_check_domain accepted. -/// * ctx_len must be generationPrecomputedWords(p_len). ctx must contain the unchanged words written by vg_ffdh_generate_key_precompute for these parameters. -/// * state must be initialized by vg_ffdh_key_generation_init for p_len, or be the state returned by this routine. -/// * x_len and y_len must be p_len. -/// * `scratch_len` must be at least `16 * p_len + 32`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `x`, `y`, `used`, `scratch` and `state` must not overlap each other, `domain`, `rand`, `ctx` or the arguments on the stack (distinct Rust objects never do). -/// * None of `x`, `y`, `used`, `domain`, `rand`, `scratch`, `state` and `ctx` may overlap the return address on the stack or the 72 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_ffdh_generate_key_search_cached_auto(_dit: verified_garbage_cpu::Dit<'_>, x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, used: *mut [u64; 1], domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16], ctx: *const u64, ctx_len: usize) -> u32 { - /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, used: *mut [u64; 1], domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16], ctx: *const u64, ctx_len: usize) -> u32; - /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only - /// features the CPU has. - #[cold] - fn select() -> Impl { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::ffdh::VG_FFDH_GENERATE_KEY_SEARCH_CACHED_ADX_FEATURES) } - || cpu.contains(crate::ffdh::VG_FFDH_GENERATE_KEY_SEARCH_CACHED_ADX_FEATURES) - { - return crate::ffdh::vg_ffdh_generate_key_search_cached_adx; - } - crate::ffdh::vg_ffdh_generate_key_search_cached - } - /// The first call: chooses the implementation, records it in `CHOSEN` for the calls - /// after it, and runs it. - #[cold] - unsafe extern "sysv64" fn first(x: *mut u8, x_len: usize, y: *mut u8, y_len: usize, used: *mut [u64; 1], domain: *const u8, domain_len: usize, p_len: usize, rand: *const u8, rand_len: usize, scratch: *mut u64, scratch_len: usize, state: *mut [u64; 16], ctx: *const u64, ctx_len: usize) -> u32 { - let f = select(); - CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); - // SAFETY: the caller's obligations, which are those of every implementation, `f` - // among them, and the CPU has the features `f` needs. - unsafe { f(x, x_len, y, y_len, used, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, state, ctx, ctx_len) } - } - /// The function to call: `first`, until it records the implementation it chose. - static CHOSEN: core::sync::atomic::AtomicPtr<()> = - core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); - // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, - // or an implementation `select` returned, which needs only features the CPU has. Calling - // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(x, x_len, y, y_len, used, domain, domain_len, p_len, rand, rand_len, scratch, scratch_len, state, ctx, ctx_len) } -} diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/fn_tables.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/fn_tables.rs index a2658dcfbf7..76f998457dd 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/fn_tables.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/fn_tables.rs @@ -2,36 +2,13 @@ //! The dispatchers of the tables of functions for `x86_64` //! (`crate::fn_tables`, `VG.Rust.tableDispatcher`). -/// The table of functions `VG_HASH_MD5` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). -#[inline] -pub fn vg_hash_md5_auto() -> &'static crate::fn_tables::Hash { - &crate::fn_tables::VG_HASH_MD5 -} - /// The table of functions `VG_HMAC_MD5` of the interface `Hmac`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). #[inline] pub fn vg_hmac_md5_auto() -> &'static crate::fn_tables::Hmac { &crate::fn_tables::VG_HMAC_MD5 } -/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA1`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HASH_SHA1_SHANI` if the CPU supports its features (`VG_HASH_SHA1_SHANI_FEATURES`); `VG_HASH_SHA1_AVX_BMI` if the CPU supports its features (`VG_HASH_SHA1_AVX_BMI_FEATURES`); or else `VG_HASH_SHA1`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. -#[inline] -pub fn vg_hash_sha1_auto() -> &'static crate::fn_tables::Hash { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HASH_SHA1_SHANI_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HASH_SHA1_SHANI_FEATURES) - { - return &crate::fn_tables::VG_HASH_SHA1_SHANI; - } - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HASH_SHA1_AVX_BMI_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HASH_SHA1_AVX_BMI_FEATURES) - { - return &crate::fn_tables::VG_HASH_SHA1_AVX_BMI; - } - &crate::fn_tables::VG_HASH_SHA1 -} - -/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA1`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HMAC_SHA1_SHANI` if the CPU supports its features (`VG_HMAC_SHA1_SHANI_FEATURES`); `VG_HMAC_SHA1_AVX_BMI` if the CPU supports its features (`VG_HMAC_SHA1_AVX_BMI_FEATURES`); or else `VG_HMAC_SHA1`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA1`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HMAC_SHA1_SHANI` if the CPU supports its features (`VG_HMAC_SHA1_SHANI_FEATURES`); `VG_HMAC_SHA1_AVX_BMI` if the CPU supports its features (`VG_HMAC_SHA1_AVX_BMI_FEATURES`); or else `VG_HMAC_SHA1`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_hmac_sha1_auto() -> &'static crate::fn_tables::Hmac { let cpu = verified_garbage_cpu::detected(); @@ -48,7 +25,132 @@ pub fn vg_hmac_sha1_auto() -> &'static crate::fn_tables::Hmac { &crate::fn_tables::VG_HMAC_SHA1 } -/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HASH_SHA224_SHANI` if the CPU supports its features (`VG_HASH_SHA224_SHANI_FEATURES`); `VG_HASH_SHA224_AVX2` if the CPU supports its features (`VG_HASH_SHA224_AVX2_FEATURES`); or else `VG_HASH_SHA224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HMAC_SHA224_SHANI` if the CPU supports its features (`VG_HMAC_SHA224_SHANI_FEATURES`); `VG_HMAC_SHA224_AVX2` if the CPU supports its features (`VG_HMAC_SHA224_AVX2_FEATURES`); or else `VG_HMAC_SHA224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +#[inline] +pub fn vg_hmac_sha224_auto() -> &'static crate::fn_tables::Hmac { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA224_SHANI_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA224_SHANI_FEATURES) + { + return &crate::fn_tables::VG_HMAC_SHA224_SHANI; + } + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA224_AVX2_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA224_AVX2_FEATURES) + { + return &crate::fn_tables::VG_HMAC_SHA224_AVX2; + } + &crate::fn_tables::VG_HMAC_SHA224 +} + +/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HMAC_SHA256_SHANI` if the CPU supports its features (`VG_HMAC_SHA256_SHANI_FEATURES`); `VG_HMAC_SHA256_AVX2` if the CPU supports its features (`VG_HMAC_SHA256_AVX2_FEATURES`); or else `VG_HMAC_SHA256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +#[inline] +pub fn vg_hmac_sha256_auto() -> &'static crate::fn_tables::Hmac { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA256_SHANI_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA256_SHANI_FEATURES) + { + return &crate::fn_tables::VG_HMAC_SHA256_SHANI; + } + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA256_AVX2_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA256_AVX2_FEATURES) + { + return &crate::fn_tables::VG_HMAC_SHA256_AVX2; + } + &crate::fn_tables::VG_HMAC_SHA256 +} + +/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA384`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HMAC_SHA384_SHANI` if the CPU supports its features (`VG_HMAC_SHA384_SHANI_FEATURES`); `VG_HMAC_SHA384_AVX2` if the CPU supports its features (`VG_HMAC_SHA384_AVX2_FEATURES`); or else `VG_HMAC_SHA384`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +#[inline] +pub fn vg_hmac_sha384_auto() -> &'static crate::fn_tables::Hmac { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA384_SHANI_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA384_SHANI_FEATURES) + { + return &crate::fn_tables::VG_HMAC_SHA384_SHANI; + } + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA384_AVX2_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA384_AVX2_FEATURES) + { + return &crate::fn_tables::VG_HMAC_SHA384_AVX2; + } + &crate::fn_tables::VG_HMAC_SHA384 +} + +/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA512`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HMAC_SHA512_SHANI` if the CPU supports its features (`VG_HMAC_SHA512_SHANI_FEATURES`); `VG_HMAC_SHA512_AVX2` if the CPU supports its features (`VG_HMAC_SHA512_AVX2_FEATURES`); or else `VG_HMAC_SHA512`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +#[inline] +pub fn vg_hmac_sha512_auto() -> &'static crate::fn_tables::Hmac { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA512_SHANI_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA512_SHANI_FEATURES) + { + return &crate::fn_tables::VG_HMAC_SHA512_SHANI; + } + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA512_AVX2_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA512_AVX2_FEATURES) + { + return &crate::fn_tables::VG_HMAC_SHA512_AVX2; + } + &crate::fn_tables::VG_HMAC_SHA512 +} + +/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA512_224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HMAC_SHA512_224_SHANI` if the CPU supports its features (`VG_HMAC_SHA512_224_SHANI_FEATURES`); `VG_HMAC_SHA512_224_AVX2` if the CPU supports its features (`VG_HMAC_SHA512_224_AVX2_FEATURES`); or else `VG_HMAC_SHA512_224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +#[inline] +pub fn vg_hmac_sha512_224_auto() -> &'static crate::fn_tables::Hmac { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA512_224_SHANI_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA512_224_SHANI_FEATURES) + { + return &crate::fn_tables::VG_HMAC_SHA512_224_SHANI; + } + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA512_224_AVX2_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA512_224_AVX2_FEATURES) + { + return &crate::fn_tables::VG_HMAC_SHA512_224_AVX2; + } + &crate::fn_tables::VG_HMAC_SHA512_224 +} + +/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA512_256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HMAC_SHA512_256_SHANI` if the CPU supports its features (`VG_HMAC_SHA512_256_SHANI_FEATURES`); `VG_HMAC_SHA512_256_AVX2` if the CPU supports its features (`VG_HMAC_SHA512_256_AVX2_FEATURES`); or else `VG_HMAC_SHA512_256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +#[inline] +pub fn vg_hmac_sha512_256_auto() -> &'static crate::fn_tables::Hmac { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA512_256_SHANI_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA512_256_SHANI_FEATURES) + { + return &crate::fn_tables::VG_HMAC_SHA512_256_SHANI; + } + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA512_256_AVX2_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HMAC_SHA512_256_AVX2_FEATURES) + { + return &crate::fn_tables::VG_HMAC_SHA512_256_AVX2; + } + &crate::fn_tables::VG_HMAC_SHA512_256 +} + +/// The table of functions `VG_HASH_MD5` of the interface `Hash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). +#[inline] +pub fn vg_hash_md5_auto() -> &'static crate::fn_tables::Hash { + &crate::fn_tables::VG_HASH_MD5 +} + +/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA1`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HASH_SHA1_SHANI` if the CPU supports its features (`VG_HASH_SHA1_SHANI_FEATURES`); `VG_HASH_SHA1_AVX_BMI` if the CPU supports its features (`VG_HASH_SHA1_AVX_BMI_FEATURES`); or else `VG_HASH_SHA1`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +#[inline] +pub fn vg_hash_sha1_auto() -> &'static crate::fn_tables::Hash { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HASH_SHA1_SHANI_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HASH_SHA1_SHANI_FEATURES) + { + return &crate::fn_tables::VG_HASH_SHA1_SHANI; + } + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HASH_SHA1_AVX_BMI_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HASH_SHA1_AVX_BMI_FEATURES) + { + return &crate::fn_tables::VG_HASH_SHA1_AVX_BMI; + } + &crate::fn_tables::VG_HASH_SHA1 +} + +/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HASH_SHA224_SHANI` if the CPU supports its features (`VG_HASH_SHA224_SHANI_FEATURES`); `VG_HASH_SHA224_AVX2` if the CPU supports its features (`VG_HASH_SHA224_AVX2_FEATURES`); or else `VG_HASH_SHA224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_hash_sha224_auto() -> &'static crate::fn_tables::Hash { let cpu = verified_garbage_cpu::detected(); @@ -65,7 +167,7 @@ pub fn vg_hash_sha224_auto() -> &'static crate::fn_tables::Hash { &crate::fn_tables::VG_HASH_SHA224 } -/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HASH_SHA256_SHANI` if the CPU supports its features (`VG_HASH_SHA256_SHANI_FEATURES`); `VG_HASH_SHA256_AVX2` if the CPU supports its features (`VG_HASH_SHA256_AVX2_FEATURES`); or else `VG_HASH_SHA256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HASH_SHA256_SHANI` if the CPU supports its features (`VG_HASH_SHA256_SHANI_FEATURES`); `VG_HASH_SHA256_AVX2` if the CPU supports its features (`VG_HASH_SHA256_AVX2_FEATURES`); or else `VG_HASH_SHA256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_hash_sha256_auto() -> &'static crate::fn_tables::Hash { let cpu = verified_garbage_cpu::detected(); @@ -82,7 +184,7 @@ pub fn vg_hash_sha256_auto() -> &'static crate::fn_tables::Hash { &crate::fn_tables::VG_HASH_SHA256 } -/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA384`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HASH_SHA384_SHANI` if the CPU supports its features (`VG_HASH_SHA384_SHANI_FEATURES`); `VG_HASH_SHA384_AVX2` if the CPU supports its features (`VG_HASH_SHA384_AVX2_FEATURES`); or else `VG_HASH_SHA384`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA384`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HASH_SHA384_SHANI` if the CPU supports its features (`VG_HASH_SHA384_SHANI_FEATURES`); `VG_HASH_SHA384_AVX2` if the CPU supports its features (`VG_HASH_SHA384_AVX2_FEATURES`); or else `VG_HASH_SHA384`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_hash_sha384_auto() -> &'static crate::fn_tables::Hash { let cpu = verified_garbage_cpu::detected(); @@ -99,7 +201,7 @@ pub fn vg_hash_sha384_auto() -> &'static crate::fn_tables::Hash { &crate::fn_tables::VG_HASH_SHA384 } -/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA512`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HASH_SHA512_SHANI` if the CPU supports its features (`VG_HASH_SHA512_SHANI_FEATURES`); `VG_HASH_SHA512_AVX2` if the CPU supports its features (`VG_HASH_SHA512_AVX2_FEATURES`); or else `VG_HASH_SHA512`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA512`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HASH_SHA512_SHANI` if the CPU supports its features (`VG_HASH_SHA512_SHANI_FEATURES`); `VG_HASH_SHA512_AVX2` if the CPU supports its features (`VG_HASH_SHA512_AVX2_FEATURES`); or else `VG_HASH_SHA512`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_hash_sha512_auto() -> &'static crate::fn_tables::Hash { let cpu = verified_garbage_cpu::detected(); @@ -116,72 +218,38 @@ pub fn vg_hash_sha512_auto() -> &'static crate::fn_tables::Hash { &crate::fn_tables::VG_HASH_SHA512 } -/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HMAC_SHA224_SHANI` if the CPU supports its features (`VG_HMAC_SHA224_SHANI_FEATURES`); `VG_HMAC_SHA224_AVX2` if the CPU supports its features (`VG_HMAC_SHA224_AVX2_FEATURES`); or else `VG_HMAC_SHA224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. -#[inline] -pub fn vg_hmac_sha224_auto() -> &'static crate::fn_tables::Hmac { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA224_SHANI_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA224_SHANI_FEATURES) - { - return &crate::fn_tables::VG_HMAC_SHA224_SHANI; - } - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA224_AVX2_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA224_AVX2_FEATURES) - { - return &crate::fn_tables::VG_HMAC_SHA224_AVX2; - } - &crate::fn_tables::VG_HMAC_SHA224 -} - -/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HMAC_SHA256_SHANI` if the CPU supports its features (`VG_HMAC_SHA256_SHANI_FEATURES`); `VG_HMAC_SHA256_AVX2` if the CPU supports its features (`VG_HMAC_SHA256_AVX2_FEATURES`); or else `VG_HMAC_SHA256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA512_224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HASH_SHA512_224_SHANI` if the CPU supports its features (`VG_HASH_SHA512_224_SHANI_FEATURES`); `VG_HASH_SHA512_224_AVX2` if the CPU supports its features (`VG_HASH_SHA512_224_AVX2_FEATURES`); or else `VG_HASH_SHA512_224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] -pub fn vg_hmac_sha256_auto() -> &'static crate::fn_tables::Hmac { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA256_SHANI_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA256_SHANI_FEATURES) - { - return &crate::fn_tables::VG_HMAC_SHA256_SHANI; - } - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA256_AVX2_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA256_AVX2_FEATURES) - { - return &crate::fn_tables::VG_HMAC_SHA256_AVX2; - } - &crate::fn_tables::VG_HMAC_SHA256 -} - -/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA384`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HMAC_SHA384_SHANI` if the CPU supports its features (`VG_HMAC_SHA384_SHANI_FEATURES`); `VG_HMAC_SHA384_AVX2` if the CPU supports its features (`VG_HMAC_SHA384_AVX2_FEATURES`); or else `VG_HMAC_SHA384`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. -#[inline] -pub fn vg_hmac_sha384_auto() -> &'static crate::fn_tables::Hmac { +pub fn vg_hash_sha512_224_auto() -> &'static crate::fn_tables::Hash { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA384_SHANI_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA384_SHANI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HASH_SHA512_224_SHANI_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HASH_SHA512_224_SHANI_FEATURES) { - return &crate::fn_tables::VG_HMAC_SHA384_SHANI; + return &crate::fn_tables::VG_HASH_SHA512_224_SHANI; } - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA384_AVX2_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA384_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HASH_SHA512_224_AVX2_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HASH_SHA512_224_AVX2_FEATURES) { - return &crate::fn_tables::VG_HMAC_SHA384_AVX2; + return &crate::fn_tables::VG_HASH_SHA512_224_AVX2; } - &crate::fn_tables::VG_HMAC_SHA384 + &crate::fn_tables::VG_HASH_SHA512_224 } -/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA512`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HMAC_SHA512_SHANI` if the CPU supports its features (`VG_HMAC_SHA512_SHANI_FEATURES`); `VG_HMAC_SHA512_AVX2` if the CPU supports its features (`VG_HMAC_SHA512_AVX2_FEATURES`); or else `VG_HMAC_SHA512`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA512_256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_HASH_SHA512_256_SHANI` if the CPU supports its features (`VG_HASH_SHA512_256_SHANI_FEATURES`); `VG_HASH_SHA512_256_AVX2` if the CPU supports its features (`VG_HASH_SHA512_256_AVX2_FEATURES`); or else `VG_HASH_SHA512_256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] -pub fn vg_hmac_sha512_auto() -> &'static crate::fn_tables::Hmac { +pub fn vg_hash_sha512_256_auto() -> &'static crate::fn_tables::Hash { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA512_SHANI_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA512_SHANI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HASH_SHA512_256_SHANI_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HASH_SHA512_256_SHANI_FEATURES) { - return &crate::fn_tables::VG_HMAC_SHA512_SHANI; + return &crate::fn_tables::VG_HASH_SHA512_256_SHANI; } - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA512_AVX2_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA512_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HASH_SHA512_256_AVX2_FEATURES) } + || cpu.contains(crate::fn_tables::VG_HASH_SHA512_256_AVX2_FEATURES) { - return &crate::fn_tables::VG_HMAC_SHA512_AVX2; + return &crate::fn_tables::VG_HASH_SHA512_256_AVX2; } - &crate::fn_tables::VG_HMAC_SHA512 + &crate::fn_tables::VG_HASH_SHA512_256 } /// The table of functions `VG_MD_HASH_MD5` of the interface `MdHash`, the only one with its data on this target, which needs no CPU feature (`VG.Rust.tableDispatcher`). @@ -190,7 +258,7 @@ pub fn vg_md_hash_md5_auto() -> &'static crate::fn_tables::MdHash { &crate::fn_tables::VG_MD_HASH_MD5 } -/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA1`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_MD_HASH_SHA1_SHANI` if the CPU supports its features (`VG_MD_HASH_SHA1_SHANI_FEATURES`); `VG_MD_HASH_SHA1_AVX_BMI` if the CPU supports its features (`VG_MD_HASH_SHA1_AVX_BMI_FEATURES`); or else `VG_MD_HASH_SHA1`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA1`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_MD_HASH_SHA1_SHANI` if the CPU supports its features (`VG_MD_HASH_SHA1_SHANI_FEATURES`); `VG_MD_HASH_SHA1_AVX_BMI` if the CPU supports its features (`VG_MD_HASH_SHA1_AVX_BMI_FEATURES`); or else `VG_MD_HASH_SHA1`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_md_hash_sha1_auto() -> &'static crate::fn_tables::MdHash { let cpu = verified_garbage_cpu::detected(); @@ -207,7 +275,7 @@ pub fn vg_md_hash_sha1_auto() -> &'static crate::fn_tables::MdHash { &crate::fn_tables::VG_MD_HASH_SHA1 } -/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_MD_HASH_SHA224_SHANI` if the CPU supports its features (`VG_MD_HASH_SHA224_SHANI_FEATURES`); `VG_MD_HASH_SHA224_AVX2` if the CPU supports its features (`VG_MD_HASH_SHA224_AVX2_FEATURES`); or else `VG_MD_HASH_SHA224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_MD_HASH_SHA224_SHANI` if the CPU supports its features (`VG_MD_HASH_SHA224_SHANI_FEATURES`); `VG_MD_HASH_SHA224_AVX2` if the CPU supports its features (`VG_MD_HASH_SHA224_AVX2_FEATURES`); or else `VG_MD_HASH_SHA224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_md_hash_sha224_auto() -> &'static crate::fn_tables::MdHash { let cpu = verified_garbage_cpu::detected(); @@ -224,7 +292,7 @@ pub fn vg_md_hash_sha224_auto() -> &'static crate::fn_tables::MdHash { &crate::fn_tables::VG_MD_HASH_SHA224 } -/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_MD_HASH_SHA256_SHANI` if the CPU supports its features (`VG_MD_HASH_SHA256_SHANI_FEATURES`); `VG_MD_HASH_SHA256_AVX2` if the CPU supports its features (`VG_MD_HASH_SHA256_AVX2_FEATURES`); or else `VG_MD_HASH_SHA256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_MD_HASH_SHA256_SHANI` if the CPU supports its features (`VG_MD_HASH_SHA256_SHANI_FEATURES`); `VG_MD_HASH_SHA256_AVX2` if the CPU supports its features (`VG_MD_HASH_SHA256_AVX2_FEATURES`); or else `VG_MD_HASH_SHA256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_md_hash_sha256_auto() -> &'static crate::fn_tables::MdHash { let cpu = verified_garbage_cpu::detected(); @@ -241,7 +309,7 @@ pub fn vg_md_hash_sha256_auto() -> &'static crate::fn_tables::MdHash { &crate::fn_tables::VG_MD_HASH_SHA256 } -/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA384`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_MD_HASH_SHA384_SHANI` if the CPU supports its features (`VG_MD_HASH_SHA384_SHANI_FEATURES`); `VG_MD_HASH_SHA384_AVX2` if the CPU supports its features (`VG_MD_HASH_SHA384_AVX2_FEATURES`); or else `VG_MD_HASH_SHA384`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA384`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_MD_HASH_SHA384_SHANI` if the CPU supports its features (`VG_MD_HASH_SHA384_SHANI_FEATURES`); `VG_MD_HASH_SHA384_AVX2` if the CPU supports its features (`VG_MD_HASH_SHA384_AVX2_FEATURES`); or else `VG_MD_HASH_SHA384`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_md_hash_sha384_auto() -> &'static crate::fn_tables::MdHash { let cpu = verified_garbage_cpu::detected(); @@ -258,7 +326,7 @@ pub fn vg_md_hash_sha384_auto() -> &'static crate::fn_tables::MdHash { &crate::fn_tables::VG_MD_HASH_SHA384 } -/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA512`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_MD_HASH_SHA512_SHANI` if the CPU supports its features (`VG_MD_HASH_SHA512_SHANI_FEATURES`); `VG_MD_HASH_SHA512_AVX2` if the CPU supports its features (`VG_MD_HASH_SHA512_AVX2_FEATURES`); or else `VG_MD_HASH_SHA512`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA512`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_MD_HASH_SHA512_SHANI` if the CPU supports its features (`VG_MD_HASH_SHA512_SHANI_FEATURES`); `VG_MD_HASH_SHA512_AVX2` if the CPU supports its features (`VG_MD_HASH_SHA512_AVX2_FEATURES`); or else `VG_MD_HASH_SHA512`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_md_hash_sha512_auto() -> &'static crate::fn_tables::MdHash { let cpu = verified_garbage_cpu::detected(); @@ -275,75 +343,7 @@ pub fn vg_md_hash_sha512_auto() -> &'static crate::fn_tables::MdHash { &crate::fn_tables::VG_MD_HASH_SHA512 } -/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA512_224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HASH_SHA512_224_SHANI` if the CPU supports its features (`VG_HASH_SHA512_224_SHANI_FEATURES`); `VG_HASH_SHA512_224_AVX2` if the CPU supports its features (`VG_HASH_SHA512_224_AVX2_FEATURES`); or else `VG_HASH_SHA512_224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. -#[inline] -pub fn vg_hash_sha512_224_auto() -> &'static crate::fn_tables::Hash { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HASH_SHA512_224_SHANI_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HASH_SHA512_224_SHANI_FEATURES) - { - return &crate::fn_tables::VG_HASH_SHA512_224_SHANI; - } - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HASH_SHA512_224_AVX2_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HASH_SHA512_224_AVX2_FEATURES) - { - return &crate::fn_tables::VG_HASH_SHA512_224_AVX2; - } - &crate::fn_tables::VG_HASH_SHA512_224 -} - -/// A table of functions of the interface `Hash` with the data of `VG_HASH_SHA512_256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HASH_SHA512_256_SHANI` if the CPU supports its features (`VG_HASH_SHA512_256_SHANI_FEATURES`); `VG_HASH_SHA512_256_AVX2` if the CPU supports its features (`VG_HASH_SHA512_256_AVX2_FEATURES`); or else `VG_HASH_SHA512_256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. -#[inline] -pub fn vg_hash_sha512_256_auto() -> &'static crate::fn_tables::Hash { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HASH_SHA512_256_SHANI_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HASH_SHA512_256_SHANI_FEATURES) - { - return &crate::fn_tables::VG_HASH_SHA512_256_SHANI; - } - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HASH_SHA512_256_AVX2_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HASH_SHA512_256_AVX2_FEATURES) - { - return &crate::fn_tables::VG_HASH_SHA512_256_AVX2; - } - &crate::fn_tables::VG_HASH_SHA512_256 -} - -/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA512_224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HMAC_SHA512_224_SHANI` if the CPU supports its features (`VG_HMAC_SHA512_224_SHANI_FEATURES`); `VG_HMAC_SHA512_224_AVX2` if the CPU supports its features (`VG_HMAC_SHA512_224_AVX2_FEATURES`); or else `VG_HMAC_SHA512_224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. -#[inline] -pub fn vg_hmac_sha512_224_auto() -> &'static crate::fn_tables::Hmac { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA512_224_SHANI_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA512_224_SHANI_FEATURES) - { - return &crate::fn_tables::VG_HMAC_SHA512_224_SHANI; - } - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA512_224_AVX2_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA512_224_AVX2_FEATURES) - { - return &crate::fn_tables::VG_HMAC_SHA512_224_AVX2; - } - &crate::fn_tables::VG_HMAC_SHA512_224 -} - -/// A table of functions of the interface `Hmac` with the data of `VG_HMAC_SHA512_256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_HMAC_SHA512_256_SHANI` if the CPU supports its features (`VG_HMAC_SHA512_256_SHANI_FEATURES`); `VG_HMAC_SHA512_256_AVX2` if the CPU supports its features (`VG_HMAC_SHA512_256_AVX2_FEATURES`); or else `VG_HMAC_SHA512_256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. -#[inline] -pub fn vg_hmac_sha512_256_auto() -> &'static crate::fn_tables::Hmac { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA512_256_SHANI_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA512_256_SHANI_FEATURES) - { - return &crate::fn_tables::VG_HMAC_SHA512_256_SHANI; - } - if const { verified_garbage_cpu::target_contains(crate::fn_tables::VG_HMAC_SHA512_256_AVX2_FEATURES) } - || cpu.contains(crate::fn_tables::VG_HMAC_SHA512_256_AVX2_FEATURES) - { - return &crate::fn_tables::VG_HMAC_SHA512_256_AVX2; - } - &crate::fn_tables::VG_HMAC_SHA512_256 -} - -/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA512_224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_MD_HASH_SHA512_224_SHANI` if the CPU supports its features (`VG_MD_HASH_SHA512_224_SHANI_FEATURES`); `VG_MD_HASH_SHA512_224_AVX2` if the CPU supports its features (`VG_MD_HASH_SHA512_224_AVX2_FEATURES`); or else `VG_MD_HASH_SHA512_224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA512_224`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_MD_HASH_SHA512_224_SHANI` if the CPU supports its features (`VG_MD_HASH_SHA512_224_SHANI_FEATURES`); `VG_MD_HASH_SHA512_224_AVX2` if the CPU supports its features (`VG_MD_HASH_SHA512_224_AVX2_FEATURES`); or else `VG_MD_HASH_SHA512_224`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_md_hash_sha512_224_auto() -> &'static crate::fn_tables::MdHash { let cpu = verified_garbage_cpu::detected(); @@ -360,7 +360,7 @@ pub fn vg_md_hash_sha512_224_auto() -> &'static crate::fn_tables::MdHash { &crate::fn_tables::VG_MD_HASH_SHA512_224 } -/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA512_256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `VG_MD_HASH_SHA512_256_SHANI` if the CPU supports its features (`VG_MD_HASH_SHA512_256_SHANI_FEATURES`); `VG_MD_HASH_SHA512_256_AVX2` if the CPU supports its features (`VG_MD_HASH_SHA512_256_AVX2_FEATURES`); or else `VG_MD_HASH_SHA512_256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. +/// A table of functions of the interface `MdHash` with the data of `VG_MD_HASH_SHA512_256`, which every function taking one computes the same with (`VG.Rust.tableDispatcher`): the first of these that applies, in the order its registration file lists them: `VG_MD_HASH_SHA512_256_SHANI` if the CPU supports its features (`VG_MD_HASH_SHA512_256_SHANI_FEATURES`); `VG_MD_HASH_SHA512_256_AVX2` if the CPU supports its features (`VG_MD_HASH_SHA512_256_AVX2_FEATURES`); or else `VG_MD_HASH_SHA512_256`, which needs no CPU feature. Its CPU features, if any, are those of the table returned, which the CPU has. #[inline] pub fn vg_md_hash_sha512_256_auto() -> &'static crate::fn_tables::MdHash { let cpu = verified_garbage_cpu::detected(); diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/gcm.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/gcm.rs index cf7f82f8ca8..28688b0517c 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/gcm.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/gcm.rs @@ -2,229 +2,11 @@ //! The dispatchers of the verified `gcm` functions for `x86_64` //! (`crate::gcm`, `VG.Rust.dispatcher`). -/// The AES-GCM key setup: writes the key context of the `key_len`-byte AES key at `key` to `*ctx`: its key schedule for `Nr = key_len / 4 + 6` rounds (FIPS 197 §5.2, as `vg_aes_expand_key` writes it) in the first `16 * (Nr + 1)` bytes, and the hash subkey `H = CIPH_K(0¹²⁸)` (NIST SP 800-38D §7.1 step 1) in bytes 240–255. The other bytes are unspecified. The other `vg_aes_gcm_*` functions read it, with `Nr` as their `rounds`. -/// -/// Contract: `VG.Spec.Gcm.initContract`. The key context is `VG.Spec.Gcm.KeyRepr`. Constant time: only the pointers and `key_len` may affect timing, not the key. -/// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_init_vaes` if the CPU supports its features (`VG_AES_GCM_INIT_VAES_FEATURES`); `vg_aes_gcm_init_aesni` if the CPU supports its features (`VG_AES_GCM_INIT_AESNI_FEATURES`); or else `vg_aes_gcm_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. -/// -/// # Safety -/// -/// * `key` must be valid for reads of `key_len` bytes. -/// * `ctx` must be valid for reads and writes of 256 bytes. -/// * `key_len` must be 16, 24 or 32. -/// * `ctx` must not overlap `key` (distinct Rust objects never do). -/// * Neither `key` nor `ctx` may overlap the return address on the stack or the 2576 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_aes_gcm_init_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *const u8, key_len: usize, ctx: *mut [u64; 32]) { - /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(key: *const u8, key_len: usize, ctx: *mut [u64; 32]); - /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only - /// features the CPU has. - #[cold] - fn select() -> Impl { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_INIT_VAES_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_INIT_VAES_FEATURES) - { - return crate::gcm::vg_aes_gcm_init_vaes; - } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_INIT_AESNI_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_INIT_AESNI_FEATURES) - { - return crate::gcm::vg_aes_gcm_init_aesni; - } - crate::gcm::vg_aes_gcm_init - } - /// The first call: chooses the implementation, records it in `CHOSEN` for the calls - /// after it, and runs it. - #[cold] - unsafe extern "sysv64" fn first(key: *const u8, key_len: usize, ctx: *mut [u64; 32]) { - let f = select(); - CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); - // SAFETY: the caller's obligations, which are those of every implementation, `f` - // among them, and the CPU has the features `f` needs. - unsafe { f(key, key_len, ctx) } - } - /// The function to call: `first`, until it records the implementation it chose. - static CHOSEN: core::sync::atomic::AtomicPtr<()> = - core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); - // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, - // or an implementation `select` returned, which needs only features the CPU has. Calling - // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(key, key_len, ctx) } -} - -/// Makes the 1032-byte `*ctx`, whose first 256 bytes are a key context of `vg_aes_gcm_init`, the prepared key context of the same key, in place: keeps its first 256 bytes, at byte `256 + 32*k`, for `k` from 0 to `n/2 - 1`, stores `preparedPower(H^(2*k+2))` and then `preparedPower(H^(2*k+1))`, as two little-endian 128-bit words, `H` being the hash subkey in its bytes 240–255, and stores `n` in the 64-bit word at byte 1024, `n` being 48, 16 or 0, as the implementation's other functions read. Use only the `*_prepared` APIs, or APIs that read the first 256 bytes, with this format. Contract: `VG.Spec.Gcm.prepareContract`: the first 256 bytes are kept and the context satisfies `PreparedRepr`. Constant time: only the pointer may affect timing, not the hash subkey. -/// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_prepare_vpclmul_avx512` if the CPU supports its features (`VG_AES_GCM_PREPARE_VPCLMUL_AVX512_FEATURES`); `vg_aes_gcm_prepare_vpclmul` if the CPU supports its features (`VG_AES_GCM_PREPARE_VPCLMUL_FEATURES`); `vg_aes_gcm_prepare_pclmul_avx` if the CPU supports its features (`VG_AES_GCM_PREPARE_PCLMUL_AVX_FEATURES`); or else `vg_aes_gcm_prepare`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. -/// -/// # Safety -/// -/// * `ctx` must be valid for reads and writes of 1032 bytes. -/// * `ctx` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_aes_gcm_prepare_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *mut [u64; 129]) { - /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(ctx: *mut [u64; 129]); - /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only - /// features the CPU has. - #[cold] - fn select() -> Impl { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_PREPARE_VPCLMUL_AVX512_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_PREPARE_VPCLMUL_AVX512_FEATURES) - { - return crate::gcm::vg_aes_gcm_prepare_vpclmul_avx512; - } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_PREPARE_VPCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_PREPARE_VPCLMUL_FEATURES) - { - return crate::gcm::vg_aes_gcm_prepare_vpclmul; - } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_PREPARE_PCLMUL_AVX_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_PREPARE_PCLMUL_AVX_FEATURES) - { - return crate::gcm::vg_aes_gcm_prepare_pclmul_avx; - } - crate::gcm::vg_aes_gcm_prepare - } - /// The first call: chooses the implementation, records it in `CHOSEN` for the calls - /// after it, and runs it. - #[cold] - unsafe extern "sysv64" fn first(ctx: *mut [u64; 129]) { - let f = select(); - CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); - // SAFETY: the caller's obligations, which are those of every implementation, `f` - // among them, and the CPU has the features `f` needs. - unsafe { f(ctx) } - } - /// The function to call: `first`, until it records the implementation it chose. - static CHOSEN: core::sync::atomic::AtomicPtr<()> = - core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); - // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, - // or an implementation `select` returned, which needs only features the CPU has. Calling - // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx) } -} - -/// Absorbs additional data into an incremental AES-GCM encryption or decryption: with the key context `*ctx`, if the streaming state `*state` represents a message with `aad_len` bytes (modulo 2⁶⁴) of additional data and no text yet, it then represents that message with the `len` bytes at `data` appended to its additional data. All of the additional data comes before the text (§7.1 step 5): after text, the state represents no message. -/// -/// Contract: `VG.Spec.Gcm.streamAadContract`. Constant time: only the pointers, `aad_len` and `len` may affect timing, not the key context, the state or the data. -/// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_stream_aad_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_AAD_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_aad_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_AAD_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_aad`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. -/// -/// # Safety -/// -/// * `ctx` must be valid for reads of 256 bytes. -/// * `state` must be valid for reads and writes of 80 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `state` must not overlap `ctx` or `data` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the return address on the stack or the 2576 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_aes_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { - /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize); - /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only - /// features the CPU has. - #[cold] - fn select() -> Impl { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_AAD_VPCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_AAD_VPCLMUL_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_aad_vpclmul; - } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_AAD_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_AAD_PCLMUL_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_aad_pclmul; - } - crate::gcm::vg_aes_gcm_stream_aad - } - /// The first call: chooses the implementation, records it in `CHOSEN` for the calls - /// after it, and runs it. - #[cold] - unsafe extern "sysv64" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { - let f = select(); - CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); - // SAFETY: the caller's obligations, which are those of every implementation, `f` - // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, state, aad_len, data, len) } - } - /// The function to call: `first`, until it records the implementation it chose. - static CHOSEN: core::sync::atomic::AtomicPtr<()> = - core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); - // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, - // or an implementation `select` returned, which needs only features the CPU has. Calling - // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, data, len) } -} - -/// Starts an incremental AES-GCM encryption or decryption (NIST SP 800-38D §7): with the key context `*ctx` that `vg_aes_gcm_init` wrote, makes the streaming state `*state` represent the message with the IV the `nonce_len` bytes at `nonce`, and no additional data or text yet. Continue with `vg_aes_gcm_stream_aad`, then `vg_aes_gcm_stream_encrypt` or `vg_aes_gcm_stream_decrypt`, then `vg_aes_gcm_stream_finish` or `vg_aes_gcm_stream_verify`, with the same key context. -/// -/// The function checks no length: GCM requires a nonce of 1 to `2^61 - 1` bytes (§5.2.1.1), which the caller must check. -/// -/// Contract: `VG.Spec.Gcm.streamInitContract`. The streaming state is `J₀`, the GHASH accumulator, a partial block, the next counter block and a keystream block (`VG.Spec.Gcm.StreamRepr`). Constant time: only the pointers and `nonce_len` may affect timing, not the key context or the nonce. -/// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_stream_init_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_INIT_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_init_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_INIT_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. -/// -/// # Safety -/// -/// * `ctx` must be valid for reads of 256 bytes. -/// * `nonce` must be valid for reads of `nonce_len` bytes. -/// * `state` must be valid for reads and writes of 80 bytes. -/// * `state` must not overlap `ctx` or `nonce` (distinct Rust objects never do). -/// * None of `ctx`, `nonce` and `state` may overlap the return address on the stack or the 2576 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_aes_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { - /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]); - /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only - /// features the CPU has. - #[cold] - fn select() -> Impl { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_INIT_VPCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_INIT_VPCLMUL_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_init_vpclmul; - } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_INIT_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_INIT_PCLMUL_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_init_pclmul; - } - crate::gcm::vg_aes_gcm_stream_init - } - /// The first call: chooses the implementation, records it in `CHOSEN` for the calls - /// after it, and runs it. - #[cold] - unsafe extern "sysv64" fn first(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { - let f = select(); - CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); - // SAFETY: the caller's obligations, which are those of every implementation, `f` - // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, nonce, nonce_len, state) } - } - /// The function to call: `first`, until it records the implementation it chose. - static CHOSEN: core::sync::atomic::AtomicPtr<()> = - core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); - // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, - // or an implementation `select` returned, which needs only features the CPU has. Calling - // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, nonce, nonce_len, state) } -} - /// Finishes an incremental AES-GCM encryption or decryption (NIST SP 800-38D §7.1 steps 5–6, §7.2 steps 6–7): with the key context `*ctx` that `vg_aes_gcm_init` wrote for `rounds` rounds, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, writes its 128-bit tag to `*tag`. A shorter tag is the first bytes of this one; to check a received tag, use `vg_aes_gcm_stream_verify`. /// /// Contract: `VG.Spec.Gcm.streamFinishContract`. Constant time: only the pointers, `rounds`, `aad_len` and `text_len` may affect timing, not the key context or the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_stream_finish_vaes_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_VAES_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_finish_vaes_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_VAES_PCLMUL_FEATURES`); `vg_aes_gcm_stream_finish_aesni_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_AESNI_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_finish_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_stream_finish_vaes` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_VAES_FEATURES`); `vg_aes_gcm_stream_finish_aesni` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_AESNI_FEATURES`); `vg_aes_gcm_stream_finish_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_finish_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_finish`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_stream_finish_vaes_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_VAES_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_finish_vaes_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_VAES_PCLMUL_FEATURES`); `vg_aes_gcm_stream_finish_vaes` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_VAES_FEATURES`); `vg_aes_gcm_stream_finish_aesni_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_AESNI_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_finish_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_stream_finish_aesni` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_AESNI_FEATURES`); `vg_aes_gcm_stream_finish_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_finish_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_FINISH_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_finish`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -255,6 +37,11 @@ pub unsafe fn vg_aes_gcm_stream_finish_auto(_dit: verified_garbage_cpu::Dit<'_>, { return crate::gcm::vg_aes_gcm_stream_finish_vaes_pclmul; } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_FINISH_VAES_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_FINISH_VAES_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_finish_vaes; + } if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_FINISH_AESNI_VPCLMUL_FEATURES) } || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_FINISH_AESNI_VPCLMUL_FEATURES) { @@ -265,11 +52,6 @@ pub unsafe fn vg_aes_gcm_stream_finish_auto(_dit: verified_garbage_cpu::Dit<'_>, { return crate::gcm::vg_aes_gcm_stream_finish_aesni_pclmul; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_FINISH_VAES_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_FINISH_VAES_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_finish_vaes; - } if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_FINISH_AESNI_FEATURES) } || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_FINISH_AESNI_FEATURES) { @@ -310,7 +92,7 @@ pub unsafe fn vg_aes_gcm_stream_finish_auto(_dit: verified_garbage_cpu::Dit<'_>, /// /// Contract: `VG.Spec.Gcm.streamVerifyContract`. Constant time: only the pointers, `rounds`, `aad_len`, `text_len` and `tag_len` may affect timing, not the key context, the state or the tags. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_stream_verify_vaes_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_VAES_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_verify_vaes_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_VAES_PCLMUL_FEATURES`); `vg_aes_gcm_stream_verify_aesni_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_AESNI_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_verify_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_stream_verify_vaes` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_VAES_FEATURES`); `vg_aes_gcm_stream_verify_aesni` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_AESNI_FEATURES`); `vg_aes_gcm_stream_verify_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_verify_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_stream_verify_vaes_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_VAES_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_verify_vaes_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_VAES_PCLMUL_FEATURES`); `vg_aes_gcm_stream_verify_vaes` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_VAES_FEATURES`); `vg_aes_gcm_stream_verify_aesni_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_AESNI_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_verify_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_stream_verify_aesni` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_AESNI_FEATURES`); `vg_aes_gcm_stream_verify_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_verify_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_VERIFY_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -341,6 +123,11 @@ pub unsafe fn vg_aes_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, { return crate::gcm::vg_aes_gcm_stream_verify_vaes_pclmul; } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_VAES_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_VAES_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_verify_vaes; + } if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_AESNI_VPCLMUL_FEATURES) } || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_AESNI_VPCLMUL_FEATURES) { @@ -351,11 +138,6 @@ pub unsafe fn vg_aes_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, { return crate::gcm::vg_aes_gcm_stream_verify_aesni_pclmul; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_VAES_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_VAES_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_verify_vaes; - } if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_AESNI_FEATURES) } || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_AESNI_FEATURES) { @@ -364,24 +146,78 @@ pub unsafe fn vg_aes_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_VPCLMUL_FEATURES) } || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_VPCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_verify_vpclmul; + return crate::gcm::vg_aes_gcm_stream_verify_vpclmul; + } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_PCLMUL_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_verify_pclmul; + } + crate::gcm::vg_aes_gcm_stream_verify + } + /// The first call: chooses the implementation, records it in `CHOSEN` for the calls + /// after it, and runs it. + #[cold] + unsafe extern "sysv64" fn first(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *const u8, tag_len: usize) -> u32 { + let f = select(); + CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); + // SAFETY: the caller's obligations, which are those of every implementation, `f` + // among them, and the CPU has the features `f` needs. + unsafe { f(ctx, rounds, state, aad_len, text_len, tag, tag_len) } + } + /// The function to call: `first`, until it records the implementation it chose. + static CHOSEN: core::sync::atomic::AtomicPtr<()> = + core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); + // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, + // or an implementation `select` returned, which needs only features the CPU has. Calling + // either carries the caller's obligations, which are those of every implementation. + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, state, aad_len, text_len, tag, tag_len) } +} + +/// The AES-GCM key setup: writes the key context of the `key_len`-byte AES key at `key` to `*ctx`: its key schedule for `Nr = key_len / 4 + 6` rounds (FIPS 197 §5.2, as `vg_aes_expand_key` writes it) in the first `16 * (Nr + 1)` bytes, and the hash subkey `H = CIPH_K(0¹²⁸)` (NIST SP 800-38D §7.1 step 1) in bytes 240–255. The other bytes are unspecified. The other `vg_aes_gcm_*` functions read it, with `Nr` as their `rounds`. +/// +/// Contract: `VG.Spec.Gcm.initContract`. The key context is `VG.Spec.Gcm.KeyRepr`. Constant time: only the pointers and `key_len` may affect timing, not the key. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_init_vaes` if the CPU supports its features (`VG_AES_GCM_INIT_VAES_FEATURES`); `vg_aes_gcm_init_aesni` if the CPU supports its features (`VG_AES_GCM_INIT_AESNI_FEATURES`); or else `vg_aes_gcm_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// +/// # Safety +/// +/// * `key` must be valid for reads of `key_len` bytes. +/// * `ctx` must be valid for reads and writes of 256 bytes. +/// * `key_len` must be 16, 24 or 32. +/// * `ctx` must not overlap `key` (distinct Rust objects never do). +/// * Neither `key` nor `ctx` may overlap the return address on the stack or the 2576 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_aes_gcm_init_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *const u8, key_len: usize, ctx: *mut [u64; 32]) { + /// The type of the implementations. + type Impl = unsafe extern "sysv64" fn(key: *const u8, key_len: usize, ctx: *mut [u64; 32]); + /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only + /// features the CPU has. + #[cold] + fn select() -> Impl { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_INIT_VAES_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_INIT_VAES_FEATURES) + { + return crate::gcm::vg_aes_gcm_init_vaes; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_VERIFY_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_INIT_AESNI_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_INIT_AESNI_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_verify_pclmul; + return crate::gcm::vg_aes_gcm_init_aesni; } - crate::gcm::vg_aes_gcm_stream_verify + crate::gcm::vg_aes_gcm_init } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(ctx: *const [u64; 32], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *const u8, tag_len: usize) -> u32 { + unsafe extern "sysv64" fn first(key: *const u8, key_len: usize, ctx: *mut [u64; 32]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, rounds, state, aad_len, text_len, tag, tag_len) } + unsafe { f(key, key_len, ctx) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -389,12 +225,12 @@ pub unsafe fn vg_aes_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, state, aad_len, text_len, tag, tag_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(key, key_len, ctx) } } /// `vg_aes_gcm_seal` with a 1032-byte context from `vg_aes_gcm_prepare`. Computes the same result, using the AES key schedule and hash subkey in its first 256 bytes, and may read the prepared powers its count says are present. The powers remain secret; how many are present may affect timing; all other memory, aliasing and constant-time requirements are unchanged. Contract: `VG.Spec.Gcm.sealPreparedContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_seal_prepared_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_GCM_SEAL_PREPARED_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_gcm_seal_prepared_vaes_vpclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_PREPARED_VAES_VPCLMUL_FEATURES`); `vg_aes_gcm_seal_prepared_vaes_pclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_PREPARED_VAES_PCLMUL_FEATURES`); `vg_aes_gcm_seal_prepared_aesni_vpclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_PREPARED_AESNI_VPCLMUL_FEATURES`); `vg_aes_gcm_seal_prepared_aesni_pclmul_avx` if the CPU supports its features (`VG_AES_GCM_SEAL_PREPARED_AESNI_PCLMUL_AVX_FEATURES`); `vg_aes_gcm_seal_prepared_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_PREPARED_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_seal_prepared_vaes` if the CPU supports its features (`VG_AES_GCM_SEAL_PREPARED_VAES_FEATURES`); `vg_aes_gcm_seal_prepared_aesni` if the CPU supports its features (`VG_AES_GCM_SEAL_PREPARED_AESNI_FEATURES`); `vg_aes_gcm_seal_prepared_vpclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_PREPARED_VPCLMUL_FEATURES`); `vg_aes_gcm_seal_prepared_pclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_PREPARED_PCLMUL_FEATURES`); or else `vg_aes_gcm_seal_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_seal_prepared_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_GCM_SEAL_PREPARED_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_gcm_seal_prepared_vaes_vpclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_PREPARED_VAES_VPCLMUL_FEATURES`); `vg_aes_gcm_seal_prepared_vaes_pclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_PREPARED_VAES_PCLMUL_FEATURES`); `vg_aes_gcm_seal_prepared_aesni_vpclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_PREPARED_AESNI_VPCLMUL_FEATURES`); `vg_aes_gcm_seal_prepared_aesni_pclmul_avx` if the CPU supports its features (`VG_AES_GCM_SEAL_PREPARED_AESNI_PCLMUL_AVX_FEATURES`); `vg_aes_gcm_seal_prepared_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_PREPARED_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_seal_prepared_vaes` if the CPU supports its features (`VG_AES_GCM_SEAL_PREPARED_VAES_FEATURES`); `vg_aes_gcm_seal_prepared_aesni` if the CPU supports its features (`VG_AES_GCM_SEAL_PREPARED_AESNI_FEATURES`); `vg_aes_gcm_seal_prepared_vpclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_PREPARED_VPCLMUL_FEATURES`); `vg_aes_gcm_seal_prepared_pclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_PREPARED_PCLMUL_FEATURES`); or else `vg_aes_gcm_seal_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -490,7 +326,7 @@ pub unsafe fn vg_aes_gcm_seal_prepared_auto(_dit: verified_garbage_cpu::Dit<'_>, /// `vg_aes_gcm_open` with a 1032-byte context from `vg_aes_gcm_prepare`. Computes the same result, using the AES key schedule and hash subkey in its first 256 bytes, and may read the prepared powers its count says are present. The powers remain secret; how many are present may affect timing; all other memory, aliasing and constant-time requirements are unchanged. Contract: `VG.Spec.Gcm.openPreparedContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_open_prepared_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_GCM_OPEN_PREPARED_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_gcm_open_prepared_vaes_vpclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_PREPARED_VAES_VPCLMUL_FEATURES`); `vg_aes_gcm_open_prepared_vaes_pclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_PREPARED_VAES_PCLMUL_FEATURES`); `vg_aes_gcm_open_prepared_aesni_vpclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_PREPARED_AESNI_VPCLMUL_FEATURES`); `vg_aes_gcm_open_prepared_aesni_pclmul_avx` if the CPU supports its features (`VG_AES_GCM_OPEN_PREPARED_AESNI_PCLMUL_AVX_FEATURES`); `vg_aes_gcm_open_prepared_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_PREPARED_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_open_prepared_vaes` if the CPU supports its features (`VG_AES_GCM_OPEN_PREPARED_VAES_FEATURES`); `vg_aes_gcm_open_prepared_aesni` if the CPU supports its features (`VG_AES_GCM_OPEN_PREPARED_AESNI_FEATURES`); `vg_aes_gcm_open_prepared_vpclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_PREPARED_VPCLMUL_FEATURES`); `vg_aes_gcm_open_prepared_pclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_PREPARED_PCLMUL_FEATURES`); or else `vg_aes_gcm_open_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_open_prepared_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_GCM_OPEN_PREPARED_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_gcm_open_prepared_vaes_vpclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_PREPARED_VAES_VPCLMUL_FEATURES`); `vg_aes_gcm_open_prepared_vaes_pclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_PREPARED_VAES_PCLMUL_FEATURES`); `vg_aes_gcm_open_prepared_aesni_vpclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_PREPARED_AESNI_VPCLMUL_FEATURES`); `vg_aes_gcm_open_prepared_aesni_pclmul_avx` if the CPU supports its features (`VG_AES_GCM_OPEN_PREPARED_AESNI_PCLMUL_AVX_FEATURES`); `vg_aes_gcm_open_prepared_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_PREPARED_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_open_prepared_vaes` if the CPU supports its features (`VG_AES_GCM_OPEN_PREPARED_VAES_FEATURES`); `vg_aes_gcm_open_prepared_aesni` if the CPU supports its features (`VG_AES_GCM_OPEN_PREPARED_AESNI_FEATURES`); `vg_aes_gcm_open_prepared_vpclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_PREPARED_VPCLMUL_FEATURES`); `vg_aes_gcm_open_prepared_pclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_PREPARED_PCLMUL_FEATURES`); or else `vg_aes_gcm_open_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -584,53 +420,9 @@ pub unsafe fn vg_aes_gcm_open_prepared_auto(_dit: verified_garbage_cpu::Dit<'_>, unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, nonce, nonce_len, aad, aad_len, data, len, tag, tag_len) } } -/// Wipes the 1032-byte `*ctx`, a prepared key context of `vg_aes_gcm_prepare`: sets to zero the key context and the powers its count (the 64-bit word at byte 1024) says are present: its first 1024 bytes if the count is at least 48, 512 if it is at least 16, and 256 otherwise (the bytes after them were not written). Contract: `VG.Spec.Gcm.wipePreparedContract`. Constant time: only the pointer and which of those the count is may affect timing, not the bytes wiped. -/// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_wipe_prepared_avx` if the CPU supports its features (`VG_AES_GCM_WIPE_PREPARED_AVX_FEATURES`); or else `vg_aes_gcm_wipe_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. -/// -/// # Safety -/// -/// * `ctx` must be valid for reads and writes of 1032 bytes. -/// * `ctx` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_aes_gcm_wipe_prepared_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *mut [u64; 129]) { - /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(ctx: *mut [u64; 129]); - /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only - /// features the CPU has. - #[cold] - fn select() -> Impl { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_WIPE_PREPARED_AVX_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_WIPE_PREPARED_AVX_FEATURES) - { - return crate::gcm::vg_aes_gcm_wipe_prepared_avx; - } - crate::gcm::vg_aes_gcm_wipe_prepared - } - /// The first call: chooses the implementation, records it in `CHOSEN` for the calls - /// after it, and runs it. - #[cold] - unsafe extern "sysv64" fn first(ctx: *mut [u64; 129]) { - let f = select(); - CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); - // SAFETY: the caller's obligations, which are those of every implementation, `f` - // among them, and the CPU has the features `f` needs. - unsafe { f(ctx) } - } - /// The function to call: `first`, until it records the implementation it chose. - static CHOSEN: core::sync::atomic::AtomicPtr<()> = - core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); - // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, - // or an implementation `select` returned, which needs only features the CPU has. Calling - // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx) } -} - /// `vg_aes_gcm_open_to` with a 1032-byte context from `vg_aes_gcm_prepare`. Computes the same result, using the AES key schedule and hash subkey in its first 256 bytes, and may read the prepared powers its count says are present. The powers remain secret; how many are present may affect timing; all other memory, aliasing and constant-time requirements are unchanged. Contract: `VG.Spec.Gcm.openToPreparedContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_open_to_prepared_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PREPARED_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_gcm_open_to_prepared_vaes_vpclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PREPARED_VAES_VPCLMUL_FEATURES`); `vg_aes_gcm_open_to_prepared_vaes_pclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PREPARED_VAES_PCLMUL_FEATURES`); `vg_aes_gcm_open_to_prepared_aesni_vpclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PREPARED_AESNI_VPCLMUL_FEATURES`); `vg_aes_gcm_open_to_prepared_aesni_pclmul_avx` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PREPARED_AESNI_PCLMUL_AVX_FEATURES`); `vg_aes_gcm_open_to_prepared_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PREPARED_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_open_to_prepared_vaes` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PREPARED_VAES_FEATURES`); `vg_aes_gcm_open_to_prepared_aesni` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PREPARED_AESNI_FEATURES`); `vg_aes_gcm_open_to_prepared_vpclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PREPARED_VPCLMUL_FEATURES`); `vg_aes_gcm_open_to_prepared_pclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PREPARED_PCLMUL_FEATURES`); or else `vg_aes_gcm_open_to_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_open_to_prepared_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PREPARED_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_gcm_open_to_prepared_vaes_vpclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PREPARED_VAES_VPCLMUL_FEATURES`); `vg_aes_gcm_open_to_prepared_vaes_pclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PREPARED_VAES_PCLMUL_FEATURES`); `vg_aes_gcm_open_to_prepared_aesni_vpclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PREPARED_AESNI_VPCLMUL_FEATURES`); `vg_aes_gcm_open_to_prepared_aesni_pclmul_avx` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PREPARED_AESNI_PCLMUL_AVX_FEATURES`); `vg_aes_gcm_open_to_prepared_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PREPARED_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_open_to_prepared_vaes` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PREPARED_VAES_FEATURES`); `vg_aes_gcm_open_to_prepared_aesni` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PREPARED_AESNI_FEATURES`); `vg_aes_gcm_open_to_prepared_vpclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PREPARED_VPCLMUL_FEATURES`); `vg_aes_gcm_open_to_prepared_pclmul` if the CPU supports its features (`VG_AES_GCM_OPEN_TO_PREPARED_PCLMUL_FEATURES`); or else `vg_aes_gcm_open_to_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -698,24 +490,212 @@ pub unsafe fn vg_aes_gcm_open_to_prepared_auto(_dit: verified_garbage_cpu::Dit<' if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_OPEN_TO_PREPARED_VPCLMUL_FEATURES) } || cpu.contains(crate::gcm::VG_AES_GCM_OPEN_TO_PREPARED_VPCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_open_to_prepared_vpclmul; + return crate::gcm::vg_aes_gcm_open_to_prepared_vpclmul; + } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_OPEN_TO_PREPARED_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_OPEN_TO_PREPARED_PCLMUL_FEATURES) + { + return crate::gcm::vg_aes_gcm_open_to_prepared_pclmul; + } + crate::gcm::vg_aes_gcm_open_to_prepared + } + /// The first call: chooses the implementation, records it in `CHOSEN` for the calls + /// after it, and runs it. + #[cold] + unsafe extern "sysv64" fn first(ctx: *const [u64; 129], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const u8, len: usize, dst: *mut u8, dst_len: usize, tag: *const u8, tag_len: usize) -> u32 { + let f = select(); + CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); + // SAFETY: the caller's obligations, which are those of every implementation, `f` + // among them, and the CPU has the features `f` needs. + unsafe { f(ctx, rounds, nonce, nonce_len, aad, aad_len, src, len, dst, dst_len, tag, tag_len) } + } + /// The function to call: `first`, until it records the implementation it chose. + static CHOSEN: core::sync::atomic::AtomicPtr<()> = + core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); + // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, + // or an implementation `select` returned, which needs only features the CPU has. Calling + // either carries the caller's obligations, which are those of every implementation. + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, nonce, nonce_len, aad, aad_len, src, len, dst, dst_len, tag, tag_len) } +} + +/// `vg_aes_gcm_stream_encrypt` with a 1032-byte context from `vg_aes_gcm_prepare`. Computes the same result, using the AES key schedule and hash subkey in its first 256 bytes, and may read the prepared powers its count says are present. The powers remain secret; how many are present may affect timing; all other memory, aliasing and constant-time requirements are unchanged. Contract: `VG.Spec.Gcm.streamEncryptPreparedContract`. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_stream_encrypt_prepared_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_gcm_stream_encrypt_prepared_vaes_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_encrypt_prepared_vaes_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_PCLMUL_FEATURES`); `vg_aes_gcm_stream_encrypt_prepared_aesni_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_encrypt_prepared_aesni_pclmul_avx` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_PCLMUL_AVX_FEATURES`); `vg_aes_gcm_stream_encrypt_prepared_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_stream_encrypt_prepared_vaes` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_FEATURES`); `vg_aes_gcm_stream_encrypt_prepared_aesni` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_FEATURES`); `vg_aes_gcm_stream_encrypt_prepared_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_encrypt_prepared_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PREPARED_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_encrypt_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// +/// # Safety +/// +/// * `ctx` must be valid for reads of 1032 bytes. +/// * `state` must be valid for reads and writes of 80 bytes. +/// * `data` must be valid for reads and writes of `len` bytes. +/// * `rounds` must be 10, 12 or 14. +/// * `*ctx` must have the prepared format written by `vg_aes_gcm_prepare` (not the format of `vg_aes_gcm_init_precomputed`). +/// * `state` and `data` must not overlap each other, `ctx` or the arguments on the stack (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the return address on the stack or the 2608 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_aes_gcm_stream_encrypt_prepared_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 129], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { + /// The type of the implementations. + type Impl = unsafe extern "sysv64" fn(ctx: *const [u64; 129], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); + /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only + /// features the CPU has. + #[cold] + fn select() -> Impl { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_VPCLMUL_AVX512_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_VPCLMUL_AVX512_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_encrypt_prepared_vaes_vpclmul_avx512; + } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_VPCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_VPCLMUL_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_encrypt_prepared_vaes_vpclmul; + } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_PCLMUL_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_encrypt_prepared_vaes_pclmul; + } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_VPCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_VPCLMUL_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_encrypt_prepared_aesni_vpclmul; + } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_PCLMUL_AVX_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_PCLMUL_AVX_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_encrypt_prepared_aesni_pclmul_avx; + } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_PCLMUL_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_encrypt_prepared_aesni_pclmul; + } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_encrypt_prepared_vaes; + } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_encrypt_prepared_aesni; + } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VPCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VPCLMUL_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_encrypt_prepared_vpclmul; + } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_PCLMUL_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_encrypt_prepared_pclmul; + } + crate::gcm::vg_aes_gcm_stream_encrypt_prepared + } + /// The first call: chooses the implementation, records it in `CHOSEN` for the calls + /// after it, and runs it. + #[cold] + unsafe extern "sysv64" fn first(ctx: *const [u64; 129], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { + let f = select(); + CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); + // SAFETY: the caller's obligations, which are those of every implementation, `f` + // among them, and the CPU has the features `f` needs. + unsafe { f(ctx, rounds, state, aad_len, text_len, data, len) } + } + /// The function to call: `first`, until it records the implementation it chose. + static CHOSEN: core::sync::atomic::AtomicPtr<()> = + core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); + // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, + // or an implementation `select` returned, which needs only features the CPU has. Calling + // either carries the caller's obligations, which are those of every implementation. + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, state, aad_len, text_len, data, len) } +} + +/// `vg_aes_gcm_stream_decrypt` with a 1032-byte context from `vg_aes_gcm_prepare`. Computes the same result, using the AES key schedule and hash subkey in its first 256 bytes, and may read the prepared powers its count says are present. The powers remain secret; how many are present may affect timing; all other memory, aliasing and constant-time requirements are unchanged. Contract: `VG.Spec.Gcm.streamDecryptPreparedContract`. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_stream_decrypt_prepared_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_gcm_stream_decrypt_prepared_vaes_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_decrypt_prepared_vaes_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_PCLMUL_FEATURES`); `vg_aes_gcm_stream_decrypt_prepared_aesni_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_decrypt_prepared_aesni_pclmul_avx` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_PCLMUL_AVX_FEATURES`); `vg_aes_gcm_stream_decrypt_prepared_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_stream_decrypt_prepared_vaes` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_FEATURES`); `vg_aes_gcm_stream_decrypt_prepared_aesni` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_FEATURES`); `vg_aes_gcm_stream_decrypt_prepared_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PREPARED_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_decrypt_prepared_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PREPARED_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_decrypt_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// +/// # Safety +/// +/// * `ctx` must be valid for reads of 1032 bytes. +/// * `state` must be valid for reads and writes of 80 bytes. +/// * `data` must be valid for reads and writes of `len` bytes. +/// * `rounds` must be 10, 12 or 14. +/// * `*ctx` must have the prepared format written by `vg_aes_gcm_prepare` (not the format of `vg_aes_gcm_init_precomputed`). +/// * `state` and `data` must not overlap each other, `ctx` or the arguments on the stack (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the return address on the stack or the 2608 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_aes_gcm_stream_decrypt_prepared_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 129], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { + /// The type of the implementations. + type Impl = unsafe extern "sysv64" fn(ctx: *const [u64; 129], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); + /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only + /// features the CPU has. + #[cold] + fn select() -> Impl { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_VPCLMUL_AVX512_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_VPCLMUL_AVX512_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_decrypt_prepared_vaes_vpclmul_avx512; + } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_VPCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_VPCLMUL_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_decrypt_prepared_vaes_vpclmul; + } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_PCLMUL_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_decrypt_prepared_vaes_pclmul; + } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_VPCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_VPCLMUL_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_decrypt_prepared_aesni_vpclmul; + } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_PCLMUL_AVX_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_PCLMUL_AVX_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_decrypt_prepared_aesni_pclmul_avx; + } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_PCLMUL_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_decrypt_prepared_aesni_pclmul; + } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_decrypt_prepared_vaes; + } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_decrypt_prepared_aesni; + } + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_VPCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_VPCLMUL_FEATURES) + { + return crate::gcm::vg_aes_gcm_stream_decrypt_prepared_vpclmul; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_OPEN_TO_PREPARED_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_OPEN_TO_PREPARED_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_PCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_open_to_prepared_pclmul; + return crate::gcm::vg_aes_gcm_stream_decrypt_prepared_pclmul; } - crate::gcm::vg_aes_gcm_open_to_prepared + crate::gcm::vg_aes_gcm_stream_decrypt_prepared } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(ctx: *const [u64; 129], rounds: usize, nonce: *const u8, nonce_len: usize, aad: *const u8, aad_len: usize, src: *const u8, len: usize, dst: *mut u8, dst_len: usize, tag: *const u8, tag_len: usize) -> u32 { + unsafe extern "sysv64" fn first(ctx: *const [u64; 129], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, rounds, nonce, nonce_len, aad, aad_len, src, len, dst, dst_len, tag, tag_len) } + unsafe { f(ctx, rounds, state, aad_len, text_len, data, len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -723,12 +703,12 @@ pub unsafe fn vg_aes_gcm_open_to_prepared_auto(_dit: verified_garbage_cpu::Dit<' // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, nonce, nonce_len, aad, aad_len, src, len, dst, dst_len, tag, tag_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, state, aad_len, text_len, data, len) } } /// `vg_aes_gcm_seal_gather` with a 1032-byte context from `vg_aes_gcm_prepare`. Computes the same result, using the AES key schedule and hash subkey in its first 256 bytes, and may read the prepared powers its count says are present. The powers remain secret; how many are present may affect timing; all other memory, aliasing and constant-time requirements are unchanged. Contract: `VG.Spec.Gcm.sealGatherPreparedContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_seal_gather_prepared_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PREPARED_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_gcm_seal_gather_prepared_vaes_vpclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PREPARED_VAES_VPCLMUL_FEATURES`); `vg_aes_gcm_seal_gather_prepared_vaes_pclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PREPARED_VAES_PCLMUL_FEATURES`); `vg_aes_gcm_seal_gather_prepared_aesni_vpclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PREPARED_AESNI_VPCLMUL_FEATURES`); `vg_aes_gcm_seal_gather_prepared_aesni_pclmul_avx` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PREPARED_AESNI_PCLMUL_AVX_FEATURES`); `vg_aes_gcm_seal_gather_prepared_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PREPARED_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_seal_gather_prepared_vaes` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PREPARED_VAES_FEATURES`); `vg_aes_gcm_seal_gather_prepared_aesni` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PREPARED_AESNI_FEATURES`); `vg_aes_gcm_seal_gather_prepared_vpclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PREPARED_VPCLMUL_FEATURES`); `vg_aes_gcm_seal_gather_prepared_pclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PREPARED_PCLMUL_FEATURES`); or else `vg_aes_gcm_seal_gather_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_seal_gather_prepared_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PREPARED_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_gcm_seal_gather_prepared_vaes_vpclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PREPARED_VAES_VPCLMUL_FEATURES`); `vg_aes_gcm_seal_gather_prepared_vaes_pclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PREPARED_VAES_PCLMUL_FEATURES`); `vg_aes_gcm_seal_gather_prepared_aesni_vpclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PREPARED_AESNI_VPCLMUL_FEATURES`); `vg_aes_gcm_seal_gather_prepared_aesni_pclmul_avx` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PREPARED_AESNI_PCLMUL_AVX_FEATURES`); `vg_aes_gcm_seal_gather_prepared_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PREPARED_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_seal_gather_prepared_vaes` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PREPARED_VAES_FEATURES`); `vg_aes_gcm_seal_gather_prepared_aesni` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PREPARED_AESNI_FEATURES`); `vg_aes_gcm_seal_gather_prepared_vpclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PREPARED_VPCLMUL_FEATURES`); `vg_aes_gcm_seal_gather_prepared_pclmul` if the CPU supports its features (`VG_AES_GCM_SEAL_GATHER_PREPARED_PCLMUL_FEATURES`); or else `vg_aes_gcm_seal_gather_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -824,90 +804,52 @@ pub unsafe fn vg_aes_gcm_seal_gather_prepared_auto(_dit: verified_garbage_cpu::D unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, nonce, nonce_len, aad, aad_len, src, src_count, dst, len, tag) } } -/// `vg_aes_gcm_stream_encrypt` with a 1032-byte context from `vg_aes_gcm_prepare`. Computes the same result, using the AES key schedule and hash subkey in its first 256 bytes, and may read the prepared powers its count says are present. The powers remain secret; how many are present may affect timing; all other memory, aliasing and constant-time requirements are unchanged. Contract: `VG.Spec.Gcm.streamEncryptPreparedContract`. +/// Starts an incremental AES-GCM encryption or decryption (NIST SP 800-38D §7): with the key context `*ctx` that `vg_aes_gcm_init` wrote, makes the streaming state `*state` represent the message with the IV the `nonce_len` bytes at `nonce`, and no additional data or text yet. Continue with `vg_aes_gcm_stream_aad`, then `vg_aes_gcm_stream_encrypt` or `vg_aes_gcm_stream_decrypt`, then `vg_aes_gcm_stream_finish` or `vg_aes_gcm_stream_verify`, with the same key context. +/// +/// The function checks no length: GCM requires a nonce of 1 to `2^61 - 1` bytes (§5.2.1.1), which the caller must check. +/// +/// Contract: `VG.Spec.Gcm.streamInitContract`. The streaming state is `J₀`, the GHASH accumulator, a partial block, the next counter block and a keystream block (`VG.Spec.Gcm.StreamRepr`). Constant time: only the pointers and `nonce_len` may affect timing, not the key context or the nonce. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_stream_encrypt_prepared_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_gcm_stream_encrypt_prepared_vaes_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_encrypt_prepared_vaes_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_PCLMUL_FEATURES`); `vg_aes_gcm_stream_encrypt_prepared_aesni_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_encrypt_prepared_aesni_pclmul_avx` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_PCLMUL_AVX_FEATURES`); `vg_aes_gcm_stream_encrypt_prepared_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_stream_encrypt_prepared_vaes` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_FEATURES`); `vg_aes_gcm_stream_encrypt_prepared_aesni` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_FEATURES`); `vg_aes_gcm_stream_encrypt_prepared_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_encrypt_prepared_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_ENCRYPT_PREPARED_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_encrypt_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_stream_init_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_INIT_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_init_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_INIT_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `ctx` must be valid for reads of 1032 bytes. +/// * `ctx` must be valid for reads of 256 bytes. +/// * `nonce` must be valid for reads of `nonce_len` bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `data` must be valid for reads and writes of `len` bytes. -/// * `rounds` must be 10, 12 or 14. -/// * `*ctx` must have the prepared format written by `vg_aes_gcm_prepare` (not the format of `vg_aes_gcm_init_precomputed`). -/// * `state` and `data` must not overlap each other, `ctx` or the arguments on the stack (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the return address on the stack or the 2608 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `state` must not overlap `ctx` or `nonce` (distinct Rust objects never do). +/// * None of `ctx`, `nonce` and `state` may overlap the return address on the stack or the 2576 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_stream_encrypt_prepared_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 129], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { +pub unsafe fn vg_aes_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(ctx: *const [u64; 129], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); + type Impl = unsafe extern "sysv64" fn(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_VPCLMUL_AVX512_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_VPCLMUL_AVX512_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_encrypt_prepared_vaes_vpclmul_avx512; - } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_VPCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_VPCLMUL_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_encrypt_prepared_vaes_vpclmul; - } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_PCLMUL_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_encrypt_prepared_vaes_pclmul; - } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_VPCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_VPCLMUL_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_encrypt_prepared_aesni_vpclmul; - } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_PCLMUL_AVX_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_PCLMUL_AVX_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_encrypt_prepared_aesni_pclmul_avx; - } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_PCLMUL_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_encrypt_prepared_aesni_pclmul; - } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VAES_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_encrypt_prepared_vaes; - } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_AESNI_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_encrypt_prepared_aesni; - } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VPCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_VPCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_INIT_VPCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_INIT_VPCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_encrypt_prepared_vpclmul; + return crate::gcm::vg_aes_gcm_stream_init_vpclmul; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_ENCRYPT_PREPARED_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_INIT_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_INIT_PCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_encrypt_prepared_pclmul; + return crate::gcm::vg_aes_gcm_stream_init_pclmul; } - crate::gcm::vg_aes_gcm_stream_encrypt_prepared + crate::gcm::vg_aes_gcm_stream_init } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(ctx: *const [u64; 129], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { + unsafe extern "sysv64" fn first(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, rounds, state, aad_len, text_len, data, len) } + unsafe { f(ctx, nonce, nonce_len, state) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -915,93 +857,151 @@ pub unsafe fn vg_aes_gcm_stream_encrypt_prepared_auto(_dit: verified_garbage_cpu // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, state, aad_len, text_len, data, len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, nonce, nonce_len, state) } } -/// `vg_aes_gcm_stream_decrypt` with a 1032-byte context from `vg_aes_gcm_prepare`. Computes the same result, using the AES key schedule and hash subkey in its first 256 bytes, and may read the prepared powers its count says are present. The powers remain secret; how many are present may affect timing; all other memory, aliasing and constant-time requirements are unchanged. Contract: `VG.Spec.Gcm.streamDecryptPreparedContract`. +/// Absorbs additional data into an incremental AES-GCM encryption or decryption: with the key context `*ctx`, if the streaming state `*state` represents a message with `aad_len` bytes (modulo 2⁶⁴) of additional data and no text yet, it then represents that message with the `len` bytes at `data` appended to its additional data. All of the additional data comes before the text (§7.1 step 5): after text, the state represents no message. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_aes_gcm_stream_decrypt_prepared_vaes_vpclmul_avx512` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_VPCLMUL_AVX512_FEATURES`); `vg_aes_gcm_stream_decrypt_prepared_vaes_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_decrypt_prepared_vaes_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_PCLMUL_FEATURES`); `vg_aes_gcm_stream_decrypt_prepared_aesni_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_decrypt_prepared_aesni_pclmul_avx` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_PCLMUL_AVX_FEATURES`); `vg_aes_gcm_stream_decrypt_prepared_aesni_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_PCLMUL_FEATURES`); `vg_aes_gcm_stream_decrypt_prepared_vaes` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_FEATURES`); `vg_aes_gcm_stream_decrypt_prepared_aesni` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_FEATURES`); `vg_aes_gcm_stream_decrypt_prepared_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PREPARED_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_decrypt_prepared_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_DECRYPT_PREPARED_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_decrypt_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// Contract: `VG.Spec.Gcm.streamAadContract`. Constant time: only the pointers, `aad_len` and `len` may affect timing, not the key context, the state or the data. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_stream_aad_vpclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_AAD_VPCLMUL_FEATURES`); `vg_aes_gcm_stream_aad_pclmul` if the CPU supports its features (`VG_AES_GCM_STREAM_AAD_PCLMUL_FEATURES`); or else `vg_aes_gcm_stream_aad`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `ctx` must be valid for reads of 1032 bytes. +/// * `ctx` must be valid for reads of 256 bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `data` must be valid for reads and writes of `len` bytes. -/// * `rounds` must be 10, 12 or 14. -/// * `*ctx` must have the prepared format written by `vg_aes_gcm_prepare` (not the format of `vg_aes_gcm_init_precomputed`). -/// * `state` and `data` must not overlap each other, `ctx` or the arguments on the stack (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the return address on the stack or the 2608 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `state` must not overlap `ctx` or `data` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the return address on the stack or the 2576 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_aes_gcm_stream_decrypt_prepared_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 129], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { +pub unsafe fn vg_aes_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(ctx: *const [u64; 129], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); + type Impl = unsafe extern "sysv64" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_VPCLMUL_AVX512_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_VPCLMUL_AVX512_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_decrypt_prepared_vaes_vpclmul_avx512; - } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_VPCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_VPCLMUL_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_decrypt_prepared_vaes_vpclmul; - } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_PCLMUL_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_decrypt_prepared_vaes_pclmul; - } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_VPCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_VPCLMUL_FEATURES) - { - return crate::gcm::vg_aes_gcm_stream_decrypt_prepared_aesni_vpclmul; - } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_PCLMUL_AVX_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_PCLMUL_AVX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_AAD_VPCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_AAD_VPCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_decrypt_prepared_aesni_pclmul_avx; + return crate::gcm::vg_aes_gcm_stream_aad_vpclmul; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_AAD_PCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_AAD_PCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_decrypt_prepared_aesni_pclmul; + return crate::gcm::vg_aes_gcm_stream_aad_pclmul; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_VAES_FEATURES) + crate::gcm::vg_aes_gcm_stream_aad + } + /// The first call: chooses the implementation, records it in `CHOSEN` for the calls + /// after it, and runs it. + #[cold] + unsafe extern "sysv64" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { + let f = select(); + CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); + // SAFETY: the caller's obligations, which are those of every implementation, `f` + // among them, and the CPU has the features `f` needs. + unsafe { f(ctx, state, aad_len, data, len) } + } + /// The function to call: `first`, until it records the implementation it chose. + static CHOSEN: core::sync::atomic::AtomicPtr<()> = + core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); + // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, + // or an implementation `select` returned, which needs only features the CPU has. Calling + // either carries the caller's obligations, which are those of every implementation. + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, data, len) } +} + +/// Makes the 1032-byte `*ctx`, whose first 256 bytes are a key context of `vg_aes_gcm_init`, the prepared key context of the same key, in place: keeps its first 256 bytes, at byte `256 + 32*k`, for `k` from 0 to `n/2 - 1`, stores `preparedPower(H^(2*k+2))` and then `preparedPower(H^(2*k+1))`, as two little-endian 128-bit words, `H` being the hash subkey in its bytes 240–255, and stores `n` in the 64-bit word at byte 1024, `n` being 48, 16 or 0, as the implementation's other functions read. Use only the `*_prepared` APIs, or APIs that read the first 256 bytes, with this format. Contract: `VG.Spec.Gcm.prepareContract`: the first 256 bytes are kept and the context satisfies `PreparedRepr`. Constant time: only the pointer may affect timing, not the hash subkey. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_prepare_vpclmul_avx512` if the CPU supports its features (`VG_AES_GCM_PREPARE_VPCLMUL_AVX512_FEATURES`); `vg_aes_gcm_prepare_vpclmul` if the CPU supports its features (`VG_AES_GCM_PREPARE_VPCLMUL_FEATURES`); `vg_aes_gcm_prepare_pclmul_avx` if the CPU supports its features (`VG_AES_GCM_PREPARE_PCLMUL_AVX_FEATURES`); or else `vg_aes_gcm_prepare`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// +/// # Safety +/// +/// * `ctx` must be valid for reads and writes of 1032 bytes. +/// * `ctx` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_aes_gcm_prepare_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *mut [u64; 129]) { + /// The type of the implementations. + type Impl = unsafe extern "sysv64" fn(ctx: *mut [u64; 129]); + /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only + /// features the CPU has. + #[cold] + fn select() -> Impl { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_PREPARE_VPCLMUL_AVX512_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_PREPARE_VPCLMUL_AVX512_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_decrypt_prepared_vaes; + return crate::gcm::vg_aes_gcm_prepare_vpclmul_avx512; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_AESNI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_PREPARE_VPCLMUL_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_PREPARE_VPCLMUL_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_decrypt_prepared_aesni; + return crate::gcm::vg_aes_gcm_prepare_vpclmul; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_VPCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_VPCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_PREPARE_PCLMUL_AVX_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_PREPARE_PCLMUL_AVX_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_decrypt_prepared_vpclmul; + return crate::gcm::vg_aes_gcm_prepare_pclmul_avx; } - if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_PCLMUL_FEATURES) } - || cpu.contains(crate::gcm::VG_AES_GCM_STREAM_DECRYPT_PREPARED_PCLMUL_FEATURES) + crate::gcm::vg_aes_gcm_prepare + } + /// The first call: chooses the implementation, records it in `CHOSEN` for the calls + /// after it, and runs it. + #[cold] + unsafe extern "sysv64" fn first(ctx: *mut [u64; 129]) { + let f = select(); + CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); + // SAFETY: the caller's obligations, which are those of every implementation, `f` + // among them, and the CPU has the features `f` needs. + unsafe { f(ctx) } + } + /// The function to call: `first`, until it records the implementation it chose. + static CHOSEN: core::sync::atomic::AtomicPtr<()> = + core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); + // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, + // or an implementation `select` returned, which needs only features the CPU has. Calling + // either carries the caller's obligations, which are those of every implementation. + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx) } +} + +/// Wipes the 1032-byte `*ctx`, a prepared key context of `vg_aes_gcm_prepare`: sets to zero the key context and the powers its count (the 64-bit word at byte 1024) says are present: its first 1024 bytes if the count is at least 48, 512 if it is at least 16, and 256 otherwise (the bytes after them were not written). Contract: `VG.Spec.Gcm.wipePreparedContract`. Constant time: only the pointer and which of those the count is may affect timing, not the bytes wiped. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_aes_gcm_wipe_prepared_avx` if the CPU supports its features (`VG_AES_GCM_WIPE_PREPARED_AVX_FEATURES`); or else `vg_aes_gcm_wipe_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// +/// # Safety +/// +/// * `ctx` must be valid for reads and writes of 1032 bytes. +/// * `ctx` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_aes_gcm_wipe_prepared_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *mut [u64; 129]) { + /// The type of the implementations. + type Impl = unsafe extern "sysv64" fn(ctx: *mut [u64; 129]); + /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only + /// features the CPU has. + #[cold] + fn select() -> Impl { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::gcm::VG_AES_GCM_WIPE_PREPARED_AVX_FEATURES) } + || cpu.contains(crate::gcm::VG_AES_GCM_WIPE_PREPARED_AVX_FEATURES) { - return crate::gcm::vg_aes_gcm_stream_decrypt_prepared_pclmul; + return crate::gcm::vg_aes_gcm_wipe_prepared_avx; } - crate::gcm::vg_aes_gcm_stream_decrypt_prepared + crate::gcm::vg_aes_gcm_wipe_prepared } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(ctx: *const [u64; 129], rounds: usize, state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { + unsafe extern "sysv64" fn first(ctx: *mut [u64; 129]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, rounds, state, aad_len, text_len, data, len) } + unsafe { f(ctx) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -1009,5 +1009,5 @@ pub unsafe fn vg_aes_gcm_stream_decrypt_prepared_auto(_dit: verified_garbage_cpu // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, rounds, state, aad_len, text_len, data, len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx) } } diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha1.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha1.rs index 13562c10dfb..cb4a297ce36 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha1.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha1.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.initAnyKeyContract` of `VG.Spec.Hmac.sha1I`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha1_init_shani` if the CPU supports its features (`VG_HMAC_SHA1_INIT_SHANI_FEATURES`); `vg_hmac_sha1_init_avx_bmi` if the CPU supports its features (`VG_HMAC_SHA1_INIT_AVX_BMI_FEATURES`); or else `vg_hmac_sha1_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha1_init_shani` if the CPU supports its features (`VG_HMAC_SHA1_INIT_SHANI_FEATURES`); `vg_hmac_sha1_init_avx_bmi` if the CPU supports its features (`VG_HMAC_SHA1_INIT_AVX_BMI_FEATURES`); or else `vg_hmac_sha1_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -60,7 +60,7 @@ pub unsafe fn vg_hmac_sha1_init_auto(_dit: verified_garbage_cpu::Dit<'_>, inner: /// /// Contract: `VG.Spec.Hmac.Instance.finalizeContract` of `VG.Spec.Hmac.sha1I`. Constant time: only the pointers and `count` may affect timing, not the states. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha1_finalize_shani` if the CPU supports its features (`VG_HMAC_SHA1_FINALIZE_SHANI_FEATURES`); `vg_hmac_sha1_finalize_avx_bmi` if the CPU supports its features (`VG_HMAC_SHA1_FINALIZE_AVX_BMI_FEATURES`); or else `vg_hmac_sha1_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha1_finalize_shani` if the CPU supports its features (`VG_HMAC_SHA1_FINALIZE_SHANI_FEATURES`); `vg_hmac_sha1_finalize_avx_bmi` if the CPU supports its features (`VG_HMAC_SHA1_FINALIZE_AVX_BMI_FEATURES`); or else `vg_hmac_sha1_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha224.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha224.rs index e5bcacd1025..bb0ff7cef57 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha224.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha224.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.initAnyKeyContract` of `VG.Spec.Hmac.sha224I`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha224_init_shani` if the CPU supports its features (`VG_HMAC_SHA224_INIT_SHANI_FEATURES`); `vg_hmac_sha224_init_avx2` if the CPU supports its features (`VG_HMAC_SHA224_INIT_AVX2_FEATURES`); or else `vg_hmac_sha224_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha224_init_shani` if the CPU supports its features (`VG_HMAC_SHA224_INIT_SHANI_FEATURES`); `vg_hmac_sha224_init_avx2` if the CPU supports its features (`VG_HMAC_SHA224_INIT_AVX2_FEATURES`); or else `vg_hmac_sha224_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -60,7 +60,7 @@ pub unsafe fn vg_hmac_sha224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, inne /// /// Contract: `VG.Spec.Hmac.Instance.finalizeContract` of `VG.Spec.Hmac.sha224I`. Constant time: only the pointers and `count` may affect timing, not the states. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha224_finalize_shani` if the CPU supports its features (`VG_HMAC_SHA224_FINALIZE_SHANI_FEATURES`); `vg_hmac_sha224_finalize_avx2` if the CPU supports its features (`VG_HMAC_SHA224_FINALIZE_AVX2_FEATURES`); or else `vg_hmac_sha224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha224_finalize_shani` if the CPU supports its features (`VG_HMAC_SHA224_FINALIZE_SHANI_FEATURES`); `vg_hmac_sha224_finalize_avx2` if the CPU supports its features (`VG_HMAC_SHA224_FINALIZE_AVX2_FEATURES`); or else `vg_hmac_sha224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha256.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha256.rs index bdf9ace2790..fb2e47f52bc 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha256.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha256.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.initAnyKeyContract` of `VG.Spec.Hmac.sha256I`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha256_init_shani` if the CPU supports its features (`VG_HMAC_SHA256_INIT_SHANI_FEATURES`); `vg_hmac_sha256_init_avx2` if the CPU supports its features (`VG_HMAC_SHA256_INIT_AVX2_FEATURES`); or else `vg_hmac_sha256_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha256_init_shani` if the CPU supports its features (`VG_HMAC_SHA256_INIT_SHANI_FEATURES`); `vg_hmac_sha256_init_avx2` if the CPU supports its features (`VG_HMAC_SHA256_INIT_AVX2_FEATURES`); or else `vg_hmac_sha256_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -60,7 +60,7 @@ pub unsafe fn vg_hmac_sha256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, inne /// /// Contract: `VG.Spec.Hmac.Instance.finalizeContract` of `VG.Spec.Hmac.sha256I`. Constant time: only the pointers and `count` may affect timing, not the states. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha256_finalize_shani` if the CPU supports its features (`VG_HMAC_SHA256_FINALIZE_SHANI_FEATURES`); `vg_hmac_sha256_finalize_avx2` if the CPU supports its features (`VG_HMAC_SHA256_FINALIZE_AVX2_FEATURES`); or else `vg_hmac_sha256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha256_finalize_shani` if the CPU supports its features (`VG_HMAC_SHA256_FINALIZE_SHANI_FEATURES`); `vg_hmac_sha256_finalize_avx2` if the CPU supports its features (`VG_HMAC_SHA256_FINALIZE_AVX2_FEATURES`); or else `vg_hmac_sha256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha384.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha384.rs index 653638bd01a..e2629478945 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha384.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha384.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.initAnyKeyContract` of `VG.Spec.Hmac.sha384I`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha384_init_shani` if the CPU supports its features (`VG_HMAC_SHA384_INIT_SHANI_FEATURES`); `vg_hmac_sha384_init_avx2` if the CPU supports its features (`VG_HMAC_SHA384_INIT_AVX2_FEATURES`); or else `vg_hmac_sha384_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha384_init_shani` if the CPU supports its features (`VG_HMAC_SHA384_INIT_SHANI_FEATURES`); `vg_hmac_sha384_init_avx2` if the CPU supports its features (`VG_HMAC_SHA384_INIT_AVX2_FEATURES`); or else `vg_hmac_sha384_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -60,7 +60,7 @@ pub unsafe fn vg_hmac_sha384_init_auto(_dit: verified_garbage_cpu::Dit<'_>, inne /// /// Contract: `VG.Spec.Hmac.Instance.finalizeContract` of `VG.Spec.Hmac.sha384I`. Constant time: only the pointers and `count` may affect timing, not the states. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha384_finalize_shani` if the CPU supports its features (`VG_HMAC_SHA384_FINALIZE_SHANI_FEATURES`); `vg_hmac_sha384_finalize_avx2` if the CPU supports its features (`VG_HMAC_SHA384_FINALIZE_AVX2_FEATURES`); or else `vg_hmac_sha384_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha384_finalize_shani` if the CPU supports its features (`VG_HMAC_SHA384_FINALIZE_SHANI_FEATURES`); `vg_hmac_sha384_finalize_avx2` if the CPU supports its features (`VG_HMAC_SHA384_FINALIZE_AVX2_FEATURES`); or else `vg_hmac_sha384_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha512.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha512.rs index 112c8319c99..0600ab7ab0a 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha512.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha512.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.initAnyKeyContract` of `VG.Spec.Hmac.sha512I`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha512_init_shani` if the CPU supports its features (`VG_HMAC_SHA512_INIT_SHANI_FEATURES`); `vg_hmac_sha512_init_avx2` if the CPU supports its features (`VG_HMAC_SHA512_INIT_AVX2_FEATURES`); or else `vg_hmac_sha512_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha512_init_shani` if the CPU supports its features (`VG_HMAC_SHA512_INIT_SHANI_FEATURES`); `vg_hmac_sha512_init_avx2` if the CPU supports its features (`VG_HMAC_SHA512_INIT_AVX2_FEATURES`); or else `vg_hmac_sha512_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -60,7 +60,7 @@ pub unsafe fn vg_hmac_sha512_init_auto(_dit: verified_garbage_cpu::Dit<'_>, inne /// /// Contract: `VG.Spec.Hmac.Instance.finalizeContract` of `VG.Spec.Hmac.sha512I`. Constant time: only the pointers and `count` may affect timing, not the states. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha512_finalize_shani` if the CPU supports its features (`VG_HMAC_SHA512_FINALIZE_SHANI_FEATURES`); `vg_hmac_sha512_finalize_avx2` if the CPU supports its features (`VG_HMAC_SHA512_FINALIZE_AVX2_FEATURES`); or else `vg_hmac_sha512_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha512_finalize_shani` if the CPU supports its features (`VG_HMAC_SHA512_FINALIZE_SHANI_FEATURES`); `vg_hmac_sha512_finalize_avx2` if the CPU supports its features (`VG_HMAC_SHA512_FINALIZE_AVX2_FEATURES`); or else `vg_hmac_sha512_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha512_224.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha512_224.rs index a2371e294dd..ecd3abd35fd 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha512_224.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha512_224.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.initAnyKeyContract` of `VG.Spec.Hmac.sha512_224I`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha512_224_init_shani` if the CPU supports its features (`VG_HMAC_SHA512_224_INIT_SHANI_FEATURES`); `vg_hmac_sha512_224_init_avx2` if the CPU supports its features (`VG_HMAC_SHA512_224_INIT_AVX2_FEATURES`); or else `vg_hmac_sha512_224_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha512_224_init_shani` if the CPU supports its features (`VG_HMAC_SHA512_224_INIT_SHANI_FEATURES`); `vg_hmac_sha512_224_init_avx2` if the CPU supports its features (`VG_HMAC_SHA512_224_INIT_AVX2_FEATURES`); or else `vg_hmac_sha512_224_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -60,7 +60,7 @@ pub unsafe fn vg_hmac_sha512_224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, /// /// Contract: `VG.Spec.Hmac.Instance.finalizeContract` of `VG.Spec.Hmac.sha512_224I`. Constant time: only the pointers and `count` may affect timing, not the states. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha512_224_finalize_shani` if the CPU supports its features (`VG_HMAC_SHA512_224_FINALIZE_SHANI_FEATURES`); `vg_hmac_sha512_224_finalize_avx2` if the CPU supports its features (`VG_HMAC_SHA512_224_FINALIZE_AVX2_FEATURES`); or else `vg_hmac_sha512_224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha512_224_finalize_shani` if the CPU supports its features (`VG_HMAC_SHA512_224_FINALIZE_SHANI_FEATURES`); `vg_hmac_sha512_224_finalize_avx2` if the CPU supports its features (`VG_HMAC_SHA512_224_FINALIZE_AVX2_FEATURES`); or else `vg_hmac_sha512_224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha512_256.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha512_256.rs index c8d95f19fd3..16ab3100b57 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha512_256.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/hmac_sha512_256.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.initAnyKeyContract` of `VG.Spec.Hmac.sha512_256I`. Constant time: only the pointers and `key_len` may affect timing, not the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha512_256_init_shani` if the CPU supports its features (`VG_HMAC_SHA512_256_INIT_SHANI_FEATURES`); `vg_hmac_sha512_256_init_avx2` if the CPU supports its features (`VG_HMAC_SHA512_256_INIT_AVX2_FEATURES`); or else `vg_hmac_sha512_256_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha512_256_init_shani` if the CPU supports its features (`VG_HMAC_SHA512_256_INIT_SHANI_FEATURES`); `vg_hmac_sha512_256_init_avx2` if the CPU supports its features (`VG_HMAC_SHA512_256_INIT_AVX2_FEATURES`); or else `vg_hmac_sha512_256_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -60,7 +60,7 @@ pub unsafe fn vg_hmac_sha512_256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, /// /// Contract: `VG.Spec.Hmac.Instance.finalizeContract` of `VG.Spec.Hmac.sha512_256I`. Constant time: only the pointers and `count` may affect timing, not the states. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_hmac_sha512_256_finalize_shani` if the CPU supports its features (`VG_HMAC_SHA512_256_FINALIZE_SHANI_FEATURES`); `vg_hmac_sha512_256_finalize_avx2` if the CPU supports its features (`VG_HMAC_SHA512_256_FINALIZE_AVX2_FEATURES`); or else `vg_hmac_sha512_256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_hmac_sha512_256_finalize_shani` if the CPU supports its features (`VG_HMAC_SHA512_256_FINALIZE_SHANI_FEATURES`); `vg_hmac_sha512_256_finalize_avx2` if the CPU supports its features (`VG_HMAC_SHA512_256_FINALIZE_AVX2_FEATURES`); or else `vg_hmac_sha512_256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/idea.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/idea.rs index 22ed55f3a60..32f714e92a3 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/idea.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/idea.rs @@ -2,25 +2,6 @@ //! The dispatchers of the verified `idea` functions for `x86_64` //! (`crate::idea`, `VG.Rust.dispatcher`). -/// IDEA in ECB mode (Lai, 1992, §3.3; SP 800-38A §6.1) on `n` complete 8-byte blocks at `data`, in place, under the 52 subkeys at `schedule`: encryption under the subkeys `vg_idea_expand_key` writes, decryption under those `vg_idea_invert_key` writes. No padding is added or removed. For `n = 0`, no data is transformed. -/// -/// Contract: `VG.Spec.Idea.ecbContract`. Constant time: only pointers and `n` may affect timing, not the subkeys or data, including words or subkeys that are 0. -/// -/// It runs `vg_idea_ecb`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `schedule` must be valid for reads of 104 bytes. -/// * `data` must be valid for reads and writes of `8 * n` bytes. -/// * `data` must not overlap `schedule` (distinct Rust objects never do). -/// * Neither `schedule` nor `data` may overlap the return address on the stack or the 24 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_idea_ecb_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *const [u8; 104], data: *mut [u8; 8], n: usize) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::idea::vg_idea_ecb(schedule, data, n) } -} - /// IDEA encryption key schedule (Lai, 1992, §3.3): writes the 52 encryption subkeys of the 16-byte key at `key` (as eight big-endian 16-bit words, rotated left by 25 bits after every eight subkeys) to `*schedule`, as little-endian 16-bit words. /// /// Contract: `VG.Spec.Idea.expandKeyContract`. Constant time: only pointers may affect timing, not the key. @@ -58,3 +39,22 @@ pub unsafe fn vg_idea_invert_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sched // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::idea::vg_idea_invert_key(schedule, inverse) } } + +/// IDEA in ECB mode (Lai, 1992, §3.3; SP 800-38A §6.1) on `n` complete 8-byte blocks at `data`, in place, under the 52 subkeys at `schedule`: encryption under the subkeys `vg_idea_expand_key` writes, decryption under those `vg_idea_invert_key` writes. No padding is added or removed. For `n = 0`, no data is transformed. +/// +/// Contract: `VG.Spec.Idea.ecbContract`. Constant time: only pointers and `n` may affect timing, not the subkeys or data, including words or subkeys that are 0. +/// +/// It runs `vg_idea_ecb`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `schedule` must be valid for reads of 104 bytes. +/// * `data` must be valid for reads and writes of `8 * n` bytes. +/// * `data` must not overlap `schedule` (distinct Rust objects never do). +/// * Neither `schedule` nor `data` may overlap the return address on the stack or the 24 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_idea_ecb_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *const [u8; 104], data: *mut [u8; 8], n: usize) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::idea::vg_idea_ecb(schedule, data, n) } +} diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/mldsa44.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/mldsa44.rs index f65bc4da0b4..946cc78d749 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/mldsa44.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/mldsa44.rs @@ -2,55 +2,53 @@ //! The dispatchers of the verified `mldsa44` functions for `x86_64` //! (`crate::mldsa44`, `VG.Rust.dispatcher`). -/// ML-DSA-44 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-44 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. +/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa44_sign_avx512` if the CPU supports its features (`VG_MLDSA44_SIGN_AVX512_FEATURES`); `vg_mldsa44_sign_avx2` if the CPU supports its features (`VG_MLDSA44_SIGN_AVX2_FEATURES`); or else `vg_mldsa44_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa44_keygen_avx512` if the CPU supports its features (`VG_MLDSA44_KEYGEN_AVX512_FEATURES`); `vg_mldsa44_keygen_avx2` if the CPU supports its features (`VG_MLDSA44_KEYGEN_AVX2_FEATURES`); or else `vg_mldsa44_keygen`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `sk` must be valid for reads of 2560 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `rnd` must be valid for reads of 32 bytes. -/// * `sig` must be valid for reads and writes of 2420 bytes. +/// * `seed` must be valid for reads of 32 bytes. +/// * `pk` must be valid for reads and writes of 1312 bytes. +/// * `sk` must be valid for reads and writes of 2560 bytes. /// * `scratch` must be valid for reads and writes of 77824 bytes. -/// * `sk` must have been written by `vg_mldsa44_keygen`. -/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. +/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the return address on the stack or the 40 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the return address on the stack or the 32 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { +pub unsafe fn vg_mldsa44_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1312], sk: *mut [u8; 2560], scratch: *mut [u64; 9728]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 2560], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9728]) -> u32; + type Impl = unsafe extern "sysv64" fn(seed: *const [u8; 32], pk: *mut [u8; 1312], sk: *mut [u8; 2560], scratch: *mut [u64; 9728]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_SIGN_AVX512_FEATURES) } - || cpu.contains(crate::mldsa44::VG_MLDSA44_SIGN_AVX512_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_KEYGEN_AVX512_FEATURES) } + || cpu.contains(crate::mldsa44::VG_MLDSA44_KEYGEN_AVX512_FEATURES) { - return crate::mldsa44::vg_mldsa44_sign_avx512; + return crate::mldsa44::vg_mldsa44_keygen_avx512; } - if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_SIGN_AVX2_FEATURES) } - || cpu.contains(crate::mldsa44::VG_MLDSA44_SIGN_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_KEYGEN_AVX2_FEATURES) } + || cpu.contains(crate::mldsa44::VG_MLDSA44_KEYGEN_AVX2_FEATURES) { - return crate::mldsa44::vg_mldsa44_sign_avx2; + return crate::mldsa44::vg_mldsa44_keygen_avx2; } - crate::mldsa44::vg_mldsa44_sign + crate::mldsa44::vg_mldsa44_keygen } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(sk: *const [u8; 2560], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { + unsafe extern "sysv64" fn first(seed: *const [u8; 32], pk: *mut [u8; 1312], sk: *mut [u8; 2560], scratch: *mut [u64; 9728]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(sk, mu, rnd, sig, scratch) } + unsafe { f(seed, pk, sk, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -58,7 +56,7 @@ pub unsafe fn vg_mldsa44_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *con // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, mu, rnd, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(seed, pk, sk, scratch) } } /// ML-DSA-44 public-key hash: writes `tr = H(pk, 64)` (FIPS 204 Algorithm 8, line 6), SHAKE256 of the public key `*pk` to 64 bytes, to `*tr`, the digest `vg_mldsa44_verify_message_cached` takes. @@ -82,53 +80,46 @@ pub unsafe fn vg_mldsa44_pk_tr_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *co unsafe { crate::mldsa44::vg_mldsa44_pk_tr(pk, tr, scratch) } } -/// ML-DSA-44 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-44 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa44_verify_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. +/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa44_keygen_avx512` if the CPU supports its features (`VG_MLDSA44_KEYGEN_AVX512_FEATURES`); `vg_mldsa44_keygen_avx2` if the CPU supports its features (`VG_MLDSA44_KEYGEN_AVX2_FEATURES`); or else `vg_mldsa44_keygen`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa44_prepare_verifying_key_avx2` if the CPU supports its features (`VG_MLDSA44_PREPARE_VERIFYING_KEY_AVX2_FEATURES`); or else `vg_mldsa44_prepare_verifying_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `seed` must be valid for reads of 32 bytes. -/// * `pk` must be valid for reads and writes of 1312 bytes. -/// * `sk` must be valid for reads and writes of 2560 bytes. -/// * `scratch` must be valid for reads and writes of 77824 bytes. -/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. +/// * `pk` must be valid for reads of 1312 bytes. +/// * `prepared` must be valid for reads and writes of 5408 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the return address on the stack or the 32 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). +/// * None of `pk`, `prepared` and `scratch` may overlap the return address on the stack or the 32 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1312], sk: *mut [u8; 2560], scratch: *mut [u64; 9728]) -> u32 { +pub unsafe fn vg_mldsa44_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], prepared: *mut [u8; 5408], scratch: *mut [u64; 128]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(seed: *const [u8; 32], pk: *mut [u8; 1312], sk: *mut [u8; 2560], scratch: *mut [u64; 9728]) -> u32; + type Impl = unsafe extern "sysv64" fn(pk: *const [u8; 1312], prepared: *mut [u8; 5408], scratch: *mut [u64; 128]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_KEYGEN_AVX512_FEATURES) } - || cpu.contains(crate::mldsa44::VG_MLDSA44_KEYGEN_AVX512_FEATURES) - { - return crate::mldsa44::vg_mldsa44_keygen_avx512; - } - if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_KEYGEN_AVX2_FEATURES) } - || cpu.contains(crate::mldsa44::VG_MLDSA44_KEYGEN_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_PREPARE_VERIFYING_KEY_AVX2_FEATURES) } + || cpu.contains(crate::mldsa44::VG_MLDSA44_PREPARE_VERIFYING_KEY_AVX2_FEATURES) { - return crate::mldsa44::vg_mldsa44_keygen_avx2; + return crate::mldsa44::vg_mldsa44_prepare_verifying_key_avx2; } - crate::mldsa44::vg_mldsa44_keygen + crate::mldsa44::vg_mldsa44_prepare_verifying_key } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(seed: *const [u8; 32], pk: *mut [u8; 1312], sk: *mut [u8; 2560], scratch: *mut [u64; 9728]) -> u32 { + unsafe extern "sysv64" fn first(pk: *const [u8; 1312], prepared: *mut [u8; 5408], scratch: *mut [u64; 128]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(seed, pk, sk, scratch) } + unsafe { f(pk, prepared, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -136,55 +127,50 @@ pub unsafe fn vg_mldsa44_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(seed, pk, sk, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, prepared, scratch) } } -/// ML-DSA-44 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// ML-DSA-44 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa44_sign_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa44_verify_avx512` if the CPU supports its features (`VG_MLDSA44_VERIFY_AVX512_FEATURES`); `vg_mldsa44_verify_avx2` if the CPU supports its features (`VG_MLDSA44_VERIFY_AVX2_FEATURES`); or else `vg_mldsa44_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa44_prepare_signing_key_avx2` if the CPU supports its features (`VG_MLDSA44_PREPARE_SIGNING_KEY_AVX2_FEATURES`); or else `vg_mldsa44_prepare_signing_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `pk` must be valid for reads of 1312 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `sig` must be valid for reads of 2420 bytes. -/// * `scratch` must be valid for reads and writes of 77824 bytes. +/// * `sk` must be valid for reads of 2560 bytes. +/// * `prepared` must be valid for reads and writes of 14848 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). -/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the return address on the stack or the 40 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). +/// * None of `sk`, `prepared` and `scratch` may overlap the return address on the stack or the 32 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], mu: *const [u8; 64], sig: *const [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { +pub unsafe fn vg_mldsa44_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], prepared: *mut [u8; 14848], scratch: *mut [u64; 128]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(pk: *const [u8; 1312], mu: *const [u8; 64], sig: *const [u8; 2420], scratch: *mut [u64; 9728]) -> u32; + type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 2560], prepared: *mut [u8; 14848], scratch: *mut [u64; 128]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_VERIFY_AVX512_FEATURES) } - || cpu.contains(crate::mldsa44::VG_MLDSA44_VERIFY_AVX512_FEATURES) - { - return crate::mldsa44::vg_mldsa44_verify_avx512; - } - if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_VERIFY_AVX2_FEATURES) } - || cpu.contains(crate::mldsa44::VG_MLDSA44_VERIFY_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_PREPARE_SIGNING_KEY_AVX2_FEATURES) } + || cpu.contains(crate::mldsa44::VG_MLDSA44_PREPARE_SIGNING_KEY_AVX2_FEATURES) { - return crate::mldsa44::vg_mldsa44_verify_avx2; + return crate::mldsa44::vg_mldsa44_prepare_signing_key_avx2; } - crate::mldsa44::vg_mldsa44_verify + crate::mldsa44::vg_mldsa44_prepare_signing_key } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(pk: *const [u8; 1312], mu: *const [u8; 64], sig: *const [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { + unsafe extern "sysv64" fn first(sk: *const [u8; 2560], prepared: *mut [u8; 14848], scratch: *mut [u64; 128]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(pk, mu, sig, scratch) } + unsafe { f(sk, prepared, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -192,59 +178,58 @@ pub unsafe fn vg_mldsa44_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *c // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, mu, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, prepared, scratch) } } -/// ML-DSA-44 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. +/// ML-DSA-44 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. +/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa44_sign_message_avx512` if the CPU supports its features (`VG_MLDSA44_SIGN_MESSAGE_AVX512_FEATURES`); `vg_mldsa44_sign_message_avx2` if the CPU supports its features (`VG_MLDSA44_SIGN_MESSAGE_AVX2_FEATURES`); or else `vg_mldsa44_sign_message`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa44_sign_avx512` if the CPU supports its features (`VG_MLDSA44_SIGN_AVX512_FEATURES`); `vg_mldsa44_sign_avx2` if the CPU supports its features (`VG_MLDSA44_SIGN_AVX2_FEATURES`); or else `vg_mldsa44_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `sk` must be valid for reads of 2560 bytes. -/// * `msg` must be valid for reads of `msg_len` bytes. -/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `mu` must be valid for reads of 64 bytes. /// * `rnd` must be valid for reads of 32 bytes. /// * `sig` must be valid for reads and writes of 2420 bytes. -/// * `scratch` must be valid for reads and writes of 78848 bytes. +/// * `scratch` must be valid for reads and writes of 77824 bytes. /// * `sk` must have been written by `vg_mldsa44_keygen`. /// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx`, `rnd` or the arguments on the stack (distinct Rust objects never do). -/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the return address on the stack or the 112 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the return address on the stack or the 40 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9856]) -> u32 { +pub unsafe fn vg_mldsa44_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 2560], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9856]) -> u32; + type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 2560], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9728]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_SIGN_MESSAGE_AVX512_FEATURES) } - || cpu.contains(crate::mldsa44::VG_MLDSA44_SIGN_MESSAGE_AVX512_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_SIGN_AVX512_FEATURES) } + || cpu.contains(crate::mldsa44::VG_MLDSA44_SIGN_AVX512_FEATURES) { - return crate::mldsa44::vg_mldsa44_sign_message_avx512; + return crate::mldsa44::vg_mldsa44_sign_avx512; } - if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_SIGN_MESSAGE_AVX2_FEATURES) } - || cpu.contains(crate::mldsa44::VG_MLDSA44_SIGN_MESSAGE_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_SIGN_AVX2_FEATURES) } + || cpu.contains(crate::mldsa44::VG_MLDSA44_SIGN_AVX2_FEATURES) { - return crate::mldsa44::vg_mldsa44_sign_message_avx2; + return crate::mldsa44::vg_mldsa44_sign_avx2; } - crate::mldsa44::vg_mldsa44_sign_message + crate::mldsa44::vg_mldsa44_sign } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(sk: *const [u8; 2560], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9856]) -> u32 { + unsafe extern "sysv64" fn first(sk: *const [u8; 2560], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { f(sk, mu, rnd, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -252,50 +237,59 @@ pub unsafe fn vg_mldsa44_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, mu, rnd, sig, scratch) } } -/// ML-DSA-44 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa44_sign_message_prepared`. +/// ML-DSA-44 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. /// -/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. +/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa44_prepare_signing_key_avx2` if the CPU supports its features (`VG_MLDSA44_PREPARE_SIGNING_KEY_AVX2_FEATURES`); or else `vg_mldsa44_prepare_signing_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa44_sign_message_avx512` if the CPU supports its features (`VG_MLDSA44_SIGN_MESSAGE_AVX512_FEATURES`); `vg_mldsa44_sign_message_avx2` if the CPU supports its features (`VG_MLDSA44_SIGN_MESSAGE_AVX2_FEATURES`); or else `vg_mldsa44_sign_message`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `sk` must be valid for reads of 2560 bytes. -/// * `prepared` must be valid for reads and writes of 14848 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. -/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). +/// * `msg` must be valid for reads of `msg_len` bytes. +/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `rnd` must be valid for reads of 32 bytes. +/// * `sig` must be valid for reads and writes of 2420 bytes. +/// * `scratch` must be valid for reads and writes of 78848 bytes. +/// * `sk` must have been written by `vg_mldsa44_keygen`. +/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). -/// * None of `sk`, `prepared` and `scratch` may overlap the return address on the stack or the 32 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx`, `rnd` or the arguments on the stack (distinct Rust objects never do). +/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the return address on the stack or the 112 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], prepared: *mut [u8; 14848], scratch: *mut [u64; 128]) { +pub unsafe fn vg_mldsa44_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 2560], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9856]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 2560], prepared: *mut [u8; 14848], scratch: *mut [u64; 128]); + type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 2560], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9856]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_PREPARE_SIGNING_KEY_AVX2_FEATURES) } - || cpu.contains(crate::mldsa44::VG_MLDSA44_PREPARE_SIGNING_KEY_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_SIGN_MESSAGE_AVX512_FEATURES) } + || cpu.contains(crate::mldsa44::VG_MLDSA44_SIGN_MESSAGE_AVX512_FEATURES) { - return crate::mldsa44::vg_mldsa44_prepare_signing_key_avx2; + return crate::mldsa44::vg_mldsa44_sign_message_avx512; } - crate::mldsa44::vg_mldsa44_prepare_signing_key + if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_SIGN_MESSAGE_AVX2_FEATURES) } + || cpu.contains(crate::mldsa44::VG_MLDSA44_SIGN_MESSAGE_AVX2_FEATURES) + { + return crate::mldsa44::vg_mldsa44_sign_message_avx2; + } + crate::mldsa44::vg_mldsa44_sign_message } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(sk: *const [u8; 2560], prepared: *mut [u8; 14848], scratch: *mut [u64; 128]) { + unsafe extern "sysv64" fn first(sk: *const [u8; 2560], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9856]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(sk, prepared, scratch) } + unsafe { f(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -303,49 +297,60 @@ pub unsafe fn vg_mldsa44_prepare_signing_key_auto(_dit: verified_garbage_cpu::Di // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, prepared, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } -/// ML-DSA-44 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa44_verify_message_prepared`. +/// ML-DSA-44 signing with a prepared private key: `vg_mldsa44_sign_message` (with its results, timing and leakage) of the private key that `*sk` begins with. /// -/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. +/// Contract: `VG.Spec.MlDsa.preparedSignMessageContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa44_prepare_verifying_key_avx2` if the CPU supports its features (`VG_MLDSA44_PREPARE_VERIFYING_KEY_AVX2_FEATURES`); or else `vg_mldsa44_prepare_verifying_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa44_sign_message_prepared_avx512` if the CPU supports its features (`VG_MLDSA44_SIGN_MESSAGE_PREPARED_AVX512_FEATURES`); `vg_mldsa44_sign_message_prepared_avx2` if the CPU supports its features (`VG_MLDSA44_SIGN_MESSAGE_PREPARED_AVX2_FEATURES`); or else `vg_mldsa44_sign_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `pk` must be valid for reads of 1312 bytes. -/// * `prepared` must be valid for reads and writes of 5408 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `sk` must be valid for reads of 14848 bytes. +/// * `msg` must be valid for reads of `msg_len` bytes. +/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `rnd` must be valid for reads of 32 bytes. +/// * `sig` must be valid for reads and writes of 2420 bytes. +/// * `scratch` must be valid for reads and writes of 78848 bytes. +/// * `sk` must have been written by `vg_mldsa44_prepare_signing_key` from a private key written by `vg_mldsa44_keygen`, and not changed since. +/// * `*sk` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). +/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). -/// * None of `pk`, `prepared` and `scratch` may overlap the return address on the stack or the 32 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx`, `rnd` or the arguments on the stack (distinct Rust objects never do). +/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the return address on the stack or the 112 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], prepared: *mut [u8; 5408], scratch: *mut [u64; 128]) { +pub unsafe fn vg_mldsa44_sign_message_prepared_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 14848], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9856]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(pk: *const [u8; 1312], prepared: *mut [u8; 5408], scratch: *mut [u64; 128]); + type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 14848], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9856]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_PREPARE_VERIFYING_KEY_AVX2_FEATURES) } - || cpu.contains(crate::mldsa44::VG_MLDSA44_PREPARE_VERIFYING_KEY_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_SIGN_MESSAGE_PREPARED_AVX512_FEATURES) } + || cpu.contains(crate::mldsa44::VG_MLDSA44_SIGN_MESSAGE_PREPARED_AVX512_FEATURES) { - return crate::mldsa44::vg_mldsa44_prepare_verifying_key_avx2; + return crate::mldsa44::vg_mldsa44_sign_message_prepared_avx512; } - crate::mldsa44::vg_mldsa44_prepare_verifying_key + if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_SIGN_MESSAGE_PREPARED_AVX2_FEATURES) } + || cpu.contains(crate::mldsa44::VG_MLDSA44_SIGN_MESSAGE_PREPARED_AVX2_FEATURES) + { + return crate::mldsa44::vg_mldsa44_sign_message_prepared_avx2; + } + crate::mldsa44::vg_mldsa44_sign_message_prepared } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(pk: *const [u8; 1312], prepared: *mut [u8; 5408], scratch: *mut [u64; 128]) { + unsafe extern "sysv64" fn first(sk: *const [u8; 14848], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9856]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(pk, prepared, scratch) } + unsafe { f(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -353,60 +358,55 @@ pub unsafe fn vg_mldsa44_prepare_verifying_key_auto(_dit: verified_garbage_cpu:: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, prepared, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } -/// ML-DSA-44 signing with a prepared private key: `vg_mldsa44_sign_message` (with its results, timing and leakage) of the private key that `*sk` begins with. +/// ML-DSA-44 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). /// -/// Contract: `VG.Spec.MlDsa.preparedSignMessageContract`. +/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa44_sign_message_prepared_avx512` if the CPU supports its features (`VG_MLDSA44_SIGN_MESSAGE_PREPARED_AVX512_FEATURES`); `vg_mldsa44_sign_message_prepared_avx2` if the CPU supports its features (`VG_MLDSA44_SIGN_MESSAGE_PREPARED_AVX2_FEATURES`); or else `vg_mldsa44_sign_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa44_verify_avx512` if the CPU supports its features (`VG_MLDSA44_VERIFY_AVX512_FEATURES`); `vg_mldsa44_verify_avx2` if the CPU supports its features (`VG_MLDSA44_VERIFY_AVX2_FEATURES`); or else `vg_mldsa44_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `sk` must be valid for reads of 14848 bytes. -/// * `msg` must be valid for reads of `msg_len` bytes. -/// * `ctx` must be valid for reads of `ctx_len` bytes. -/// * `rnd` must be valid for reads of 32 bytes. -/// * `sig` must be valid for reads and writes of 2420 bytes. -/// * `scratch` must be valid for reads and writes of 78848 bytes. -/// * `sk` must have been written by `vg_mldsa44_prepare_signing_key` from a private key written by `vg_mldsa44_keygen`, and not changed since. -/// * `*sk` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). -/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. +/// * `pk` must be valid for reads of 1312 bytes. +/// * `mu` must be valid for reads of 64 bytes. +/// * `sig` must be valid for reads of 2420 bytes. +/// * `scratch` must be valid for reads and writes of 77824 bytes. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx`, `rnd` or the arguments on the stack (distinct Rust objects never do). -/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the return address on the stack or the 112 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). +/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the return address on the stack or the 40 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa44_sign_message_prepared_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 14848], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9856]) -> u32 { +pub unsafe fn vg_mldsa44_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1312], mu: *const [u8; 64], sig: *const [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 14848], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9856]) -> u32; + type Impl = unsafe extern "sysv64" fn(pk: *const [u8; 1312], mu: *const [u8; 64], sig: *const [u8; 2420], scratch: *mut [u64; 9728]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_SIGN_MESSAGE_PREPARED_AVX512_FEATURES) } - || cpu.contains(crate::mldsa44::VG_MLDSA44_SIGN_MESSAGE_PREPARED_AVX512_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_VERIFY_AVX512_FEATURES) } + || cpu.contains(crate::mldsa44::VG_MLDSA44_VERIFY_AVX512_FEATURES) { - return crate::mldsa44::vg_mldsa44_sign_message_prepared_avx512; + return crate::mldsa44::vg_mldsa44_verify_avx512; } - if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_SIGN_MESSAGE_PREPARED_AVX2_FEATURES) } - || cpu.contains(crate::mldsa44::VG_MLDSA44_SIGN_MESSAGE_PREPARED_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa44::VG_MLDSA44_VERIFY_AVX2_FEATURES) } + || cpu.contains(crate::mldsa44::VG_MLDSA44_VERIFY_AVX2_FEATURES) { - return crate::mldsa44::vg_mldsa44_sign_message_prepared_avx2; + return crate::mldsa44::vg_mldsa44_verify_avx2; } - crate::mldsa44::vg_mldsa44_sign_message_prepared + crate::mldsa44::vg_mldsa44_verify } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(sk: *const [u8; 14848], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 2420], scratch: *mut [u64; 9856]) -> u32 { + unsafe extern "sysv64" fn first(pk: *const [u8; 1312], mu: *const [u8; 64], sig: *const [u8; 2420], scratch: *mut [u64; 9728]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { f(pk, mu, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -414,14 +414,14 @@ pub unsafe fn vg_mldsa44_sign_message_prepared_auto(_dit: verified_garbage_cpu:: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, mu, sig, scratch) } } /// ML-DSA-44 message verification with a cached public-key digest. Returns 1 for a valid signature, 0 for an invalid signature or exhausted sampling bound, and 2 if the context exceeds 255 bytes. It is `ML-DSA.Verify(pk, M, σ, ctx)` (FIPS 204 Algorithm 3) with `H(pk, 64)` read from `*tr`: with the public key `*pk`, the `msg_len` bytes of the message at `msg`, the `ctx_len` bytes of the context string at `ctx` and the signature `*sig`, it formats the message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 5), computes `μ = H(tr ‖ M′, 64)`, and verifies `*sig` on `μ` with `*pk`, `ML-DSA.Verify_internal(pk, M′, σ)` (Algorithm 8). /// /// Contract: `VG.Spec.MlDsa.verifyMessageCachedContract`. Timing may depend on the public key, message, context string and signature, and on argument addresses and lengths. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa44_verify_message_cached_avx512` if the CPU supports its features (`VG_MLDSA44_VERIFY_MESSAGE_CACHED_AVX512_FEATURES`); `vg_mldsa44_verify_message_cached_avx2` if the CPU supports its features (`VG_MLDSA44_VERIFY_MESSAGE_CACHED_AVX2_FEATURES`); or else `vg_mldsa44_verify_message_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa44_verify_message_cached_avx512` if the CPU supports its features (`VG_MLDSA44_VERIFY_MESSAGE_CACHED_AVX512_FEATURES`); `vg_mldsa44_verify_message_cached_avx2` if the CPU supports its features (`VG_MLDSA44_VERIFY_MESSAGE_CACHED_AVX2_FEATURES`); or else `vg_mldsa44_verify_message_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -480,7 +480,7 @@ pub unsafe fn vg_mldsa44_verify_message_cached_auto(_dit: verified_garbage_cpu:: /// /// Contract: `VG.Spec.MlDsa.preparedVerifyMessageContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa44_verify_message_prepared_avx512` if the CPU supports its features (`VG_MLDSA44_VERIFY_MESSAGE_PREPARED_AVX512_FEATURES`); `vg_mldsa44_verify_message_prepared_avx2` if the CPU supports its features (`VG_MLDSA44_VERIFY_MESSAGE_PREPARED_AVX2_FEATURES`); or else `vg_mldsa44_verify_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa44_verify_message_prepared_avx512` if the CPU supports its features (`VG_MLDSA44_VERIFY_MESSAGE_PREPARED_AVX512_FEATURES`); `vg_mldsa44_verify_message_prepared_avx2` if the CPU supports its features (`VG_MLDSA44_VERIFY_MESSAGE_PREPARED_AVX2_FEATURES`); or else `vg_mldsa44_verify_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/mldsa65.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/mldsa65.rs index dd7de14df74..7816c64adb9 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/mldsa65.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/mldsa65.rs @@ -2,55 +2,53 @@ //! The dispatchers of the verified `mldsa65` functions for `x86_64` //! (`crate::mldsa65`, `VG.Rust.dispatcher`). -/// ML-DSA-65 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-65 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. +/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa65_sign_avx512` if the CPU supports its features (`VG_MLDSA65_SIGN_AVX512_FEATURES`); `vg_mldsa65_sign_avx2` if the CPU supports its features (`VG_MLDSA65_SIGN_AVX2_FEATURES`); or else `vg_mldsa65_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa65_keygen_avx512` if the CPU supports its features (`VG_MLDSA65_KEYGEN_AVX512_FEATURES`); `vg_mldsa65_keygen_avx2` if the CPU supports its features (`VG_MLDSA65_KEYGEN_AVX2_FEATURES`); or else `vg_mldsa65_keygen`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `sk` must be valid for reads of 4032 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `rnd` must be valid for reads of 32 bytes. -/// * `sig` must be valid for reads and writes of 3309 bytes. +/// * `seed` must be valid for reads of 32 bytes. +/// * `pk` must be valid for reads and writes of 1952 bytes. +/// * `sk` must be valid for reads and writes of 4032 bytes. /// * `scratch` must be valid for reads and writes of 103424 bytes. -/// * `sk` must have been written by `vg_mldsa65_keygen`. -/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. +/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the return address on the stack or the 40 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the return address on the stack or the 32 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { +pub unsafe fn vg_mldsa65_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1952], sk: *mut [u8; 4032], scratch: *mut [u64; 12928]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 4032], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 12928]) -> u32; + type Impl = unsafe extern "sysv64" fn(seed: *const [u8; 32], pk: *mut [u8; 1952], sk: *mut [u8; 4032], scratch: *mut [u64; 12928]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_SIGN_AVX512_FEATURES) } - || cpu.contains(crate::mldsa65::VG_MLDSA65_SIGN_AVX512_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_KEYGEN_AVX512_FEATURES) } + || cpu.contains(crate::mldsa65::VG_MLDSA65_KEYGEN_AVX512_FEATURES) { - return crate::mldsa65::vg_mldsa65_sign_avx512; + return crate::mldsa65::vg_mldsa65_keygen_avx512; } - if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_SIGN_AVX2_FEATURES) } - || cpu.contains(crate::mldsa65::VG_MLDSA65_SIGN_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_KEYGEN_AVX2_FEATURES) } + || cpu.contains(crate::mldsa65::VG_MLDSA65_KEYGEN_AVX2_FEATURES) { - return crate::mldsa65::vg_mldsa65_sign_avx2; + return crate::mldsa65::vg_mldsa65_keygen_avx2; } - crate::mldsa65::vg_mldsa65_sign + crate::mldsa65::vg_mldsa65_keygen } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(sk: *const [u8; 4032], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { + unsafe extern "sysv64" fn first(seed: *const [u8; 32], pk: *mut [u8; 1952], sk: *mut [u8; 4032], scratch: *mut [u64; 12928]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(sk, mu, rnd, sig, scratch) } + unsafe { f(seed, pk, sk, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -58,7 +56,7 @@ pub unsafe fn vg_mldsa65_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *con // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, mu, rnd, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(seed, pk, sk, scratch) } } /// ML-DSA-65 public-key hash: writes `tr = H(pk, 64)` (FIPS 204 Algorithm 8, line 6), SHAKE256 of the public key `*pk` to 64 bytes, to `*tr`, the digest `vg_mldsa65_verify_message_cached` takes. @@ -82,53 +80,46 @@ pub unsafe fn vg_mldsa65_pk_tr_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *co unsafe { crate::mldsa65::vg_mldsa65_pk_tr(pk, tr, scratch) } } -/// ML-DSA-65 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-65 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa65_verify_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. +/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa65_keygen_avx512` if the CPU supports its features (`VG_MLDSA65_KEYGEN_AVX512_FEATURES`); `vg_mldsa65_keygen_avx2` if the CPU supports its features (`VG_MLDSA65_KEYGEN_AVX2_FEATURES`); or else `vg_mldsa65_keygen`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa65_prepare_verifying_key_avx2` if the CPU supports its features (`VG_MLDSA65_PREPARE_VERIFYING_KEY_AVX2_FEATURES`); or else `vg_mldsa65_prepare_verifying_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `seed` must be valid for reads of 32 bytes. -/// * `pk` must be valid for reads and writes of 1952 bytes. -/// * `sk` must be valid for reads and writes of 4032 bytes. -/// * `scratch` must be valid for reads and writes of 103424 bytes. -/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. +/// * `pk` must be valid for reads of 1952 bytes. +/// * `prepared` must be valid for reads and writes of 8096 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the return address on the stack or the 32 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). +/// * None of `pk`, `prepared` and `scratch` may overlap the return address on the stack or the 32 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 1952], sk: *mut [u8; 4032], scratch: *mut [u64; 12928]) -> u32 { +pub unsafe fn vg_mldsa65_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], prepared: *mut [u8; 8096], scratch: *mut [u64; 128]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(seed: *const [u8; 32], pk: *mut [u8; 1952], sk: *mut [u8; 4032], scratch: *mut [u64; 12928]) -> u32; + type Impl = unsafe extern "sysv64" fn(pk: *const [u8; 1952], prepared: *mut [u8; 8096], scratch: *mut [u64; 128]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_KEYGEN_AVX512_FEATURES) } - || cpu.contains(crate::mldsa65::VG_MLDSA65_KEYGEN_AVX512_FEATURES) - { - return crate::mldsa65::vg_mldsa65_keygen_avx512; - } - if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_KEYGEN_AVX2_FEATURES) } - || cpu.contains(crate::mldsa65::VG_MLDSA65_KEYGEN_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_PREPARE_VERIFYING_KEY_AVX2_FEATURES) } + || cpu.contains(crate::mldsa65::VG_MLDSA65_PREPARE_VERIFYING_KEY_AVX2_FEATURES) { - return crate::mldsa65::vg_mldsa65_keygen_avx2; + return crate::mldsa65::vg_mldsa65_prepare_verifying_key_avx2; } - crate::mldsa65::vg_mldsa65_keygen + crate::mldsa65::vg_mldsa65_prepare_verifying_key } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(seed: *const [u8; 32], pk: *mut [u8; 1952], sk: *mut [u8; 4032], scratch: *mut [u64; 12928]) -> u32 { + unsafe extern "sysv64" fn first(pk: *const [u8; 1952], prepared: *mut [u8; 8096], scratch: *mut [u64; 128]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(seed, pk, sk, scratch) } + unsafe { f(pk, prepared, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -136,55 +127,50 @@ pub unsafe fn vg_mldsa65_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(seed, pk, sk, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, prepared, scratch) } } -/// ML-DSA-65 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// ML-DSA-65 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa65_sign_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa65_verify_avx512` if the CPU supports its features (`VG_MLDSA65_VERIFY_AVX512_FEATURES`); `vg_mldsa65_verify_avx2` if the CPU supports its features (`VG_MLDSA65_VERIFY_AVX2_FEATURES`); or else `vg_mldsa65_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa65_prepare_signing_key_avx2` if the CPU supports its features (`VG_MLDSA65_PREPARE_SIGNING_KEY_AVX2_FEATURES`); or else `vg_mldsa65_prepare_signing_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `pk` must be valid for reads of 1952 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `sig` must be valid for reads of 3309 bytes. -/// * `scratch` must be valid for reads and writes of 103424 bytes. +/// * `sk` must be valid for reads of 4032 bytes. +/// * `prepared` must be valid for reads and writes of 21440 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). -/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the return address on the stack or the 40 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). +/// * None of `sk`, `prepared` and `scratch` may overlap the return address on the stack or the 32 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], mu: *const [u8; 64], sig: *const [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { +pub unsafe fn vg_mldsa65_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], prepared: *mut [u8; 21440], scratch: *mut [u64; 128]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(pk: *const [u8; 1952], mu: *const [u8; 64], sig: *const [u8; 3309], scratch: *mut [u64; 12928]) -> u32; + type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 4032], prepared: *mut [u8; 21440], scratch: *mut [u64; 128]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_VERIFY_AVX512_FEATURES) } - || cpu.contains(crate::mldsa65::VG_MLDSA65_VERIFY_AVX512_FEATURES) - { - return crate::mldsa65::vg_mldsa65_verify_avx512; - } - if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_VERIFY_AVX2_FEATURES) } - || cpu.contains(crate::mldsa65::VG_MLDSA65_VERIFY_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_PREPARE_SIGNING_KEY_AVX2_FEATURES) } + || cpu.contains(crate::mldsa65::VG_MLDSA65_PREPARE_SIGNING_KEY_AVX2_FEATURES) { - return crate::mldsa65::vg_mldsa65_verify_avx2; + return crate::mldsa65::vg_mldsa65_prepare_signing_key_avx2; } - crate::mldsa65::vg_mldsa65_verify + crate::mldsa65::vg_mldsa65_prepare_signing_key } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(pk: *const [u8; 1952], mu: *const [u8; 64], sig: *const [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { + unsafe extern "sysv64" fn first(sk: *const [u8; 4032], prepared: *mut [u8; 21440], scratch: *mut [u64; 128]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(pk, mu, sig, scratch) } + unsafe { f(sk, prepared, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -192,59 +178,58 @@ pub unsafe fn vg_mldsa65_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *c // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, mu, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, prepared, scratch) } } -/// ML-DSA-65 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. +/// ML-DSA-65 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. +/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa65_sign_message_avx512` if the CPU supports its features (`VG_MLDSA65_SIGN_MESSAGE_AVX512_FEATURES`); `vg_mldsa65_sign_message_avx2` if the CPU supports its features (`VG_MLDSA65_SIGN_MESSAGE_AVX2_FEATURES`); or else `vg_mldsa65_sign_message`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa65_sign_avx512` if the CPU supports its features (`VG_MLDSA65_SIGN_AVX512_FEATURES`); `vg_mldsa65_sign_avx2` if the CPU supports its features (`VG_MLDSA65_SIGN_AVX2_FEATURES`); or else `vg_mldsa65_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `sk` must be valid for reads of 4032 bytes. -/// * `msg` must be valid for reads of `msg_len` bytes. -/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `mu` must be valid for reads of 64 bytes. /// * `rnd` must be valid for reads of 32 bytes. /// * `sig` must be valid for reads and writes of 3309 bytes. -/// * `scratch` must be valid for reads and writes of 104448 bytes. +/// * `scratch` must be valid for reads and writes of 103424 bytes. /// * `sk` must have been written by `vg_mldsa65_keygen`. /// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx`, `rnd` or the arguments on the stack (distinct Rust objects never do). -/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the return address on the stack or the 112 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the return address on the stack or the 40 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 13056]) -> u32 { +pub unsafe fn vg_mldsa65_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 4032], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 13056]) -> u32; + type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 4032], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 12928]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_SIGN_MESSAGE_AVX512_FEATURES) } - || cpu.contains(crate::mldsa65::VG_MLDSA65_SIGN_MESSAGE_AVX512_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_SIGN_AVX512_FEATURES) } + || cpu.contains(crate::mldsa65::VG_MLDSA65_SIGN_AVX512_FEATURES) { - return crate::mldsa65::vg_mldsa65_sign_message_avx512; + return crate::mldsa65::vg_mldsa65_sign_avx512; } - if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_SIGN_MESSAGE_AVX2_FEATURES) } - || cpu.contains(crate::mldsa65::VG_MLDSA65_SIGN_MESSAGE_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_SIGN_AVX2_FEATURES) } + || cpu.contains(crate::mldsa65::VG_MLDSA65_SIGN_AVX2_FEATURES) { - return crate::mldsa65::vg_mldsa65_sign_message_avx2; + return crate::mldsa65::vg_mldsa65_sign_avx2; } - crate::mldsa65::vg_mldsa65_sign_message + crate::mldsa65::vg_mldsa65_sign } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(sk: *const [u8; 4032], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 13056]) -> u32 { + unsafe extern "sysv64" fn first(sk: *const [u8; 4032], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { f(sk, mu, rnd, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -252,50 +237,59 @@ pub unsafe fn vg_mldsa65_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, mu, rnd, sig, scratch) } } -/// ML-DSA-65 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa65_sign_message_prepared`. +/// ML-DSA-65 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. /// -/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. +/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa65_prepare_signing_key_avx2` if the CPU supports its features (`VG_MLDSA65_PREPARE_SIGNING_KEY_AVX2_FEATURES`); or else `vg_mldsa65_prepare_signing_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa65_sign_message_avx512` if the CPU supports its features (`VG_MLDSA65_SIGN_MESSAGE_AVX512_FEATURES`); `vg_mldsa65_sign_message_avx2` if the CPU supports its features (`VG_MLDSA65_SIGN_MESSAGE_AVX2_FEATURES`); or else `vg_mldsa65_sign_message`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `sk` must be valid for reads of 4032 bytes. -/// * `prepared` must be valid for reads and writes of 21440 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. -/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). +/// * `msg` must be valid for reads of `msg_len` bytes. +/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `rnd` must be valid for reads of 32 bytes. +/// * `sig` must be valid for reads and writes of 3309 bytes. +/// * `scratch` must be valid for reads and writes of 104448 bytes. +/// * `sk` must have been written by `vg_mldsa65_keygen`. +/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). -/// * None of `sk`, `prepared` and `scratch` may overlap the return address on the stack or the 32 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx`, `rnd` or the arguments on the stack (distinct Rust objects never do). +/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the return address on the stack or the 112 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], prepared: *mut [u8; 21440], scratch: *mut [u64; 128]) { +pub unsafe fn vg_mldsa65_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4032], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 13056]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 4032], prepared: *mut [u8; 21440], scratch: *mut [u64; 128]); + type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 4032], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 13056]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_PREPARE_SIGNING_KEY_AVX2_FEATURES) } - || cpu.contains(crate::mldsa65::VG_MLDSA65_PREPARE_SIGNING_KEY_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_SIGN_MESSAGE_AVX512_FEATURES) } + || cpu.contains(crate::mldsa65::VG_MLDSA65_SIGN_MESSAGE_AVX512_FEATURES) { - return crate::mldsa65::vg_mldsa65_prepare_signing_key_avx2; + return crate::mldsa65::vg_mldsa65_sign_message_avx512; } - crate::mldsa65::vg_mldsa65_prepare_signing_key + if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_SIGN_MESSAGE_AVX2_FEATURES) } + || cpu.contains(crate::mldsa65::VG_MLDSA65_SIGN_MESSAGE_AVX2_FEATURES) + { + return crate::mldsa65::vg_mldsa65_sign_message_avx2; + } + crate::mldsa65::vg_mldsa65_sign_message } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(sk: *const [u8; 4032], prepared: *mut [u8; 21440], scratch: *mut [u64; 128]) { + unsafe extern "sysv64" fn first(sk: *const [u8; 4032], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 13056]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(sk, prepared, scratch) } + unsafe { f(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -303,49 +297,60 @@ pub unsafe fn vg_mldsa65_prepare_signing_key_auto(_dit: verified_garbage_cpu::Di // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, prepared, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } -/// ML-DSA-65 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa65_verify_message_prepared`. +/// ML-DSA-65 signing with a prepared private key: `vg_mldsa65_sign_message` (with its results, timing and leakage) of the private key that `*sk` begins with. /// -/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. +/// Contract: `VG.Spec.MlDsa.preparedSignMessageContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa65_prepare_verifying_key_avx2` if the CPU supports its features (`VG_MLDSA65_PREPARE_VERIFYING_KEY_AVX2_FEATURES`); or else `vg_mldsa65_prepare_verifying_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa65_sign_message_prepared_avx512` if the CPU supports its features (`VG_MLDSA65_SIGN_MESSAGE_PREPARED_AVX512_FEATURES`); `vg_mldsa65_sign_message_prepared_avx2` if the CPU supports its features (`VG_MLDSA65_SIGN_MESSAGE_PREPARED_AVX2_FEATURES`); or else `vg_mldsa65_sign_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `pk` must be valid for reads of 1952 bytes. -/// * `prepared` must be valid for reads and writes of 8096 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `sk` must be valid for reads of 21440 bytes. +/// * `msg` must be valid for reads of `msg_len` bytes. +/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `rnd` must be valid for reads of 32 bytes. +/// * `sig` must be valid for reads and writes of 3309 bytes. +/// * `scratch` must be valid for reads and writes of 104448 bytes. +/// * `sk` must have been written by `vg_mldsa65_prepare_signing_key` from a private key written by `vg_mldsa65_keygen`, and not changed since. +/// * `*sk` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). +/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). -/// * None of `pk`, `prepared` and `scratch` may overlap the return address on the stack or the 32 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx`, `rnd` or the arguments on the stack (distinct Rust objects never do). +/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the return address on the stack or the 112 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], prepared: *mut [u8; 8096], scratch: *mut [u64; 128]) { +pub unsafe fn vg_mldsa65_sign_message_prepared_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 21440], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 13056]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(pk: *const [u8; 1952], prepared: *mut [u8; 8096], scratch: *mut [u64; 128]); + type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 21440], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 13056]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_PREPARE_VERIFYING_KEY_AVX2_FEATURES) } - || cpu.contains(crate::mldsa65::VG_MLDSA65_PREPARE_VERIFYING_KEY_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_SIGN_MESSAGE_PREPARED_AVX512_FEATURES) } + || cpu.contains(crate::mldsa65::VG_MLDSA65_SIGN_MESSAGE_PREPARED_AVX512_FEATURES) { - return crate::mldsa65::vg_mldsa65_prepare_verifying_key_avx2; + return crate::mldsa65::vg_mldsa65_sign_message_prepared_avx512; } - crate::mldsa65::vg_mldsa65_prepare_verifying_key + if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_SIGN_MESSAGE_PREPARED_AVX2_FEATURES) } + || cpu.contains(crate::mldsa65::VG_MLDSA65_SIGN_MESSAGE_PREPARED_AVX2_FEATURES) + { + return crate::mldsa65::vg_mldsa65_sign_message_prepared_avx2; + } + crate::mldsa65::vg_mldsa65_sign_message_prepared } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(pk: *const [u8; 1952], prepared: *mut [u8; 8096], scratch: *mut [u64; 128]) { + unsafe extern "sysv64" fn first(sk: *const [u8; 21440], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 13056]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(pk, prepared, scratch) } + unsafe { f(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -353,60 +358,55 @@ pub unsafe fn vg_mldsa65_prepare_verifying_key_auto(_dit: verified_garbage_cpu:: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, prepared, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } -/// ML-DSA-65 signing with a prepared private key: `vg_mldsa65_sign_message` (with its results, timing and leakage) of the private key that `*sk` begins with. +/// ML-DSA-65 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). /// -/// Contract: `VG.Spec.MlDsa.preparedSignMessageContract`. +/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa65_sign_message_prepared_avx512` if the CPU supports its features (`VG_MLDSA65_SIGN_MESSAGE_PREPARED_AVX512_FEATURES`); `vg_mldsa65_sign_message_prepared_avx2` if the CPU supports its features (`VG_MLDSA65_SIGN_MESSAGE_PREPARED_AVX2_FEATURES`); or else `vg_mldsa65_sign_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa65_verify_avx512` if the CPU supports its features (`VG_MLDSA65_VERIFY_AVX512_FEATURES`); `vg_mldsa65_verify_avx2` if the CPU supports its features (`VG_MLDSA65_VERIFY_AVX2_FEATURES`); or else `vg_mldsa65_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `sk` must be valid for reads of 21440 bytes. -/// * `msg` must be valid for reads of `msg_len` bytes. -/// * `ctx` must be valid for reads of `ctx_len` bytes. -/// * `rnd` must be valid for reads of 32 bytes. -/// * `sig` must be valid for reads and writes of 3309 bytes. -/// * `scratch` must be valid for reads and writes of 104448 bytes. -/// * `sk` must have been written by `vg_mldsa65_prepare_signing_key` from a private key written by `vg_mldsa65_keygen`, and not changed since. -/// * `*sk` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). -/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. +/// * `pk` must be valid for reads of 1952 bytes. +/// * `mu` must be valid for reads of 64 bytes. +/// * `sig` must be valid for reads of 3309 bytes. +/// * `scratch` must be valid for reads and writes of 103424 bytes. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx`, `rnd` or the arguments on the stack (distinct Rust objects never do). -/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the return address on the stack or the 112 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). +/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the return address on the stack or the 40 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa65_sign_message_prepared_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 21440], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 13056]) -> u32 { +pub unsafe fn vg_mldsa65_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 1952], mu: *const [u8; 64], sig: *const [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 21440], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 13056]) -> u32; + type Impl = unsafe extern "sysv64" fn(pk: *const [u8; 1952], mu: *const [u8; 64], sig: *const [u8; 3309], scratch: *mut [u64; 12928]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_SIGN_MESSAGE_PREPARED_AVX512_FEATURES) } - || cpu.contains(crate::mldsa65::VG_MLDSA65_SIGN_MESSAGE_PREPARED_AVX512_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_VERIFY_AVX512_FEATURES) } + || cpu.contains(crate::mldsa65::VG_MLDSA65_VERIFY_AVX512_FEATURES) { - return crate::mldsa65::vg_mldsa65_sign_message_prepared_avx512; + return crate::mldsa65::vg_mldsa65_verify_avx512; } - if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_SIGN_MESSAGE_PREPARED_AVX2_FEATURES) } - || cpu.contains(crate::mldsa65::VG_MLDSA65_SIGN_MESSAGE_PREPARED_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa65::VG_MLDSA65_VERIFY_AVX2_FEATURES) } + || cpu.contains(crate::mldsa65::VG_MLDSA65_VERIFY_AVX2_FEATURES) { - return crate::mldsa65::vg_mldsa65_sign_message_prepared_avx2; + return crate::mldsa65::vg_mldsa65_verify_avx2; } - crate::mldsa65::vg_mldsa65_sign_message_prepared + crate::mldsa65::vg_mldsa65_verify } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(sk: *const [u8; 21440], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 3309], scratch: *mut [u64; 13056]) -> u32 { + unsafe extern "sysv64" fn first(pk: *const [u8; 1952], mu: *const [u8; 64], sig: *const [u8; 3309], scratch: *mut [u64; 12928]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { f(pk, mu, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -414,14 +414,14 @@ pub unsafe fn vg_mldsa65_sign_message_prepared_auto(_dit: verified_garbage_cpu:: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, mu, sig, scratch) } } /// ML-DSA-65 message verification with a cached public-key digest. Returns 1 for a valid signature, 0 for an invalid signature or exhausted sampling bound, and 2 if the context exceeds 255 bytes. It is `ML-DSA.Verify(pk, M, σ, ctx)` (FIPS 204 Algorithm 3) with `H(pk, 64)` read from `*tr`: with the public key `*pk`, the `msg_len` bytes of the message at `msg`, the `ctx_len` bytes of the context string at `ctx` and the signature `*sig`, it formats the message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 5), computes `μ = H(tr ‖ M′, 64)`, and verifies `*sig` on `μ` with `*pk`, `ML-DSA.Verify_internal(pk, M′, σ)` (Algorithm 8). /// /// Contract: `VG.Spec.MlDsa.verifyMessageCachedContract`. Timing may depend on the public key, message, context string and signature, and on argument addresses and lengths. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa65_verify_message_cached_avx512` if the CPU supports its features (`VG_MLDSA65_VERIFY_MESSAGE_CACHED_AVX512_FEATURES`); `vg_mldsa65_verify_message_cached_avx2` if the CPU supports its features (`VG_MLDSA65_VERIFY_MESSAGE_CACHED_AVX2_FEATURES`); or else `vg_mldsa65_verify_message_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa65_verify_message_cached_avx512` if the CPU supports its features (`VG_MLDSA65_VERIFY_MESSAGE_CACHED_AVX512_FEATURES`); `vg_mldsa65_verify_message_cached_avx2` if the CPU supports its features (`VG_MLDSA65_VERIFY_MESSAGE_CACHED_AVX2_FEATURES`); or else `vg_mldsa65_verify_message_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -480,7 +480,7 @@ pub unsafe fn vg_mldsa65_verify_message_cached_auto(_dit: verified_garbage_cpu:: /// /// Contract: `VG.Spec.MlDsa.preparedVerifyMessageContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa65_verify_message_prepared_avx512` if the CPU supports its features (`VG_MLDSA65_VERIFY_MESSAGE_PREPARED_AVX512_FEATURES`); `vg_mldsa65_verify_message_prepared_avx2` if the CPU supports its features (`VG_MLDSA65_VERIFY_MESSAGE_PREPARED_AVX2_FEATURES`); or else `vg_mldsa65_verify_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa65_verify_message_prepared_avx512` if the CPU supports its features (`VG_MLDSA65_VERIFY_MESSAGE_PREPARED_AVX512_FEATURES`); `vg_mldsa65_verify_message_prepared_avx2` if the CPU supports its features (`VG_MLDSA65_VERIFY_MESSAGE_PREPARED_AVX2_FEATURES`); or else `vg_mldsa65_verify_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/mldsa87.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/mldsa87.rs index 5ce0e1a8adf..80c2fed288e 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/mldsa87.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/mldsa87.rs @@ -2,55 +2,53 @@ //! The dispatchers of the verified `mldsa87` functions for `x86_64` //! (`crate::mldsa87`, `VG.Rust.dispatcher`). -/// ML-DSA-87 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-87 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. +/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa87_sign_avx512` if the CPU supports its features (`VG_MLDSA87_SIGN_AVX512_FEATURES`); `vg_mldsa87_sign_avx2` if the CPU supports its features (`VG_MLDSA87_SIGN_AVX2_FEATURES`); or else `vg_mldsa87_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa87_keygen_avx512` if the CPU supports its features (`VG_MLDSA87_KEYGEN_AVX512_FEATURES`); `vg_mldsa87_keygen_avx2` if the CPU supports its features (`VG_MLDSA87_KEYGEN_AVX2_FEATURES`); or else `vg_mldsa87_keygen`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `sk` must be valid for reads of 4896 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `rnd` must be valid for reads of 32 bytes. -/// * `sig` must be valid for reads and writes of 4627 bytes. +/// * `seed` must be valid for reads of 32 bytes. +/// * `pk` must be valid for reads and writes of 2592 bytes. +/// * `sk` must be valid for reads and writes of 4896 bytes. /// * `scratch` must be valid for reads and writes of 144384 bytes. -/// * `sk` must have been written by `vg_mldsa87_keygen`. -/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. +/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). -/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the return address on the stack or the 40 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). +/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the return address on the stack or the 32 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { +pub unsafe fn vg_mldsa87_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 2592], sk: *mut [u8; 4896], scratch: *mut [u64; 18048]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 4896], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18048]) -> u32; + type Impl = unsafe extern "sysv64" fn(seed: *const [u8; 32], pk: *mut [u8; 2592], sk: *mut [u8; 4896], scratch: *mut [u64; 18048]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_SIGN_AVX512_FEATURES) } - || cpu.contains(crate::mldsa87::VG_MLDSA87_SIGN_AVX512_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_KEYGEN_AVX512_FEATURES) } + || cpu.contains(crate::mldsa87::VG_MLDSA87_KEYGEN_AVX512_FEATURES) { - return crate::mldsa87::vg_mldsa87_sign_avx512; + return crate::mldsa87::vg_mldsa87_keygen_avx512; } - if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_SIGN_AVX2_FEATURES) } - || cpu.contains(crate::mldsa87::VG_MLDSA87_SIGN_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_KEYGEN_AVX2_FEATURES) } + || cpu.contains(crate::mldsa87::VG_MLDSA87_KEYGEN_AVX2_FEATURES) { - return crate::mldsa87::vg_mldsa87_sign_avx2; + return crate::mldsa87::vg_mldsa87_keygen_avx2; } - crate::mldsa87::vg_mldsa87_sign + crate::mldsa87::vg_mldsa87_keygen } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(sk: *const [u8; 4896], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { + unsafe extern "sysv64" fn first(seed: *const [u8; 32], pk: *mut [u8; 2592], sk: *mut [u8; 4896], scratch: *mut [u64; 18048]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(sk, mu, rnd, sig, scratch) } + unsafe { f(seed, pk, sk, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -58,7 +56,7 @@ pub unsafe fn vg_mldsa87_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *con // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, mu, rnd, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(seed, pk, sk, scratch) } } /// ML-DSA-87 public-key hash: writes `tr = H(pk, 64)` (FIPS 204 Algorithm 8, line 6), SHAKE256 of the public key `*pk` to 64 bytes, to `*tr`, the digest `vg_mldsa87_verify_message_cached` takes. @@ -82,53 +80,46 @@ pub unsafe fn vg_mldsa87_pk_tr_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *co unsafe { crate::mldsa87::vg_mldsa87_pk_tr(pk, tr, scratch) } } -/// ML-DSA-87 key generation from a seed, `ML-DSA.KeyGen_internal(ξ)` (FIPS 204 Algorithm 6): with the 32-byte seed `ξ` at `seed`, writes the public key to `*pk` and the private key to `*sk`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// ML-DSA-87 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa87_verify_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.keyGenContract`. Constant time but for `ρ` and rejections: timing may depend on the pointers, on `ρ` (the first 32 bytes of the public key), and on which half-bytes of the SHAKE256 outputs `RejBoundedPoly` rejects (`rejBoundedLeak`, which is independent of the coefficients it samples), but not on anything else of the seed or the keys. +/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa87_keygen_avx512` if the CPU supports its features (`VG_MLDSA87_KEYGEN_AVX512_FEATURES`); `vg_mldsa87_keygen_avx2` if the CPU supports its features (`VG_MLDSA87_KEYGEN_AVX2_FEATURES`); or else `vg_mldsa87_keygen`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa87_prepare_verifying_key_avx2` if the CPU supports its features (`VG_MLDSA87_PREPARE_VERIFYING_KEY_AVX2_FEATURES`); or else `vg_mldsa87_prepare_verifying_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `seed` must be valid for reads of 32 bytes. -/// * `pk` must be valid for reads and writes of 2592 bytes. -/// * `sk` must be valid for reads and writes of 4896 bytes. -/// * `scratch` must be valid for reads and writes of 144384 bytes. -/// * `seed` must be random bytes from an approved RBG (FIPS 204 §3.6.1), or a seed so generated before. +/// * `pk` must be valid for reads of 2592 bytes. +/// * `prepared` must be valid for reads and writes of 10784 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `pk`, `sk` and `scratch` must not overlap each other or `seed` (distinct Rust objects never do). -/// * None of `seed`, `pk`, `sk` and `scratch` may overlap the return address on the stack or the 32 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). +/// * None of `pk`, `prepared` and `scratch` may overlap the return address on the stack or the 32 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: *const [u8; 32], pk: *mut [u8; 2592], sk: *mut [u8; 4896], scratch: *mut [u64; 18048]) -> u32 { +pub unsafe fn vg_mldsa87_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], prepared: *mut [u8; 10784], scratch: *mut [u64; 128]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(seed: *const [u8; 32], pk: *mut [u8; 2592], sk: *mut [u8; 4896], scratch: *mut [u64; 18048]) -> u32; + type Impl = unsafe extern "sysv64" fn(pk: *const [u8; 2592], prepared: *mut [u8; 10784], scratch: *mut [u64; 128]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_KEYGEN_AVX512_FEATURES) } - || cpu.contains(crate::mldsa87::VG_MLDSA87_KEYGEN_AVX512_FEATURES) - { - return crate::mldsa87::vg_mldsa87_keygen_avx512; - } - if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_KEYGEN_AVX2_FEATURES) } - || cpu.contains(crate::mldsa87::VG_MLDSA87_KEYGEN_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_PREPARE_VERIFYING_KEY_AVX2_FEATURES) } + || cpu.contains(crate::mldsa87::VG_MLDSA87_PREPARE_VERIFYING_KEY_AVX2_FEATURES) { - return crate::mldsa87::vg_mldsa87_keygen_avx2; + return crate::mldsa87::vg_mldsa87_prepare_verifying_key_avx2; } - crate::mldsa87::vg_mldsa87_keygen + crate::mldsa87::vg_mldsa87_prepare_verifying_key } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(seed: *const [u8; 32], pk: *mut [u8; 2592], sk: *mut [u8; 4896], scratch: *mut [u64; 18048]) -> u32 { + unsafe extern "sysv64" fn first(pk: *const [u8; 2592], prepared: *mut [u8; 10784], scratch: *mut [u64; 128]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(seed, pk, sk, scratch) } + unsafe { f(pk, prepared, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -136,55 +127,50 @@ pub unsafe fn vg_mldsa87_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(seed, pk, sk, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, prepared, scratch) } } -/// ML-DSA-87 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). +/// ML-DSA-87 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa87_sign_message_prepared`. /// -/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. +/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa87_verify_avx512` if the CPU supports its features (`VG_MLDSA87_VERIFY_AVX512_FEATURES`); `vg_mldsa87_verify_avx2` if the CPU supports its features (`VG_MLDSA87_VERIFY_AVX2_FEATURES`); or else `vg_mldsa87_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa87_prepare_signing_key_avx2` if the CPU supports its features (`VG_MLDSA87_PREPARE_SIGNING_KEY_AVX2_FEATURES`); or else `vg_mldsa87_prepare_signing_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `pk` must be valid for reads of 2592 bytes. -/// * `mu` must be valid for reads of 64 bytes. -/// * `sig` must be valid for reads of 4627 bytes. -/// * `scratch` must be valid for reads and writes of 144384 bytes. +/// * `sk` must be valid for reads of 4896 bytes. +/// * `prepared` must be valid for reads and writes of 28448 bytes. +/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). -/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the return address on the stack or the 40 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). +/// * None of `sk`, `prepared` and `scratch` may overlap the return address on the stack or the 32 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], mu: *const [u8; 64], sig: *const [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { +pub unsafe fn vg_mldsa87_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], prepared: *mut [u8; 28448], scratch: *mut [u64; 128]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(pk: *const [u8; 2592], mu: *const [u8; 64], sig: *const [u8; 4627], scratch: *mut [u64; 18048]) -> u32; + type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 4896], prepared: *mut [u8; 28448], scratch: *mut [u64; 128]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_VERIFY_AVX512_FEATURES) } - || cpu.contains(crate::mldsa87::VG_MLDSA87_VERIFY_AVX512_FEATURES) - { - return crate::mldsa87::vg_mldsa87_verify_avx512; - } - if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_VERIFY_AVX2_FEATURES) } - || cpu.contains(crate::mldsa87::VG_MLDSA87_VERIFY_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_PREPARE_SIGNING_KEY_AVX2_FEATURES) } + || cpu.contains(crate::mldsa87::VG_MLDSA87_PREPARE_SIGNING_KEY_AVX2_FEATURES) { - return crate::mldsa87::vg_mldsa87_verify_avx2; + return crate::mldsa87::vg_mldsa87_prepare_signing_key_avx2; } - crate::mldsa87::vg_mldsa87_verify + crate::mldsa87::vg_mldsa87_prepare_signing_key } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(pk: *const [u8; 2592], mu: *const [u8; 64], sig: *const [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { + unsafe extern "sysv64" fn first(sk: *const [u8; 4896], prepared: *mut [u8; 28448], scratch: *mut [u64; 128]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(pk, mu, sig, scratch) } + unsafe { f(sk, prepared, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -192,59 +178,58 @@ pub unsafe fn vg_mldsa87_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *c // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, mu, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, prepared, scratch) } } -/// ML-DSA-87 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. +/// ML-DSA-87 signing of a message representative, `ML-DSA.Sign_internal(sk, M′, rnd)` (FIPS 204 Algorithm 7) with `μ` computed by the caller: with the private key `*sk`, the 64-byte message representative `μ = H(tr ‖ M′, 64)` at `mu` (for the public key hash `tr`, bytes 64–127 of `*sk`) and the randomness `*rnd`, writes the signature to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// -/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. +/// Contract: `VG.Spec.MlDsa.signContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signLeak`), but not on anything else of the key, the message representative or the randomness. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa87_sign_message_avx512` if the CPU supports its features (`VG_MLDSA87_SIGN_MESSAGE_AVX512_FEATURES`); `vg_mldsa87_sign_message_avx2` if the CPU supports its features (`VG_MLDSA87_SIGN_MESSAGE_AVX2_FEATURES`); or else `vg_mldsa87_sign_message`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa87_sign_avx512` if the CPU supports its features (`VG_MLDSA87_SIGN_AVX512_FEATURES`); `vg_mldsa87_sign_avx2` if the CPU supports its features (`VG_MLDSA87_SIGN_AVX2_FEATURES`); or else `vg_mldsa87_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `sk` must be valid for reads of 4896 bytes. -/// * `msg` must be valid for reads of `msg_len` bytes. -/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `mu` must be valid for reads of 64 bytes. /// * `rnd` must be valid for reads of 32 bytes. /// * `sig` must be valid for reads and writes of 4627 bytes. -/// * `scratch` must be valid for reads and writes of 145408 bytes. +/// * `scratch` must be valid for reads and writes of 144384 bytes. /// * `sk` must have been written by `vg_mldsa87_keygen`. /// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx`, `rnd` or the arguments on the stack (distinct Rust objects never do). -/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the return address on the stack or the 112 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `mu` or `rnd` (distinct Rust objects never do). +/// * None of `sk`, `mu`, `rnd`, `sig` and `scratch` may overlap the return address on the stack or the 40 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18176]) -> u32 { +pub unsafe fn vg_mldsa87_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 4896], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18176]) -> u32; + type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 4896], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18048]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_SIGN_MESSAGE_AVX512_FEATURES) } - || cpu.contains(crate::mldsa87::VG_MLDSA87_SIGN_MESSAGE_AVX512_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_SIGN_AVX512_FEATURES) } + || cpu.contains(crate::mldsa87::VG_MLDSA87_SIGN_AVX512_FEATURES) { - return crate::mldsa87::vg_mldsa87_sign_message_avx512; + return crate::mldsa87::vg_mldsa87_sign_avx512; } - if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_SIGN_MESSAGE_AVX2_FEATURES) } - || cpu.contains(crate::mldsa87::VG_MLDSA87_SIGN_MESSAGE_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_SIGN_AVX2_FEATURES) } + || cpu.contains(crate::mldsa87::VG_MLDSA87_SIGN_AVX2_FEATURES) { - return crate::mldsa87::vg_mldsa87_sign_message_avx2; + return crate::mldsa87::vg_mldsa87_sign_avx2; } - crate::mldsa87::vg_mldsa87_sign_message + crate::mldsa87::vg_mldsa87_sign } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(sk: *const [u8; 4896], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18176]) -> u32 { + unsafe extern "sysv64" fn first(sk: *const [u8; 4896], mu: *const [u8; 64], rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { f(sk, mu, rnd, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -252,50 +237,59 @@ pub unsafe fn vg_mldsa87_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, mu, rnd, sig, scratch) } } -/// ML-DSA-87 private-key preparation: with the private key `*sk`, writes to `*prepared` the key, followed by the `ℓ + 2k` polynomials `NTT(s₁)`, `NTT(s₂)` and `NTT(t₀)` that signing (FIPS 204 Algorithm 7, lines 2–4) computes from it, for `vg_mldsa87_sign_message_prepared`. +/// ML-DSA-87 signing, `ML-DSA.Sign(sk, M, ctx)` (FIPS 204 Algorithm 2) on the randomness `*rnd` (line 5): with the private key `*sk`, the `msg_len` bytes of the message at `msg` and the `ctx_len` bytes of the context string at `ctx`, writes the signature of the formatted message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 10), `ML-DSA.Sign_internal(sk, M′, rnd)` (Algorithm 7), to `*sig`. Returns 1 on success. Returns 0 if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. Returns 2 if `ctx_len` is greater than 255 (FIPS 204 returns the error indication `⊥`): `*sig` is then unspecified. /// -/// Contract: `VG.Spec.MlDsa.prepareSecretKeyContract`. Constant time. +/// Contract: `VG.Spec.MlDsa.signMessageContract`. Constant time but for `ρ` and the rejection sampling: timing may depend on the pointers, on `msg_len` and `ctx_len`, on `ρ` (the first 32 bytes of `*sk`), on the number of iterations of the signing loop, the commitment hash of each and which validity check rejected it, and on the hint of the signature (`signMessageLeak`), but not on anything else of the key, the message, the context string or the randomness. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa87_prepare_signing_key_avx2` if the CPU supports its features (`VG_MLDSA87_PREPARE_SIGNING_KEY_AVX2_FEATURES`); or else `vg_mldsa87_prepare_signing_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa87_sign_message_avx512` if the CPU supports its features (`VG_MLDSA87_SIGN_MESSAGE_AVX512_FEATURES`); `vg_mldsa87_sign_message_avx2` if the CPU supports its features (`VG_MLDSA87_SIGN_MESSAGE_AVX2_FEATURES`); or else `vg_mldsa87_sign_message`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `sk` must be valid for reads of 4896 bytes. -/// * `prepared` must be valid for reads and writes of 28448 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. -/// * `*prepared` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). +/// * `msg` must be valid for reads of `msg_len` bytes. +/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `rnd` must be valid for reads of 32 bytes. +/// * `sig` must be valid for reads and writes of 4627 bytes. +/// * `scratch` must be valid for reads and writes of 145408 bytes. +/// * `sk` must have been written by `vg_mldsa87_keygen`. +/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `sk` (distinct Rust objects never do). -/// * None of `sk`, `prepared` and `scratch` may overlap the return address on the stack or the 32 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx`, `rnd` or the arguments on the stack (distinct Rust objects never do). +/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the return address on the stack or the 112 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_prepare_signing_key_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], prepared: *mut [u8; 28448], scratch: *mut [u64; 128]) { +pub unsafe fn vg_mldsa87_sign_message_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 4896], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18176]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 4896], prepared: *mut [u8; 28448], scratch: *mut [u64; 128]); + type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 4896], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18176]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_PREPARE_SIGNING_KEY_AVX2_FEATURES) } - || cpu.contains(crate::mldsa87::VG_MLDSA87_PREPARE_SIGNING_KEY_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_SIGN_MESSAGE_AVX512_FEATURES) } + || cpu.contains(crate::mldsa87::VG_MLDSA87_SIGN_MESSAGE_AVX512_FEATURES) { - return crate::mldsa87::vg_mldsa87_prepare_signing_key_avx2; + return crate::mldsa87::vg_mldsa87_sign_message_avx512; } - crate::mldsa87::vg_mldsa87_prepare_signing_key + if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_SIGN_MESSAGE_AVX2_FEATURES) } + || cpu.contains(crate::mldsa87::VG_MLDSA87_SIGN_MESSAGE_AVX2_FEATURES) + { + return crate::mldsa87::vg_mldsa87_sign_message_avx2; + } + crate::mldsa87::vg_mldsa87_sign_message } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(sk: *const [u8; 4896], prepared: *mut [u8; 28448], scratch: *mut [u64; 128]) { + unsafe extern "sysv64" fn first(sk: *const [u8; 4896], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18176]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(sk, prepared, scratch) } + unsafe { f(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -303,49 +297,60 @@ pub unsafe fn vg_mldsa87_prepare_signing_key_auto(_dit: verified_garbage_cpu::Di // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, prepared, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } -/// ML-DSA-87 public-key preparation: with the public key `*pk`, writes to `*prepared` the key, followed by the `k` polynomials `NTT(t₁ · 2^d)` that verification (FIPS 204 Algorithm 8, line 9) computes from it, for `vg_mldsa87_verify_message_prepared`. +/// ML-DSA-87 signing with a prepared private key: `vg_mldsa87_sign_message` (with its results, timing and leakage) of the private key that `*sk` begins with. /// -/// Contract: `VG.Spec.MlDsa.preparePublicKeyContract`. Constant time. +/// Contract: `VG.Spec.MlDsa.preparedSignMessageContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa87_prepare_verifying_key_avx2` if the CPU supports its features (`VG_MLDSA87_PREPARE_VERIFYING_KEY_AVX2_FEATURES`); or else `vg_mldsa87_prepare_verifying_key`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa87_sign_message_prepared_avx512` if the CPU supports its features (`VG_MLDSA87_SIGN_MESSAGE_PREPARED_AVX512_FEATURES`); `vg_mldsa87_sign_message_prepared_avx2` if the CPU supports its features (`VG_MLDSA87_SIGN_MESSAGE_PREPARED_AVX2_FEATURES`); or else `vg_mldsa87_sign_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `pk` must be valid for reads of 2592 bytes. -/// * `prepared` must be valid for reads and writes of 10784 bytes. -/// * `scratch` must be valid for reads and writes of 1024 bytes. +/// * `sk` must be valid for reads of 28448 bytes. +/// * `msg` must be valid for reads of `msg_len` bytes. +/// * `ctx` must be valid for reads of `ctx_len` bytes. +/// * `rnd` must be valid for reads of 32 bytes. +/// * `sig` must be valid for reads and writes of 4627 bytes. +/// * `scratch` must be valid for reads and writes of 145408 bytes. +/// * `sk` must have been written by `vg_mldsa87_prepare_signing_key` from a private key written by `vg_mldsa87_keygen`, and not changed since. +/// * `*sk` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). +/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `prepared` and `scratch` must not overlap each other or `pk` (distinct Rust objects never do). -/// * None of `pk`, `prepared` and `scratch` may overlap the return address on the stack or the 32 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx`, `rnd` or the arguments on the stack (distinct Rust objects never do). +/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the return address on the stack or the 112 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_prepare_verifying_key_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], prepared: *mut [u8; 10784], scratch: *mut [u64; 128]) { +pub unsafe fn vg_mldsa87_sign_message_prepared_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 28448], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18176]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(pk: *const [u8; 2592], prepared: *mut [u8; 10784], scratch: *mut [u64; 128]); + type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 28448], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18176]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_PREPARE_VERIFYING_KEY_AVX2_FEATURES) } - || cpu.contains(crate::mldsa87::VG_MLDSA87_PREPARE_VERIFYING_KEY_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_SIGN_MESSAGE_PREPARED_AVX512_FEATURES) } + || cpu.contains(crate::mldsa87::VG_MLDSA87_SIGN_MESSAGE_PREPARED_AVX512_FEATURES) { - return crate::mldsa87::vg_mldsa87_prepare_verifying_key_avx2; + return crate::mldsa87::vg_mldsa87_sign_message_prepared_avx512; } - crate::mldsa87::vg_mldsa87_prepare_verifying_key + if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_SIGN_MESSAGE_PREPARED_AVX2_FEATURES) } + || cpu.contains(crate::mldsa87::VG_MLDSA87_SIGN_MESSAGE_PREPARED_AVX2_FEATURES) + { + return crate::mldsa87::vg_mldsa87_sign_message_prepared_avx2; + } + crate::mldsa87::vg_mldsa87_sign_message_prepared } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(pk: *const [u8; 2592], prepared: *mut [u8; 10784], scratch: *mut [u64; 128]) { + unsafe extern "sysv64" fn first(sk: *const [u8; 28448], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18176]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(pk, prepared, scratch) } + unsafe { f(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -353,60 +358,55 @@ pub unsafe fn vg_mldsa87_prepare_verifying_key_auto(_dit: verified_garbage_cpu:: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, prepared, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } } -/// ML-DSA-87 signing with a prepared private key: `vg_mldsa87_sign_message` (with its results, timing and leakage) of the private key that `*sk` begins with. +/// ML-DSA-87 verification of a signature of a message representative, `ML-DSA.Verify_internal(pk, M′, σ)` (FIPS 204 Algorithm 8) with `μ` computed by the caller: with the public key `*pk`, the 64-byte message representative `μ = H(H(pk, 64) ‖ M′, 64)` at `mu` and the signature `*sig`, returns 1 if the signature is valid, and 0 if it is not or if a loop reaches its bound, which is at least the limit of FIPS 204 Appendix C, Table 3 (this happens with probability about 2^-256 or less). /// -/// Contract: `VG.Spec.MlDsa.preparedSignMessageContract`. +/// Contract: `VG.Spec.MlDsa.verifyContract`. Not constant time: timing may depend on the public key, the message representative and the signature. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa87_sign_message_prepared_avx512` if the CPU supports its features (`VG_MLDSA87_SIGN_MESSAGE_PREPARED_AVX512_FEATURES`); `vg_mldsa87_sign_message_prepared_avx2` if the CPU supports its features (`VG_MLDSA87_SIGN_MESSAGE_PREPARED_AVX2_FEATURES`); or else `vg_mldsa87_sign_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa87_verify_avx512` if the CPU supports its features (`VG_MLDSA87_VERIFY_AVX512_FEATURES`); `vg_mldsa87_verify_avx2` if the CPU supports its features (`VG_MLDSA87_VERIFY_AVX2_FEATURES`); or else `vg_mldsa87_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `sk` must be valid for reads of 28448 bytes. -/// * `msg` must be valid for reads of `msg_len` bytes. -/// * `ctx` must be valid for reads of `ctx_len` bytes. -/// * `rnd` must be valid for reads of 32 bytes. -/// * `sig` must be valid for reads and writes of 4627 bytes. -/// * `scratch` must be valid for reads and writes of 145408 bytes. -/// * `sk` must have been written by `vg_mldsa87_prepare_signing_key` from a private key written by `vg_mldsa87_keygen`, and not changed since. -/// * `*sk` holds private key material, which the caller must destroy (FIPS 204 §3.6.3). -/// * `rnd` must be fresh random bytes (FIPS 204 §3.6.1), or 32 zero bytes for deterministic signing. +/// * `pk` must be valid for reads of 2592 bytes. +/// * `mu` must be valid for reads of 64 bytes. +/// * `sig` must be valid for reads of 4627 bytes. +/// * `scratch` must be valid for reads and writes of 144384 bytes. /// * `scratch` is working space: on return it holds intermediate values, which the caller must destroy (FIPS 204 §3.6.3). -/// * `sig` and `scratch` must not overlap each other, `sk`, `msg`, `ctx`, `rnd` or the arguments on the stack (distinct Rust objects never do). -/// * None of `sk`, `msg`, `ctx`, `rnd`, `sig` and `scratch` may overlap the return address on the stack or the 112 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `scratch` must not overlap `pk`, `mu` or `sig` (distinct Rust objects never do). +/// * None of `pk`, `mu`, `sig` and `scratch` may overlap the return address on the stack or the 40 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_mldsa87_sign_message_prepared_auto(_dit: verified_garbage_cpu::Dit<'_>, sk: *const [u8; 28448], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18176]) -> u32 { +pub unsafe fn vg_mldsa87_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, pk: *const [u8; 2592], mu: *const [u8; 64], sig: *const [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(sk: *const [u8; 28448], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18176]) -> u32; + type Impl = unsafe extern "sysv64" fn(pk: *const [u8; 2592], mu: *const [u8; 64], sig: *const [u8; 4627], scratch: *mut [u64; 18048]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_SIGN_MESSAGE_PREPARED_AVX512_FEATURES) } - || cpu.contains(crate::mldsa87::VG_MLDSA87_SIGN_MESSAGE_PREPARED_AVX512_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_VERIFY_AVX512_FEATURES) } + || cpu.contains(crate::mldsa87::VG_MLDSA87_VERIFY_AVX512_FEATURES) { - return crate::mldsa87::vg_mldsa87_sign_message_prepared_avx512; + return crate::mldsa87::vg_mldsa87_verify_avx512; } - if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_SIGN_MESSAGE_PREPARED_AVX2_FEATURES) } - || cpu.contains(crate::mldsa87::VG_MLDSA87_SIGN_MESSAGE_PREPARED_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::mldsa87::VG_MLDSA87_VERIFY_AVX2_FEATURES) } + || cpu.contains(crate::mldsa87::VG_MLDSA87_VERIFY_AVX2_FEATURES) { - return crate::mldsa87::vg_mldsa87_sign_message_prepared_avx2; + return crate::mldsa87::vg_mldsa87_verify_avx2; } - crate::mldsa87::vg_mldsa87_sign_message_prepared + crate::mldsa87::vg_mldsa87_verify } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(sk: *const [u8; 28448], msg: *const u8, msg_len: usize, ctx: *const u8, ctx_len: usize, rnd: *const [u8; 32], sig: *mut [u8; 4627], scratch: *mut [u64; 18176]) -> u32 { + unsafe extern "sysv64" fn first(pk: *const [u8; 2592], mu: *const [u8; 64], sig: *const [u8; 4627], scratch: *mut [u64; 18048]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { f(pk, mu, sig, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -414,14 +414,14 @@ pub unsafe fn vg_mldsa87_sign_message_prepared_auto(_dit: verified_garbage_cpu:: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(sk, msg, msg_len, ctx, ctx_len, rnd, sig, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pk, mu, sig, scratch) } } /// ML-DSA-87 message verification with a cached public-key digest. Returns 1 for a valid signature, 0 for an invalid signature or exhausted sampling bound, and 2 if the context exceeds 255 bytes. It is `ML-DSA.Verify(pk, M, σ, ctx)` (FIPS 204 Algorithm 3) with `H(pk, 64)` read from `*tr`: with the public key `*pk`, the `msg_len` bytes of the message at `msg`, the `ctx_len` bytes of the context string at `ctx` and the signature `*sig`, it formats the message `M′ = 0 ‖ ctx_len ‖ ctx ‖ M` (line 5), computes `μ = H(tr ‖ M′, 64)`, and verifies `*sig` on `μ` with `*pk`, `ML-DSA.Verify_internal(pk, M′, σ)` (Algorithm 8). /// /// Contract: `VG.Spec.MlDsa.verifyMessageCachedContract`. Timing may depend on the public key, message, context string and signature, and on argument addresses and lengths. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa87_verify_message_cached_avx512` if the CPU supports its features (`VG_MLDSA87_VERIFY_MESSAGE_CACHED_AVX512_FEATURES`); `vg_mldsa87_verify_message_cached_avx2` if the CPU supports its features (`VG_MLDSA87_VERIFY_MESSAGE_CACHED_AVX2_FEATURES`); or else `vg_mldsa87_verify_message_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa87_verify_message_cached_avx512` if the CPU supports its features (`VG_MLDSA87_VERIFY_MESSAGE_CACHED_AVX512_FEATURES`); `vg_mldsa87_verify_message_cached_avx2` if the CPU supports its features (`VG_MLDSA87_VERIFY_MESSAGE_CACHED_AVX2_FEATURES`); or else `vg_mldsa87_verify_message_cached`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -480,7 +480,7 @@ pub unsafe fn vg_mldsa87_verify_message_cached_auto(_dit: verified_garbage_cpu:: /// /// Contract: `VG.Spec.MlDsa.preparedVerifyMessageContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mldsa87_verify_message_prepared_avx512` if the CPU supports its features (`VG_MLDSA87_VERIFY_MESSAGE_PREPARED_AVX512_FEATURES`); `vg_mldsa87_verify_message_prepared_avx2` if the CPU supports its features (`VG_MLDSA87_VERIFY_MESSAGE_PREPARED_AVX2_FEATURES`); or else `vg_mldsa87_verify_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mldsa87_verify_message_prepared_avx512` if the CPU supports its features (`VG_MLDSA87_VERIFY_MESSAGE_PREPARED_AVX512_FEATURES`); `vg_mldsa87_verify_message_prepared_avx2` if the CPU supports its features (`VG_MLDSA87_VERIFY_MESSAGE_PREPARED_AVX2_FEATURES`); or else `vg_mldsa87_verify_message_prepared`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/mlkem1024.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/mlkem1024.rs index 830ea85044e..a8dfa4bc154 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/mlkem1024.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/mlkem1024.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.MlKem1024.keyGenContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of the encapsulation key), but not on anything else of the seed or the keys. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mlkem1024_keygen_avx512` if the CPU supports its features (`VG_MLKEM1024_KEYGEN_AVX512_FEATURES`); `vg_mlkem1024_keygen_avx2` if the CPU supports its features (`VG_MLKEM1024_KEYGEN_AVX2_FEATURES`); `vg_mlkem1024_keygen_avx` if the CPU supports its features (`VG_MLKEM1024_KEYGEN_AVX_FEATURES`); or else `vg_mlkem1024_keygen`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mlkem1024_keygen_avx512` if the CPU supports its features (`VG_MLKEM1024_KEYGEN_AVX512_FEATURES`); `vg_mlkem1024_keygen_avx2` if the CPU supports its features (`VG_MLKEM1024_KEYGEN_AVX2_FEATURES`); `vg_mlkem1024_keygen_avx` if the CPU supports its features (`VG_MLKEM1024_KEYGEN_AVX_FEATURES`); or else `vg_mlkem1024_keygen`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -64,11 +64,77 @@ pub unsafe fn vg_mlkem1024_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(seed, ek, dk, scratch) } } +/// ML-KEM-1024 encapsulation with given randomness and the hash of the key, `ML-KEM.Encaps_internal(ek, m)` (FIPS 203 Algorithm 17): with the encapsulation key `*ek`, its hash `H(ek)` in `*h` and the randomness `*m`, writes the shared secret key to `*key` and the ciphertext to `*ct`, as `vg_mlkem1024_encaps` does, using `*h` instead of hashing `*ek`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// +/// Contract: `VG.Spec.MlKem1024.encapsHContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on its hash, on the randomness or on the outputs. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mlkem1024_encaps_h_avx512` if the CPU supports its features (`VG_MLKEM1024_ENCAPS_H_AVX512_FEATURES`); `vg_mlkem1024_encaps_h_avx2` if the CPU supports its features (`VG_MLKEM1024_ENCAPS_H_AVX2_FEATURES`); `vg_mlkem1024_encaps_h_avx` if the CPU supports its features (`VG_MLKEM1024_ENCAPS_H_AVX_FEATURES`); or else `vg_mlkem1024_encaps_h`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// +/// # Safety +/// +/// * `ek` must be valid for reads of 1568 bytes. +/// * `h` must be valid for reads of 32 bytes. +/// * `m` must be valid for reads of 32 bytes. +/// * `key` must be valid for reads and writes of 32 bytes. +/// * `ct` must be valid for reads and writes of 1568 bytes. +/// * `scratch` must be valid for reads and writes of 49152 bytes. +/// * `ek` must have passed `vg_mlkem1024_check_ek` (FIPS 203 §7.2). +/// * `h` must hold `H(ek)`, the SHA3-256 hash of the bytes of `*ek` (FIPS 203 §4.1). +/// * `m` must be fresh random bytes from an approved RBG (FIPS 203 §3.3). +/// * `scratch` is working space: on return each of its bytes is unchanged or zero. +/// * `key`, `ct` and `scratch` must not overlap each other, `ek`, `h` or `m` (distinct Rust objects never do). +/// * None of `ek`, `h`, `m`, `key`, `ct` and `scratch` may overlap the return address on the stack or the 56 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mlkem1024_encaps_h_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1568], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1568], scratch: *mut [u64; 6144]) -> u32 { + /// The type of the implementations. + type Impl = unsafe extern "sysv64" fn(ek: *const [u8; 1568], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1568], scratch: *mut [u64; 6144]) -> u32; + /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only + /// features the CPU has. + #[cold] + fn select() -> Impl { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_AVX512_FEATURES) } + || cpu.contains(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_AVX512_FEATURES) + { + return crate::mlkem1024::vg_mlkem1024_encaps_h_avx512; + } + if const { verified_garbage_cpu::target_contains(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_AVX2_FEATURES) } + || cpu.contains(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_AVX2_FEATURES) + { + return crate::mlkem1024::vg_mlkem1024_encaps_h_avx2; + } + if const { verified_garbage_cpu::target_contains(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_AVX_FEATURES) } + || cpu.contains(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_AVX_FEATURES) + { + return crate::mlkem1024::vg_mlkem1024_encaps_h_avx; + } + crate::mlkem1024::vg_mlkem1024_encaps_h + } + /// The first call: chooses the implementation, records it in `CHOSEN` for the calls + /// after it, and runs it. + #[cold] + unsafe extern "sysv64" fn first(ek: *const [u8; 1568], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1568], scratch: *mut [u64; 6144]) -> u32 { + let f = select(); + CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); + // SAFETY: the caller's obligations, which are those of every implementation, `f` + // among them, and the CPU has the features `f` needs. + unsafe { f(ek, h, m, key, ct, scratch) } + } + /// The function to call: `first`, until it records the implementation it chose. + static CHOSEN: core::sync::atomic::AtomicPtr<()> = + core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); + // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, + // or an implementation `select` returned, which needs only features the CPU has. Calling + // either carries the caller's obligations, which are those of every implementation. + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ek, h, m, key, ct, scratch) } +} + /// ML-KEM-1024 encapsulation with given randomness, `ML-KEM.Encaps_internal(ek, m)` (FIPS 203 Algorithm 17): with the encapsulation key `*ek` and the randomness `*m`, writes the shared secret key to `*key` and the ciphertext to `*ct`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// /// Contract: `VG.Spec.MlKem1024.encapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on the randomness or on the outputs. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mlkem1024_encaps_avx512` if the CPU supports its features (`VG_MLKEM1024_ENCAPS_AVX512_FEATURES`); `vg_mlkem1024_encaps_avx2` if the CPU supports its features (`VG_MLKEM1024_ENCAPS_AVX2_FEATURES`); `vg_mlkem1024_encaps_avx` if the CPU supports its features (`VG_MLKEM1024_ENCAPS_AVX_FEATURES`); or else `vg_mlkem1024_encaps`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mlkem1024_encaps_avx512` if the CPU supports its features (`VG_MLKEM1024_ENCAPS_AVX512_FEATURES`); `vg_mlkem1024_encaps_avx2` if the CPU supports its features (`VG_MLKEM1024_ENCAPS_AVX2_FEATURES`); `vg_mlkem1024_encaps_avx` if the CPU supports its features (`VG_MLKEM1024_ENCAPS_AVX_FEATURES`); or else `vg_mlkem1024_encaps`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -132,7 +198,7 @@ pub unsafe fn vg_mlkem1024_encaps_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: /// /// Contract: `VG.Spec.MlKem1024.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 3072–3103 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mlkem1024_decaps_avx512` if the CPU supports its features (`VG_MLKEM1024_DECAPS_AVX512_FEATURES`); `vg_mlkem1024_decaps_avx2` if the CPU supports its features (`VG_MLKEM1024_DECAPS_AVX2_FEATURES`); `vg_mlkem1024_decaps_avx` if the CPU supports its features (`VG_MLKEM1024_DECAPS_AVX_FEATURES`); or else `vg_mlkem1024_decaps`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mlkem1024_decaps_avx512` if the CPU supports its features (`VG_MLKEM1024_DECAPS_AVX512_FEATURES`); `vg_mlkem1024_decaps_avx2` if the CPU supports its features (`VG_MLKEM1024_DECAPS_AVX2_FEATURES`); `vg_mlkem1024_decaps_avx` if the CPU supports its features (`VG_MLKEM1024_DECAPS_AVX_FEATURES`); or else `vg_mlkem1024_decaps`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -190,77 +256,11 @@ pub unsafe fn vg_mlkem1024_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(dk, ct, key, scratch) } } -/// ML-KEM-1024 encapsulation with given randomness and the hash of the key, `ML-KEM.Encaps_internal(ek, m)` (FIPS 203 Algorithm 17): with the encapsulation key `*ek`, its hash `H(ek)` in `*h` and the randomness `*m`, writes the shared secret key to `*key` and the ciphertext to `*ct`, as `vg_mlkem1024_encaps` does, using `*h` instead of hashing `*ek`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. -/// -/// Contract: `VG.Spec.MlKem1024.encapsHContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on its hash, on the randomness or on the outputs. -/// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mlkem1024_encaps_h_avx512` if the CPU supports its features (`VG_MLKEM1024_ENCAPS_H_AVX512_FEATURES`); `vg_mlkem1024_encaps_h_avx2` if the CPU supports its features (`VG_MLKEM1024_ENCAPS_H_AVX2_FEATURES`); `vg_mlkem1024_encaps_h_avx` if the CPU supports its features (`VG_MLKEM1024_ENCAPS_H_AVX_FEATURES`); or else `vg_mlkem1024_encaps_h`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. -/// -/// # Safety -/// -/// * `ek` must be valid for reads of 1568 bytes. -/// * `h` must be valid for reads of 32 bytes. -/// * `m` must be valid for reads of 32 bytes. -/// * `key` must be valid for reads and writes of 32 bytes. -/// * `ct` must be valid for reads and writes of 1568 bytes. -/// * `scratch` must be valid for reads and writes of 49152 bytes. -/// * `ek` must have passed `vg_mlkem1024_check_ek` (FIPS 203 §7.2). -/// * `h` must hold `H(ek)`, the SHA3-256 hash of the bytes of `*ek` (FIPS 203 §4.1). -/// * `m` must be fresh random bytes from an approved RBG (FIPS 203 §3.3). -/// * `scratch` is working space: on return each of its bytes is unchanged or zero. -/// * `key`, `ct` and `scratch` must not overlap each other, `ek`, `h` or `m` (distinct Rust objects never do). -/// * None of `ek`, `h`, `m`, `key`, `ct` and `scratch` may overlap the return address on the stack or the 56 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mlkem1024_encaps_h_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1568], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1568], scratch: *mut [u64; 6144]) -> u32 { - /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(ek: *const [u8; 1568], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1568], scratch: *mut [u64; 6144]) -> u32; - /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only - /// features the CPU has. - #[cold] - fn select() -> Impl { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_AVX512_FEATURES) } - || cpu.contains(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_AVX512_FEATURES) - { - return crate::mlkem1024::vg_mlkem1024_encaps_h_avx512; - } - if const { verified_garbage_cpu::target_contains(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_AVX2_FEATURES) } - || cpu.contains(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_AVX2_FEATURES) - { - return crate::mlkem1024::vg_mlkem1024_encaps_h_avx2; - } - if const { verified_garbage_cpu::target_contains(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_AVX_FEATURES) } - || cpu.contains(crate::mlkem1024::VG_MLKEM1024_ENCAPS_H_AVX_FEATURES) - { - return crate::mlkem1024::vg_mlkem1024_encaps_h_avx; - } - crate::mlkem1024::vg_mlkem1024_encaps_h - } - /// The first call: chooses the implementation, records it in `CHOSEN` for the calls - /// after it, and runs it. - #[cold] - unsafe extern "sysv64" fn first(ek: *const [u8; 1568], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1568], scratch: *mut [u64; 6144]) -> u32 { - let f = select(); - CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); - // SAFETY: the caller's obligations, which are those of every implementation, `f` - // among them, and the CPU has the features `f` needs. - unsafe { f(ek, h, m, key, ct, scratch) } - } - /// The function to call: `first`, until it records the implementation it chose. - static CHOSEN: core::sync::atomic::AtomicPtr<()> = - core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); - // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, - // or an implementation `select` returned, which needs only features the CPU has. Calling - // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ek, h, m, key, ct, scratch) } -} - /// The ML-KEM-1024 encapsulation key check (FIPS 203 §7.2): returns 1 if every 12-bit integer that the first 1536 bytes of `*ek` encode is less than `q` = 3329 (the modulus check), and 0 otherwise. An encapsulation key must pass it before it is given to `vg_mlkem1024_encaps`. /// /// Contract: `VG.Spec.MlKem1024.checkEkContract`. Constant time: only the pointer may affect timing. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mlkem1024_check_ek_avx2` if the CPU supports its features (`VG_MLKEM1024_CHECK_EK_AVX2_FEATURES`); or else `vg_mlkem1024_check_ek`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mlkem1024_check_ek_avx2` if the CPU supports its features (`VG_MLKEM1024_CHECK_EK_AVX2_FEATURES`); or else `vg_mlkem1024_check_ek`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/mlkem768.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/mlkem768.rs index 7acbc94a713..b52031a3e40 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/mlkem768.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/mlkem768.rs @@ -2,11 +2,57 @@ //! The dispatchers of the verified `mlkem768` functions for `x86_64` //! (`crate::mlkem768`, `VG.Rust.dispatcher`). +/// The ML-KEM-768 encapsulation key check (FIPS 203 §7.2): returns 1 if every 12-bit integer that the first 1152 bytes of `*ek` encode is less than `q` = 3329 (the modulus check), and 0 otherwise. An encapsulation key must pass it before it is given to `vg_mlkem768_encaps`. +/// +/// Contract: `VG.Spec.MlKem.checkEkContract`. Constant time: only the pointer may affect timing. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mlkem768_check_ek_avx2` if the CPU supports its features (`VG_MLKEM768_CHECK_EK_AVX2_FEATURES`); or else `vg_mlkem768_check_ek`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// +/// # Safety +/// +/// * `ek` must be valid for reads of 1184 bytes. +/// * `ek` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mlkem768_check_ek_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1184]) -> u32 { + /// The type of the implementations. + type Impl = unsafe extern "sysv64" fn(ek: *const [u8; 1184]) -> u32; + /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only + /// features the CPU has. + #[cold] + fn select() -> Impl { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::mlkem768::VG_MLKEM768_CHECK_EK_AVX2_FEATURES) } + || cpu.contains(crate::mlkem768::VG_MLKEM768_CHECK_EK_AVX2_FEATURES) + { + return crate::mlkem768::vg_mlkem768_check_ek_avx2; + } + crate::mlkem768::vg_mlkem768_check_ek + } + /// The first call: chooses the implementation, records it in `CHOSEN` for the calls + /// after it, and runs it. + #[cold] + unsafe extern "sysv64" fn first(ek: *const [u8; 1184]) -> u32 { + let f = select(); + CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); + // SAFETY: the caller's obligations, which are those of every implementation, `f` + // among them, and the CPU has the features `f` needs. + unsafe { f(ek) } + } + /// The function to call: `first`, until it records the implementation it chose. + static CHOSEN: core::sync::atomic::AtomicPtr<()> = + core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); + // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, + // or an implementation `select` returned, which needs only features the CPU has. Calling + // either carries the caller's obligations, which are those of every implementation. + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ek) } +} + /// ML-KEM-768 key generation from a seed, `ML-KEM.KeyGen_internal(d, z)` (FIPS 203 Algorithm 16): with `d` in bytes 0–31 of `*seed` and `z` in bytes 32–63, writes the encapsulation key to `*ek` and the decapsulation key to `*dk`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// /// Contract: `VG.Spec.MlKem.keyGenContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of the encapsulation key), but not on anything else of the seed or the keys. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mlkem768_keygen_avx512` if the CPU supports its features (`VG_MLKEM768_KEYGEN_AVX512_FEATURES`); `vg_mlkem768_keygen_avx2` if the CPU supports its features (`VG_MLKEM768_KEYGEN_AVX2_FEATURES`); `vg_mlkem768_keygen_avx` if the CPU supports its features (`VG_MLKEM768_KEYGEN_AVX_FEATURES`); or else `vg_mlkem768_keygen`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mlkem768_keygen_avx512` if the CPU supports its features (`VG_MLKEM768_KEYGEN_AVX512_FEATURES`); `vg_mlkem768_keygen_avx2` if the CPU supports its features (`VG_MLKEM768_KEYGEN_AVX2_FEATURES`); `vg_mlkem768_keygen_avx` if the CPU supports its features (`VG_MLKEM768_KEYGEN_AVX_FEATURES`); or else `vg_mlkem768_keygen`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -64,11 +110,77 @@ pub unsafe fn vg_mlkem768_keygen_auto(_dit: verified_garbage_cpu::Dit<'_>, seed: unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(seed, ek, dk, scratch) } } +/// ML-KEM-768 encapsulation with given randomness and the hash of the key, `ML-KEM.Encaps_internal(ek, m)` (FIPS 203 Algorithm 17): with the encapsulation key `*ek`, its hash `H(ek)` in `*h` and the randomness `*m`, writes the shared secret key to `*key` and the ciphertext to `*ct`, as `vg_mlkem768_encaps` does, using `*h` instead of hashing `*ek`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. +/// +/// Contract: `VG.Spec.MlKem.encapsHContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on its hash, on the randomness or on the outputs. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mlkem768_encaps_h_avx512` if the CPU supports its features (`VG_MLKEM768_ENCAPS_H_AVX512_FEATURES`); `vg_mlkem768_encaps_h_avx2` if the CPU supports its features (`VG_MLKEM768_ENCAPS_H_AVX2_FEATURES`); `vg_mlkem768_encaps_h_avx` if the CPU supports its features (`VG_MLKEM768_ENCAPS_H_AVX_FEATURES`); or else `vg_mlkem768_encaps_h`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// +/// # Safety +/// +/// * `ek` must be valid for reads of 1184 bytes. +/// * `h` must be valid for reads of 32 bytes. +/// * `m` must be valid for reads of 32 bytes. +/// * `key` must be valid for reads and writes of 32 bytes. +/// * `ct` must be valid for reads and writes of 1088 bytes. +/// * `scratch` must be valid for reads and writes of 32768 bytes. +/// * `ek` must have passed `vg_mlkem768_check_ek` (FIPS 203 §7.2). +/// * `h` must hold `H(ek)`, the SHA3-256 hash of the bytes of `*ek` (FIPS 203 §4.1). +/// * `m` must be fresh random bytes from an approved RBG (FIPS 203 §3.3). +/// * `scratch` is working space: on return each of its bytes is unchanged or zero. +/// * `key`, `ct` and `scratch` must not overlap each other, `ek`, `h` or `m` (distinct Rust objects never do). +/// * None of `ek`, `h`, `m`, `key`, `ct` and `scratch` may overlap the return address on the stack or the 56 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_mlkem768_encaps_h_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1184], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1088], scratch: *mut [u64; 4096]) -> u32 { + /// The type of the implementations. + type Impl = unsafe extern "sysv64" fn(ek: *const [u8; 1184], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1088], scratch: *mut [u64; 4096]) -> u32; + /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only + /// features the CPU has. + #[cold] + fn select() -> Impl { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::mlkem768::VG_MLKEM768_ENCAPS_H_AVX512_FEATURES) } + || cpu.contains(crate::mlkem768::VG_MLKEM768_ENCAPS_H_AVX512_FEATURES) + { + return crate::mlkem768::vg_mlkem768_encaps_h_avx512; + } + if const { verified_garbage_cpu::target_contains(crate::mlkem768::VG_MLKEM768_ENCAPS_H_AVX2_FEATURES) } + || cpu.contains(crate::mlkem768::VG_MLKEM768_ENCAPS_H_AVX2_FEATURES) + { + return crate::mlkem768::vg_mlkem768_encaps_h_avx2; + } + if const { verified_garbage_cpu::target_contains(crate::mlkem768::VG_MLKEM768_ENCAPS_H_AVX_FEATURES) } + || cpu.contains(crate::mlkem768::VG_MLKEM768_ENCAPS_H_AVX_FEATURES) + { + return crate::mlkem768::vg_mlkem768_encaps_h_avx; + } + crate::mlkem768::vg_mlkem768_encaps_h + } + /// The first call: chooses the implementation, records it in `CHOSEN` for the calls + /// after it, and runs it. + #[cold] + unsafe extern "sysv64" fn first(ek: *const [u8; 1184], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1088], scratch: *mut [u64; 4096]) -> u32 { + let f = select(); + CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); + // SAFETY: the caller's obligations, which are those of every implementation, `f` + // among them, and the CPU has the features `f` needs. + unsafe { f(ek, h, m, key, ct, scratch) } + } + /// The function to call: `first`, until it records the implementation it chose. + static CHOSEN: core::sync::atomic::AtomicPtr<()> = + core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); + // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, + // or an implementation `select` returned, which needs only features the CPU has. Calling + // either carries the caller's obligations, which are those of every implementation. + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ek, h, m, key, ct, scratch) } +} + /// ML-KEM-768 encapsulation with given randomness, `ML-KEM.Encaps_internal(ek, m)` (FIPS 203 Algorithm 17): with the encapsulation key `*ek` and the randomness `*m`, writes the shared secret key to `*key` and the ciphertext to `*ct`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. /// /// Contract: `VG.Spec.MlKem.encapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on the randomness or on the outputs. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mlkem768_encaps_avx512` if the CPU supports its features (`VG_MLKEM768_ENCAPS_AVX512_FEATURES`); `vg_mlkem768_encaps_avx2` if the CPU supports its features (`VG_MLKEM768_ENCAPS_AVX2_FEATURES`); `vg_mlkem768_encaps_avx` if the CPU supports its features (`VG_MLKEM768_ENCAPS_AVX_FEATURES`); or else `vg_mlkem768_encaps`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mlkem768_encaps_avx512` if the CPU supports its features (`VG_MLKEM768_ENCAPS_AVX512_FEATURES`); `vg_mlkem768_encaps_avx2` if the CPU supports its features (`VG_MLKEM768_ENCAPS_AVX2_FEATURES`); `vg_mlkem768_encaps_avx` if the CPU supports its features (`VG_MLKEM768_ENCAPS_AVX_FEATURES`); or else `vg_mlkem768_encaps`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -132,7 +244,7 @@ pub unsafe fn vg_mlkem768_encaps_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: * /// /// Contract: `VG.Spec.MlKem.decapsContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (bytes 2304–2335 of `*dk`), but not on anything else of the key, on the ciphertext, or on whether it was rejected. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mlkem768_decaps_avx512` if the CPU supports its features (`VG_MLKEM768_DECAPS_AVX512_FEATURES`); `vg_mlkem768_decaps_avx2` if the CPU supports its features (`VG_MLKEM768_DECAPS_AVX2_FEATURES`); `vg_mlkem768_decaps_avx` if the CPU supports its features (`VG_MLKEM768_DECAPS_AVX_FEATURES`); or else `vg_mlkem768_decaps`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_mlkem768_decaps_avx512` if the CPU supports its features (`VG_MLKEM768_DECAPS_AVX512_FEATURES`); `vg_mlkem768_decaps_avx2` if the CPU supports its features (`VG_MLKEM768_DECAPS_AVX2_FEATURES`); `vg_mlkem768_decaps_avx` if the CPU supports its features (`VG_MLKEM768_DECAPS_AVX_FEATURES`); or else `vg_mlkem768_decaps`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -189,115 +301,3 @@ pub unsafe fn vg_mlkem768_decaps_auto(_dit: verified_garbage_cpu::Dit<'_>, dk: * // either carries the caller's obligations, which are those of every implementation. unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(dk, ct, key, scratch) } } - -/// The ML-KEM-768 encapsulation key check (FIPS 203 §7.2): returns 1 if every 12-bit integer that the first 1152 bytes of `*ek` encode is less than `q` = 3329 (the modulus check), and 0 otherwise. An encapsulation key must pass it before it is given to `vg_mlkem768_encaps`. -/// -/// Contract: `VG.Spec.MlKem.checkEkContract`. Constant time: only the pointer may affect timing. -/// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mlkem768_check_ek_avx2` if the CPU supports its features (`VG_MLKEM768_CHECK_EK_AVX2_FEATURES`); or else `vg_mlkem768_check_ek`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. -/// -/// # Safety -/// -/// * `ek` must be valid for reads of 1184 bytes. -/// * `ek` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mlkem768_check_ek_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1184]) -> u32 { - /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(ek: *const [u8; 1184]) -> u32; - /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only - /// features the CPU has. - #[cold] - fn select() -> Impl { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mlkem768::VG_MLKEM768_CHECK_EK_AVX2_FEATURES) } - || cpu.contains(crate::mlkem768::VG_MLKEM768_CHECK_EK_AVX2_FEATURES) - { - return crate::mlkem768::vg_mlkem768_check_ek_avx2; - } - crate::mlkem768::vg_mlkem768_check_ek - } - /// The first call: chooses the implementation, records it in `CHOSEN` for the calls - /// after it, and runs it. - #[cold] - unsafe extern "sysv64" fn first(ek: *const [u8; 1184]) -> u32 { - let f = select(); - CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); - // SAFETY: the caller's obligations, which are those of every implementation, `f` - // among them, and the CPU has the features `f` needs. - unsafe { f(ek) } - } - /// The function to call: `first`, until it records the implementation it chose. - static CHOSEN: core::sync::atomic::AtomicPtr<()> = - core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); - // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, - // or an implementation `select` returned, which needs only features the CPU has. Calling - // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ek) } -} - -/// ML-KEM-768 encapsulation with given randomness and the hash of the key, `ML-KEM.Encaps_internal(ek, m)` (FIPS 203 Algorithm 17): with the encapsulation key `*ek`, its hash `H(ek)` in `*h` and the randomness `*m`, writes the shared secret key to `*key` and the ciphertext to `*ct`, as `vg_mlkem768_encaps` does, using `*h` instead of hashing `*ek`. Returns 1 on success. Returns 0 if a `SampleNTT` (FIPS 203 Algorithm 7) reaches the bound on its loop's iterations, which is at least 280 (FIPS 203 Appendix B; this happens with probability less than 2^-261): the outputs are then unspecified, and the caller must destroy them and treat the operation as failed. -/// -/// Contract: `VG.Spec.MlKem.encapsHContract`. Constant time but for `ρ`: timing may depend on the pointers and on `ρ` (the last 32 bytes of `*ek`), but not on anything else of the key, on its hash, on the randomness or on the outputs. -/// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_mlkem768_encaps_h_avx512` if the CPU supports its features (`VG_MLKEM768_ENCAPS_H_AVX512_FEATURES`); `vg_mlkem768_encaps_h_avx2` if the CPU supports its features (`VG_MLKEM768_ENCAPS_H_AVX2_FEATURES`); `vg_mlkem768_encaps_h_avx` if the CPU supports its features (`VG_MLKEM768_ENCAPS_H_AVX_FEATURES`); or else `vg_mlkem768_encaps_h`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. -/// -/// # Safety -/// -/// * `ek` must be valid for reads of 1184 bytes. -/// * `h` must be valid for reads of 32 bytes. -/// * `m` must be valid for reads of 32 bytes. -/// * `key` must be valid for reads and writes of 32 bytes. -/// * `ct` must be valid for reads and writes of 1088 bytes. -/// * `scratch` must be valid for reads and writes of 32768 bytes. -/// * `ek` must have passed `vg_mlkem768_check_ek` (FIPS 203 §7.2). -/// * `h` must hold `H(ek)`, the SHA3-256 hash of the bytes of `*ek` (FIPS 203 §4.1). -/// * `m` must be fresh random bytes from an approved RBG (FIPS 203 §3.3). -/// * `scratch` is working space: on return each of its bytes is unchanged or zero. -/// * `key`, `ct` and `scratch` must not overlap each other, `ek`, `h` or `m` (distinct Rust objects never do). -/// * None of `ek`, `h`, `m`, `key`, `ct` and `scratch` may overlap the return address on the stack or the 56 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_mlkem768_encaps_h_auto(_dit: verified_garbage_cpu::Dit<'_>, ek: *const [u8; 1184], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1088], scratch: *mut [u64; 4096]) -> u32 { - /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(ek: *const [u8; 1184], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1088], scratch: *mut [u64; 4096]) -> u32; - /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only - /// features the CPU has. - #[cold] - fn select() -> Impl { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::mlkem768::VG_MLKEM768_ENCAPS_H_AVX512_FEATURES) } - || cpu.contains(crate::mlkem768::VG_MLKEM768_ENCAPS_H_AVX512_FEATURES) - { - return crate::mlkem768::vg_mlkem768_encaps_h_avx512; - } - if const { verified_garbage_cpu::target_contains(crate::mlkem768::VG_MLKEM768_ENCAPS_H_AVX2_FEATURES) } - || cpu.contains(crate::mlkem768::VG_MLKEM768_ENCAPS_H_AVX2_FEATURES) - { - return crate::mlkem768::vg_mlkem768_encaps_h_avx2; - } - if const { verified_garbage_cpu::target_contains(crate::mlkem768::VG_MLKEM768_ENCAPS_H_AVX_FEATURES) } - || cpu.contains(crate::mlkem768::VG_MLKEM768_ENCAPS_H_AVX_FEATURES) - { - return crate::mlkem768::vg_mlkem768_encaps_h_avx; - } - crate::mlkem768::vg_mlkem768_encaps_h - } - /// The first call: chooses the implementation, records it in `CHOSEN` for the calls - /// after it, and runs it. - #[cold] - unsafe extern "sysv64" fn first(ek: *const [u8; 1184], h: *const [u8; 32], m: *const [u8; 32], key: *mut [u8; 32], ct: *mut [u8; 1088], scratch: *mut [u64; 4096]) -> u32 { - let f = select(); - CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); - // SAFETY: the caller's obligations, which are those of every implementation, `f` - // among them, and the CPU has the features `f` needs. - unsafe { f(ek, h, m, key, ct, scratch) } - } - /// The function to call: `first`, until it records the implementation it chose. - static CHOSEN: core::sync::atomic::AtomicPtr<()> = - core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); - // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, - // or an implementation `select` returned, which needs only features the CPU has. Calling - // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ek, h, m, key, ct, scratch) } -} diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha1.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha1.rs index 51aacdfd84e..b8a1b5bc3a2 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha1.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha1.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.pbkdf2Contract` of `VG.Spec.Hmac.sha1I`. Constant time: only the pointers, the lengths and `c` may affect timing, not the password, the salt or the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_pbkdf2_hmac_sha1_shani` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA1_SHANI_FEATURES`); `vg_pbkdf2_hmac_sha1_avx_bmi` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA1_AVX_BMI_FEATURES`); or else `vg_pbkdf2_hmac_sha1`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_pbkdf2_hmac_sha1_shani` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA1_SHANI_FEATURES`); `vg_pbkdf2_hmac_sha1_avx_bmi` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA1_AVX_BMI_FEATURES`); or else `vg_pbkdf2_hmac_sha1`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha224.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha224.rs index 91743021ea7..c404a7a22c5 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha224.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha224.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.pbkdf2Contract` of `VG.Spec.Hmac.sha224I`. Constant time: only the pointers, the lengths and `c` may affect timing, not the password, the salt or the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_pbkdf2_hmac_sha224_shani` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA224_SHANI_FEATURES`); `vg_pbkdf2_hmac_sha224_avx2` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA224_AVX2_FEATURES`); or else `vg_pbkdf2_hmac_sha224`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_pbkdf2_hmac_sha224_shani` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA224_SHANI_FEATURES`); `vg_pbkdf2_hmac_sha224_avx2` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA224_AVX2_FEATURES`); or else `vg_pbkdf2_hmac_sha224`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha256.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha256.rs index 902b000e160..5bb4a9d6dab 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha256.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha256.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.pbkdf2Contract` of `VG.Spec.Hmac.sha256I`. Constant time: only the pointers, the lengths and `c` may affect timing, not the password, the salt or the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_pbkdf2_hmac_sha256_shani` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA256_SHANI_FEATURES`); `vg_pbkdf2_hmac_sha256_avx2` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA256_AVX2_FEATURES`); or else `vg_pbkdf2_hmac_sha256`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_pbkdf2_hmac_sha256_shani` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA256_SHANI_FEATURES`); `vg_pbkdf2_hmac_sha256_avx2` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA256_AVX2_FEATURES`); or else `vg_pbkdf2_hmac_sha256`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha384.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha384.rs index b207058dfc6..4b8e180bca9 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha384.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha384.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.pbkdf2Contract` of `VG.Spec.Hmac.sha384I`. Constant time: only the pointers, the lengths and `c` may affect timing, not the password, the salt or the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_pbkdf2_hmac_sha384_shani` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA384_SHANI_FEATURES`); `vg_pbkdf2_hmac_sha384_avx2` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA384_AVX2_FEATURES`); or else `vg_pbkdf2_hmac_sha384`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_pbkdf2_hmac_sha384_shani` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA384_SHANI_FEATURES`); `vg_pbkdf2_hmac_sha384_avx2` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA384_AVX2_FEATURES`); or else `vg_pbkdf2_hmac_sha384`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha512.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha512.rs index bf45837f39a..a49f0f85d4d 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha512.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha512.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.pbkdf2Contract` of `VG.Spec.Hmac.sha512I`. Constant time: only the pointers, the lengths and `c` may affect timing, not the password, the salt or the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_pbkdf2_hmac_sha512_shani` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA512_SHANI_FEATURES`); `vg_pbkdf2_hmac_sha512_avx2` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA512_AVX2_FEATURES`); or else `vg_pbkdf2_hmac_sha512`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_pbkdf2_hmac_sha512_shani` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA512_SHANI_FEATURES`); `vg_pbkdf2_hmac_sha512_avx2` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA512_AVX2_FEATURES`); or else `vg_pbkdf2_hmac_sha512`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha512_224.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha512_224.rs index 5413e86bfd4..018bb3458ab 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha512_224.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha512_224.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.pbkdf2Contract` of `VG.Spec.Hmac.sha512_224I`. Constant time: only the pointers, the lengths and `c` may affect timing, not the password, the salt or the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_pbkdf2_hmac_sha512_224_shani` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA512_224_SHANI_FEATURES`); `vg_pbkdf2_hmac_sha512_224_avx2` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA512_224_AVX2_FEATURES`); or else `vg_pbkdf2_hmac_sha512_224`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_pbkdf2_hmac_sha512_224_shani` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA512_224_SHANI_FEATURES`); `vg_pbkdf2_hmac_sha512_224_avx2` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA512_224_AVX2_FEATURES`); or else `vg_pbkdf2_hmac_sha512_224`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha512_256.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha512_256.rs index a2f2c6f8d1a..d81dc285508 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha512_256.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/pbkdf2_sha512_256.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Hmac.Instance.pbkdf2Contract` of `VG.Spec.Hmac.sha512_256I`. Constant time: only the pointers, the lengths and `c` may affect timing, not the password, the salt or the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_pbkdf2_hmac_sha512_256_shani` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA512_256_SHANI_FEATURES`); `vg_pbkdf2_hmac_sha512_256_avx2` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA512_256_AVX2_FEATURES`); or else `vg_pbkdf2_hmac_sha512_256`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_pbkdf2_hmac_sha512_256_shani` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA512_256_SHANI_FEATURES`); `vg_pbkdf2_hmac_sha512_256_avx2` if the CPU supports its features (`VG_PBKDF2_HMAC_SHA512_256_AVX2_FEATURES`); or else `vg_pbkdf2_hmac_sha512_256`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/poly1305.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/poly1305.rs index 227409af0dc..96e382f6e9d 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/poly1305.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/poly1305.rs @@ -21,11 +21,31 @@ pub unsafe fn vg_poly1305_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: unsafe { crate::poly1305::vg_poly1305_init(state, key) } } +/// Finishes a Poly1305 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴) under a key, writes the tag of that message, under that key, to `*out`. +/// +/// Contract: `VG.Spec.Poly1305.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// +/// It runs `vg_poly1305_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 128 bytes. +/// * `out` must be valid for reads and writes of 16 bytes. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_poly1305_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 16], count: u64, out: *mut [u8; 16]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::poly1305::vg_poly1305_finalize(state, count, out) } +} + /// Absorbs data into a Poly1305 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴) under a key, it then represents that message followed by the `len` bytes at `data`, under the same key. /// /// Contract: `VG.Spec.Poly1305.updateContract`. The streaming state is the accumulator, the key and the message's last bytes that do not fill a block (`VG.Spec.Poly1305.Buffered`). Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_poly1305_update_ifma` if the CPU supports its features (`VG_POLY1305_UPDATE_IFMA_FEATURES`); `vg_poly1305_update_avx512` if the CPU supports its features (`VG_POLY1305_UPDATE_AVX512_FEATURES`); `vg_poly1305_update_avx2` if the CPU supports its features (`VG_POLY1305_UPDATE_AVX2_FEATURES`); or else `vg_poly1305_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_poly1305_update_ifma` if the CPU supports its features (`VG_POLY1305_UPDATE_IFMA_FEATURES`); `vg_poly1305_update_avx512` if the CPU supports its features (`VG_POLY1305_UPDATE_AVX512_FEATURES`); `vg_poly1305_update_avx2` if the CPU supports its features (`VG_POLY1305_UPDATE_AVX2_FEATURES`); or else `vg_poly1305_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -78,23 +98,3 @@ pub unsafe fn vg_poly1305_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state // either carries the caller's obligations, which are those of every implementation. unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, data, len) } } - -/// Finishes a Poly1305 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴) under a key, writes the tag of that message, under that key, to `*out`. -/// -/// Contract: `VG.Spec.Poly1305.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. -/// -/// It runs `vg_poly1305_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 128 bytes. -/// * `out` must be valid for reads and writes of 16 bytes. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the return address on the stack or the 136 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_poly1305_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 16], count: u64, out: *mut [u8; 16]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::poly1305::vg_poly1305_finalize(state, count, out) } -} diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/rsa.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/rsa.rs index 92bc17368b2..46af3d1c7d6 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/rsa.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/rsa.rs @@ -2,74 +2,11 @@ //! The dispatchers of the verified `rsa` functions for `x86_64` //! (`crate::rsa`, `VG.Rust.dispatcher`). -/// Checks an RSA private key `(n, e, d, p, q, dP, dQ, qInv)` exactly as BoringSSL's `RSA_check_key` does. With the modulus `n` (`n_len` bytes), the public exponent `e` (`e_len` bytes), the private exponent `d` (`d_len` bytes), `p`, `dp` and `qinv` (`p_len` bytes) and `q` and `dq` (`q_len` bytes), all most significant first, returns 1 if `n` is odd, from 512 to 8192 bits and its first byte not zero, `e` is odd and from 3 to `2^33 - 1`, `d < n`, `p < n`, `q < n`, `p q = n`, `d e ≡ 1 (mod p - 1)`, `d e ≡ 1 (mod q - 1)`, `dP < p - 1`, `e dP ≡ 1 (mod p - 1)`, `dQ < q - 1`, `e dQ ≡ 1 (mod q - 1)`, `qInv < p` and `q qInv ≡ 1 (mod p)`; and 0 otherwise. Like `RSA_check_key`, it does not check that `p` and `q` are prime; if they are, `vg_rsa_private_checked` never returns its internal error for a key it accepts. -/// -/// Contract: `VG.Spec.Rsa.checkKeyContract`. Constant time but for the public key: timing may depend on the pointers, the lengths and the contents of `n` and `e`, not on the private key. -/// -/// It runs `vg_rsa_check_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `n` must be valid for reads of `n_len` bytes. -/// * `e` must be valid for reads of `e_len` bytes. -/// * `d` must be valid for reads of `d_len` bytes. -/// * `p` must be valid for reads of `p_len` bytes. -/// * `q` must be valid for reads of `q_len` bytes. -/// * `dp` must be valid for reads of `dp_len` bytes. -/// * `dq` must be valid for reads of `dq_len` bytes. -/// * `qinv` must be valid for reads of `qinv_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `n_len` must be in 64..=1024. -/// * `e_len` and `d_len` must be in 1..=`n_len`. -/// * `p_len` and `q_len` must be in 1..`n_len`. -/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. -/// * `scratch_len` must be at least `16 * n_len + 32`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `scratch` must not overlap `n`, `e`, `d`, `p`, `q`, `dp`, `dq`, `qinv` or the arguments on the stack (distinct Rust objects never do). -/// * None of `n`, `e`, `d`, `p`, `q`, `dp`, `dq`, `qinv` and `scratch` may overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_rsa_check_key_auto(_dit: verified_garbage_cpu::Dit<'_>, n: *const u8, n_len: usize, e: *const u8, e_len: usize, d: *const u8, d_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa::vg_rsa_check_key(n, n_len, e, e_len, d, d_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, scratch, scratch_len) } -} - -/// The CRT values of an RSA private key `(p, q, d)` (SP 800-56B Rev. 2 §6.2.2's prime-factor format), which bring it to the CRT format `(p, q, dP, dQ, qInv)` that `vg_rsa_private_checked` takes: `dP = d mod (p - 1)`, `dQ = d mod (q - 1)` and `qInv = q⁻¹ mod p` (§6.2.2's CRT format). With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), `p` (`p_len` bytes), `q` (`q_len` bytes) and `d` (`d_len` bytes), all most significant first, writes `dP` to `dp` and `qInv` to `qinv` (`p_len` bytes each) and `dQ` to `dq` (`q_len` bytes), most significant first, and returns 1; or writes zeros to all three and returns 0 if `n` is not such a modulus, `p q ≠ n`, or `q` has no inverse modulo `p`. Nothing checks that `p` and `q` are prime or that `d` is the key's private exponent. -/// -/// Contract: `VG.Spec.Rsa.crtValuesContract`. Constant time but for the modulus: timing may depend on the pointers, the lengths and the contents of `n`, not on the private key. -/// -/// It runs `vg_rsa_crt_values`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `dp` must be valid for reads and writes of `dp_len` bytes. -/// * `dq` must be valid for reads and writes of `dq_len` bytes. -/// * `qinv` must be valid for reads and writes of `qinv_len` bytes. -/// * `n` must be valid for reads of `n_len` bytes. -/// * `p` must be valid for reads of `p_len` bytes. -/// * `q` must be valid for reads of `q_len` bytes. -/// * `d` must be valid for reads of `d_len` bytes. -/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. -/// * `n_len` must be in 64..=1024. -/// * `p_len` and `q_len` must be in 1..`n_len`. -/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. -/// * `d_len` must be in 1..=`n_len`. -/// * `scratch_len` must be at least `16 * n_len + 32`. -/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `dp`, `dq`, `qinv` and `scratch` must not overlap each other, `n`, `p`, `q`, `d` or the arguments on the stack (distinct Rust objects never do). -/// * None of `dp`, `dq`, `qinv`, `n`, `p`, `q`, `d` and `scratch` may overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_rsa_crt_values_auto(_dit: verified_garbage_cpu::Dit<'_>, dp: *mut u8, dp_len: usize, dq: *mut u8, dq_len: usize, qinv: *mut u8, qinv_len: usize, n: *const u8, n_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, d: *const u8, d_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa::vg_rsa_crt_values(dp, dp_len, dq, dq_len, qinv, qinv_len, n, n_len, p, p_len, q, q_len, d, d_len, scratch, scratch_len) } -} - /// The values of an RSA modulus that `vg_rsa_public_precomputed_checked` takes, so that they are computed once per public key rather than once per operation. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero) of `w = ⌈n_len / 8⌉` words of 64 bits, writes `n` and then `R² mod n` for `R = 2^(64 w)` to `pre` (`w` words each, least significant first) and returns 1; or writes zeros and returns 0 if `n` is not such a modulus. /// /// Contract: `VG.Spec.Rsa.publicPrecomputeContract`. Timing may depend on the pointers, the lengths and the contents of `n`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_public_precompute_adx` if the CPU supports its features (`VG_RSA_PUBLIC_PRECOMPUTE_ADX_FEATURES`); or else `vg_rsa_public_precompute`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_public_precompute_adx` if the CPU supports its features (`VG_RSA_PUBLIC_PRECOMPUTE_ADX_FEATURES`); or else `vg_rsa_public_precompute`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -118,6 +55,37 @@ pub unsafe fn vg_rsa_public_precompute_auto(_dit: verified_garbage_cpu::Dit<'_>, unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(pre, pre_len, n, n_len, scratch, scratch_len) } } +/// The CRT values of an RSA private key `(p, q, d)` (SP 800-56B Rev. 2 §6.2.2's prime-factor format), which bring it to the CRT format `(p, q, dP, dQ, qInv)` that `vg_rsa_private_checked` takes: `dP = d mod (p - 1)`, `dQ = d mod (q - 1)` and `qInv = q⁻¹ mod p` (§6.2.2's CRT format). With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), `p` (`p_len` bytes), `q` (`q_len` bytes) and `d` (`d_len` bytes), all most significant first, writes `dP` to `dp` and `qInv` to `qinv` (`p_len` bytes each) and `dQ` to `dq` (`q_len` bytes), most significant first, and returns 1; or writes zeros to all three and returns 0 if `n` is not such a modulus, `p q ≠ n`, or `q` has no inverse modulo `p`. Nothing checks that `p` and `q` are prime or that `d` is the key's private exponent. +/// +/// Contract: `VG.Spec.Rsa.crtValuesContract`. Constant time but for the modulus: timing may depend on the pointers, the lengths and the contents of `n`, not on the private key. +/// +/// It runs `vg_rsa_crt_values`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `dp` must be valid for reads and writes of `dp_len` bytes. +/// * `dq` must be valid for reads and writes of `dq_len` bytes. +/// * `qinv` must be valid for reads and writes of `qinv_len` bytes. +/// * `n` must be valid for reads of `n_len` bytes. +/// * `p` must be valid for reads of `p_len` bytes. +/// * `q` must be valid for reads of `q_len` bytes. +/// * `d` must be valid for reads of `d_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `n_len` must be in 64..=1024. +/// * `p_len` and `q_len` must be in 1..`n_len`. +/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. +/// * `d_len` must be in 1..=`n_len`. +/// * `scratch_len` must be at least `16 * n_len + 32`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `dp`, `dq`, `qinv` and `scratch` must not overlap each other, `n`, `p`, `q`, `d` or the arguments on the stack (distinct Rust objects never do). +/// * None of `dp`, `dq`, `qinv`, `n`, `p`, `q`, `d` and `scratch` may overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_rsa_crt_values_auto(_dit: verified_garbage_cpu::Dit<'_>, dp: *mut u8, dp_len: usize, dq: *mut u8, dq_len: usize, qinv: *mut u8, qinv_len: usize, n: *const u8, n_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, d: *const u8, d_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::rsa::vg_rsa_crt_values(dp, dp_len, dq, dq_len, qinv, qinv_len, n, n_len, p, p_len, q, q_len, d, d_len, scratch, scratch_len) } +} + /// The prime factors `p > q` of the modulus of an RSA private key `(n, e, d)` (SP 800-56B Rev. 2 §6.2.2's basic format), which bring it to the prime-factor format `(p, q, d)` that `vg_rsa_crt_values` takes, recovered by SP 800-56B Rev. 2 Appendix C.2's deterministic method. With the modulus `n` (`n_len` bytes, most significant first, odd, from 512 to 8192 bits, its first byte not zero), the public exponent `e` (`e_len` bytes) and the private exponent `d` (`d_len` bytes), both most significant first, writes `p` and `q` (`n_len` bytes each, most significant first) and returns 1; or writes zeros to both and returns 0 if `n` is not such a modulus or the method fails. It finds the factors of a valid two-prime key whenever `K (p + q − 1) ≤ n` for `a = K φ(n)` (Appendix C.2's step 1): with the least private exponent, whenever the smaller prime exceeds `e²`, and in practice for every key whose primes are not tiny. /// /// Contract: `VG.Spec.Rsa.recoverPrimesDetContract`. Constant time but for the public key: timing may depend on the pointers, the lengths and the contents of `n` and `e`, not on `d`, the factors, or whether they were found. @@ -146,3 +114,35 @@ pub unsafe fn vg_rsa_recover_primes_det_auto(_dit: verified_garbage_cpu::Dit<'_> // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. unsafe { crate::rsa::vg_rsa_recover_primes_det(p, p_len, q, q_len, n, n_len, e, e_len, d, d_len, scratch, scratch_len) } } + +/// Checks an RSA private key `(n, e, d, p, q, dP, dQ, qInv)` exactly as BoringSSL's `RSA_check_key` does. With the modulus `n` (`n_len` bytes), the public exponent `e` (`e_len` bytes), the private exponent `d` (`d_len` bytes), `p`, `dp` and `qinv` (`p_len` bytes) and `q` and `dq` (`q_len` bytes), all most significant first, returns 1 if `n` is odd, from 512 to 8192 bits and its first byte not zero, `e` is odd and from 3 to `2^33 - 1`, `d < n`, `p < n`, `q < n`, `p q = n`, `d e ≡ 1 (mod p - 1)`, `d e ≡ 1 (mod q - 1)`, `dP < p - 1`, `e dP ≡ 1 (mod p - 1)`, `dQ < q - 1`, `e dQ ≡ 1 (mod q - 1)`, `qInv < p` and `q qInv ≡ 1 (mod p)`; and 0 otherwise. Like `RSA_check_key`, it does not check that `p` and `q` are prime; if they are, `vg_rsa_private_checked` never returns its internal error for a key it accepts. +/// +/// Contract: `VG.Spec.Rsa.checkKeyContract`. Constant time but for the public key: timing may depend on the pointers, the lengths and the contents of `n` and `e`, not on the private key. +/// +/// It runs `vg_rsa_check_key`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `n` must be valid for reads of `n_len` bytes. +/// * `e` must be valid for reads of `e_len` bytes. +/// * `d` must be valid for reads of `d_len` bytes. +/// * `p` must be valid for reads of `p_len` bytes. +/// * `q` must be valid for reads of `q_len` bytes. +/// * `dp` must be valid for reads of `dp_len` bytes. +/// * `dq` must be valid for reads of `dq_len` bytes. +/// * `qinv` must be valid for reads of `qinv_len` bytes. +/// * `scratch` must be valid for reads and writes of `8 * scratch_len` bytes. +/// * `n_len` must be in 64..=1024. +/// * `e_len` and `d_len` must be in 1..=`n_len`. +/// * `p_len` and `q_len` must be in 1..`n_len`. +/// * `dp_len` and `qinv_len` must be `p_len`, and `dq_len` must be `q_len`. +/// * `scratch_len` must be at least `16 * n_len + 32`. +/// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. +/// * `scratch` must not overlap `n`, `e`, `d`, `p`, `q`, `dp`, `dq`, `qinv` or the arguments on the stack (distinct Rust objects never do). +/// * None of `n`, `e`, `d`, `p`, `q`, `dp`, `dq`, `qinv` and `scratch` may overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_rsa_check_key_auto(_dit: verified_garbage_cpu::Dit<'_>, n: *const u8, n_len: usize, e: *const u8, e_len: usize, d: *const u8, d_len: usize, p: *const u8, p_len: usize, q: *const u8, q_len: usize, dp: *const u8, dp_len: usize, dq: *const u8, dq_len: usize, qinv: *const u8, qinv_len: usize, scratch: *mut u64, scratch_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::rsa::vg_rsa_check_key(n, n_len, e, e_len, d, d_len, p, p_len, q, q_len, dp, dp_len, dq, dq_len, qinv, qinv_len, scratch, scratch_len) } +} diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/rsa_keygen.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/rsa_keygen.rs index 423dc5ea5ab..6c5bfa0a182 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/rsa_keygen.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/rsa_keygen.rs @@ -2,11 +2,30 @@ //! The dispatchers of the verified `rsa_keygen` functions for `x86_64` //! (`crate::rsa_keygen`, `VG.Rust.dispatcher`). +/// Start generating an RSA key, as BoringSSL's `RSA_generate_key_ex` (FIPS 186-5 Appendix A.1.3), with a modulus of `bits` bits rounded down to a multiple of 128, and the public exponent `e` (`e_len` octets, most significant first, with any number of leading zeros). Returns 0 if `e` is not odd from 3 to `2^32 - 1`, or 2 if `bits` is above 8192 or, rounded down, below 512, and writes zeros to `state`. Otherwise returns 1 and writes the search's seven public words to `state`: the rejected candidates the first prime's search allows, the primes' length in octets (`nlen / 16`), the entropy budget, the length of the first random tape to give `vg_rsa_generate`, the phase (0), the generations allowed (4), and the exponent's eight octets, most significant first. +/// +/// Contract: `VG.Spec.RsaKeyGen.keyInitContract`. Timing may depend on `bits` and `e`. +/// +/// It runs `vg_rsa_generate_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 56 bytes. +/// * `e` must be valid for reads of `e_len` bytes. +/// * `state` must not overlap `e` (distinct Rust objects never do). +/// * Neither `state` nor `e` may overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_rsa_generate_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 7], bits: usize, e: *const u8, e_len: usize) -> u32 { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::rsa_keygen::vg_rsa_generate_init(state, bits, e, e_len) } +} + /// Generate an RSA key, as BoringSSL's `RSA_generate_key_ex` (FIPS 186-5 Appendix A.1.3), over the random tape `rand`, from the state `state` that `vg_rsa_generate_init` started: the search for `p`, then for `q` not too close to `p`, each over candidates of `p_len` octets with their top two bits and bottom bit set, rejected by trial division, a common factor of `p − 1` and `e` or Miller–Rabin, up to BoringSSL's limit of rejected candidates; then the key, `d = e⁻¹ mod lcm(p − 1, q − 1)`, `dP`, `dQ` and `qInv`, with `p` the larger prime, checked as BoringSSL's `RSA_check_key` checks it, drawing both primes again if `d` is too small; a generation whose prime's search is exhausted is started again, up to four in all. Returns 1 and writes the key to `key` (`key_len` is `9 p_len`, for the primes' length `p_len = state[1]`): `n` and `d` (`2 p_len` octets each), then `p`, `q`, `dP`, `dQ` and `qInv` (`p_len` each), all most significant first; 2 if the key failed a check; 3 if every generation was exhausted; or 0 if the tape ran out inside a candidate. Writes the octets of `rand` it consumed to `used`, and the state for the next call to `state`, whose fourth word is, for 0, the length of the next tape, more than the octets of `rand` after the first `used` and at most the entropy budget: keep those octets, in order, and append fresh random octets up to that length. For 0, `key` holds the search's `p` once it is found, which the next call reads: pass it back unchanged. `key` is otherwise zeros but for 1. /// /// Contract: `VG.Spec.RsaKeyGen.keySearchContract`. Timing may depend on the pointers, the lengths, `state`, the status and consumption it returns, and, for each prime's search, its rejected candidates and the octets its accepted candidate's tests read, and each key's status. Not on the primes or the key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_generate_ifma` if the CPU supports its features (`VG_RSA_GENERATE_IFMA_FEATURES`); `vg_rsa_generate_adx` if the CPU supports its features (`VG_RSA_GENERATE_ADX_FEATURES`); or else `vg_rsa_generate`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_generate_ifma` if the CPU supports its features (`VG_RSA_GENERATE_IFMA_FEATURES`); `vg_rsa_generate_adx` if the CPU supports its features (`VG_RSA_GENERATE_ADX_FEATURES`); or else `vg_rsa_generate`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -62,22 +81,3 @@ pub unsafe fn vg_rsa_generate_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *mu // either carries the caller's obligations, which are those of every implementation. unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(key, key_len, used, rand, rand_len, scratch, scratch_len, state) } } - -/// Start generating an RSA key, as BoringSSL's `RSA_generate_key_ex` (FIPS 186-5 Appendix A.1.3), with a modulus of `bits` bits rounded down to a multiple of 128, and the public exponent `e` (`e_len` octets, most significant first, with any number of leading zeros). Returns 0 if `e` is not odd from 3 to `2^32 - 1`, or 2 if `bits` is above 8192 or, rounded down, below 512, and writes zeros to `state`. Otherwise returns 1 and writes the search's seven public words to `state`: the rejected candidates the first prime's search allows, the primes' length in octets (`nlen / 16`), the entropy budget, the length of the first random tape to give `vg_rsa_generate`, the phase (0), the generations allowed (4), and the exponent's eight octets, most significant first. -/// -/// Contract: `VG.Spec.RsaKeyGen.keyInitContract`. Timing may depend on `bits` and `e`. -/// -/// It runs `vg_rsa_generate_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 56 bytes. -/// * `e` must be valid for reads of `e_len` bytes. -/// * `state` must not overlap `e` (distinct Rust objects never do). -/// * Neither `state` nor `e` may overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_rsa_generate_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 7], bits: usize, e: *const u8, e_len: usize) -> u32 { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::rsa_keygen::vg_rsa_generate_init(state, bits, e, e_len) } -} diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/rsa_oaep.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/rsa_oaep.rs index 0b49b1657a6..84b804b498b 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/rsa_oaep.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/rsa_oaep.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.RsaOaep.decryptTablesContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, the contents of `n` and `e`, and which tables `hash` and `mgf1` are, not on the private key, the label, the ciphertext, whether its decoding is valid or the message's length. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_oaep_decrypt_crt_ifma` if the CPU supports its features (`VG_RSA_OAEP_DECRYPT_CRT_IFMA_FEATURES`); `vg_rsa_oaep_decrypt_crt_adx` if the CPU supports its features (`VG_RSA_OAEP_DECRYPT_CRT_ADX_FEATURES`); or else `vg_rsa_oaep_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_oaep_decrypt_crt_ifma` if the CPU supports its features (`VG_RSA_OAEP_DECRYPT_CRT_IFMA_FEATURES`); `vg_rsa_oaep_decrypt_crt_adx` if the CPU supports its features (`VG_RSA_OAEP_DECRYPT_CRT_ADX_FEATURES`); or else `vg_rsa_oaep_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -82,7 +82,7 @@ pub unsafe fn vg_rsa_oaep_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, out: /// /// Contract: `VG.Spec.RsaOaep.encryptTablesContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, the contents of `n`, `e` and `pre`, and which tables `hash` and `mgf1` are, not on the message, the label or the seed. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_oaep_encrypt_precomputed_rsa_ifma` if the CPU supports its features (`VG_RSA_OAEP_ENCRYPT_PRECOMPUTED_RSA_IFMA_FEATURES`); `vg_rsa_oaep_encrypt_precomputed_rsa_adx` if the CPU supports its features (`VG_RSA_OAEP_ENCRYPT_PRECOMPUTED_RSA_ADX_FEATURES`); or else `vg_rsa_oaep_encrypt_precomputed`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_oaep_encrypt_precomputed_rsa_ifma` if the CPU supports its features (`VG_RSA_OAEP_ENCRYPT_PRECOMPUTED_RSA_IFMA_FEATURES`); `vg_rsa_oaep_encrypt_precomputed_rsa_adx` if the CPU supports its features (`VG_RSA_OAEP_ENCRYPT_PRECOMPUTED_RSA_ADX_FEATURES`); or else `vg_rsa_oaep_encrypt_precomputed`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/rsa_pkcs1_enc.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/rsa_pkcs1_enc.rs index 12ab4341a96..194ec274442 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/rsa_pkcs1_enc.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/rsa_pkcs1_enc.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.RsaPkcs1Enc.decryptContract`. Constant time but for the public key: timing may depend on the pointers, the lengths and the contents of `n` and `e`, not on the ciphertext, the private key, the validity of the padding, the length of the message or which message is returned. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_pkcs1_decrypt_sha256_shani_crt_ifma` if the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_SHA256_SHANI_CRT_IFMA_FEATURES`); `vg_rsa_pkcs1_decrypt_sha256_shani_crt_adx` if the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_SHA256_SHANI_CRT_ADX_FEATURES`); `vg_rsa_pkcs1_decrypt_sha256_shani` if the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_SHA256_SHANI_FEATURES`); `vg_rsa_pkcs1_decrypt_sha256_avx2_crt_ifma` if the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_SHA256_AVX2_CRT_IFMA_FEATURES`); `vg_rsa_pkcs1_decrypt_sha256_avx2_crt_adx` if the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_SHA256_AVX2_CRT_ADX_FEATURES`); `vg_rsa_pkcs1_decrypt_sha256_avx2` if the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_SHA256_AVX2_FEATURES`); `vg_rsa_pkcs1_decrypt_crt_ifma` if the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_CRT_IFMA_FEATURES`); `vg_rsa_pkcs1_decrypt_crt_adx` if the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_CRT_ADX_FEATURES`); or else `vg_rsa_pkcs1_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_pkcs1_decrypt_sha256_shani_crt_ifma` if the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_SHA256_SHANI_CRT_IFMA_FEATURES`); `vg_rsa_pkcs1_decrypt_sha256_shani_crt_adx` if the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_SHA256_SHANI_CRT_ADX_FEATURES`); `vg_rsa_pkcs1_decrypt_sha256_shani` if the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_SHA256_SHANI_FEATURES`); `vg_rsa_pkcs1_decrypt_sha256_avx2_crt_ifma` if the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_SHA256_AVX2_CRT_IFMA_FEATURES`); `vg_rsa_pkcs1_decrypt_sha256_avx2_crt_adx` if the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_SHA256_AVX2_CRT_ADX_FEATURES`); `vg_rsa_pkcs1_decrypt_sha256_avx2` if the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_SHA256_AVX2_FEATURES`); `vg_rsa_pkcs1_decrypt_crt_ifma` if the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_CRT_IFMA_FEATURES`); `vg_rsa_pkcs1_decrypt_crt_adx` if the CPU supports its features (`VG_RSA_PKCS1_DECRYPT_CRT_ADX_FEATURES`); or else `vg_rsa_pkcs1_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -106,7 +106,7 @@ pub unsafe fn vg_rsa_pkcs1_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, out /// /// Contract: `VG.Spec.RsaPkcs1Enc.encryptRandomContract`. Constant time but for the public key and the tape's zero bytes: timing may depend on the pointers, the lengths, the contents of `n`, `e` and `pre`, and which bytes of `rand` are zero, not on the message or the other bytes of `rand`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_pkcs1_encrypt_random_ifma` if the CPU supports its features (`VG_RSA_PKCS1_ENCRYPT_RANDOM_IFMA_FEATURES`); `vg_rsa_pkcs1_encrypt_random_adx` if the CPU supports its features (`VG_RSA_PKCS1_ENCRYPT_RANDOM_ADX_FEATURES`); or else `vg_rsa_pkcs1_encrypt_random`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_pkcs1_encrypt_random_ifma` if the CPU supports its features (`VG_RSA_PKCS1_ENCRYPT_RANDOM_IFMA_FEATURES`); `vg_rsa_pkcs1_encrypt_random_adx` if the CPU supports its features (`VG_RSA_PKCS1_ENCRYPT_RANDOM_ADX_FEATURES`); or else `vg_rsa_pkcs1_encrypt_random`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/rsa_pkcs1_sig.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/rsa_pkcs1_sig.rs index d80570b6cba..2774dc45044 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/rsa_pkcs1_sig.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/rsa_pkcs1_sig.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.RsaPkcs1Sig.signContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, `hash` and the contents of `n` and `e`, not on `digest` or the private key. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_pkcs1_sign_ifma` if the CPU supports its features (`VG_RSA_PKCS1_SIGN_IFMA_FEATURES`); `vg_rsa_pkcs1_sign_adx` if the CPU supports its features (`VG_RSA_PKCS1_SIGN_ADX_FEATURES`); or else `vg_rsa_pkcs1_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_pkcs1_sign_ifma` if the CPU supports its features (`VG_RSA_PKCS1_SIGN_IFMA_FEATURES`); `vg_rsa_pkcs1_sign_adx` if the CPU supports its features (`VG_RSA_PKCS1_SIGN_ADX_FEATURES`); or else `vg_rsa_pkcs1_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -78,7 +78,7 @@ pub unsafe fn vg_rsa_pkcs1_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: * /// /// Contract: `VG.Spec.RsaPkcs1Sig.verifyPrecomputedContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_pkcs1_verify_precomputed_ifma` if the CPU supports its features (`VG_RSA_PKCS1_VERIFY_PRECOMPUTED_IFMA_FEATURES`); `vg_rsa_pkcs1_verify_precomputed_adx` if the CPU supports its features (`VG_RSA_PKCS1_VERIFY_PRECOMPUTED_ADX_FEATURES`); or else `vg_rsa_pkcs1_verify_precomputed`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_pkcs1_verify_precomputed_ifma` if the CPU supports its features (`VG_RSA_PKCS1_VERIFY_PRECOMPUTED_IFMA_FEATURES`); `vg_rsa_pkcs1_verify_precomputed_adx` if the CPU supports its features (`VG_RSA_PKCS1_VERIFY_PRECOMPUTED_ADX_FEATURES`); or else `vg_rsa_pkcs1_verify_precomputed`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -145,7 +145,7 @@ pub unsafe fn vg_rsa_pkcs1_verify_precomputed_auto(_dit: verified_garbage_cpu::D /// /// Contract: `VG.Spec.RsaPkcs1Sig.recoverPrecomputedContract`. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_pkcs1_recover_precomputed_ifma` if the CPU supports its features (`VG_RSA_PKCS1_RECOVER_PRECOMPUTED_IFMA_FEATURES`); `vg_rsa_pkcs1_recover_precomputed_adx` if the CPU supports its features (`VG_RSA_PKCS1_RECOVER_PRECOMPUTED_ADX_FEATURES`); or else `vg_rsa_pkcs1_recover_precomputed`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_pkcs1_recover_precomputed_ifma` if the CPU supports its features (`VG_RSA_PKCS1_RECOVER_PRECOMPUTED_IFMA_FEATURES`); `vg_rsa_pkcs1_recover_precomputed_adx` if the CPU supports its features (`VG_RSA_PKCS1_RECOVER_PRECOMPUTED_ADX_FEATURES`); or else `vg_rsa_pkcs1_recover_precomputed`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/rsa_pss.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/rsa_pss.rs index 684cecfbf4b..0b19040ab53 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/rsa_pss.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/rsa_pss.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.RsaPss.signTableContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, the contents of `n` and `e`, and which tables `hash` and `mgf1` are, not on the private key, the digest or the salt. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_pss_sign_crt_ifma` if the CPU supports its features (`VG_RSA_PSS_SIGN_CRT_IFMA_FEATURES`); `vg_rsa_pss_sign_crt_adx` if the CPU supports its features (`VG_RSA_PSS_SIGN_CRT_ADX_FEATURES`); or else `vg_rsa_pss_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_pss_sign_crt_ifma` if the CPU supports its features (`VG_RSA_PSS_SIGN_CRT_IFMA_FEATURES`); `vg_rsa_pss_sign_crt_adx` if the CPU supports its features (`VG_RSA_PSS_SIGN_CRT_ADX_FEATURES`); or else `vg_rsa_pss_sign`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -82,7 +82,7 @@ pub unsafe fn vg_rsa_pss_sign_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mu /// /// Contract: `VG.Spec.RsaPss.verifyPrecomputedTableContract`. Constant time but for the public key: timing may depend on the pointers, the lengths, `salt_len`, `any_salt_len`, the contents of `n`, `e` and `pre`, and which tables `hash` and `mgf1` are, not on the digest, the signature or anything computed from them but the result. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_rsa_pss_verify_precomputed_rsa_ifma` if the CPU supports its features (`VG_RSA_PSS_VERIFY_PRECOMPUTED_RSA_IFMA_FEATURES`); `vg_rsa_pss_verify_precomputed_rsa_adx` if the CPU supports its features (`VG_RSA_PSS_VERIFY_PRECOMPUTED_RSA_ADX_FEATURES`); or else `vg_rsa_pss_verify_precomputed`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_rsa_pss_verify_precomputed_rsa_ifma` if the CPU supports its features (`VG_RSA_PSS_VERIFY_PRECOMPUTED_RSA_IFMA_FEATURES`); `vg_rsa_pss_verify_precomputed_rsa_adx` if the CPU supports its features (`VG_RSA_PSS_VERIFY_PRECOMPUTED_RSA_ADX_FEATURES`); or else `vg_rsa_pss_verify_precomputed`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/scrypt.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/scrypt.rs index 0c17b5bdb2a..52e1fc53f95 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/scrypt.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/scrypt.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Scrypt.scryptContract`. Not constant time in the indices: timing may depend on the pointers, the lengths, `r`, `N`, `p` and the indices `j` of step 3 of each scryptROMix, which are derived from the password and the salt and so leak information about them (as in every scrypt that indexes `V` directly), but on nothing else. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_scrypt_shani` if the CPU supports its features (`VG_SCRYPT_SHANI_FEATURES`); `vg_scrypt_avx2` if the CPU supports its features (`VG_SCRYPT_AVX2_FEATURES`); or else `vg_scrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_scrypt_shani` if the CPU supports its features (`VG_SCRYPT_SHANI_FEATURES`); `vg_scrypt_avx2` if the CPU supports its features (`VG_SCRYPT_AVX2_FEATURES`); or else `vg_scrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/sha1.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/sha1.rs index fc200126a56..bd31495f73b 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/sha1.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/sha1.rs @@ -23,7 +23,7 @@ pub unsafe fn vg_sha1_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut /// /// Contract: `VG.Spec.Sha1.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha1_update_shani` if the CPU supports its features (`VG_SHA1_UPDATE_SHANI_FEATURES`); `vg_sha1_update_avx_bmi` if the CPU supports its features (`VG_SHA1_UPDATE_AVX_BMI_FEATURES`); or else `vg_sha1_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha1_update_shani` if the CPU supports its features (`VG_SHA1_UPDATE_SHANI_FEATURES`); `vg_sha1_update_avx_bmi` if the CPU supports its features (`VG_SHA1_UPDATE_AVX_BMI_FEATURES`); or else `vg_sha1_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -72,49 +72,50 @@ pub unsafe fn vg_sha1_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *m unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, data, len) } } -/// Computes the SHA-1 digest of the `len` bytes at `data` (20 bytes, `VG.Spec.Sha1.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-1 computation with that message. +/// Finishes a SHA-1 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), writes the SHA-1 digest of that message to `*out`. /// -/// Contract: `VG.Spec.Sha1.digestContract`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha1.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha1_digest_shani` if the CPU supports its features (`VG_SHA1_DIGEST_SHANI_FEATURES`); `vg_sha1_digest_avx_bmi` if the CPU supports its features (`VG_SHA1_DIGEST_AVX_BMI_FEATURES`); or else `vg_sha1_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha1_finalize_shani` if the CPU supports its features (`VG_SHA1_FINALIZE_SHANI_FEATURES`); `vg_sha1_finalize_avx_bmi` if the CPU supports its features (`VG_SHA1_FINALIZE_AVX_BMI_FEATURES`); or else `vg_sha1_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `data` must be valid for reads of `len` bytes. +/// * `state` must be valid for reads and writes of 84 bytes. /// * `out` must be valid for reads and writes of 20 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the return address on the stack or the 288 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the return address on the stack or the 176 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha1_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 20]) { +pub unsafe fn vg_sha1_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 84], count: u64, out: *mut [u8; 20]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(data: *const u8, len: usize, out: *mut [u8; 20]); + type Impl = unsafe extern "sysv64" fn(state: *mut [u8; 84], count: u64, out: *mut [u8; 20]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha1::VG_SHA1_DIGEST_SHANI_FEATURES) } - || cpu.contains(crate::sha1::VG_SHA1_DIGEST_SHANI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha1::VG_SHA1_FINALIZE_SHANI_FEATURES) } + || cpu.contains(crate::sha1::VG_SHA1_FINALIZE_SHANI_FEATURES) { - return crate::sha1::vg_sha1_digest_shani; + return crate::sha1::vg_sha1_finalize_shani; } - if const { verified_garbage_cpu::target_contains(crate::sha1::VG_SHA1_DIGEST_AVX_BMI_FEATURES) } - || cpu.contains(crate::sha1::VG_SHA1_DIGEST_AVX_BMI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha1::VG_SHA1_FINALIZE_AVX_BMI_FEATURES) } + || cpu.contains(crate::sha1::VG_SHA1_FINALIZE_AVX_BMI_FEATURES) { - return crate::sha1::vg_sha1_digest_avx_bmi; + return crate::sha1::vg_sha1_finalize_avx_bmi; } - crate::sha1::vg_sha1_digest + crate::sha1::vg_sha1_finalize } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(data: *const u8, len: usize, out: *mut [u8; 20]) { + unsafe extern "sysv64" fn first(state: *mut [u8; 84], count: u64, out: *mut [u8; 20]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(data, len, out) } + unsafe { f(state, count, out) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -122,53 +123,52 @@ pub unsafe fn vg_sha1_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *co // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, out) } } -/// Finishes a SHA-1 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), writes the SHA-1 digest of that message to `*out`. +/// Computes the SHA-1 digest of the `len` bytes at `data` (20 bytes, `VG.Spec.Sha1.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-1 computation with that message. /// -/// Contract: `VG.Spec.Sha1.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha1.digestContract`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha1_finalize_shani` if the CPU supports its features (`VG_SHA1_FINALIZE_SHANI_FEATURES`); `vg_sha1_finalize_avx_bmi` if the CPU supports its features (`VG_SHA1_FINALIZE_AVX_BMI_FEATURES`); or else `vg_sha1_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha1_digest_shani` if the CPU supports its features (`VG_SHA1_DIGEST_SHANI_FEATURES`); `vg_sha1_digest_avx_bmi` if the CPU supports its features (`VG_SHA1_DIGEST_AVX_BMI_FEATURES`); or else `vg_sha1_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 84 bytes. +/// * `data` must be valid for reads of `len` bytes. /// * `out` must be valid for reads and writes of 20 bytes. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the return address on the stack or the 176 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the return address on the stack or the 288 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha1_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 84], count: u64, out: *mut [u8; 20]) { +pub unsafe fn vg_sha1_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 20]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(state: *mut [u8; 84], count: u64, out: *mut [u8; 20]); + type Impl = unsafe extern "sysv64" fn(data: *const u8, len: usize, out: *mut [u8; 20]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha1::VG_SHA1_FINALIZE_SHANI_FEATURES) } - || cpu.contains(crate::sha1::VG_SHA1_FINALIZE_SHANI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha1::VG_SHA1_DIGEST_SHANI_FEATURES) } + || cpu.contains(crate::sha1::VG_SHA1_DIGEST_SHANI_FEATURES) { - return crate::sha1::vg_sha1_finalize_shani; + return crate::sha1::vg_sha1_digest_shani; } - if const { verified_garbage_cpu::target_contains(crate::sha1::VG_SHA1_FINALIZE_AVX_BMI_FEATURES) } - || cpu.contains(crate::sha1::VG_SHA1_FINALIZE_AVX_BMI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha1::VG_SHA1_DIGEST_AVX_BMI_FEATURES) } + || cpu.contains(crate::sha1::VG_SHA1_DIGEST_AVX_BMI_FEATURES) { - return crate::sha1::vg_sha1_finalize_avx_bmi; + return crate::sha1::vg_sha1_digest_avx_bmi; } - crate::sha1::vg_sha1_finalize + crate::sha1::vg_sha1_digest } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(state: *mut [u8; 84], count: u64, out: *mut [u8; 20]) { + unsafe extern "sysv64" fn first(data: *const u8, len: usize, out: *mut [u8; 20]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, count, out) } + unsafe { f(data, len, out) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -176,5 +176,5 @@ pub unsafe fn vg_sha1_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, out) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } } diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/sha256.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/sha256.rs index f56705f0b2e..1e378c2987f 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/sha256.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/sha256.rs @@ -36,49 +36,50 @@ pub unsafe fn vg_sha256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *m unsafe { crate::sha256::vg_sha256_init(state) } } -/// Computes the SHA-224 digest of the `len` bytes at `data` (28 bytes, `VG.Spec.Sha256.sha224`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-224 computation with that message. +/// Finishes a SHA-224 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), hashed from the initial hash value of SHA-224 (as `vg_sha224_init` starts it), writes the SHA-224 digest of that message (28 bytes, `VG.Spec.Sha256.sha224`) to `*out`. /// -/// Contract: `VG.Spec.Sha256.digestContract` for `VG.Spec.Sha256.sha224`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha256.finalize224Contract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha224_digest_shani` if the CPU supports its features (`VG_SHA224_DIGEST_SHANI_FEATURES`); `vg_sha224_digest_avx2` if the CPU supports its features (`VG_SHA224_DIGEST_AVX2_FEATURES`); or else `vg_sha224_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha224_finalize_shani` if the CPU supports its features (`VG_SHA224_FINALIZE_SHANI_FEATURES`); `vg_sha224_finalize_avx2` if the CPU supports its features (`VG_SHA224_FINALIZE_AVX2_FEATURES`); or else `vg_sha224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `data` must be valid for reads of `len` bytes. +/// * `state` must be valid for reads and writes of 96 bytes. /// * `out` must be valid for reads and writes of 28 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the return address on the stack or the 736 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the return address on the stack or the 624 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha224_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 28]) { +pub unsafe fn vg_sha224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, out: *mut [u8; 28]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(data: *const u8, len: usize, out: *mut [u8; 28]); + type Impl = unsafe extern "sysv64" fn(state: *mut [u8; 96], count: u64, out: *mut [u8; 28]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA224_DIGEST_SHANI_FEATURES) } - || cpu.contains(crate::sha256::VG_SHA224_DIGEST_SHANI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA224_FINALIZE_SHANI_FEATURES) } + || cpu.contains(crate::sha256::VG_SHA224_FINALIZE_SHANI_FEATURES) { - return crate::sha256::vg_sha224_digest_shani; + return crate::sha256::vg_sha224_finalize_shani; } - if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA224_DIGEST_AVX2_FEATURES) } - || cpu.contains(crate::sha256::VG_SHA224_DIGEST_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA224_FINALIZE_AVX2_FEATURES) } + || cpu.contains(crate::sha256::VG_SHA224_FINALIZE_AVX2_FEATURES) { - return crate::sha256::vg_sha224_digest_avx2; + return crate::sha256::vg_sha224_finalize_avx2; } - crate::sha256::vg_sha224_digest + crate::sha256::vg_sha224_finalize } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(data: *const u8, len: usize, out: *mut [u8; 28]) { + unsafe extern "sysv64" fn first(state: *mut [u8; 96], count: u64, out: *mut [u8; 28]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(data, len, out) } + unsafe { f(state, count, out) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -86,52 +87,52 @@ pub unsafe fn vg_sha224_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: * // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, out) } } -/// Absorbs data into a SHA-224 or SHA-256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), it then represents that message followed by the `len` bytes at `data`. +/// Computes the SHA-224 digest of the `len` bytes at `data` (28 bytes, `VG.Spec.Sha256.sha224`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-224 computation with that message. /// -/// Contract: `VG.Spec.Sha256.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha256.digestContract` for `VG.Spec.Sha256.sha224`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha256_update_shani` if the CPU supports its features (`VG_SHA256_UPDATE_SHANI_FEATURES`); `vg_sha256_update_avx2` if the CPU supports its features (`VG_SHA256_UPDATE_AVX2_FEATURES`); or else `vg_sha256_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha224_digest_shani` if the CPU supports its features (`VG_SHA224_DIGEST_SHANI_FEATURES`); `vg_sha224_digest_avx2` if the CPU supports its features (`VG_SHA224_DIGEST_AVX2_FEATURES`); or else `vg_sha224_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 96 bytes. /// * `data` must be valid for reads of `len` bytes. -/// * `state` must not overlap `data` (distinct Rust objects never do). -/// * Neither `state` nor `data` may overlap the return address on the stack or the 624 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 28 bytes. +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the return address on the stack or the 736 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, data: *const u8, len: usize) { +pub unsafe fn vg_sha224_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 28]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(state: *mut [u8; 96], count: u64, data: *const u8, len: usize); + type Impl = unsafe extern "sysv64" fn(data: *const u8, len: usize, out: *mut [u8; 28]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA256_UPDATE_SHANI_FEATURES) } - || cpu.contains(crate::sha256::VG_SHA256_UPDATE_SHANI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA224_DIGEST_SHANI_FEATURES) } + || cpu.contains(crate::sha256::VG_SHA224_DIGEST_SHANI_FEATURES) { - return crate::sha256::vg_sha256_update_shani; + return crate::sha256::vg_sha224_digest_shani; } - if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA256_UPDATE_AVX2_FEATURES) } - || cpu.contains(crate::sha256::VG_SHA256_UPDATE_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA224_DIGEST_AVX2_FEATURES) } + || cpu.contains(crate::sha256::VG_SHA224_DIGEST_AVX2_FEATURES) { - return crate::sha256::vg_sha256_update_avx2; + return crate::sha256::vg_sha224_digest_avx2; } - crate::sha256::vg_sha256_update + crate::sha256::vg_sha224_digest } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(state: *mut [u8; 96], count: u64, data: *const u8, len: usize) { + unsafe extern "sysv64" fn first(data: *const u8, len: usize, out: *mut [u8; 28]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, count, data, len) } + unsafe { f(data, len, out) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -139,52 +140,52 @@ pub unsafe fn vg_sha256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, data, len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } } -/// Computes the SHA-256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha256.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-256 computation with that message. +/// Absorbs data into a SHA-224 or SHA-256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), it then represents that message followed by the `len` bytes at `data`. /// -/// Contract: `VG.Spec.Sha256.digestContract` for `VG.Spec.Sha256.hash`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha256.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha256_digest_shani` if the CPU supports its features (`VG_SHA256_DIGEST_SHANI_FEATURES`); `vg_sha256_digest_avx2` if the CPU supports its features (`VG_SHA256_DIGEST_AVX2_FEATURES`); or else `vg_sha256_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha256_update_shani` if the CPU supports its features (`VG_SHA256_UPDATE_SHANI_FEATURES`); `vg_sha256_update_avx2` if the CPU supports its features (`VG_SHA256_UPDATE_AVX2_FEATURES`); or else `vg_sha256_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// +/// * `state` must be valid for reads and writes of 96 bytes. /// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 32 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the return address on the stack or the 736 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `state` must not overlap `data` (distinct Rust objects never do). +/// * Neither `state` nor `data` may overlap the return address on the stack or the 624 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { +pub unsafe fn vg_sha256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, data: *const u8, len: usize) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(data: *const u8, len: usize, out: *mut [u8; 32]); + type Impl = unsafe extern "sysv64" fn(state: *mut [u8; 96], count: u64, data: *const u8, len: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA256_DIGEST_SHANI_FEATURES) } - || cpu.contains(crate::sha256::VG_SHA256_DIGEST_SHANI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA256_UPDATE_SHANI_FEATURES) } + || cpu.contains(crate::sha256::VG_SHA256_UPDATE_SHANI_FEATURES) { - return crate::sha256::vg_sha256_digest_shani; + return crate::sha256::vg_sha256_update_shani; } - if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA256_DIGEST_AVX2_FEATURES) } - || cpu.contains(crate::sha256::VG_SHA256_DIGEST_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA256_UPDATE_AVX2_FEATURES) } + || cpu.contains(crate::sha256::VG_SHA256_UPDATE_AVX2_FEATURES) { - return crate::sha256::vg_sha256_digest_avx2; + return crate::sha256::vg_sha256_update_avx2; } - crate::sha256::vg_sha256_digest + crate::sha256::vg_sha256_update } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(data: *const u8, len: usize, out: *mut [u8; 32]) { + unsafe extern "sysv64" fn first(state: *mut [u8; 96], count: u64, data: *const u8, len: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(data, len, out) } + unsafe { f(state, count, data, len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -192,48 +193,48 @@ pub unsafe fn vg_sha256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: * // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, data, len) } } -/// Finishes a SHA-224 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), hashed from the initial hash value of SHA-224 (as `vg_sha224_init` starts it), writes the SHA-224 digest of that message (28 bytes, `VG.Spec.Sha256.sha224`) to `*out`. +/// Finishes a SHA-224 or SHA-256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (32 bytes) to `*out`. The SHA-256 digest is all of it; the SHA-224 digest is its first 28 bytes. /// -/// Contract: `VG.Spec.Sha256.finalize224Contract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha256.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha224_finalize_shani` if the CPU supports its features (`VG_SHA224_FINALIZE_SHANI_FEATURES`); `vg_sha224_finalize_avx2` if the CPU supports its features (`VG_SHA224_FINALIZE_AVX2_FEATURES`); or else `vg_sha224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha256_finalize_shani` if the CPU supports its features (`VG_SHA256_FINALIZE_SHANI_FEATURES`); `vg_sha256_finalize_avx2` if the CPU supports its features (`VG_SHA256_FINALIZE_AVX2_FEATURES`); or else `vg_sha256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 96 bytes. -/// * `out` must be valid for reads and writes of 28 bytes. +/// * `out` must be valid for reads and writes of 32 bytes. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the return address on the stack or the 624 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, out: *mut [u8; 28]) { +pub unsafe fn vg_sha256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, out: *mut [u8; 32]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(state: *mut [u8; 96], count: u64, out: *mut [u8; 28]); + type Impl = unsafe extern "sysv64" fn(state: *mut [u8; 96], count: u64, out: *mut [u8; 32]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA224_FINALIZE_SHANI_FEATURES) } - || cpu.contains(crate::sha256::VG_SHA224_FINALIZE_SHANI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA256_FINALIZE_SHANI_FEATURES) } + || cpu.contains(crate::sha256::VG_SHA256_FINALIZE_SHANI_FEATURES) { - return crate::sha256::vg_sha224_finalize_shani; + return crate::sha256::vg_sha256_finalize_shani; } - if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA224_FINALIZE_AVX2_FEATURES) } - || cpu.contains(crate::sha256::VG_SHA224_FINALIZE_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA256_FINALIZE_AVX2_FEATURES) } + || cpu.contains(crate::sha256::VG_SHA256_FINALIZE_AVX2_FEATURES) { - return crate::sha256::vg_sha224_finalize_avx2; + return crate::sha256::vg_sha256_finalize_avx2; } - crate::sha256::vg_sha224_finalize + crate::sha256::vg_sha256_finalize } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(state: *mut [u8; 96], count: u64, out: *mut [u8; 28]) { + unsafe extern "sysv64" fn first(state: *mut [u8; 96], count: u64, out: *mut [u8; 32]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` @@ -249,50 +250,49 @@ pub unsafe fn vg_sha224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, out) } } -/// Finishes a SHA-224 or SHA-256 computation: if the streaming state `*state` represents a message of `count` bytes (modulo 2⁶⁴), hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (32 bytes) to `*out`. The SHA-256 digest is all of it; the SHA-224 digest is its first 28 bytes. +/// Computes the SHA-256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha256.hash`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-256 computation with that message. /// -/// Contract: `VG.Spec.Sha256.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha256.digestContract` for `VG.Spec.Sha256.hash`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha256_finalize_shani` if the CPU supports its features (`VG_SHA256_FINALIZE_SHANI_FEATURES`); `vg_sha256_finalize_avx2` if the CPU supports its features (`VG_SHA256_FINALIZE_AVX2_FEATURES`); or else `vg_sha256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha256_digest_shani` if the CPU supports its features (`VG_SHA256_DIGEST_SHANI_FEATURES`); `vg_sha256_digest_avx2` if the CPU supports its features (`VG_SHA256_DIGEST_AVX2_FEATURES`); or else `vg_sha256_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 96 bytes. +/// * `data` must be valid for reads of `len` bytes. /// * `out` must be valid for reads and writes of 32 bytes. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the return address on the stack or the 624 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the return address on the stack or the 736 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 96], count: u64, out: *mut [u8; 32]) { +pub unsafe fn vg_sha256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(state: *mut [u8; 96], count: u64, out: *mut [u8; 32]); + type Impl = unsafe extern "sysv64" fn(data: *const u8, len: usize, out: *mut [u8; 32]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA256_FINALIZE_SHANI_FEATURES) } - || cpu.contains(crate::sha256::VG_SHA256_FINALIZE_SHANI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA256_DIGEST_SHANI_FEATURES) } + || cpu.contains(crate::sha256::VG_SHA256_DIGEST_SHANI_FEATURES) { - return crate::sha256::vg_sha256_finalize_shani; + return crate::sha256::vg_sha256_digest_shani; } - if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA256_FINALIZE_AVX2_FEATURES) } - || cpu.contains(crate::sha256::VG_SHA256_FINALIZE_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha256::VG_SHA256_DIGEST_AVX2_FEATURES) } + || cpu.contains(crate::sha256::VG_SHA256_DIGEST_AVX2_FEATURES) { - return crate::sha256::vg_sha256_finalize_avx2; + return crate::sha256::vg_sha256_digest_avx2; } - crate::sha256::vg_sha256_finalize + crate::sha256::vg_sha256_digest } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(state: *mut [u8; 96], count: u64, out: *mut [u8; 32]) { + unsafe extern "sysv64" fn first(data: *const u8, len: usize, out: *mut [u8; 32]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, count, out) } + unsafe { f(data, len, out) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -300,5 +300,5 @@ pub unsafe fn vg_sha256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, out) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } } diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/sha3.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/sha3.rs index d72fed46704..4ce6b123602 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/sha3.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/sha3.rs @@ -24,250 +24,250 @@ pub unsafe fn vg_sha3_224_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state unsafe { crate::sha3::vg_sha3_224_update(state, pos, data, len, scratch) } } -/// Absorbs data into a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, 136), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 136`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Finishes a SHA3-224 computation: if the state `*state` represents a message whose length is `pos` modulo 144 (`VG.Spec.Sha3.Repr` with SHA3-224's rate, as `vg_sha3_224_update` leaves it), writes the SHA3-224 digest of that message (28 bytes, `VG.Spec.Sha3.sha3_224`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 136. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_224`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs `vg_sha3_256_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `scratch` must be valid for reads and writes of 640 bytes. -/// * `pos` must be less than 136. -/// * The contents of `scratch` on return are unspecified. -/// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). -/// * None of `state`, `data` and `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 28 bytes. +/// * `pos` must be less than 144. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the return address on the stack or the 680 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 28]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_256_update(state, pos, data, len, scratch) } + unsafe { crate::sha3::vg_sha3_224_finalize(state, pos, out) } } -/// Absorbs data into a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, 104), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 104`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Absorbs data into a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, 136), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 136`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 104. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 136. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_sha3_384_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_256_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. /// * `data` must be valid for reads of `len` bytes. /// * `scratch` must be valid for reads and writes of 640 bytes. -/// * `pos` must be less than 104. +/// * `pos` must be less than 136. /// * The contents of `scratch` on return are unspecified. /// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). /// * None of `state`, `data` and `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_384_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_256_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_384_update(state, pos, data, len, scratch) } + unsafe { crate::sha3::vg_sha3_256_update(state, pos, data, len, scratch) } } -/// Absorbs data into a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, 72), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 72`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Finishes a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, as `vg_sha3_256_update` leaves it), writes the SHA3-256 digest of that message (32 bytes, `VG.Spec.Sha3.sha3_256`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 72. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_256`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs `vg_sha3_512_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `scratch` must be valid for reads and writes of 640 bytes. -/// * `pos` must be less than 72. -/// * The contents of `scratch` on return are unspecified. -/// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). -/// * None of `state`, `data` and `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of 32 bytes. +/// * `pos` must be less than 136. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the return address on the stack or the 680 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 32]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_512_update(state, pos, data, len, scratch) } + unsafe { crate::sha3::vg_sha3_256_finalize(state, pos, out) } } -/// Absorbs data into a SHAKE128 computation: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, 168), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 168`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. +/// Absorbs data into a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, 104), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 104`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.updateContract` with the rate 168. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 104. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_shake128_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_384_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. /// * `data` must be valid for reads of `len` bytes. /// * `scratch` must be valid for reads and writes of 640 bytes. -/// * `pos` must be less than 168. +/// * `pos` must be less than 104. /// * The contents of `scratch` on return are unspecified. /// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). /// * None of `state`, `data` and `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake128_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { +pub unsafe fn vg_sha3_384_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake128_update(state, pos, data, len, scratch) } + unsafe { crate::sha3::vg_sha3_384_update(state, pos, data, len, scratch) } } -/// Squeezes SHAKE128 output: if the state `*state`, from position `pos`, continues the output of SHAKE128 of a message (`VG.Spec.Sha3.shake128`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake128_finalize` leaves and position 0. +/// Finishes a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, as `vg_sha3_384_update` leaves it), writes the SHA3-384 digest of that message (48 bytes, `VG.Spec.Sha3.sha3_384`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_384`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs `vg_shake128_squeeze`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_384_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of `outlen` bytes. -/// * `pos` must be at most 168. +/// * `out` must be valid for reads and writes of 48 bytes. +/// * `pos` must be less than 104. +/// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the return address on the stack or the 656 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * Neither `state` nor `out` may overlap the return address on the stack or the 680 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake128_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { +pub unsafe fn vg_sha3_384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 48]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake128_squeeze(state, pos, out, outlen) } + unsafe { crate::sha3::vg_sha3_384_finalize(state, pos, out) } } -/// Squeezes SHAKE256 output: if the state `*state`, from position `pos`, continues the output of SHAKE256 of a message (`VG.Spec.Sha3.shake256`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake256_finalize` leaves and position 0. +/// Absorbs data into a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, 72), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 72`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 72. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_shake256_squeeze`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_512_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of `outlen` bytes. -/// * `pos` must be at most 136. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the return address on the stack or the 656 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `scratch` must be valid for reads and writes of 640 bytes. +/// * `pos` must be less than 72. +/// * The contents of `scratch` on return are unspecified. +/// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). +/// * None of `state`, `data` and `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake256_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { +pub unsafe fn vg_sha3_512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake256_squeeze(state, pos, out, outlen) } + unsafe { crate::sha3::vg_sha3_512_update(state, pos, data, len, scratch) } } -/// Finishes a SHA3-224 computation: if the state `*state` represents a message whose length is `pos` modulo 144 (`VG.Spec.Sha3.Repr` with SHA3-224's rate, as `vg_sha3_224_update` leaves it), writes the SHA3-224 digest of that message (28 bytes, `VG.Spec.Sha3.sha3_224`) to `*out`. +/// Finishes a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, as `vg_sha3_512_update` leaves it), writes the SHA3-512 digest of that message (64 bytes, `VG.Spec.Sha3.sha3_512`) to `*out`. /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_224`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_512`. Constant time: only the pointers and `pos` may affect timing, not the state. /// -/// It runs `vg_sha3_224_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_sha3_512_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of 28 bytes. -/// * `pos` must be less than 144. +/// * `out` must be valid for reads and writes of 64 bytes. +/// * `pos` must be less than 72. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the return address on the stack or the 680 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 28]) { +pub unsafe fn vg_sha3_512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 64]) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_224_finalize(state, pos, out) } + unsafe { crate::sha3::vg_sha3_512_finalize(state, pos, out) } } -/// Finishes a SHA3-256 computation: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHA3-256's rate, as `vg_sha3_256_update` leaves it), writes the SHA3-256 digest of that message (32 bytes, `VG.Spec.Sha3.sha3_256`) to `*out`. +/// Absorbs data into a SHAKE128 computation: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, 168), it then represents that message followed by the `len` bytes at `data`. Returns the position after them, `(pos + len) % 168`. `*scratch` is its working space. A computation starts from the all-zero state, at position 0. /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_256`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.updateContract` with the rate 168. Constant time: only the pointers, `pos` and `len` may affect timing, not the state or the data. /// -/// It runs `vg_sha3_256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake128_update`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of 32 bytes. -/// * `pos` must be less than 136. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the return address on the stack or the 680 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `scratch` must be valid for reads and writes of 640 bytes. +/// * `pos` must be less than 168. +/// * The contents of `scratch` on return are unspecified. +/// * `state` and `scratch` must not overlap each other or `data` (distinct Rust objects never do). +/// * None of `state`, `data` and `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 32]) { +pub unsafe fn vg_shake128_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, data: *const u8, len: usize, scratch: *mut [u64; 80]) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_256_finalize(state, pos, out) } + unsafe { crate::sha3::vg_shake128_update(state, pos, data, len, scratch) } } -/// Finishes a SHA3-384 computation: if the state `*state` represents a message whose length is `pos` modulo 104 (`VG.Spec.Sha3.Repr` with SHA3-384's rate, as `vg_sha3_384_update` leaves it), writes the SHA3-384 digest of that message (48 bytes, `VG.Spec.Sha3.sha3_384`) to `*out`. +/// Finishes absorbing a SHAKE128 message: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, as `vg_shake128_update` leaves it), it becomes a state that, squeezed by `vg_shake128_squeeze` from position 0, gives the output of SHAKE128 of that message (`VG.Spec.Sha3.shake128`) from its first byte on (`VG.Spec.Sha3.XofRepr`). /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_384`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointer and `pos` may affect timing, not the state. /// -/// It runs `vg_sha3_384_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake128_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of 48 bytes. -/// * `pos` must be less than 104. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the return address on the stack or the 680 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `pos` must be less than 168. +/// * `state` must not overlap the return address on the stack or the 656 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 48]) { +pub unsafe fn vg_shake128_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_384_finalize(state, pos, out) } + unsafe { crate::sha3::vg_shake128_finalize(state, pos) } } -/// Finishes a SHA3-512 computation: if the state `*state` represents a message whose length is `pos` modulo 72 (`VG.Spec.Sha3.Repr` with SHA3-512's rate, as `vg_sha3_512_update` leaves it), writes the SHA3-512 digest of that message (64 bytes, `VG.Spec.Sha3.sha3_512`) to `*out`. +/// Squeezes SHAKE128 output: if the state `*state`, from position `pos`, continues the output of SHAKE128 of a message (`VG.Spec.Sha3.shake128`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake128_finalize` leaves and position 0. /// -/// Contract: `VG.Spec.Sha3.finalizeContract` for `VG.Spec.Sha3.hashSha3_512`. Constant time: only the pointers and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. /// -/// It runs `vg_sha3_512_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake128_squeeze`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `out` must be valid for reads and writes of 64 bytes. -/// * `pos` must be less than 72. -/// * The contents of `state` on return are unspecified. +/// * `out` must be valid for reads and writes of `outlen` bytes. +/// * `pos` must be at most 168. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the return address on the stack or the 680 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * Neither `state` nor `out` may overlap the return address on the stack or the 656 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha3_512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut [u8; 64]) { +pub unsafe fn vg_shake128_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_sha3_512_finalize(state, pos, out) } + unsafe { crate::sha3::vg_shake128_squeeze(state, pos, out, outlen) } } -/// Finishes absorbing a SHAKE128 message: if the state `*state` represents a message whose length is `pos` modulo 168 (`VG.Spec.Sha3.Repr` with SHAKE128's rate, as `vg_shake128_update` leaves it), it becomes a state that, squeezed by `vg_shake128_squeeze` from position 0, gives the output of SHAKE128 of that message (`VG.Spec.Sha3.shake128`) from its first byte on (`VG.Spec.Sha3.XofRepr`). +/// Finishes absorbing a SHAKE256 message: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHAKE256's rate, as `vg_sha3_256_update` leaves it), it becomes a state that, squeezed by `vg_shake256_squeeze` from position 0, gives the output of SHAKE256 of that message (`VG.Spec.Sha3.shake256`) from its first byte on (`VG.Spec.Sha3.XofRepr`). /// -/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake128`. Constant time: only the pointer and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointer and `pos` may affect timing, not the state. /// -/// It runs `vg_shake128_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `pos` must be less than 168. +/// * `pos` must be less than 136. /// * `state` must not overlap the return address on the stack or the 656 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake128_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { +pub unsafe fn vg_shake256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake128_finalize(state, pos) } + unsafe { crate::sha3::vg_shake256_finalize(state, pos) } } -/// Finishes absorbing a SHAKE256 message: if the state `*state` represents a message whose length is `pos` modulo 136 (`VG.Spec.Sha3.Repr` with SHAKE256's rate, as `vg_sha3_256_update` leaves it), it becomes a state that, squeezed by `vg_shake256_squeeze` from position 0, gives the output of SHAKE256 of that message (`VG.Spec.Sha3.shake256`) from its first byte on (`VG.Spec.Sha3.XofRepr`). +/// Squeezes SHAKE256 output: if the state `*state`, from position `pos`, continues the output of SHAKE256 of a message (`VG.Spec.Sha3.shake256`) after its first `n` bytes (`VG.Spec.Sha3.XofRepr`), writes the next `outlen` bytes of that output to `out`, and leaves in `*state` a state, and returns a position, that continue it after its first `n + outlen` bytes. Start from the state `vg_shake256_finalize` leaves and position 0. /// -/// Contract: `VG.Spec.Sha3.xofFinalizeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointer and `pos` may affect timing, not the state. +/// Contract: `VG.Spec.Sha3.xofSqueezeContract` for `VG.Spec.Sha3.xofShake256`. Constant time: only the pointers, `pos` and `outlen` may affect timing, not the state. /// -/// It runs `vg_shake256_finalize`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// It runs `vg_shake256_squeeze`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. /// /// # Safety /// /// * `state` must be valid for reads and writes of 200 bytes. -/// * `pos` must be less than 136. -/// * `state` must not overlap the return address on the stack or the 656 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` must be valid for reads and writes of `outlen` bytes. +/// * `pos` must be at most 136. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the return address on the stack or the 656 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_shake256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize) { +pub unsafe fn vg_shake256_squeeze_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u64; 25], pos: usize, out: *mut u8, outlen: usize) -> usize { // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha3::vg_shake256_finalize(state, pos) } + unsafe { crate::sha3::vg_shake256_squeeze(state, pos, out, outlen) } } diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/sha512.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/sha512.rs index 78c7c42436d..04b1a698e00 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/sha512.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/sha512.rs @@ -36,11 +36,100 @@ pub unsafe fn vg_sha512_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *m unsafe { crate::sha512::vg_sha512_init(state) } } +/// Starts a SHA-512/224 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/224 (`VG.Spec.Sha512.H0_512_224`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. +/// +/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_224`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). +/// +/// It runs `vg_sha512_224_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 192 bytes. +/// * `state` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_sha512_224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::sha512::vg_sha512_224_init(state) } +} + +/// Starts a SHA-512/256 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/256 (`VG.Spec.Sha512.H0_512_256`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. +/// +/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_256`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). +/// +/// It runs `vg_sha512_256_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 192 bytes. +/// * `state` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_sha512_256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::sha512::vg_sha512_256_init(state) } +} + +/// Finishes a SHA-384 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-384 (as `vg_sha384_init` starts it), writes the SHA-384 digest of that message (48 bytes, `VG.Spec.Sha512.sha384`) to `*out`. +/// +/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_384` and `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `count` may affect timing, not the state. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha384_finalize_shani` if the CPU supports its features (`VG_SHA384_FINALIZE_SHANI_FEATURES`); `vg_sha384_finalize_avx2` if the CPU supports its features (`VG_SHA384_FINALIZE_AVX2_FEATURES`); or else `vg_sha384_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// +/// # Safety +/// +/// * `state` must be valid for reads and writes of 192 bytes. +/// * `out` must be valid for reads and writes of 48 bytes. +/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the return address on the stack or the 1392 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_sha384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 48]) { + /// The type of the implementations. + type Impl = unsafe extern "sysv64" fn(state: *mut [u8; 192], count: u64, out: *mut [u8; 48]); + /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only + /// features the CPU has. + #[cold] + fn select() -> Impl { + let cpu = verified_garbage_cpu::detected(); + if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA384_FINALIZE_SHANI_FEATURES) } + || cpu.contains(crate::sha512::VG_SHA384_FINALIZE_SHANI_FEATURES) + { + return crate::sha512::vg_sha384_finalize_shani; + } + if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA384_FINALIZE_AVX2_FEATURES) } + || cpu.contains(crate::sha512::VG_SHA384_FINALIZE_AVX2_FEATURES) + { + return crate::sha512::vg_sha384_finalize_avx2; + } + crate::sha512::vg_sha384_finalize + } + /// The first call: chooses the implementation, records it in `CHOSEN` for the calls + /// after it, and runs it. + #[cold] + unsafe extern "sysv64" fn first(state: *mut [u8; 192], count: u64, out: *mut [u8; 48]) { + let f = select(); + CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); + // SAFETY: the caller's obligations, which are those of every implementation, `f` + // among them, and the CPU has the features `f` needs. + unsafe { f(state, count, out) } + } + /// The function to call: `first`, until it records the implementation it chose. + static CHOSEN: core::sync::atomic::AtomicPtr<()> = + core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); + // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, + // or an implementation `select` returned, which needs only features the CPU has. Calling + // either carries the caller's obligations, which are those of every implementation. + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, out) } +} + /// Computes the SHA-384 digest of the `len` bytes at `data` (48 bytes, `VG.Spec.Sha512.sha384`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-384 computation with that message. /// /// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha384_digest_shani` if the CPU supports its features (`VG_SHA384_DIGEST_SHANI_FEATURES`); `vg_sha384_digest_avx2` if the CPU supports its features (`VG_SHA384_DIGEST_AVX2_FEATURES`); or else `vg_sha384_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha384_digest_shani` if the CPU supports its features (`VG_SHA384_DIGEST_SHANI_FEATURES`); `vg_sha384_digest_avx2` if the CPU supports its features (`VG_SHA384_DIGEST_AVX2_FEATURES`); or else `vg_sha384_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -93,7 +182,7 @@ pub unsafe fn vg_sha384_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: * /// /// Contract: `VG.Spec.Sha512.updateContract`. Constant time: only the pointers, `count` and `len` may affect timing, not the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha512_update_shani` if the CPU supports its features (`VG_SHA512_UPDATE_SHANI_FEATURES`); `vg_sha512_update_avx2` if the CPU supports its features (`VG_SHA512_UPDATE_AVX2_FEATURES`); or else `vg_sha512_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha512_update_shani` if the CPU supports its features (`VG_SHA512_UPDATE_SHANI_FEATURES`); `vg_sha512_update_avx2` if the CPU supports its features (`VG_SHA512_UPDATE_AVX2_FEATURES`); or else `vg_sha512_update`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -142,49 +231,51 @@ pub unsafe fn vg_sha512_update_auto(_dit: verified_garbage_cpu::Dit<'_>, state: unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, data, len) } } -/// Computes the SHA-512 digest of the `len` bytes at `data` (64 bytes, `VG.Spec.Sha512.sha512`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512 computation with that message. +/// Finishes a SHA-384, SHA-512, SHA-512/224 or SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (64 bytes) to `*out`. The SHA-512 digest is all of it; the SHA-384, SHA-512/224 and SHA-512/256 digests are its first 48, 28 and 32 bytes. /// -/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512`. Constant time: only the pointers and `len` may affect timing, not the data. +/// Contract: `VG.Spec.Sha512.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha512_digest_shani` if the CPU supports its features (`VG_SHA512_DIGEST_SHANI_FEATURES`); `vg_sha512_digest_avx2` if the CPU supports its features (`VG_SHA512_DIGEST_AVX2_FEATURES`); or else `vg_sha512_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha512_finalize_shani` if the CPU supports its features (`VG_SHA512_FINALIZE_SHANI_FEATURES`); `vg_sha512_finalize_avx2` if the CPU supports its features (`VG_SHA512_FINALIZE_AVX2_FEATURES`); or else `vg_sha512_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `data` must be valid for reads of `len` bytes. +/// * `state` must be valid for reads and writes of 192 bytes. /// * `out` must be valid for reads and writes of 64 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the return address on the stack or the 1600 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. +/// * The contents of `state` on return are unspecified. +/// * `state` and `out` must not overlap each other (distinct Rust objects never do). +/// * Neither `state` nor `out` may overlap the return address on the stack or the 1392 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 64]) { +pub unsafe fn vg_sha512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 64]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(data: *const u8, len: usize, out: *mut [u8; 64]); + type Impl = unsafe extern "sysv64" fn(state: *mut [u8; 192], count: u64, out: *mut [u8; 64]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_DIGEST_SHANI_FEATURES) } - || cpu.contains(crate::sha512::VG_SHA512_DIGEST_SHANI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_FINALIZE_SHANI_FEATURES) } + || cpu.contains(crate::sha512::VG_SHA512_FINALIZE_SHANI_FEATURES) { - return crate::sha512::vg_sha512_digest_shani; + return crate::sha512::vg_sha512_finalize_shani; } - if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_DIGEST_AVX2_FEATURES) } - || cpu.contains(crate::sha512::VG_SHA512_DIGEST_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_FINALIZE_AVX2_FEATURES) } + || cpu.contains(crate::sha512::VG_SHA512_FINALIZE_AVX2_FEATURES) { - return crate::sha512::vg_sha512_digest_avx2; + return crate::sha512::vg_sha512_finalize_avx2; } - crate::sha512::vg_sha512_digest + crate::sha512::vg_sha512_finalize } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(data: *const u8, len: usize, out: *mut [u8; 64]) { + unsafe extern "sysv64" fn first(state: *mut [u8; 192], count: u64, out: *mut [u8; 64]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(data, len, out) } + unsafe { f(state, count, out) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -192,88 +283,52 @@ pub unsafe fn vg_sha512_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: * // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } -} - -/// Starts a SHA-512/224 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/224 (`VG.Spec.Sha512.H0_512_224`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. -/// -/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_224`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). -/// -/// It runs `vg_sha512_224_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 192 bytes. -/// * `state` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_sha512_224_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_224_init(state) } -} - -/// Starts a SHA-512/256 computation: makes the SHA-512 streaming state `*state` represent the empty message, hashed from the initial hash value of SHA-512/256 (`VG.Spec.Sha512.H0_512_256`). Continue with `vg_sha512_update` and `vg_sha512_finalize`. -/// -/// Contract: `VG.Spec.Sha512.initContract` for `VG.Spec.Sha512.H0_512_256`. The streaming state is the hash value followed by a buffered partial block (`VG.Spec.Sha512.Repr`). -/// -/// It runs `vg_sha512_256_init`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `state` must be valid for reads and writes of 192 bytes. -/// * `state` must not overlap the return address on the stack, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_sha512_256_init_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192]) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::sha512::vg_sha512_256_init(state) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, out) } } -/// Finishes a SHA-384 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-384 (as `vg_sha384_init` starts it), writes the SHA-384 digest of that message (48 bytes, `VG.Spec.Sha512.sha384`) to `*out`. +/// Computes the SHA-512 digest of the `len` bytes at `data` (64 bytes, `VG.Spec.Sha512.sha512`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512 computation with that message. /// -/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_384` and `VG.Spec.Sha512.sha384`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha384_finalize_shani` if the CPU supports its features (`VG_SHA384_FINALIZE_SHANI_FEATURES`); `vg_sha384_finalize_avx2` if the CPU supports its features (`VG_SHA384_FINALIZE_AVX2_FEATURES`); or else `vg_sha384_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha512_digest_shani` if the CPU supports its features (`VG_SHA512_DIGEST_SHANI_FEATURES`); `vg_sha512_digest_avx2` if the CPU supports its features (`VG_SHA512_DIGEST_AVX2_FEATURES`); or else `vg_sha512_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 48 bytes. -/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the return address on the stack or the 1392 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `out` must be valid for reads and writes of 64 bytes. +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the return address on the stack or the 1600 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 48]) { +pub unsafe fn vg_sha512_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 64]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(state: *mut [u8; 192], count: u64, out: *mut [u8; 48]); + type Impl = unsafe extern "sysv64" fn(data: *const u8, len: usize, out: *mut [u8; 64]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA384_FINALIZE_SHANI_FEATURES) } - || cpu.contains(crate::sha512::VG_SHA384_FINALIZE_SHANI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_DIGEST_SHANI_FEATURES) } + || cpu.contains(crate::sha512::VG_SHA512_DIGEST_SHANI_FEATURES) { - return crate::sha512::vg_sha384_finalize_shani; + return crate::sha512::vg_sha512_digest_shani; } - if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA384_FINALIZE_AVX2_FEATURES) } - || cpu.contains(crate::sha512::VG_SHA384_FINALIZE_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_DIGEST_AVX2_FEATURES) } + || cpu.contains(crate::sha512::VG_SHA512_DIGEST_AVX2_FEATURES) { - return crate::sha512::vg_sha384_finalize_avx2; + return crate::sha512::vg_sha512_digest_avx2; } - crate::sha512::vg_sha384_finalize + crate::sha512::vg_sha512_digest } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(state: *mut [u8; 192], count: u64, out: *mut [u8; 48]) { + unsafe extern "sysv64" fn first(data: *const u8, len: usize, out: *mut [u8; 64]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, count, out) } + unsafe { f(data, len, out) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -281,49 +336,49 @@ pub unsafe fn vg_sha384_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, out) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } } -/// Finishes a SHA-384, SHA-512, SHA-512/224 or SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from an initial hash value, writes the final hash value `H⁽ᴺ⁾` of that message (64 bytes) to `*out`. The SHA-512 digest is all of it; the SHA-384, SHA-512/224 and SHA-512/256 digests are its first 48, 28 and 32 bytes. +/// Finishes a SHA-512/224 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/224 (as `vg_sha512_224_init` starts it), writes the SHA-512/224 digest of that message (28 bytes, `VG.Spec.Sha512.sha512_224`) to `*out`. /// -/// Contract: `VG.Spec.Sha512.finalizeContract`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_224` and `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha512_finalize_shani` if the CPU supports its features (`VG_SHA512_FINALIZE_SHANI_FEATURES`); `vg_sha512_finalize_avx2` if the CPU supports its features (`VG_SHA512_FINALIZE_AVX2_FEATURES`); or else `vg_sha512_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha512_224_finalize_shani` if the CPU supports its features (`VG_SHA512_224_FINALIZE_SHANI_FEATURES`); `vg_sha512_224_finalize_avx2` if the CPU supports its features (`VG_SHA512_224_FINALIZE_AVX2_FEATURES`); or else `vg_sha512_224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 64 bytes. +/// * `out` must be valid for reads and writes of 28 bytes. /// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the return address on the stack or the 1392 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 64]) { +pub unsafe fn vg_sha512_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 28]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(state: *mut [u8; 192], count: u64, out: *mut [u8; 64]); + type Impl = unsafe extern "sysv64" fn(state: *mut [u8; 192], count: u64, out: *mut [u8; 28]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_FINALIZE_SHANI_FEATURES) } - || cpu.contains(crate::sha512::VG_SHA512_FINALIZE_SHANI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_224_FINALIZE_SHANI_FEATURES) } + || cpu.contains(crate::sha512::VG_SHA512_224_FINALIZE_SHANI_FEATURES) { - return crate::sha512::vg_sha512_finalize_shani; + return crate::sha512::vg_sha512_224_finalize_shani; } - if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_FINALIZE_AVX2_FEATURES) } - || cpu.contains(crate::sha512::VG_SHA512_FINALIZE_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_224_FINALIZE_AVX2_FEATURES) } + || cpu.contains(crate::sha512::VG_SHA512_224_FINALIZE_AVX2_FEATURES) { - return crate::sha512::vg_sha512_finalize_avx2; + return crate::sha512::vg_sha512_224_finalize_avx2; } - crate::sha512::vg_sha512_finalize + crate::sha512::vg_sha512_224_finalize } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(state: *mut [u8; 192], count: u64, out: *mut [u8; 64]) { + unsafe extern "sysv64" fn first(state: *mut [u8; 192], count: u64, out: *mut [u8; 28]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` @@ -343,7 +398,7 @@ pub unsafe fn vg_sha512_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state /// /// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha512_224_digest_shani` if the CPU supports its features (`VG_SHA512_224_DIGEST_SHANI_FEATURES`); `vg_sha512_224_digest_avx2` if the CPU supports its features (`VG_SHA512_224_DIGEST_AVX2_FEATURES`); or else `vg_sha512_224_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha512_224_digest_shani` if the CPU supports its features (`VG_SHA512_224_DIGEST_SHANI_FEATURES`); `vg_sha512_224_digest_avx2` if the CPU supports its features (`VG_SHA512_224_DIGEST_AVX2_FEATURES`); or else `vg_sha512_224_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -392,99 +447,46 @@ pub unsafe fn vg_sha512_224_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, dat unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } } -/// Computes the SHA-512/256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha512.sha512_256`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512/256 computation with that message. -/// -/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `len` may affect timing, not the data. -/// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha512_256_digest_shani` if the CPU supports its features (`VG_SHA512_256_DIGEST_SHANI_FEATURES`); `vg_sha512_256_digest_avx2` if the CPU supports its features (`VG_SHA512_256_DIGEST_AVX2_FEATURES`); or else `vg_sha512_256_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. -/// -/// # Safety -/// -/// * `data` must be valid for reads of `len` bytes. -/// * `out` must be valid for reads and writes of 32 bytes. -/// * `out` must not overlap `data` (distinct Rust objects never do). -/// * Neither `data` nor `out` may overlap the return address on the stack or the 1600 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_sha512_256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { - /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(data: *const u8, len: usize, out: *mut [u8; 32]); - /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only - /// features the CPU has. - #[cold] - fn select() -> Impl { - let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_256_DIGEST_SHANI_FEATURES) } - || cpu.contains(crate::sha512::VG_SHA512_256_DIGEST_SHANI_FEATURES) - { - return crate::sha512::vg_sha512_256_digest_shani; - } - if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_256_DIGEST_AVX2_FEATURES) } - || cpu.contains(crate::sha512::VG_SHA512_256_DIGEST_AVX2_FEATURES) - { - return crate::sha512::vg_sha512_256_digest_avx2; - } - crate::sha512::vg_sha512_256_digest - } - /// The first call: chooses the implementation, records it in `CHOSEN` for the calls - /// after it, and runs it. - #[cold] - unsafe extern "sysv64" fn first(data: *const u8, len: usize, out: *mut [u8; 32]) { - let f = select(); - CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); - // SAFETY: the caller's obligations, which are those of every implementation, `f` - // among them, and the CPU has the features `f` needs. - unsafe { f(data, len, out) } - } - /// The function to call: `first`, until it records the implementation it chose. - static CHOSEN: core::sync::atomic::AtomicPtr<()> = - core::sync::atomic::AtomicPtr::new(first as Impl as *mut ()); - // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, - // or an implementation `select` returned, which needs only features the CPU has. Calling - // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } -} - -/// Finishes a SHA-512/224 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/224 (as `vg_sha512_224_init` starts it), writes the SHA-512/224 digest of that message (28 bytes, `VG.Spec.Sha512.sha512_224`) to `*out`. +/// Finishes a SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/256 (as `vg_sha512_256_init` starts it), writes the SHA-512/256 digest of that message (32 bytes, `VG.Spec.Sha512.sha512_256`) to `*out`. /// -/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_224` and `VG.Spec.Sha512.sha512_224`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_256` and `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `count` may affect timing, not the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha512_224_finalize_shani` if the CPU supports its features (`VG_SHA512_224_FINALIZE_SHANI_FEATURES`); `vg_sha512_224_finalize_avx2` if the CPU supports its features (`VG_SHA512_224_FINALIZE_AVX2_FEATURES`); or else `vg_sha512_224_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha512_256_finalize_shani` if the CPU supports its features (`VG_SHA512_256_FINALIZE_SHANI_FEATURES`); `vg_sha512_256_finalize_avx2` if the CPU supports its features (`VG_SHA512_256_FINALIZE_AVX2_FEATURES`); or else `vg_sha512_256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `state` must be valid for reads and writes of 192 bytes. -/// * `out` must be valid for reads and writes of 28 bytes. +/// * `out` must be valid for reads and writes of 32 bytes. /// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. /// * The contents of `state` on return are unspecified. /// * `state` and `out` must not overlap each other (distinct Rust objects never do). /// * Neither `state` nor `out` may overlap the return address on the stack or the 1392 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 28]) { +pub unsafe fn vg_sha512_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 32]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(state: *mut [u8; 192], count: u64, out: *mut [u8; 28]); + type Impl = unsafe extern "sysv64" fn(state: *mut [u8; 192], count: u64, out: *mut [u8; 32]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_224_FINALIZE_SHANI_FEATURES) } - || cpu.contains(crate::sha512::VG_SHA512_224_FINALIZE_SHANI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_256_FINALIZE_SHANI_FEATURES) } + || cpu.contains(crate::sha512::VG_SHA512_256_FINALIZE_SHANI_FEATURES) { - return crate::sha512::vg_sha512_224_finalize_shani; + return crate::sha512::vg_sha512_256_finalize_shani; } - if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_224_FINALIZE_AVX2_FEATURES) } - || cpu.contains(crate::sha512::VG_SHA512_224_FINALIZE_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_256_FINALIZE_AVX2_FEATURES) } + || cpu.contains(crate::sha512::VG_SHA512_256_FINALIZE_AVX2_FEATURES) { - return crate::sha512::vg_sha512_224_finalize_avx2; + return crate::sha512::vg_sha512_256_finalize_avx2; } - crate::sha512::vg_sha512_224_finalize + crate::sha512::vg_sha512_256_finalize } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(state: *mut [u8; 192], count: u64, out: *mut [u8; 28]) { + unsafe extern "sysv64" fn first(state: *mut [u8; 192], count: u64, out: *mut [u8; 32]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` @@ -500,51 +502,49 @@ pub unsafe fn vg_sha512_224_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, s unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, out) } } -/// Finishes a SHA-512/256 computation: if the streaming state `*state` represents a message of `count` bytes, hashed from the initial hash value of SHA-512/256 (as `vg_sha512_256_init` starts it), writes the SHA-512/256 digest of that message (32 bytes, `VG.Spec.Sha512.sha512_256`) to `*out`. +/// Computes the SHA-512/256 digest of the `len` bytes at `data` (32 bytes, `VG.Spec.Sha512.sha512_256`) and writes it to `*out`, in one call: the same result as starting, updating and finishing a SHA-512/256 computation with that message. /// -/// Contract: `VG.Spec.Sha512.finalizeDigestContract` for `VG.Spec.Sha512.H0_512_256` and `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `count` may affect timing, not the state. +/// Contract: `VG.Spec.Sha512.digestContract` for `VG.Spec.Sha512.sha512_256`. Constant time: only the pointers and `len` may affect timing, not the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sha512_256_finalize_shani` if the CPU supports its features (`VG_SHA512_256_FINALIZE_SHANI_FEATURES`); `vg_sha512_256_finalize_avx2` if the CPU supports its features (`VG_SHA512_256_FINALIZE_AVX2_FEATURES`); or else `vg_sha512_256_finalize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sha512_256_digest_shani` if the CPU supports its features (`VG_SHA512_256_DIGEST_SHANI_FEATURES`); `vg_sha512_256_digest_avx2` if the CPU supports its features (`VG_SHA512_256_DIGEST_AVX2_FEATURES`); or else `vg_sha512_256_digest`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// -/// * `state` must be valid for reads and writes of 192 bytes. +/// * `data` must be valid for reads of `len` bytes. /// * `out` must be valid for reads and writes of 32 bytes. -/// * `count` must be the exact length of the message: messages of 2⁶⁴ bytes or more are not supported. -/// * The contents of `state` on return are unspecified. -/// * `state` and `out` must not overlap each other (distinct Rust objects never do). -/// * Neither `state` nor `out` may overlap the return address on the stack or the 1392 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` must not overlap `data` (distinct Rust objects never do). +/// * Neither `data` nor `out` may overlap the return address on the stack or the 1600 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sha512_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, state: *mut [u8; 192], count: u64, out: *mut [u8; 32]) { +pub unsafe fn vg_sha512_256_digest_auto(_dit: verified_garbage_cpu::Dit<'_>, data: *const u8, len: usize, out: *mut [u8; 32]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(state: *mut [u8; 192], count: u64, out: *mut [u8; 32]); + type Impl = unsafe extern "sysv64" fn(data: *const u8, len: usize, out: *mut [u8; 32]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_256_FINALIZE_SHANI_FEATURES) } - || cpu.contains(crate::sha512::VG_SHA512_256_FINALIZE_SHANI_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_256_DIGEST_SHANI_FEATURES) } + || cpu.contains(crate::sha512::VG_SHA512_256_DIGEST_SHANI_FEATURES) { - return crate::sha512::vg_sha512_256_finalize_shani; + return crate::sha512::vg_sha512_256_digest_shani; } - if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_256_FINALIZE_AVX2_FEATURES) } - || cpu.contains(crate::sha512::VG_SHA512_256_FINALIZE_AVX2_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sha512::VG_SHA512_256_DIGEST_AVX2_FEATURES) } + || cpu.contains(crate::sha512::VG_SHA512_256_DIGEST_AVX2_FEATURES) { - return crate::sha512::vg_sha512_256_finalize_avx2; + return crate::sha512::vg_sha512_256_digest_avx2; } - crate::sha512::vg_sha512_256_finalize + crate::sha512::vg_sha512_256_digest } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(state: *mut [u8; 192], count: u64, out: *mut [u8; 32]) { + unsafe extern "sysv64" fn first(data: *const u8, len: usize, out: *mut [u8; 32]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(state, count, out) } + unsafe { f(data, len, out) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -552,5 +552,5 @@ pub unsafe fn vg_sha512_256_finalize_auto(_dit: verified_garbage_cpu::Dit<'_>, s // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(state, count, out) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(data, len, out) } } diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/sm4_gcm.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/sm4_gcm.rs index 2eecaa0418f..76b77324f80 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/sm4_gcm.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/sm4_gcm.rs @@ -27,7 +27,7 @@ pub unsafe fn vg_sm4_gcm_init_auto(_dit: verified_garbage_cpu::Dit<'_>, key: *co /// /// Contract: `VG.Spec.Sm4.Gcm.sealContract`. Constant time: only the pointers and the lengths may affect timing, not the key context, the nonce, the additional data or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_seal_vpclmul` if the CPU supports its features (`VG_SM4_GCM_SEAL_VPCLMUL_FEATURES`); `vg_sm4_gcm_seal_pclmul` if the CPU supports its features (`VG_SM4_GCM_SEAL_PCLMUL_FEATURES`); or else `vg_sm4_gcm_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_seal_vpclmul` if the CPU supports its features (`VG_SM4_GCM_SEAL_VPCLMUL_FEATURES`); `vg_sm4_gcm_seal_pclmul` if the CPU supports its features (`VG_SM4_GCM_SEAL_PCLMUL_FEATURES`); or else `vg_sm4_gcm_seal`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -85,7 +85,7 @@ pub unsafe fn vg_sm4_gcm_seal_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *co /// /// Contract: `VG.Spec.Sm4.Gcm.openContract`. Constant time but for the result: only the pointers, the lengths, `tag_len` and whether the function returns 1 or 0 may affect timing, not the key context, the nonce, the additional data, the data or the tag. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_open_vpclmul` if the CPU supports its features (`VG_SM4_GCM_OPEN_VPCLMUL_FEATURES`); `vg_sm4_gcm_open_pclmul` if the CPU supports its features (`VG_SM4_GCM_OPEN_PCLMUL_FEATURES`); or else `vg_sm4_gcm_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_open_vpclmul` if the CPU supports its features (`VG_SM4_GCM_OPEN_VPCLMUL_FEATURES`); `vg_sm4_gcm_open_pclmul` if the CPU supports its features (`VG_SM4_GCM_OPEN_PCLMUL_FEATURES`); or else `vg_sm4_gcm_open`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -137,50 +137,52 @@ pub unsafe fn vg_sm4_gcm_open_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *co unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, nonce, nonce_len, aad, aad_len, data, len, tag, tag_len) } } -/// Absorbs additional data into an incremental SM4-GCM encryption or decryption: with the key context `*ctx`, if the streaming state `*state` represents a message with `aad_len` bytes (modulo 2⁶⁴) of additional data and no text yet, it then represents that message with the `len` bytes at `data` appended to its additional data. All of the additional data comes before the text (§7.1 step 5): after text, the state represents no message. +/// Starts an incremental SM4-GCM encryption or decryption (NIST SP 800-38D §7): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, makes the streaming state `*state` represent the message with the IV the `nonce_len` bytes at `nonce`, and no additional data or text yet. Continue with `vg_sm4_gcm_stream_aad`, then `vg_sm4_gcm_stream_encrypt` or `vg_sm4_gcm_stream_decrypt`, then `vg_sm4_gcm_stream_finish` or `vg_sm4_gcm_stream_verify`, with the same key context. /// -/// Contract: `VG.Spec.Gcm.streamAadContract` (AES-GCM's, which reads only the hash subkey of the key context). Constant time: only the pointers, `aad_len` and `len` may affect timing, not the key context, the state or the data. +/// The function checks no length: GCM requires a nonce of 1 to `2^61 - 1` bytes (§5.2.1.1), which the caller must check. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_stream_aad_vpclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_AAD_VPCLMUL_FEATURES`); `vg_sm4_gcm_stream_aad_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_AAD_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_aad`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// Contract: `VG.Spec.Gcm.streamInitContract` (AES-GCM's, which reads only the hash subkey of the key context). The streaming state is `J₀`, the GHASH accumulator, a partial block, the next counter block and a keystream block (`VG.Spec.Gcm.StreamRepr`). Constant time: only the pointers and `nonce_len` may affect timing, not the key context or the nonce. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_stream_init_vpclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_INIT_VPCLMUL_FEATURES`); `vg_sm4_gcm_stream_init_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_INIT_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. +/// * `nonce` must be valid for reads of `nonce_len` bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `data` must be valid for reads of `len` bytes. -/// * `state` must not overlap `ctx` or `data` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the return address on the stack or the 2576 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `state` must not overlap `ctx` or `nonce` (distinct Rust objects never do). +/// * None of `ctx`, `nonce` and `state` may overlap the return address on the stack or the 2576 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sm4_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { +pub unsafe fn vg_sm4_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize); + type Impl = unsafe extern "sysv64" fn(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_AAD_VPCLMUL_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_AAD_VPCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_INIT_VPCLMUL_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_INIT_VPCLMUL_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_stream_aad_vpclmul; + return crate::sm4_gcm::vg_sm4_gcm_stream_init_vpclmul; } - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_AAD_PCLMUL_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_AAD_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_INIT_PCLMUL_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_INIT_PCLMUL_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_stream_aad_pclmul; + return crate::sm4_gcm::vg_sm4_gcm_stream_init_pclmul; } - crate::sm4_gcm::vg_sm4_gcm_stream_aad + crate::sm4_gcm::vg_sm4_gcm_stream_init } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { + unsafe extern "sysv64" fn first(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, state, aad_len, data, len) } + unsafe { f(ctx, nonce, nonce_len, state) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -188,55 +190,53 @@ pub unsafe fn vg_sm4_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ct // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, data, len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, nonce, nonce_len, state) } } -/// Starts an incremental SM4-GCM encryption or decryption (NIST SP 800-38D §7): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, makes the streaming state `*state` represent the message with the IV the `nonce_len` bytes at `nonce`, and no additional data or text yet. Continue with `vg_sm4_gcm_stream_aad`, then `vg_sm4_gcm_stream_encrypt` or `vg_sm4_gcm_stream_decrypt`, then `vg_sm4_gcm_stream_finish` or `vg_sm4_gcm_stream_verify`, with the same key context. -/// -/// The function checks no length: GCM requires a nonce of 1 to `2^61 - 1` bytes (§5.2.1.1), which the caller must check. +/// Absorbs additional data into an incremental SM4-GCM encryption or decryption: with the key context `*ctx`, if the streaming state `*state` represents a message with `aad_len` bytes (modulo 2⁶⁴) of additional data and no text yet, it then represents that message with the `len` bytes at `data` appended to its additional data. All of the additional data comes before the text (§7.1 step 5): after text, the state represents no message. /// -/// Contract: `VG.Spec.Gcm.streamInitContract` (AES-GCM's, which reads only the hash subkey of the key context). The streaming state is `J₀`, the GHASH accumulator, a partial block, the next counter block and a keystream block (`VG.Spec.Gcm.StreamRepr`). Constant time: only the pointers and `nonce_len` may affect timing, not the key context or the nonce. +/// Contract: `VG.Spec.Gcm.streamAadContract` (AES-GCM's, which reads only the hash subkey of the key context). Constant time: only the pointers, `aad_len` and `len` may affect timing, not the key context, the state or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_stream_init_vpclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_INIT_VPCLMUL_FEATURES`); `vg_sm4_gcm_stream_init_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_INIT_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_init`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_stream_aad_vpclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_AAD_VPCLMUL_FEATURES`); `vg_sm4_gcm_stream_aad_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_AAD_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_aad`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. -/// * `nonce` must be valid for reads of `nonce_len` bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `state` must not overlap `ctx` or `nonce` (distinct Rust objects never do). -/// * None of `ctx`, `nonce` and `state` may overlap the return address on the stack or the 2576 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads of `len` bytes. +/// * `state` must not overlap `ctx` or `data` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the return address on the stack or the 2576 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sm4_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { +pub unsafe fn vg_sm4_gcm_stream_aad_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]); + type Impl = unsafe extern "sysv64" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_INIT_VPCLMUL_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_INIT_VPCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_AAD_VPCLMUL_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_AAD_VPCLMUL_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_stream_init_vpclmul; + return crate::sm4_gcm::vg_sm4_gcm_stream_aad_vpclmul; } - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_INIT_PCLMUL_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_INIT_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_AAD_PCLMUL_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_AAD_PCLMUL_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_stream_init_pclmul; + return crate::sm4_gcm::vg_sm4_gcm_stream_aad_pclmul; } - crate::sm4_gcm::vg_sm4_gcm_stream_init + crate::sm4_gcm::vg_sm4_gcm_stream_aad } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(ctx: *const [u64; 32], nonce: *const u8, nonce_len: usize, state: *mut [u64; 10]) { + unsafe extern "sysv64" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, data: *const u8, len: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, nonce, nonce_len, state) } + unsafe { f(ctx, state, aad_len, data, len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -244,54 +244,55 @@ pub unsafe fn vg_sm4_gcm_stream_init_auto(_dit: verified_garbage_cpu::Dit<'_>, c // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, nonce, nonce_len, state) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, data, len) } } -/// Finishes an incremental SM4-GCM encryption or decryption (NIST SP 800-38D §7.1 steps 5–6, §7.2 steps 6–7): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, writes its 128-bit tag to `*tag`. A shorter tag is the first bytes of this one; to check a received tag, use `vg_sm4_gcm_stream_verify`. +/// Encrypts the next piece of the text of an incremental SM4-GCM encryption (NIST SP 800-38D §7.1): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and the ciphertext of exactly `text_len` bytes of plaintext so far, encrypts the `len` bytes at `data` in place, as the continuation of that plaintext, and the state then represents the message with them appended. /// -/// Contract: `VG.Spec.Sm4.Gcm.streamFinishContract`. Constant time: only the pointers, `aad_len` and `text_len` may affect timing, not the key context or the state. +/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_stream_finish_vpclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_FINISH_VPCLMUL_FEATURES`); `vg_sm4_gcm_stream_finish_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_FINISH_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_finish`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// Contract: `VG.Spec.Sm4.Gcm.streamEncryptContract`. Constant time: only the pointers, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_stream_encrypt_vpclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_ENCRYPT_VPCLMUL_FEATURES`); `vg_sm4_gcm_stream_encrypt_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_ENCRYPT_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `tag` must be valid for reads and writes of 16 bytes. -/// * The contents of `state` on return are unspecified. -/// * `state` and `tag` must not overlap each other or `ctx` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `tag` may overlap the return address on the stack or the 5792 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads and writes of `len` bytes. +/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the return address on the stack or the 5800 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sm4_gcm_stream_finish_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *mut [u8; 16]) { +pub unsafe fn vg_sm4_gcm_stream_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *mut [u8; 16]); + type Impl = unsafe extern "sysv64" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_FINISH_VPCLMUL_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_FINISH_VPCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_ENCRYPT_VPCLMUL_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_ENCRYPT_VPCLMUL_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_stream_finish_vpclmul; + return crate::sm4_gcm::vg_sm4_gcm_stream_encrypt_vpclmul; } - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_FINISH_PCLMUL_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_FINISH_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_ENCRYPT_PCLMUL_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_ENCRYPT_PCLMUL_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_stream_finish_pclmul; + return crate::sm4_gcm::vg_sm4_gcm_stream_encrypt_pclmul; } - crate::sm4_gcm::vg_sm4_gcm_stream_finish + crate::sm4_gcm::vg_sm4_gcm_stream_encrypt } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *mut [u8; 16]) { + unsafe extern "sysv64" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, state, aad_len, text_len, tag) } + unsafe { f(ctx, state, aad_len, text_len, data, len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -299,54 +300,55 @@ pub unsafe fn vg_sm4_gcm_stream_finish_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, text_len, tag) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, text_len, data, len) } } -/// Finishes an incremental SM4-GCM decryption and checks its tag (NIST SP 800-38D §7.2 steps 6–8): with the key context `*ctx` that `vg_sm4_gcm_init` wrote and the received tag the `tag_len` bytes at `tag`, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, returns 1 if `tag_len` is between 4 and 16 and the received tag is the first `tag_len` bytes of the message's 128-bit tag, and 0 otherwise. The tags are compared without a branch. +/// Decrypts the next piece of the text of an incremental SM4-GCM decryption (NIST SP 800-38D §7.2): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext so far, decrypts the `len` bytes of ciphertext at `data` in place, as the continuation of that ciphertext, and the state then represents the message with them appended. The plaintext is not authenticated until `vg_sm4_gcm_stream_verify` has returned 1. /// -/// Contract: `VG.Spec.Sm4.Gcm.streamVerifyContract`. Constant time: only the pointers, `aad_len`, `text_len` and `tag_len` may affect timing, not the key context, the state or the tags. +/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_stream_verify_vpclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_VERIFY_VPCLMUL_FEATURES`); `vg_sm4_gcm_stream_verify_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_VERIFY_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// Contract: `VG.Spec.Sm4.Gcm.streamDecryptContract`. Constant time: only the pointers, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. +/// +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_stream_decrypt_vpclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_DECRYPT_VPCLMUL_FEATURES`); `vg_sm4_gcm_stream_decrypt_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_DECRYPT_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `tag` must be valid for reads of `tag_len` bytes. -/// * The contents of `state` on return are unspecified. -/// * `state` must not overlap `ctx` or `tag` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `tag` may overlap the return address on the stack or the 5800 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `data` must be valid for reads and writes of `len` bytes. +/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `data` may overlap the return address on the stack or the 5800 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sm4_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *const u8, tag_len: usize) -> u32 { +pub unsafe fn vg_sm4_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *const u8, tag_len: usize) -> u32; + type Impl = unsafe extern "sysv64" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_VERIFY_VPCLMUL_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_VERIFY_VPCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_DECRYPT_VPCLMUL_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_DECRYPT_VPCLMUL_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_stream_verify_vpclmul; + return crate::sm4_gcm::vg_sm4_gcm_stream_decrypt_vpclmul; } - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_VERIFY_PCLMUL_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_VERIFY_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_DECRYPT_PCLMUL_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_DECRYPT_PCLMUL_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_stream_verify_pclmul; + return crate::sm4_gcm::vg_sm4_gcm_stream_decrypt_pclmul; } - crate::sm4_gcm::vg_sm4_gcm_stream_verify + crate::sm4_gcm::vg_sm4_gcm_stream_decrypt } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *const u8, tag_len: usize) -> u32 { + unsafe extern "sysv64" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, state, aad_len, text_len, tag, tag_len) } + unsafe { f(ctx, state, aad_len, text_len, data, len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -354,55 +356,54 @@ pub unsafe fn vg_sm4_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, text_len, tag, tag_len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, text_len, data, len) } } -/// Encrypts the next piece of the text of an incremental SM4-GCM encryption (NIST SP 800-38D §7.1): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and the ciphertext of exactly `text_len` bytes of plaintext so far, encrypts the `len` bytes at `data` in place, as the continuation of that plaintext, and the state then represents the message with them appended. -/// -/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// Finishes an incremental SM4-GCM encryption or decryption (NIST SP 800-38D §7.1 steps 5–6, §7.2 steps 6–7): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, writes its 128-bit tag to `*tag`. A shorter tag is the first bytes of this one; to check a received tag, use `vg_sm4_gcm_stream_verify`. /// -/// Contract: `VG.Spec.Sm4.Gcm.streamEncryptContract`. Constant time: only the pointers, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. +/// Contract: `VG.Spec.Sm4.Gcm.streamFinishContract`. Constant time: only the pointers, `aad_len` and `text_len` may affect timing, not the key context or the state. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_stream_encrypt_vpclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_ENCRYPT_VPCLMUL_FEATURES`); `vg_sm4_gcm_stream_encrypt_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_ENCRYPT_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_stream_finish_vpclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_FINISH_VPCLMUL_FEATURES`); `vg_sm4_gcm_stream_finish_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_FINISH_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_finish`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `data` must be valid for reads and writes of `len` bytes. -/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the return address on the stack or the 5800 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `tag` must be valid for reads and writes of 16 bytes. +/// * The contents of `state` on return are unspecified. +/// * `state` and `tag` must not overlap each other or `ctx` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `tag` may overlap the return address on the stack or the 5792 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sm4_gcm_stream_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { +pub unsafe fn vg_sm4_gcm_stream_finish_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *mut [u8; 16]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); + type Impl = unsafe extern "sysv64" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *mut [u8; 16]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_ENCRYPT_VPCLMUL_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_ENCRYPT_VPCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_FINISH_VPCLMUL_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_FINISH_VPCLMUL_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_stream_encrypt_vpclmul; + return crate::sm4_gcm::vg_sm4_gcm_stream_finish_vpclmul; } - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_ENCRYPT_PCLMUL_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_ENCRYPT_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_FINISH_PCLMUL_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_FINISH_PCLMUL_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_stream_encrypt_pclmul; + return crate::sm4_gcm::vg_sm4_gcm_stream_finish_pclmul; } - crate::sm4_gcm::vg_sm4_gcm_stream_encrypt + crate::sm4_gcm::vg_sm4_gcm_stream_finish } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { + unsafe extern "sysv64" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *mut [u8; 16]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, state, aad_len, text_len, data, len) } + unsafe { f(ctx, state, aad_len, text_len, tag) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -410,55 +411,54 @@ pub unsafe fn vg_sm4_gcm_stream_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_> // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, text_len, data, len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, text_len, tag) } } -/// Decrypts the next piece of the text of an incremental SM4-GCM decryption (NIST SP 800-38D §7.2): with the key context `*ctx` that `vg_sm4_gcm_init` wrote, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext so far, decrypts the `len` bytes of ciphertext at `data` in place, as the continuation of that ciphertext, and the state then represents the message with them appended. The plaintext is not authenticated until `vg_sm4_gcm_stream_verify` has returned 1. -/// -/// The function checks no length: GCM requires at most `2^36 - 32` bytes of text and at most `2^61 - 1` bytes of additional data (§5.2.1.1), which the caller must check. +/// Finishes an incremental SM4-GCM decryption and checks its tag (NIST SP 800-38D §7.2 steps 6–8): with the key context `*ctx` that `vg_sm4_gcm_init` wrote and the received tag the `tag_len` bytes at `tag`, if the streaming state `*state` represents a message with `aad_len` bytes of additional data and exactly `text_len` bytes of ciphertext, returns 1 if `tag_len` is between 4 and 16 and the received tag is the first `tag_len` bytes of the message's 128-bit tag, and 0 otherwise. The tags are compared without a branch. /// -/// Contract: `VG.Spec.Sm4.Gcm.streamDecryptContract`. Constant time: only the pointers, `aad_len`, `text_len` and `len` may affect timing, not the key context, the state or the data. +/// Contract: `VG.Spec.Sm4.Gcm.streamVerifyContract`. Constant time: only the pointers, `aad_len`, `text_len` and `tag_len` may affect timing, not the key context, the state or the tags. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_sm4_gcm_stream_decrypt_vpclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_DECRYPT_VPCLMUL_FEATURES`); `vg_sm4_gcm_stream_decrypt_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_DECRYPT_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_sm4_gcm_stream_verify_vpclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_VERIFY_VPCLMUL_FEATURES`); `vg_sm4_gcm_stream_verify_pclmul` if the CPU supports its features (`VG_SM4_GCM_STREAM_VERIFY_PCLMUL_FEATURES`); or else `vg_sm4_gcm_stream_verify`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `ctx` must be valid for reads of 256 bytes. /// * `state` must be valid for reads and writes of 80 bytes. -/// * `data` must be valid for reads and writes of `len` bytes. -/// * `state` and `data` must not overlap each other or `ctx` (distinct Rust objects never do). -/// * None of `ctx`, `state` and `data` may overlap the return address on the stack or the 5800 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `tag` must be valid for reads of `tag_len` bytes. +/// * The contents of `state` on return are unspecified. +/// * `state` must not overlap `ctx` or `tag` (distinct Rust objects never do). +/// * None of `ctx`, `state` and `tag` may overlap the return address on the stack or the 5800 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_sm4_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { +pub unsafe fn vg_sm4_gcm_stream_verify_auto(_dit: verified_garbage_cpu::Dit<'_>, ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *const u8, tag_len: usize) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize); + type Impl = unsafe extern "sysv64" fn(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *const u8, tag_len: usize) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_DECRYPT_VPCLMUL_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_DECRYPT_VPCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_VERIFY_VPCLMUL_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_VERIFY_VPCLMUL_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_stream_decrypt_vpclmul; + return crate::sm4_gcm::vg_sm4_gcm_stream_verify_vpclmul; } - if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_DECRYPT_PCLMUL_FEATURES) } - || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_DECRYPT_PCLMUL_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_VERIFY_PCLMUL_FEATURES) } + || cpu.contains(crate::sm4_gcm::VG_SM4_GCM_STREAM_VERIFY_PCLMUL_FEATURES) { - return crate::sm4_gcm::vg_sm4_gcm_stream_decrypt_pclmul; + return crate::sm4_gcm::vg_sm4_gcm_stream_verify_pclmul; } - crate::sm4_gcm::vg_sm4_gcm_stream_decrypt + crate::sm4_gcm::vg_sm4_gcm_stream_verify } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, data: *mut u8, len: usize) { + unsafe extern "sysv64" fn first(ctx: *const [u64; 32], state: *mut [u64; 10], aad_len: u64, text_len: u64, tag: *const u8, tag_len: usize) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(ctx, state, aad_len, text_len, data, len) } + unsafe { f(ctx, state, aad_len, text_len, tag, tag_len) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -466,5 +466,5 @@ pub unsafe fn vg_sm4_gcm_stream_decrypt_auto(_dit: verified_garbage_cpu::Dit<'_> // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, text_len, data, len) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(ctx, state, aad_len, text_len, tag, tag_len) } } diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/triple_des.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/triple_des.rs index ce853bd2b02..5ff01e6c2bf 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/triple_des.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/triple_des.rs @@ -26,7 +26,7 @@ pub unsafe fn vg_triple_des_expand_key_auto(_dit: verified_garbage_cpu::Dit<'_>, /// /// Contract: `VG.Spec.TripleDes.ecbEncryptContract`. Constant time: only pointers and `n` may affect timing, not the schedule or data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_triple_des_ecb_encrypt_avx512` if the CPU supports its features (`VG_TRIPLE_DES_ECB_ENCRYPT_AVX512_FEATURES`); `vg_triple_des_ecb_encrypt_avx2` if the CPU supports its features (`VG_TRIPLE_DES_ECB_ENCRYPT_AVX2_FEATURES`); or else `vg_triple_des_ecb_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_triple_des_ecb_encrypt_avx512` if the CPU supports its features (`VG_TRIPLE_DES_ECB_ENCRYPT_AVX512_FEATURES`); `vg_triple_des_ecb_encrypt_avx2` if the CPU supports its features (`VG_TRIPLE_DES_ECB_ENCRYPT_AVX2_FEATURES`); or else `vg_triple_des_ecb_encrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -79,7 +79,7 @@ pub unsafe fn vg_triple_des_ecb_encrypt_auto(_dit: verified_garbage_cpu::Dit<'_> /// /// Contract: `VG.Spec.TripleDes.ecbDecryptContract`. Constant time: only pointers and `n` may affect timing, not the schedule or data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_triple_des_ecb_decrypt_avx512` if the CPU supports its features (`VG_TRIPLE_DES_ECB_DECRYPT_AVX512_FEATURES`); `vg_triple_des_ecb_decrypt_avx2` if the CPU supports its features (`VG_TRIPLE_DES_ECB_DECRYPT_AVX2_FEATURES`); or else `vg_triple_des_ecb_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_triple_des_ecb_decrypt_avx512` if the CPU supports its features (`VG_TRIPLE_DES_ECB_DECRYPT_AVX512_FEATURES`); `vg_triple_des_ecb_decrypt_avx2` if the CPU supports its features (`VG_TRIPLE_DES_ECB_DECRYPT_AVX2_FEATURES`); or else `vg_triple_des_ecb_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/triple_des_cbc.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/triple_des_cbc.rs index 73be33c79ae..ff6560fc5e9 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/triple_des_cbc.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/triple_des_cbc.rs @@ -26,7 +26,7 @@ pub unsafe fn vg_triple_des_cbc_encrypt_chained_auto(_dit: verified_garbage_cpu: /// /// Contract: `VG.Spec.TripleDes.cbcDecryptChainedContract`. Constant time: only the pointers and `n` may affect timing, not the schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_triple_des_cbc_decrypt_chained_avx512` if the CPU supports its features (`VG_TRIPLE_DES_CBC_DECRYPT_CHAINED_AVX512_FEATURES`); `vg_triple_des_cbc_decrypt_chained_avx2` if the CPU supports its features (`VG_TRIPLE_DES_CBC_DECRYPT_CHAINED_AVX2_FEATURES`); or else `vg_triple_des_cbc_decrypt_chained`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_triple_des_cbc_decrypt_chained_avx512` if the CPU supports its features (`VG_TRIPLE_DES_CBC_DECRYPT_CHAINED_AVX512_FEATURES`); `vg_triple_des_cbc_decrypt_chained_avx2` if the CPU supports its features (`VG_TRIPLE_DES_CBC_DECRYPT_CHAINED_AVX2_FEATURES`); or else `vg_triple_des_cbc_decrypt_chained`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/triple_des_cfb.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/triple_des_cfb.rs index 447b9042987..fb38fde4753 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/triple_des_cfb.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/triple_des_cfb.rs @@ -22,11 +22,51 @@ pub unsafe fn vg_triple_des_cfb64_encrypt_auto(_dit: verified_garbage_cpu::Dit<' unsafe { crate::triple_des_cfb::vg_triple_des_cfb64_encrypt(schedule, iv, data, n) } } +/// Triple DES-CFB64 encryption (NIST SP 800-38A §6.3, with 64-bit segments; FIPS 46-3) of the last piece of a message, of any length, in place: replaces its whole 8-byte blocks `P₁ … Pₙ₋₁` at `data` with `Cⱼ = Pⱼ ⊕ CIPH_K(Cⱼ₋₁)`, where `C₀` is the block at `*iv`, and a partial last segment `Pₙ*` of `u` bytes with `Cₙ* = Pₙ* ⊕ MSB_u(Oₙ)`, `Oₙ = CIPH_K(Cₙ₋₁)` (an extension of SP 800-38A, which defines CFB only for whole segments, by the rule of §6.4 and §6.5). Replaces `*iv` with the last ciphertext block, so that a further call continues the message, if `len` is a multiple of 8 (leaving it unchanged if `len = 0`), and otherwise with `Oₙ`: keystream, to be wiped, after which the message must end. `CIPH_K` is Triple DES encryption under the schedule written by `vg_triple_des_expand_key`. +/// +/// Contract: `VG.Spec.TripleDes.cfbEncryptFinalContract`. Constant time: only the pointers and `len` may affect timing, not the schedule, the chaining value or the data. +/// +/// It runs `vg_triple_des_cfb64_encrypt_final`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `schedule` must be valid for reads of 384 bytes. +/// * `iv` must be valid for reads and writes of 8 bytes. +/// * `data` must be valid for reads and writes of `len` bytes. +/// * `iv` and `data` must not overlap each other or `schedule` (distinct Rust objects never do). +/// * None of `schedule`, `iv` and `data` may overlap the return address on the stack or the 984 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_triple_des_cfb64_encrypt_final_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *const [u8; 384], iv: *mut [u8; 8], data: *mut u8, len: usize) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::triple_des_cfb::vg_triple_des_cfb64_encrypt_final(schedule, iv, data, len) } +} + +/// Triple DES-CFB64 decryption (NIST SP 800-38A §6.3, with 64-bit segments; FIPS 46-3) of the last piece of a message, of any length, in place: replaces its whole 8-byte blocks `C₁ … Cₙ₋₁` at `data` with `Pⱼ = Cⱼ ⊕ CIPH_K(Cⱼ₋₁)`, where `C₀` is the block at `*iv`, and a partial last segment `Cₙ*` of `u` bytes with `Pₙ* = Cₙ* ⊕ MSB_u(Oₙ)`, `Oₙ = CIPH_K(Cₙ₋₁)` (an extension of SP 800-38A, which defines CFB only for whole segments, by the rule of §6.4 and §6.5). Replaces `*iv` with the last ciphertext block, the last whole block at `data` on entry, so that a further call continues the message, if `len` is a multiple of 8 (leaving it unchanged if `len = 0`), and otherwise with `Oₙ`: keystream, to be wiped, after which the message must end. `CIPH_K` is Triple DES encryption under the schedule written by `vg_triple_des_expand_key`. +/// +/// Contract: `VG.Spec.TripleDes.cfbDecryptFinalContract`. Constant time: only the pointers and `len` may affect timing, not the schedule, the chaining value or the data. +/// +/// It runs `vg_triple_des_cfb64_decrypt_final`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. +/// +/// # Safety +/// +/// * `schedule` must be valid for reads of 384 bytes. +/// * `iv` must be valid for reads and writes of 8 bytes. +/// * `data` must be valid for reads and writes of `len` bytes. +/// * `iv` and `data` must not overlap each other or `schedule` (distinct Rust objects never do). +/// * None of `schedule`, `iv` and `data` may overlap the return address on the stack or the 984 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +#[allow(clippy::too_many_arguments)] +#[inline] +pub unsafe fn vg_triple_des_cfb64_decrypt_final_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *const [u8; 384], iv: *mut [u8; 8], data: *mut u8, len: usize) { + // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. + unsafe { crate::triple_des_cfb::vg_triple_des_cfb64_decrypt_final(schedule, iv, data, len) } +} + /// Triple DES-CFB64 decryption (NIST SP 800-38A §6.3, with 64-bit segments; FIPS 46-3) of whole blocks, in place: replaces the `n` 8-byte blocks `C₁ … Cₙ` at `data` with `Pⱼ = Cⱼ ⊕ CIPH_K(Cⱼ₋₁)`, where `C₀` is the block at `*iv`, and `*iv` with `Cₙ`, the last block at `data` on entry (leaving it unchanged if `n = 0`), so that a further call continues the message. `CIPH_K` is Triple DES encryption (also for decryption) under the schedule written by `vg_triple_des_expand_key`. /// /// Contract: `VG.Spec.TripleDes.cfbDecryptContract`. Constant time: only the pointers and `n` may affect timing, not the schedule, the chaining value or the data. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_triple_des_cfb64_decrypt_avx512` if the CPU supports its features (`VG_TRIPLE_DES_CFB64_DECRYPT_AVX512_FEATURES`); `vg_triple_des_cfb64_decrypt_avx2` if the CPU supports its features (`VG_TRIPLE_DES_CFB64_DECRYPT_AVX2_FEATURES`); or else `vg_triple_des_cfb64_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_triple_des_cfb64_decrypt_avx512` if the CPU supports its features (`VG_TRIPLE_DES_CFB64_DECRYPT_AVX512_FEATURES`); `vg_triple_des_cfb64_decrypt_avx2` if the CPU supports its features (`VG_TRIPLE_DES_CFB64_DECRYPT_AVX2_FEATURES`); or else `vg_triple_des_cfb64_decrypt`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// @@ -75,43 +115,3 @@ pub unsafe fn vg_triple_des_cfb64_decrypt_auto(_dit: verified_garbage_cpu::Dit<' // either carries the caller's obligations, which are those of every implementation. unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(schedule, iv, data, n) } } - -/// Triple DES-CFB64 encryption (NIST SP 800-38A §6.3, with 64-bit segments; FIPS 46-3) of the last piece of a message, of any length, in place: replaces its whole 8-byte blocks `P₁ … Pₙ₋₁` at `data` with `Cⱼ = Pⱼ ⊕ CIPH_K(Cⱼ₋₁)`, where `C₀` is the block at `*iv`, and a partial last segment `Pₙ*` of `u` bytes with `Cₙ* = Pₙ* ⊕ MSB_u(Oₙ)`, `Oₙ = CIPH_K(Cₙ₋₁)` (an extension of SP 800-38A, which defines CFB only for whole segments, by the rule of §6.4 and §6.5). Replaces `*iv` with the last ciphertext block, so that a further call continues the message, if `len` is a multiple of 8 (leaving it unchanged if `len = 0`), and otherwise with `Oₙ`: keystream, to be wiped, after which the message must end. `CIPH_K` is Triple DES encryption under the schedule written by `vg_triple_des_expand_key`. -/// -/// Contract: `VG.Spec.TripleDes.cfbEncryptFinalContract`. Constant time: only the pointers and `len` may affect timing, not the schedule, the chaining value or the data. -/// -/// It runs `vg_triple_des_cfb64_encrypt_final`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `schedule` must be valid for reads of 384 bytes. -/// * `iv` must be valid for reads and writes of 8 bytes. -/// * `data` must be valid for reads and writes of `len` bytes. -/// * `iv` and `data` must not overlap each other or `schedule` (distinct Rust objects never do). -/// * None of `schedule`, `iv` and `data` may overlap the return address on the stack or the 984 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_triple_des_cfb64_encrypt_final_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *const [u8; 384], iv: *mut [u8; 8], data: *mut u8, len: usize) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::triple_des_cfb::vg_triple_des_cfb64_encrypt_final(schedule, iv, data, len) } -} - -/// Triple DES-CFB64 decryption (NIST SP 800-38A §6.3, with 64-bit segments; FIPS 46-3) of the last piece of a message, of any length, in place: replaces its whole 8-byte blocks `C₁ … Cₙ₋₁` at `data` with `Pⱼ = Cⱼ ⊕ CIPH_K(Cⱼ₋₁)`, where `C₀` is the block at `*iv`, and a partial last segment `Cₙ*` of `u` bytes with `Pₙ* = Cₙ* ⊕ MSB_u(Oₙ)`, `Oₙ = CIPH_K(Cₙ₋₁)` (an extension of SP 800-38A, which defines CFB only for whole segments, by the rule of §6.4 and §6.5). Replaces `*iv` with the last ciphertext block, the last whole block at `data` on entry, so that a further call continues the message, if `len` is a multiple of 8 (leaving it unchanged if `len = 0`), and otherwise with `Oₙ`: keystream, to be wiped, after which the message must end. `CIPH_K` is Triple DES encryption under the schedule written by `vg_triple_des_expand_key`. -/// -/// Contract: `VG.Spec.TripleDes.cfbDecryptFinalContract`. Constant time: only the pointers and `len` may affect timing, not the schedule, the chaining value or the data. -/// -/// It runs `vg_triple_des_cfb64_decrypt_final`, the only implementation of it on this target, which needs no CPU feature (`VG.Rust.Group`), so that the code calling it is the same on every target. -/// -/// # Safety -/// -/// * `schedule` must be valid for reads of 384 bytes. -/// * `iv` must be valid for reads and writes of 8 bytes. -/// * `data` must be valid for reads and writes of `len` bytes. -/// * `iv` and `data` must not overlap each other or `schedule` (distinct Rust objects never do). -/// * None of `schedule`, `iv` and `data` may overlap the return address on the stack or the 984 bytes of stack below it, or wrap around the end of the address space (no Rust object does). -#[allow(clippy::too_many_arguments)] -#[inline] -pub unsafe fn vg_triple_des_cfb64_decrypt_final_auto(_dit: verified_garbage_cpu::Dit<'_>, schedule: *const [u8; 384], iv: *mut [u8; 8], data: *mut u8, len: usize) { - // SAFETY: the caller's obligations, which are the base's; it needs no CPU feature. - unsafe { crate::triple_des_cfb::vg_triple_des_cfb64_decrypt_final(schedule, iv, data, len) } -} diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/x25519.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/x25519.rs index 8d3a9370335..02b292691d5 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/x25519.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/x25519.rs @@ -2,51 +2,52 @@ //! The dispatchers of the verified `x25519` functions for `x86_64` //! (`crate::x25519`, `VG.Rust.dispatcher`). -/// Computes an X25519 public key (RFC 7748 §6.1): writes `X25519(k, 9)`, the X25519 function of the 32-byte scalar `k` at `scalar` and the base point's u-coordinate 9, to `*out`. The scalar is decoded (clamped). +/// Computes an X25519 shared secret (RFC 7748 §6.1): writes `X25519(k, u)` to `*out`, for the 32-byte scalar `k` at `scalar` and the 32-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped) and the most significant bit of `u` masked as RFC 7748 specifies (§5). /// -/// Contract: `VG.Spec.X25519.x25519BaseContract`. Constant time: only the pointers may affect timing, not the scalar. +/// Contract: `VG.Spec.X25519.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_x25519_base_ifma` if the CPU supports its features (`VG_X25519_BASE_IFMA_FEATURES`); `vg_x25519_base_adx` if the CPU supports its features (`VG_X25519_BASE_ADX_FEATURES`); or else `vg_x25519_base`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_x25519_exchange_ifma` if the CPU supports its features (`VG_X25519_EXCHANGE_IFMA_FEATURES`); `vg_x25519_exchange_adx` if the CPU supports its features (`VG_X25519_EXCHANGE_ADX_FEATURES`); or else `vg_x25519_exchange`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `out` must be valid for reads and writes of 32 bytes. /// * `scalar` must be valid for reads of 32 bytes. -/// * `scratch` must be valid for reads and writes of 8192 bytes. +/// * `point` must be valid for reads of 32 bytes. +/// * `scratch` must be valid for reads and writes of 4096 bytes. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other or `scalar` (distinct Rust objects never do). -/// * None of `out`, `scalar` and `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `scalar` or `point` (distinct Rust objects never do). +/// * None of `out`, `scalar`, `point` and `scratch` may overlap the return address on the stack or the 24 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x25519_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], scratch: *mut [u64; 1024]) { +pub unsafe fn vg_x25519_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], point: *const [u8; 32], scratch: *mut [u64; 512]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 32], scalar: *const [u8; 32], scratch: *mut [u64; 1024]); + type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 32], scalar: *const [u8; 32], point: *const [u8; 32], scratch: *mut [u64; 512]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::x25519::VG_X25519_BASE_IFMA_FEATURES) } - || cpu.contains(crate::x25519::VG_X25519_BASE_IFMA_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::x25519::VG_X25519_EXCHANGE_IFMA_FEATURES) } + || cpu.contains(crate::x25519::VG_X25519_EXCHANGE_IFMA_FEATURES) { - return crate::x25519::vg_x25519_base_ifma; + return crate::x25519::vg_x25519_exchange_ifma; } - if const { verified_garbage_cpu::target_contains(crate::x25519::VG_X25519_BASE_ADX_FEATURES) } - || cpu.contains(crate::x25519::VG_X25519_BASE_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::x25519::VG_X25519_EXCHANGE_ADX_FEATURES) } + || cpu.contains(crate::x25519::VG_X25519_EXCHANGE_ADX_FEATURES) { - return crate::x25519::vg_x25519_base_adx; + return crate::x25519::vg_x25519_exchange_adx; } - crate::x25519::vg_x25519_base + crate::x25519::vg_x25519_exchange } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(out: *mut [u8; 32], scalar: *const [u8; 32], scratch: *mut [u64; 1024]) { + unsafe extern "sysv64" fn first(out: *mut [u8; 32], scalar: *const [u8; 32], point: *const [u8; 32], scratch: *mut [u64; 512]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, scalar, scratch) } + unsafe { f(out, scalar, point, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -54,55 +55,54 @@ pub unsafe fn vg_x25519_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, scalar, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, scalar, point, scratch) } } -/// Computes an X25519 shared secret (RFC 7748 §6.1): writes `X25519(k, u)` to `*out`, for the 32-byte scalar `k` at `scalar` and the 32-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped) and the most significant bit of `u` masked as RFC 7748 specifies (§5). +/// Computes an X25519 public key (RFC 7748 §6.1): writes `X25519(k, 9)`, the X25519 function of the 32-byte scalar `k` at `scalar` and the base point's u-coordinate 9, to `*out`. The scalar is decoded (clamped). /// -/// Contract: `VG.Spec.X25519.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. +/// Contract: `VG.Spec.X25519.x25519BaseContract`. Constant time: only the pointers may affect timing, not the scalar. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_x25519_exchange_ifma` if the CPU supports its features (`VG_X25519_EXCHANGE_IFMA_FEATURES`); `vg_x25519_exchange_adx` if the CPU supports its features (`VG_X25519_EXCHANGE_ADX_FEATURES`); or else `vg_x25519_exchange`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_x25519_base_ifma` if the CPU supports its features (`VG_X25519_BASE_IFMA_FEATURES`); `vg_x25519_base_adx` if the CPU supports its features (`VG_X25519_BASE_ADX_FEATURES`); or else `vg_x25519_base`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `out` must be valid for reads and writes of 32 bytes. /// * `scalar` must be valid for reads of 32 bytes. -/// * `point` must be valid for reads of 32 bytes. -/// * `scratch` must be valid for reads and writes of 4096 bytes. +/// * `scratch` must be valid for reads and writes of 8192 bytes. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `scalar` or `point` (distinct Rust objects never do). -/// * None of `out`, `scalar`, `point` and `scratch` may overlap the return address on the stack or the 24 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other or `scalar` (distinct Rust objects never do). +/// * None of `out`, `scalar` and `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x25519_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], point: *const [u8; 32], scratch: *mut [u64; 512]) -> u32 { +pub unsafe fn vg_x25519_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 32], scalar: *const [u8; 32], scratch: *mut [u64; 1024]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 32], scalar: *const [u8; 32], point: *const [u8; 32], scratch: *mut [u64; 512]) -> u32; + type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 32], scalar: *const [u8; 32], scratch: *mut [u64; 1024]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::x25519::VG_X25519_EXCHANGE_IFMA_FEATURES) } - || cpu.contains(crate::x25519::VG_X25519_EXCHANGE_IFMA_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::x25519::VG_X25519_BASE_IFMA_FEATURES) } + || cpu.contains(crate::x25519::VG_X25519_BASE_IFMA_FEATURES) { - return crate::x25519::vg_x25519_exchange_ifma; + return crate::x25519::vg_x25519_base_ifma; } - if const { verified_garbage_cpu::target_contains(crate::x25519::VG_X25519_EXCHANGE_ADX_FEATURES) } - || cpu.contains(crate::x25519::VG_X25519_EXCHANGE_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::x25519::VG_X25519_BASE_ADX_FEATURES) } + || cpu.contains(crate::x25519::VG_X25519_BASE_ADX_FEATURES) { - return crate::x25519::vg_x25519_exchange_adx; + return crate::x25519::vg_x25519_base_adx; } - crate::x25519::vg_x25519_exchange + crate::x25519::vg_x25519_base } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(out: *mut [u8; 32], scalar: *const [u8; 32], point: *const [u8; 32], scratch: *mut [u64; 512]) -> u32 { + unsafe extern "sysv64" fn first(out: *mut [u8; 32], scalar: *const [u8; 32], scratch: *mut [u64; 1024]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, scalar, point, scratch) } + unsafe { f(out, scalar, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -110,5 +110,5 @@ pub unsafe fn vg_x25519_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, scalar, point, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, scalar, scratch) } } diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/x448.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/x448.rs index f60b120c3f0..f3f3b3442c2 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/x448.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/x448.rs @@ -2,46 +2,47 @@ //! The dispatchers of the verified `x448` functions for `x86_64` //! (`crate::x448`, `VG.Rust.dispatcher`). -/// Computes an X448 public key (RFC 7748 §6.2): writes `X448(k, 5)`, the X448 function of the 56-byte scalar `k` at `scalar` and the base point's u-coordinate 5, to `*out`. The scalar is decoded (clamped). +/// Computes an X448 shared secret (RFC 7748 §6.2): writes `X448(k, u)` to `*out`, for the 56-byte scalar `k` at `scalar` and the 56-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped); all 448 bits of `u` are used and noncanonical coordinates are reduced modulo the field prime (§5). /// -/// Contract: `VG.Spec.X448.x448BaseContract`. Constant time: only the pointers may affect timing, not the scalar. +/// Contract: `VG.Spec.X448.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_x448_base_adx` if the CPU supports its features (`VG_X448_BASE_ADX_FEATURES`); or else `vg_x448_base`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_x448_exchange_adx` if the CPU supports its features (`VG_X448_EXCHANGE_ADX_FEATURES`); or else `vg_x448_exchange`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `out` must be valid for reads and writes of 56 bytes. /// * `scalar` must be valid for reads of 56 bytes. +/// * `point` must be valid for reads of 56 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other or `scalar` (distinct Rust objects never do). -/// * None of `out`, `scalar` and `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other, `scalar` or `point` (distinct Rust objects never do). +/// * None of `out`, `scalar`, `point` and `scratch` may overlap the return address on the stack or the 24 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x448_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], scratch: *mut [u64; 1024]) { +pub unsafe fn vg_x448_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], point: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 56], scalar: *const [u8; 56], scratch: *mut [u64; 1024]); + type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 56], scalar: *const [u8; 56], point: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32; /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::x448::VG_X448_BASE_ADX_FEATURES) } - || cpu.contains(crate::x448::VG_X448_BASE_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::x448::VG_X448_EXCHANGE_ADX_FEATURES) } + || cpu.contains(crate::x448::VG_X448_EXCHANGE_ADX_FEATURES) { - return crate::x448::vg_x448_base_adx; + return crate::x448::vg_x448_exchange_adx; } - crate::x448::vg_x448_base + crate::x448::vg_x448_exchange } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(out: *mut [u8; 56], scalar: *const [u8; 56], scratch: *mut [u64; 1024]) { + unsafe extern "sysv64" fn first(out: *mut [u8; 56], scalar: *const [u8; 56], point: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, scalar, scratch) } + unsafe { f(out, scalar, point, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -49,50 +50,49 @@ pub unsafe fn vg_x448_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [ // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, scalar, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, scalar, point, scratch) } } -/// Computes an X448 shared secret (RFC 7748 §6.2): writes `X448(k, u)` to `*out`, for the 56-byte scalar `k` at `scalar` and the 56-byte u-coordinate `u` at `point`, and returns 0 if it is all zero (for a `u` of small order), 1 otherwise. The scalar is decoded (clamped); all 448 bits of `u` are used and noncanonical coordinates are reduced modulo the field prime (§5). +/// Computes an X448 public key (RFC 7748 §6.2): writes `X448(k, 5)`, the X448 function of the 56-byte scalar `k` at `scalar` and the base point's u-coordinate 5, to `*out`. The scalar is decoded (clamped). /// -/// Contract: `VG.Spec.X448.exchangeContract`. Constant time: only the pointers may affect timing, not the scalar or the u-coordinate; the return value depends on both. +/// Contract: `VG.Spec.X448.x448BaseContract`. Constant time: only the pointers may affect timing, not the scalar. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_x448_exchange_adx` if the CPU supports its features (`VG_X448_EXCHANGE_ADX_FEATURES`); or else `vg_x448_exchange`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_x448_base_adx` if the CPU supports its features (`VG_X448_BASE_ADX_FEATURES`); or else `vg_x448_base`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// /// * `out` must be valid for reads and writes of 56 bytes. /// * `scalar` must be valid for reads of 56 bytes. -/// * `point` must be valid for reads of 56 bytes. /// * `scratch` must be valid for reads and writes of 8192 bytes. /// * The contents of `scratch` on return are unspecified and may contain secrets; the caller must destroy them after use. -/// * `out` and `scratch` must not overlap each other, `scalar` or `point` (distinct Rust objects never do). -/// * None of `out`, `scalar`, `point` and `scratch` may overlap the return address on the stack or the 24 bytes of stack below it, or wrap around the end of the address space (no Rust object does). +/// * `out` and `scratch` must not overlap each other or `scalar` (distinct Rust objects never do). +/// * None of `out`, `scalar` and `scratch` may overlap the return address on the stack or the 8 bytes of stack below it, or wrap around the end of the address space (no Rust object does). #[allow(clippy::too_many_arguments)] #[inline] -pub unsafe fn vg_x448_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], point: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { +pub unsafe fn vg_x448_base_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *mut [u8; 56], scalar: *const [u8; 56], scratch: *mut [u64; 1024]) { /// The type of the implementations. - type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 56], scalar: *const [u8; 56], point: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32; + type Impl = unsafe extern "sysv64" fn(out: *mut [u8; 56], scalar: *const [u8; 56], scratch: *mut [u64; 1024]); /// The implementation to run on this CPU (`VG.Rust.dispatcher`): one that needs only /// features the CPU has. #[cold] fn select() -> Impl { let cpu = verified_garbage_cpu::detected(); - if const { verified_garbage_cpu::target_contains(crate::x448::VG_X448_EXCHANGE_ADX_FEATURES) } - || cpu.contains(crate::x448::VG_X448_EXCHANGE_ADX_FEATURES) + if const { verified_garbage_cpu::target_contains(crate::x448::VG_X448_BASE_ADX_FEATURES) } + || cpu.contains(crate::x448::VG_X448_BASE_ADX_FEATURES) { - return crate::x448::vg_x448_exchange_adx; + return crate::x448::vg_x448_base_adx; } - crate::x448::vg_x448_exchange + crate::x448::vg_x448_base } /// The first call: chooses the implementation, records it in `CHOSEN` for the calls /// after it, and runs it. #[cold] - unsafe extern "sysv64" fn first(out: *mut [u8; 56], scalar: *const [u8; 56], point: *const [u8; 56], scratch: *mut [u64; 1024]) -> u32 { + unsafe extern "sysv64" fn first(out: *mut [u8; 56], scalar: *const [u8; 56], scratch: *mut [u64; 1024]) { let f = select(); CHOSEN.store(f as *mut (), core::sync::atomic::Ordering::Relaxed); // SAFETY: the caller's obligations, which are those of every implementation, `f` // among them, and the CPU has the features `f` needs. - unsafe { f(out, scalar, point, scratch) } + unsafe { f(out, scalar, scratch) } } /// The function to call: `first`, until it records the implementation it chose. static CHOSEN: core::sync::atomic::AtomicPtr<()> = @@ -100,5 +100,5 @@ pub unsafe fn vg_x448_exchange_auto(_dit: verified_garbage_cpu::Dit<'_>, out: *m // SAFETY: `CHOSEN` only ever holds a pointer to a function of the type `Impl`: `first`, // or an implementation `select` returned, which needs only features the CPU has. Calling // either carries the caller's obligations, which are those of every implementation. - unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, scalar, point, scratch) } + unsafe { core::mem::transmute::<*mut (), Impl>(CHOSEN.load(core::sync::atomic::Ordering::Relaxed))(out, scalar, scratch) } } diff --git a/crates/verified-garbage-asm-x86_64/src/dispatch/zeroize.rs b/crates/verified-garbage-asm-x86_64/src/dispatch/zeroize.rs index 4eb603931a2..80bc128ba91 100644 --- a/crates/verified-garbage-asm-x86_64/src/dispatch/zeroize.rs +++ b/crates/verified-garbage-asm-x86_64/src/dispatch/zeroize.rs @@ -6,7 +6,7 @@ /// /// Contract: `VG.Spec.Zeroize.zeroizeContract`. Constant time: only `p` and `len` may affect timing, not the bytes wiped. /// -/// It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): `vg_zeroize_avx` if the CPU supports its features (`VG_ZEROIZE_AVX_FEATURES`); or else `vg_zeroize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. +/// It runs the first of these implementations that applies, in the order its registration file lists them: `vg_zeroize_avx` if the CPU supports its features (`VG_ZEROIZE_AVX_FEATURES`); or else `vg_zeroize`, which needs no CPU feature. It chooses on its first call, and calls the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) and what it writes. /// /// # Safety /// diff --git a/crates/verified-garbage-asm-x86_64/src/fn_tables/mod.rs b/crates/verified-garbage-asm-x86_64/src/fn_tables/mod.rs index 384828f90ea..d8e0d8e3d98 100644 --- a/crates/verified-garbage-asm-x86_64/src/fn_tables/mod.rs +++ b/crates/verified-garbage-asm-x86_64/src/fn_tables/mod.rs @@ -2,36 +2,36 @@ //! The tables of functions of the verified functions for `x86_64` (`Artifact.tables`). #![allow(dead_code, unused_imports)] -/// A table of functions of the interface `Hash` (`VG.FnTable`): the addresses of its 3 functions, then its 2 words of data. Only the statics of this module are of this type. +/// A table of functions of the interface `Hmac` (`VG.FnTable`): the addresses of its 3 functions, then its 3 words of data. Only the statics of this module are of this type. #[repr(C)] -pub struct Hash { +pub struct Hmac { fns: [*const (); 3], - data: [usize; 2], + data: [usize; 3], } -// SAFETY: a `Hash` is immutable, and the functions it holds the addresses of may be called from any thread. -unsafe impl Sync for Hash {} +// SAFETY: a `Hmac` is immutable, and the functions it holds the addresses of may be called from any thread. +unsafe impl Sync for Hmac {} -impl Hash { +impl Hmac { /// A pointer to the table's words, as the verified functions that take it read them. - pub const fn as_ptr(&'static self) -> *const [usize; 5] { + pub const fn as_ptr(&'static self) -> *const [usize; 6] { core::ptr::from_ref(self).cast() } } -/// A table of functions of the interface `Hmac` (`VG.FnTable`): the addresses of its 3 functions, then its 3 words of data. Only the statics of this module are of this type. +/// A table of functions of the interface `Hash` (`VG.FnTable`): the addresses of its 3 functions, then its 2 words of data. Only the statics of this module are of this type. #[repr(C)] -pub struct Hmac { +pub struct Hash { fns: [*const (); 3], - data: [usize; 3], + data: [usize; 2], } -// SAFETY: a `Hmac` is immutable, and the functions it holds the addresses of may be called from any thread. -unsafe impl Sync for Hmac {} +// SAFETY: a `Hash` is immutable, and the functions it holds the addresses of may be called from any thread. +unsafe impl Sync for Hash {} -impl Hmac { +impl Hash { /// A pointer to the table's words, as the verified functions that take it read them. - pub const fn as_ptr(&'static self) -> *const [usize; 6] { + pub const fn as_ptr(&'static self) -> *const [usize; 5] { core::ptr::from_ref(self).cast() } } @@ -54,265 +54,265 @@ impl MdHash { } #[rustfmt::skip] -mod vg_hash_md5; -pub use vg_hash_md5::VG_HASH_MD5; +mod vg_hmac_md5; +pub use vg_hmac_md5::VG_HMAC_MD5; #[rustfmt::skip] -mod vg_hash_sha1; -pub use vg_hash_sha1::VG_HASH_SHA1; +mod vg_hmac_sha1_shani; +pub use vg_hmac_sha1_shani::{VG_HMAC_SHA1_SHANI, VG_HMAC_SHA1_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha1_avx_bmi; -pub use vg_hash_sha1_avx_bmi::{VG_HASH_SHA1_AVX_BMI, VG_HASH_SHA1_AVX_BMI_FEATURES}; +mod vg_hmac_sha1_avx_bmi; +pub use vg_hmac_sha1_avx_bmi::{VG_HMAC_SHA1_AVX_BMI, VG_HMAC_SHA1_AVX_BMI_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha1_shani; -pub use vg_hash_sha1_shani::{VG_HASH_SHA1_SHANI, VG_HASH_SHA1_SHANI_FEATURES}; +mod vg_hmac_sha1; +pub use vg_hmac_sha1::VG_HMAC_SHA1; #[rustfmt::skip] -mod vg_hash_sha224; -pub use vg_hash_sha224::VG_HASH_SHA224; +mod vg_hmac_sha224_shani; +pub use vg_hmac_sha224_shani::{VG_HMAC_SHA224_SHANI, VG_HMAC_SHA224_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha224_avx2; -pub use vg_hash_sha224_avx2::{VG_HASH_SHA224_AVX2, VG_HASH_SHA224_AVX2_FEATURES}; +mod vg_hmac_sha224_avx2; +pub use vg_hmac_sha224_avx2::{VG_HMAC_SHA224_AVX2, VG_HMAC_SHA224_AVX2_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha224_shani; -pub use vg_hash_sha224_shani::{VG_HASH_SHA224_SHANI, VG_HASH_SHA224_SHANI_FEATURES}; +mod vg_hmac_sha224; +pub use vg_hmac_sha224::VG_HMAC_SHA224; #[rustfmt::skip] -mod vg_hash_sha256; -pub use vg_hash_sha256::VG_HASH_SHA256; +mod vg_hmac_sha256_shani; +pub use vg_hmac_sha256_shani::{VG_HMAC_SHA256_SHANI, VG_HMAC_SHA256_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha256_avx2; -pub use vg_hash_sha256_avx2::{VG_HASH_SHA256_AVX2, VG_HASH_SHA256_AVX2_FEATURES}; +mod vg_hmac_sha256_avx2; +pub use vg_hmac_sha256_avx2::{VG_HMAC_SHA256_AVX2, VG_HMAC_SHA256_AVX2_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha256_shani; -pub use vg_hash_sha256_shani::{VG_HASH_SHA256_SHANI, VG_HASH_SHA256_SHANI_FEATURES}; +mod vg_hmac_sha256; +pub use vg_hmac_sha256::VG_HMAC_SHA256; #[rustfmt::skip] -mod vg_hash_sha384; -pub use vg_hash_sha384::VG_HASH_SHA384; +mod vg_hmac_sha384_shani; +pub use vg_hmac_sha384_shani::{VG_HMAC_SHA384_SHANI, VG_HMAC_SHA384_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha384_avx2; -pub use vg_hash_sha384_avx2::{VG_HASH_SHA384_AVX2, VG_HASH_SHA384_AVX2_FEATURES}; +mod vg_hmac_sha384_avx2; +pub use vg_hmac_sha384_avx2::{VG_HMAC_SHA384_AVX2, VG_HMAC_SHA384_AVX2_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha384_shani; -pub use vg_hash_sha384_shani::{VG_HASH_SHA384_SHANI, VG_HASH_SHA384_SHANI_FEATURES}; +mod vg_hmac_sha384; +pub use vg_hmac_sha384::VG_HMAC_SHA384; #[rustfmt::skip] -mod vg_hash_sha512; -pub use vg_hash_sha512::VG_HASH_SHA512; +mod vg_hmac_sha512_shani; +pub use vg_hmac_sha512_shani::{VG_HMAC_SHA512_SHANI, VG_HMAC_SHA512_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha512_224; -pub use vg_hash_sha512_224::VG_HASH_SHA512_224; +mod vg_hmac_sha512_avx2; +pub use vg_hmac_sha512_avx2::{VG_HMAC_SHA512_AVX2, VG_HMAC_SHA512_AVX2_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha512_224_avx2; -pub use vg_hash_sha512_224_avx2::{VG_HASH_SHA512_224_AVX2, VG_HASH_SHA512_224_AVX2_FEATURES}; +mod vg_hmac_sha512; +pub use vg_hmac_sha512::VG_HMAC_SHA512; #[rustfmt::skip] -mod vg_hash_sha512_224_shani; -pub use vg_hash_sha512_224_shani::{VG_HASH_SHA512_224_SHANI, VG_HASH_SHA512_224_SHANI_FEATURES}; +mod vg_hmac_sha512_224_shani; +pub use vg_hmac_sha512_224_shani::{VG_HMAC_SHA512_224_SHANI, VG_HMAC_SHA512_224_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha512_256; -pub use vg_hash_sha512_256::VG_HASH_SHA512_256; +mod vg_hmac_sha512_224_avx2; +pub use vg_hmac_sha512_224_avx2::{VG_HMAC_SHA512_224_AVX2, VG_HMAC_SHA512_224_AVX2_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha512_256_avx2; -pub use vg_hash_sha512_256_avx2::{VG_HASH_SHA512_256_AVX2, VG_HASH_SHA512_256_AVX2_FEATURES}; +mod vg_hmac_sha512_224; +pub use vg_hmac_sha512_224::VG_HMAC_SHA512_224; #[rustfmt::skip] -mod vg_hash_sha512_256_shani; -pub use vg_hash_sha512_256_shani::{VG_HASH_SHA512_256_SHANI, VG_HASH_SHA512_256_SHANI_FEATURES}; +mod vg_hmac_sha512_256_shani; +pub use vg_hmac_sha512_256_shani::{VG_HMAC_SHA512_256_SHANI, VG_HMAC_SHA512_256_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha512_avx2; -pub use vg_hash_sha512_avx2::{VG_HASH_SHA512_AVX2, VG_HASH_SHA512_AVX2_FEATURES}; +mod vg_hmac_sha512_256_avx2; +pub use vg_hmac_sha512_256_avx2::{VG_HMAC_SHA512_256_AVX2, VG_HMAC_SHA512_256_AVX2_FEATURES}; #[rustfmt::skip] -mod vg_hash_sha512_shani; -pub use vg_hash_sha512_shani::{VG_HASH_SHA512_SHANI, VG_HASH_SHA512_SHANI_FEATURES}; +mod vg_hmac_sha512_256; +pub use vg_hmac_sha512_256::VG_HMAC_SHA512_256; #[rustfmt::skip] -mod vg_hmac_md5; -pub use vg_hmac_md5::VG_HMAC_MD5; +mod vg_hash_md5; +pub use vg_hash_md5::VG_HASH_MD5; #[rustfmt::skip] -mod vg_hmac_sha1; -pub use vg_hmac_sha1::VG_HMAC_SHA1; +mod vg_hash_sha1_shani; +pub use vg_hash_sha1_shani::{VG_HASH_SHA1_SHANI, VG_HASH_SHA1_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha1_avx_bmi; -pub use vg_hmac_sha1_avx_bmi::{VG_HMAC_SHA1_AVX_BMI, VG_HMAC_SHA1_AVX_BMI_FEATURES}; +mod vg_hash_sha1_avx_bmi; +pub use vg_hash_sha1_avx_bmi::{VG_HASH_SHA1_AVX_BMI, VG_HASH_SHA1_AVX_BMI_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha1_shani; -pub use vg_hmac_sha1_shani::{VG_HMAC_SHA1_SHANI, VG_HMAC_SHA1_SHANI_FEATURES}; +mod vg_hash_sha1; +pub use vg_hash_sha1::VG_HASH_SHA1; #[rustfmt::skip] -mod vg_hmac_sha224; -pub use vg_hmac_sha224::VG_HMAC_SHA224; +mod vg_hash_sha224_shani; +pub use vg_hash_sha224_shani::{VG_HASH_SHA224_SHANI, VG_HASH_SHA224_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha224_avx2; -pub use vg_hmac_sha224_avx2::{VG_HMAC_SHA224_AVX2, VG_HMAC_SHA224_AVX2_FEATURES}; +mod vg_hash_sha224_avx2; +pub use vg_hash_sha224_avx2::{VG_HASH_SHA224_AVX2, VG_HASH_SHA224_AVX2_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha224_shani; -pub use vg_hmac_sha224_shani::{VG_HMAC_SHA224_SHANI, VG_HMAC_SHA224_SHANI_FEATURES}; +mod vg_hash_sha224; +pub use vg_hash_sha224::VG_HASH_SHA224; #[rustfmt::skip] -mod vg_hmac_sha256; -pub use vg_hmac_sha256::VG_HMAC_SHA256; +mod vg_hash_sha256_shani; +pub use vg_hash_sha256_shani::{VG_HASH_SHA256_SHANI, VG_HASH_SHA256_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha256_avx2; -pub use vg_hmac_sha256_avx2::{VG_HMAC_SHA256_AVX2, VG_HMAC_SHA256_AVX2_FEATURES}; +mod vg_hash_sha256_avx2; +pub use vg_hash_sha256_avx2::{VG_HASH_SHA256_AVX2, VG_HASH_SHA256_AVX2_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha256_shani; -pub use vg_hmac_sha256_shani::{VG_HMAC_SHA256_SHANI, VG_HMAC_SHA256_SHANI_FEATURES}; +mod vg_hash_sha256; +pub use vg_hash_sha256::VG_HASH_SHA256; #[rustfmt::skip] -mod vg_hmac_sha384; -pub use vg_hmac_sha384::VG_HMAC_SHA384; +mod vg_hash_sha384_shani; +pub use vg_hash_sha384_shani::{VG_HASH_SHA384_SHANI, VG_HASH_SHA384_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha384_avx2; -pub use vg_hmac_sha384_avx2::{VG_HMAC_SHA384_AVX2, VG_HMAC_SHA384_AVX2_FEATURES}; +mod vg_hash_sha384_avx2; +pub use vg_hash_sha384_avx2::{VG_HASH_SHA384_AVX2, VG_HASH_SHA384_AVX2_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha384_shani; -pub use vg_hmac_sha384_shani::{VG_HMAC_SHA384_SHANI, VG_HMAC_SHA384_SHANI_FEATURES}; +mod vg_hash_sha384; +pub use vg_hash_sha384::VG_HASH_SHA384; #[rustfmt::skip] -mod vg_hmac_sha512; -pub use vg_hmac_sha512::VG_HMAC_SHA512; +mod vg_hash_sha512_shani; +pub use vg_hash_sha512_shani::{VG_HASH_SHA512_SHANI, VG_HASH_SHA512_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha512_224; -pub use vg_hmac_sha512_224::VG_HMAC_SHA512_224; +mod vg_hash_sha512_avx2; +pub use vg_hash_sha512_avx2::{VG_HASH_SHA512_AVX2, VG_HASH_SHA512_AVX2_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha512_224_avx2; -pub use vg_hmac_sha512_224_avx2::{VG_HMAC_SHA512_224_AVX2, VG_HMAC_SHA512_224_AVX2_FEATURES}; +mod vg_hash_sha512; +pub use vg_hash_sha512::VG_HASH_SHA512; #[rustfmt::skip] -mod vg_hmac_sha512_224_shani; -pub use vg_hmac_sha512_224_shani::{VG_HMAC_SHA512_224_SHANI, VG_HMAC_SHA512_224_SHANI_FEATURES}; +mod vg_hash_sha512_224_shani; +pub use vg_hash_sha512_224_shani::{VG_HASH_SHA512_224_SHANI, VG_HASH_SHA512_224_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha512_256; -pub use vg_hmac_sha512_256::VG_HMAC_SHA512_256; +mod vg_hash_sha512_224_avx2; +pub use vg_hash_sha512_224_avx2::{VG_HASH_SHA512_224_AVX2, VG_HASH_SHA512_224_AVX2_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha512_256_avx2; -pub use vg_hmac_sha512_256_avx2::{VG_HMAC_SHA512_256_AVX2, VG_HMAC_SHA512_256_AVX2_FEATURES}; +mod vg_hash_sha512_224; +pub use vg_hash_sha512_224::VG_HASH_SHA512_224; #[rustfmt::skip] -mod vg_hmac_sha512_256_shani; -pub use vg_hmac_sha512_256_shani::{VG_HMAC_SHA512_256_SHANI, VG_HMAC_SHA512_256_SHANI_FEATURES}; +mod vg_hash_sha512_256_shani; +pub use vg_hash_sha512_256_shani::{VG_HASH_SHA512_256_SHANI, VG_HASH_SHA512_256_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha512_avx2; -pub use vg_hmac_sha512_avx2::{VG_HMAC_SHA512_AVX2, VG_HMAC_SHA512_AVX2_FEATURES}; +mod vg_hash_sha512_256_avx2; +pub use vg_hash_sha512_256_avx2::{VG_HASH_SHA512_256_AVX2, VG_HASH_SHA512_256_AVX2_FEATURES}; #[rustfmt::skip] -mod vg_hmac_sha512_shani; -pub use vg_hmac_sha512_shani::{VG_HMAC_SHA512_SHANI, VG_HMAC_SHA512_SHANI_FEATURES}; +mod vg_hash_sha512_256; +pub use vg_hash_sha512_256::VG_HASH_SHA512_256; #[rustfmt::skip] mod vg_md_hash_md5; pub use vg_md_hash_md5::VG_MD_HASH_MD5; #[rustfmt::skip] -mod vg_md_hash_sha1; -pub use vg_md_hash_sha1::VG_MD_HASH_SHA1; +mod vg_md_hash_sha1_shani; +pub use vg_md_hash_sha1_shani::{VG_MD_HASH_SHA1_SHANI, VG_MD_HASH_SHA1_SHANI_FEATURES}; #[rustfmt::skip] mod vg_md_hash_sha1_avx_bmi; pub use vg_md_hash_sha1_avx_bmi::{VG_MD_HASH_SHA1_AVX_BMI, VG_MD_HASH_SHA1_AVX_BMI_FEATURES}; #[rustfmt::skip] -mod vg_md_hash_sha1_shani; -pub use vg_md_hash_sha1_shani::{VG_MD_HASH_SHA1_SHANI, VG_MD_HASH_SHA1_SHANI_FEATURES}; +mod vg_md_hash_sha1; +pub use vg_md_hash_sha1::VG_MD_HASH_SHA1; #[rustfmt::skip] -mod vg_md_hash_sha224; -pub use vg_md_hash_sha224::VG_MD_HASH_SHA224; +mod vg_md_hash_sha224_shani; +pub use vg_md_hash_sha224_shani::{VG_MD_HASH_SHA224_SHANI, VG_MD_HASH_SHA224_SHANI_FEATURES}; #[rustfmt::skip] mod vg_md_hash_sha224_avx2; pub use vg_md_hash_sha224_avx2::{VG_MD_HASH_SHA224_AVX2, VG_MD_HASH_SHA224_AVX2_FEATURES}; #[rustfmt::skip] -mod vg_md_hash_sha224_shani; -pub use vg_md_hash_sha224_shani::{VG_MD_HASH_SHA224_SHANI, VG_MD_HASH_SHA224_SHANI_FEATURES}; +mod vg_md_hash_sha224; +pub use vg_md_hash_sha224::VG_MD_HASH_SHA224; #[rustfmt::skip] -mod vg_md_hash_sha256; -pub use vg_md_hash_sha256::VG_MD_HASH_SHA256; +mod vg_md_hash_sha256_shani; +pub use vg_md_hash_sha256_shani::{VG_MD_HASH_SHA256_SHANI, VG_MD_HASH_SHA256_SHANI_FEATURES}; #[rustfmt::skip] mod vg_md_hash_sha256_avx2; pub use vg_md_hash_sha256_avx2::{VG_MD_HASH_SHA256_AVX2, VG_MD_HASH_SHA256_AVX2_FEATURES}; #[rustfmt::skip] -mod vg_md_hash_sha256_shani; -pub use vg_md_hash_sha256_shani::{VG_MD_HASH_SHA256_SHANI, VG_MD_HASH_SHA256_SHANI_FEATURES}; +mod vg_md_hash_sha256; +pub use vg_md_hash_sha256::VG_MD_HASH_SHA256; #[rustfmt::skip] -mod vg_md_hash_sha384; -pub use vg_md_hash_sha384::VG_MD_HASH_SHA384; +mod vg_md_hash_sha384_shani; +pub use vg_md_hash_sha384_shani::{VG_MD_HASH_SHA384_SHANI, VG_MD_HASH_SHA384_SHANI_FEATURES}; #[rustfmt::skip] mod vg_md_hash_sha384_avx2; pub use vg_md_hash_sha384_avx2::{VG_MD_HASH_SHA384_AVX2, VG_MD_HASH_SHA384_AVX2_FEATURES}; #[rustfmt::skip] -mod vg_md_hash_sha384_shani; -pub use vg_md_hash_sha384_shani::{VG_MD_HASH_SHA384_SHANI, VG_MD_HASH_SHA384_SHANI_FEATURES}; +mod vg_md_hash_sha384; +pub use vg_md_hash_sha384::VG_MD_HASH_SHA384; #[rustfmt::skip] -mod vg_md_hash_sha512; -pub use vg_md_hash_sha512::VG_MD_HASH_SHA512; +mod vg_md_hash_sha512_shani; +pub use vg_md_hash_sha512_shani::{VG_MD_HASH_SHA512_SHANI, VG_MD_HASH_SHA512_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_md_hash_sha512_224; -pub use vg_md_hash_sha512_224::VG_MD_HASH_SHA512_224; +mod vg_md_hash_sha512_avx2; +pub use vg_md_hash_sha512_avx2::{VG_MD_HASH_SHA512_AVX2, VG_MD_HASH_SHA512_AVX2_FEATURES}; #[rustfmt::skip] -mod vg_md_hash_sha512_224_avx2; -pub use vg_md_hash_sha512_224_avx2::{VG_MD_HASH_SHA512_224_AVX2, VG_MD_HASH_SHA512_224_AVX2_FEATURES}; +mod vg_md_hash_sha512; +pub use vg_md_hash_sha512::VG_MD_HASH_SHA512; #[rustfmt::skip] mod vg_md_hash_sha512_224_shani; pub use vg_md_hash_sha512_224_shani::{VG_MD_HASH_SHA512_224_SHANI, VG_MD_HASH_SHA512_224_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_md_hash_sha512_256; -pub use vg_md_hash_sha512_256::VG_MD_HASH_SHA512_256; +mod vg_md_hash_sha512_224_avx2; +pub use vg_md_hash_sha512_224_avx2::{VG_MD_HASH_SHA512_224_AVX2, VG_MD_HASH_SHA512_224_AVX2_FEATURES}; #[rustfmt::skip] -mod vg_md_hash_sha512_256_avx2; -pub use vg_md_hash_sha512_256_avx2::{VG_MD_HASH_SHA512_256_AVX2, VG_MD_HASH_SHA512_256_AVX2_FEATURES}; +mod vg_md_hash_sha512_224; +pub use vg_md_hash_sha512_224::VG_MD_HASH_SHA512_224; #[rustfmt::skip] mod vg_md_hash_sha512_256_shani; pub use vg_md_hash_sha512_256_shani::{VG_MD_HASH_SHA512_256_SHANI, VG_MD_HASH_SHA512_256_SHANI_FEATURES}; #[rustfmt::skip] -mod vg_md_hash_sha512_avx2; -pub use vg_md_hash_sha512_avx2::{VG_MD_HASH_SHA512_AVX2, VG_MD_HASH_SHA512_AVX2_FEATURES}; +mod vg_md_hash_sha512_256_avx2; +pub use vg_md_hash_sha512_256_avx2::{VG_MD_HASH_SHA512_256_AVX2, VG_MD_HASH_SHA512_256_AVX2_FEATURES}; #[rustfmt::skip] -mod vg_md_hash_sha512_shani; -pub use vg_md_hash_sha512_shani::{VG_MD_HASH_SHA512_SHANI, VG_MD_HASH_SHA512_SHANI_FEATURES}; +mod vg_md_hash_sha512_256; +pub use vg_md_hash_sha512_256::VG_MD_HASH_SHA512_256; diff --git a/lean/Emit.lean b/lean/Emit.lean index 7bde9b9cb77..28f26054a4c 100644 --- a/lean/Emit.lean +++ b/lean/Emit.lean @@ -3,9 +3,8 @@ import VerifiedGarbage.TCB.Emit /-! # The emitter -Renders every artifact (those of each registration file under `VerifiedGarbage/Artifacts/`, and of each -generic file under `VerifiedGarbage/Generic/` for each variant under -`VerifiedGarbage/Variants/`, see `VerifiedGarbage/TCB/Emit.lean`) into Rust: each target's +Renders every artifact (those of each registration file under `VerifiedGarbage/Artifacts/`, +see `VerifiedGarbage/TCB/Emit.lean`) into Rust: each target's functions into a crate of its own, `crates/verified-garbage-asm-/src/` (DIR, by default `../crates`, relative to `lean/`). Run it from `lean/` after `lake build` (which checks every proof): @@ -104,19 +103,18 @@ def main (args : List String) : IO UInt32 := do return 1 let some (check, none, _) := VG.Emit.parseArgs args | IO.eprintln VG.Emit.usage; return 2 - let parts ← match VG.Emit.parts (← VG.Emit.registrations) - (← VG.Emit.genericGroups VG.Emit.genericDir) (← VG.Emit.grouped VG.Emit.variantDir) with + let parts ← match VG.Emit.parts (← VG.Emit.registrations) with | .ok parts => pure parts | .error e => do IO.eprintln e; return 1 let emitDir : System.FilePath := ".lake" / "emit" IO.FS.createDirAll emitDir let lean ← IO.appPath -- Each part's driver, and the arguments of its run. - let jobs ← parts.mapM fun (t, mods, gens, vars) => do + let jobs ← parts.mapM fun (t, mods) => do let driver := emitDir / s!"Driver{t}.lean" let setup := emitDir / s!"Driver{t}.json" let targets := emitDir / s!"{t}.targets" - let text := VG.Emit.driver mods gens vars + let text := VG.Emit.driver mods -- Only if it changed, so that Lake does not take it for a new file. let old ← if ← driver.pathExists then some <$> IO.FS.readFile driver else pure none unless old == some text do IO.FS.writeFile driver text diff --git a/lean/EmitOne.lean b/lean/EmitOne.lean index bfdbb8748c8..a9423bfe47e 100644 --- a/lean/EmitOne.lean +++ b/lean/EmitOne.lean @@ -14,11 +14,6 @@ than building every module of the library as `Emit.lean` needs. Run it from * `lake env lean --run EmitOne.lean --check Rc2.AArch64 …` writes nothing and fails if those files on disk differ from what would be generated. -A name may also be a generic group, `.` (e.g. -`Sha256Compress.AArch64`), or `.…..` for callers of -several interfaces (e.g. `Blake2b.Argon2Compress.X86_64`): its generic files -applied to its variants, as `Emit.lean` emits them. - It runs the same trusted printer (`VG.Emit.run`) and the same audits (`#assert_standard_axioms`, `#assert_no_compiler_overrides`, `#assert_spec_origin`) as `Emit.lean`, on these artifacts only, into @@ -99,14 +94,6 @@ def runProc (cmd : String) (args : Array String) : IO UInt32 := do let child ← IO.Process.spawn { cmd, args } child.wait -/-- The name of a generic group on the command line, from its interfaces -(`VG.Emit.interfacesOf`): `Sha256Compress.AArch64` for -`["Sha256Compress.AArch64"]`, `Blake2b.Argon2Compress.X86_64` for -`["Blake2b.X86_64", "Argon2Compress.X86_64"]`. -/ -def groupName (is : List String) : String := - let target := ((is.headD "").splitOn ".").getLastD "" - ".".intercalate (is.map fun i => (i.splitOn ".").headD "") ++ "." ++ target - def main (args : List String) : IO UInt32 := do let (check, names) := match args with | "--check" :: rest => (true, rest) @@ -116,20 +103,15 @@ def main (args : List String) : IO UInt32 := do return 2 let regs := (← VG.Emit.registrations).filter fun m => names.any fun n => m == s!"VerifiedGarbage.Artifacts.{n}" - let gens := (← VG.Emit.genericGroups VG.Emit.genericDir).filter fun g => - names.contains (groupName g.1) - let vars := (← VG.Emit.grouped VG.Emit.variantDir).filter fun v => - gens.any (·.1.any fun i => (VG.Emit.allOf i).1 == v.1) for n in names do - unless regs.contains s!"VerifiedGarbage.Artifacts.{n}" || gens.any (groupName ·.1 == n) do - IO.eprintln s!"{n}: no registration file VerifiedGarbage/Artifacts/{n.replace "." "/"}.lean \ - and no generic group {n}" + unless regs.contains s!"VerifiedGarbage.Artifacts.{n}" do + IO.eprintln s!"{n}: no registration file VerifiedGarbage/Artifacts/{n.replace "." "/"}.lean" return 2 -- Build what the driver imports, and the trusted base's shared libraries -- (what no target owns, and their targets'); reporting warnings and -- errors, not the profile every module records (`lakefile.toml`), which -- Lake replays for each one it finds built. - let mods := regs ++ gens.flatMap (·.2) ++ vars.flatMap (·.2) + let mods := regs let libs := nativeTCB names let code ← runProc "lake" (#["build", "--log-level=warning"] ++ (libs.map fun (l, _) => s!"{l}:shared").toArray ++ @@ -139,7 +121,7 @@ def main (args : List String) : IO UInt32 := do let out : FilePath := ".lake" / "emit" / "one" IO.FS.createDirAll ".lake/emit" if ← out.pathExists then IO.FS.removeDirAll out - IO.FS.writeFile driver (VG.Emit.driver regs gens vars) + IO.FS.writeFile driver (VG.Emit.driver regs) -- A module, run with Lake's setup for it (`Emit.lean`'s `main` says why). let setup : FilePath := ".lake" / "emit" / "One.json" let r ← IO.Process.output { cmd := "lake", args := #["setup-file", driver.toString] } diff --git a/lean/README.md b/lean/README.md index 6c19861e908..1ab3463fbc7 100644 --- a/lean/README.md +++ b/lean/README.md @@ -38,9 +38,8 @@ VerifiedGarbage/ runs is what the kernel checked) and `#assert_spec_origin` (`VG.Spec` definitions are declared in `Spec/`) Emit.lean what is emitted (every registration file under - `Artifacts/`, and every generic function under `Generic/` - for every variant of its interface under `Variants/`), and - writing or checking the generated crates + `Artifacts/`, each function's implementations from one of + them), and writing or checking the generated crates X86_64/ ISA model, printer, System V ABI target PPC64LE/ ISA model, printer, ELFv2 ABI target (64-bit little-endian PowerPC) RISCV64/ ISA model, printer, psABI target (64-bit RISC-V, RV64IM) @@ -55,8 +54,6 @@ VerifiedGarbage/ (a function generic over a callee's implementations is registered once for each element of their list, `Proof/Variants//.lean`) - Dispatch.lean The order in which each target's dispatchers try the variants, by - suffix (untrusted: it only decides which proven implementation runs) VerifiedGarbageTest/ Golden tests for the (unverified) printers and calling conventions Emit.lean Renders every artifact into `../crates/` (see `TCB/Emit.lean`) EmitOne.lean The same, for the registration files named, while iterating @@ -154,12 +151,12 @@ constant, or calls the function's generated dispatcher, `_auto` the features the compilation target enables (`verified_garbage_cpu::target_contains`, in a `const` block) and those `verified_garbage_cpu::detected()` reports (`crates/verified-garbage-cpu/`), and needs none: it calls an implementation -only on a CPU with that implementation's features, in the order -`Dispatch.lean` ranks them, and otherwise the baseline. Where the +only on a CPU with that implementation's features, in the order its +registration file lists them, and otherwise the baseline. Where the compilation target enables an implementation's features (`-C target-cpu=x86-64-v3`), the compiler sees that it chooses that one, and the -implementations ranked after it are not linked. The ranking is not trusted: every implementation it -may choose is proven against the same contract. +implementations after it are not linked. The order is not trusted: every +implementation it may choose is proven against the same contract. ## What you need to trust diff --git a/lean/VerifiedGarbage/Dispatch.lean b/lean/VerifiedGarbage/Dispatch.lean deleted file mode 100644 index 53758568e2b..00000000000 --- a/lean/VerifiedGarbage/Dispatch.lean +++ /dev/null @@ -1,76 +0,0 @@ -module - -public import VerifiedGarbage.TCB.Rust - -/-! # The ranking of the dispatchers - -Each target's order of the implementations its dispatchers try -(`VG.Rust.Rank`), best first. It decides only which implementation runs on a -CPU that could run several, so it is a matter of speed, not of correctness -or safety, and is not trusted: whatever it says, a dispatcher calls only -implementations proven against its documented contract, each only where -the CPU has the features it needs. An implementation whose suffix is not -here, or not made of parts that are (`VG.Rust.partsOf`), fails the emitter. --/ - -@[expose] public section - -namespace VG.Dispatch - -open VG.Rust (Rank) - -def ranking : Rust.Ranking := [ - ("x86_64", [ - { suffix := "_vaes_avx512" }, - -- Each implementation with AVX2 copies (`_avx2`, AES-GCM-SIV's) before - -- the same one without. - { suffix := "_vaes_vpclmul_avx512" }, - { suffix := "_vaes_vpclmul" }, - { suffix := "_vaes_pclmul_avx2" }, - { suffix := "_vaes_pclmul" }, - { suffix := "_aesni_vpclmul_avx2" }, - { suffix := "_aesni_vpclmul" }, - { suffix := "_aesni_pclmul_avx_avx2" }, - { suffix := "_aesni_pclmul_avx" }, - { suffix := "_aesni_pclmul" }, - { suffix := "_vaes_avx2" }, - { suffix := "_vaes" }, - { suffix := "_aesni_avx2" }, - -- AES-NI code that also wipes its scratch with `vg_zeroize_avx`. - { suffix := "_aesni_avx" }, - { suffix := "_aesni" }, - { suffix := "_vpclmul_avx2" }, - { suffix := "_vpclmul" }, - -- `vg_aes_gcm_prepare`'s 128-bit AVX code, after its 256-bit code. - { suffix := "_pclmul_avx" }, - { suffix := "_pclmul_avx2" }, - { suffix := "_pclmul" }, - { suffix := "_ifma" }, - { suffix := "_avx512vl" }, - { suffix := "_avx512" }, - { suffix := "_shani" }, - { suffix := "_avx_bmi" }, - { suffix := "_avx2" }, - { suffix := "_adx" }, - { suffix := "_avx" }, - { suffix := "_ssse3" }]), - ("x86", [ - { suffix := "_aesni_pclmul" }, - { suffix := "_aesni" }, - { suffix := "_pclmul" }, - { suffix := "_shani" }, - { suffix := "_ssse3" }]), - ("aarch64", [ - { suffix := "_aes_sha3" }, - { suffix := "_aes" }, - { suffix := "_x2_sha3_keccak_sha3" }, - { suffix := "_x2_sha3" }, - { suffix := "_sha3" }, - { suffix := "_sha512" }, - { suffix := "_sha2" }, - { suffix := "_sve2" }, - { suffix := "_neon" }]), - ("arm", [ - { suffix := "_umaal" }])] - -end VG.Dispatch diff --git a/lean/VerifiedGarbage/Proof/Variants/AesBlocks/AArch64.lean b/lean/VerifiedGarbage/Proof/Variants/AesBlocks/AArch64.lean index 6f06189d938..cb95bc64a51 100644 --- a/lean/VerifiedGarbage/Proof/Variants/AesBlocks/AArch64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/AesBlocks/AArch64.lean @@ -13,7 +13,8 @@ def aes : Proof.Aes.AArch64.BlocksImpl := .aese def scalar : Proof.Aes.AArch64.BlocksImpl := .scalar /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [aes, scalar] end VG.Proof.Variants.AesBlocks.AArch64 diff --git a/lean/VerifiedGarbage/Proof/Variants/AesBlocks/X86.lean b/lean/VerifiedGarbage/Proof/Variants/AesBlocks/X86.lean index 819603457c8..b9c85008e50 100644 --- a/lean/VerifiedGarbage/Proof/Variants/AesBlocks/X86.lean +++ b/lean/VerifiedGarbage/Proof/Variants/AesBlocks/X86.lean @@ -13,7 +13,8 @@ def aesNi : Proof.Aes.X86.BlocksImpl := .aesni def scalar : Proof.Aes.X86.BlocksImpl := .scalar /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [aesNi, scalar] end VG.Proof.Variants.AesBlocks.X86 diff --git a/lean/VerifiedGarbage/Proof/Variants/AesBlocks/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/AesBlocks/X86_64.lean index 964e309c4dd..e2cb17e6519 100644 --- a/lean/VerifiedGarbage/Proof/Variants/AesBlocks/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/AesBlocks/X86_64.lean @@ -15,7 +15,8 @@ def scalar : Proof.Aes.X86_64.BlocksImpl := .scalar def vaes : Proof.Aes.X86_64.BlocksImpl := .vaes /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [vaes, aesNi, scalar] end VG.Proof.Variants.AesBlocks.X86_64 diff --git a/lean/VerifiedGarbage/Proof/Variants/AesCtr32/AArch64.lean b/lean/VerifiedGarbage/Proof/Variants/AesCtr32/AArch64.lean index 510d946605f..f2ea90db62e 100644 --- a/lean/VerifiedGarbage/Proof/Variants/AesCtr32/AArch64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/AesCtr32/AArch64.lean @@ -13,7 +13,8 @@ def aese : Proof.Aes.AArch64.Ctr32Impl := .aese def scalar : Proof.Aes.AArch64.Ctr32Impl := .scalar /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [aese, scalar] end VG.Proof.Variants.AesCtr32.AArch64 diff --git a/lean/VerifiedGarbage/Proof/Variants/AesCtr32/X86.lean b/lean/VerifiedGarbage/Proof/Variants/AesCtr32/X86.lean index de7ad0eeb2a..9f12e9939db 100644 --- a/lean/VerifiedGarbage/Proof/Variants/AesCtr32/X86.lean +++ b/lean/VerifiedGarbage/Proof/Variants/AesCtr32/X86.lean @@ -13,7 +13,8 @@ def aesNi : Proof.Aes.X86.Ctr32Impl := .aesni def scalar : Proof.Aes.X86.Ctr32Impl := .scalar /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [aesNi, scalar] end VG.Proof.Variants.AesCtr32.X86 diff --git a/lean/VerifiedGarbage/Proof/Variants/AesCtr32/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/AesCtr32/X86_64.lean index 1f2862e1955..c555efc0246 100644 --- a/lean/VerifiedGarbage/Proof/Variants/AesCtr32/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/AesCtr32/X86_64.lean @@ -15,7 +15,8 @@ def scalar : Proof.Aes.X86_64.Ctr32Impl := .scalar def vaes : Proof.Aes.X86_64.Ctr32Impl := .vaes /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [vaes, aesNi, scalar] end VG.Proof.Variants.AesCtr32.X86_64 diff --git a/lean/VerifiedGarbage/Proof/Variants/AesGcm/AArch64.lean b/lean/VerifiedGarbage/Proof/Variants/AesGcm/AArch64.lean index 0bfbe4ca4c0..3101dde2b7b 100644 --- a/lean/VerifiedGarbage/Proof/Variants/AesGcm/AArch64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/AesGcm/AArch64.lean @@ -15,7 +15,8 @@ def aesSha3 : Proof.AesGcm.AArch64.GcmVariant := ⟨.aese, .aese, .aes, true⟩ def scalar : Proof.AesGcm.AArch64.GcmVariant := ⟨.scalar, .scalar, .scalar, false⟩ /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [aesSha3, aes, scalar] end VG.Proof.Variants.AesGcm.AArch64 diff --git a/lean/VerifiedGarbage/Proof/Variants/AesGcm/X86.lean b/lean/VerifiedGarbage/Proof/Variants/AesGcm/X86.lean index 3fb2dc3ac99..3f1b2edefc7 100644 --- a/lean/VerifiedGarbage/Proof/Variants/AesGcm/X86.lean +++ b/lean/VerifiedGarbage/Proof/Variants/AesGcm/X86.lean @@ -18,7 +18,8 @@ def pclmul : Proof.AesGcm.X86.GcmVariant := ⟨.scalar, .pclmul⟩ def scalar : Proof.AesGcm.X86.GcmVariant := ⟨.scalar, .scalar⟩ /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [aesNiPclmul, aesNi, pclmul, scalar] end VG.Proof.Variants.AesGcm.X86 diff --git a/lean/VerifiedGarbage/Proof/Variants/AesGcm/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/AesGcm/X86_64.lean index b53b027fdb8..de62513fc8d 100644 --- a/lean/VerifiedGarbage/Proof/Variants/AesGcm/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/AesGcm/X86_64.lean @@ -37,7 +37,8 @@ def vaesVpclmulAvx512 : Proof.AesGcm.X86_64.GcmVariant := Proof.Variants.AesGcm. def vpclmul : Proof.AesGcm.X86_64.GcmVariant := ⟨.scalar, .scalar, .vpclmul, none⟩ /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [vaesVpclmulAvx512, vaesVpclmul, vaesPclmul, aesNiVpclmul, aesNiPclmulAvx, aesNiPclmul, vaes, aesNi, vpclmul, pclmul, scalar] end VG.Proof.Variants.AesGcm.X86_64 diff --git a/lean/VerifiedGarbage/Proof/Variants/AesXts/AArch64.lean b/lean/VerifiedGarbage/Proof/Variants/AesXts/AArch64.lean index c255aafa7f9..21e0ca372ed 100644 --- a/lean/VerifiedGarbage/Proof/Variants/AesXts/AArch64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/AesXts/AArch64.lean @@ -13,7 +13,8 @@ def aes : Proof.AesXts.AArch64.XtsImpl := .aes def scalar : Proof.AesXts.AArch64.XtsImpl := .blocks .scalar /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [aes, scalar] end VG.Proof.Variants.AesXts.AArch64 diff --git a/lean/VerifiedGarbage/Proof/Variants/AesXts/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/AesXts/X86_64.lean index 3f5cdd7944c..b07841c8c27 100644 --- a/lean/VerifiedGarbage/Proof/Variants/AesXts/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/AesXts/X86_64.lean @@ -21,7 +21,8 @@ def vaesVpclmul : Proof.AesXts.X86_64.XtsImpl := .vaesVpclmul def vaesVpclmulAvx512 : Proof.AesXts.X86_64.XtsImpl := .vaesVpclmulAvx512 /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [vaesVpclmulAvx512, vaesVpclmul, aesNiPclmul, vaes, aesNi, scalar] end VG.Proof.Variants.AesXts.X86_64 diff --git a/lean/VerifiedGarbage/Proof/Variants/Argon2Compress/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/Argon2Compress/X86_64.lean index 8a1c3d00872..2bfc8bcc7ed 100644 --- a/lean/VerifiedGarbage/Proof/Variants/Argon2Compress/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/Argon2Compress/X86_64.lean @@ -20,7 +20,8 @@ open VG VG.X86_64 VG.Proof.Argon2.X86_64 in @[instance_reducible] def scalar : CompressImpl := Proof.Variants.Argon2Compress.X86_64.Scalar.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [avx512, avx2, scalar] end VG.Proof.Variants.Argon2Compress.X86_64 diff --git a/lean/VerifiedGarbage/Proof/Variants/Blake2b/AArch64.lean b/lean/VerifiedGarbage/Proof/Variants/Blake2b/AArch64.lean index 30daa82d814..c9b4991e37f 100644 --- a/lean/VerifiedGarbage/Proof/Variants/Blake2b/AArch64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/Blake2b/AArch64.lean @@ -11,7 +11,8 @@ namespace VG.Proof.Variants.Blake2b.AArch64 def scalar : Proof.Argon2.AArch64.HPrime.Backend := Proof.Argon2.AArch64.HPrime.scalar /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [scalar] end VG.Proof.Variants.Blake2b.AArch64 diff --git a/lean/VerifiedGarbage/Proof/Variants/Blake2b/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/Blake2b/X86_64.lean index fcda3dbfc64..6da7a8a7e9b 100644 --- a/lean/VerifiedGarbage/Proof/Variants/Blake2b/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/Blake2b/X86_64.lean @@ -17,7 +17,8 @@ def avx512 : Proof.Blake2.X86_64.Backend := Proof.Variants.Blake2b.X86_64.Avx512 def scalar : Proof.Blake2.X86_64.Backend := Proof.Variants.Blake2b.X86_64.Scalar.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [avx512, avx2, scalar] end VG.Proof.Variants.Blake2b.X86_64 diff --git a/lean/VerifiedGarbage/Proof/Variants/Blake2s/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/Blake2s/X86_64.lean index 162d95b9a3c..76d497d441d 100644 --- a/lean/VerifiedGarbage/Proof/Variants/Blake2s/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/Blake2s/X86_64.lean @@ -14,7 +14,8 @@ def avx : Proof.Blake2.X86_64.BackendS := Proof.Variants.Blake2s.X86_64.Avx.vari def scalar : Proof.Blake2.X86_64.BackendS := Proof.Variants.Blake2s.X86_64.Scalar.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [avx, scalar] end VG.Proof.Variants.Blake2s.X86_64 diff --git a/lean/VerifiedGarbage/Proof/Variants/ChaCha20Xor/AArch64.lean b/lean/VerifiedGarbage/Proof/Variants/ChaCha20Xor/AArch64.lean index d4a44b904dc..573f304fe66 100644 --- a/lean/VerifiedGarbage/Proof/Variants/ChaCha20Xor/AArch64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/ChaCha20Xor/AArch64.lean @@ -15,7 +15,8 @@ def scalar : Proof.ChaCha20.AArch64.XorImpl := .scalar def sve2 : Proof.ChaCha20.AArch64.XorImpl := .sve2 /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [sve2, neon, scalar] end VG.Proof.Variants.ChaCha20Xor.AArch64 diff --git a/lean/VerifiedGarbage/Proof/Variants/ChaCha20Xor/X86.lean b/lean/VerifiedGarbage/Proof/Variants/ChaCha20Xor/X86.lean index ca1ccbd1a96..be04304c14b 100644 --- a/lean/VerifiedGarbage/Proof/Variants/ChaCha20Xor/X86.lean +++ b/lean/VerifiedGarbage/Proof/Variants/ChaCha20Xor/X86.lean @@ -13,7 +13,8 @@ def sse2 : Proof.ChaCha20.X86.XorImpl := .sse2 def ssse3 : Proof.ChaCha20.X86.XorImpl := .ssse3 /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [ssse3, sse2] end VG.Proof.Variants.ChaCha20Xor.X86 diff --git a/lean/VerifiedGarbage/Proof/Variants/ChaCha20Xor/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/ChaCha20Xor/X86_64.lean index f0dfed291af..cf957dc474b 100644 --- a/lean/VerifiedGarbage/Proof/Variants/ChaCha20Xor/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/ChaCha20Xor/X86_64.lean @@ -21,7 +21,8 @@ def scalar : Proof.ChaCha20.X86_64.XorImpl := .scalar def ssse3 : Proof.ChaCha20.X86_64.XorImpl := .ssse3 /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [ifma, avx512Vl, avx512, avx2, ssse3, scalar] end VG.Proof.Variants.ChaCha20Xor.X86_64 diff --git a/lean/VerifiedGarbage/Proof/Variants/CmacAesUpdate/AArch64.lean b/lean/VerifiedGarbage/Proof/Variants/CmacAesUpdate/AArch64.lean index 1f18fb7968b..3a7afaf69de 100644 --- a/lean/VerifiedGarbage/Proof/Variants/CmacAesUpdate/AArch64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/CmacAesUpdate/AArch64.lean @@ -13,7 +13,8 @@ def aese : Proof.CmacAes.AArch64.UpdateImpl := .aese def scalar : Proof.CmacAes.AArch64.UpdateImpl := .ctr32 .scalar /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [aese, scalar] end VG.Proof.Variants.CmacAesUpdate.AArch64 diff --git a/lean/VerifiedGarbage/Proof/Variants/CmacAesUpdate/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/CmacAesUpdate/X86_64.lean index 1f06b0c1b67..fc05a097b94 100644 --- a/lean/VerifiedGarbage/Proof/Variants/CmacAesUpdate/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/CmacAesUpdate/X86_64.lean @@ -13,7 +13,8 @@ def aesNi : Proof.CmacAes.X86_64.UpdateImpl := .aesniCbc def scalar : Proof.CmacAes.X86_64.UpdateImpl := .ctr32 .scalar /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [aesNi, scalar] end VG.Proof.Variants.CmacAesUpdate.X86_64 diff --git a/lean/VerifiedGarbage/Proof/Variants/Curve448Field/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/Curve448Field/X86_64.lean index 388c185c457..20c3959697f 100644 --- a/lean/VerifiedGarbage/Proof/Variants/Curve448Field/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/Curve448Field/X86_64.lean @@ -13,7 +13,8 @@ def adx : Impl.Ed448.X86_64.FieldVariant := .adx def scalar : Impl.Ed448.X86_64.FieldVariant := .scalar /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [adx, scalar] end VG.Proof.Variants.Curve448Field.X86_64 diff --git a/lean/VerifiedGarbage/Proof/Variants/DsaArithmetic/AArch64.lean b/lean/VerifiedGarbage/Proof/Variants/DsaArithmetic/AArch64.lean index e796b1b61c9..e6623a5dd8b 100644 --- a/lean/VerifiedGarbage/Proof/Variants/DsaArithmetic/AArch64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/DsaArithmetic/AArch64.lean @@ -13,7 +13,8 @@ open VG VG.AArch64 VG.Proof.Dsa.AArch64 VG.Proof.Bignum.AArch64 in def base : Backend := Proof.Variants.DsaArithmetic.AArch64.Base.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [base] end VG.Proof.Variants.DsaArithmetic.AArch64 diff --git a/lean/VerifiedGarbage/Proof/Variants/DsaArithmetic/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/DsaArithmetic/X86_64.lean index bf7f74f24da..3aece6d5374 100644 --- a/lean/VerifiedGarbage/Proof/Variants/DsaArithmetic/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/DsaArithmetic/X86_64.lean @@ -16,7 +16,8 @@ open VG VG.X86_64 VG.Proof.Dsa.X86_64 VG.Proof.Bignum.X86_64 VG.Proof.Rsa.X86_64 def base : Backend := Proof.Variants.DsaArithmetic.X86_64.Base.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [adx, base] end VG.Proof.Variants.DsaArithmetic.X86_64 diff --git a/lean/VerifiedGarbage/Proof/Variants/Ffdh/Arm.lean b/lean/VerifiedGarbage/Proof/Variants/Ffdh/Arm.lean index e1837e283fd..f8d577057d5 100644 --- a/lean/VerifiedGarbage/Proof/Variants/Ffdh/Arm.lean +++ b/lean/VerifiedGarbage/Proof/Variants/Ffdh/Arm.lean @@ -14,7 +14,8 @@ def base : Proof.Ffdh.Arm.Backend := Proof.Variants.Ffdh.Arm.Base.variant def umaal : Proof.Ffdh.Arm.Backend := Proof.Variants.Ffdh.Arm.Umaal.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [umaal, base] end VG.Proof.Variants.Ffdh.Arm diff --git a/lean/VerifiedGarbage/Proof/Variants/FfdhExp/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/FfdhExp/X86_64.lean index c670e60b16a..827b2e65a8a 100644 --- a/lean/VerifiedGarbage/Proof/Variants/FfdhExp/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/FfdhExp/X86_64.lean @@ -14,7 +14,8 @@ def base : Proof.Ffdh.X86_64.ExpImpl := Proof.Variants.FfdhExp.X86_64.Base.varia def ifma : Proof.Ffdh.X86_64.ExpImpl := Proof.Variants.FfdhExp.X86_64.Ifma.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [ifma, base] end VG.Proof.Variants.FfdhExp.X86_64 diff --git a/lean/VerifiedGarbage/Proof/Variants/Ghash/X86.lean b/lean/VerifiedGarbage/Proof/Variants/Ghash/X86.lean index d3552338b67..21d4192e5b2 100644 --- a/lean/VerifiedGarbage/Proof/Variants/Ghash/X86.lean +++ b/lean/VerifiedGarbage/Proof/Variants/Ghash/X86.lean @@ -13,7 +13,8 @@ def pclmul : Proof.AesGcm.X86.GhashName := .pclmul def scalar : Proof.AesGcm.X86.GhashName := .scalar /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [pclmul, scalar] end VG.Proof.Variants.Ghash.X86 diff --git a/lean/VerifiedGarbage/Proof/Variants/Ghash/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/Ghash/X86_64.lean index b81ed3d61a9..7453841ab3d 100644 --- a/lean/VerifiedGarbage/Proof/Variants/Ghash/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/Ghash/X86_64.lean @@ -15,7 +15,8 @@ def scalar : Proof.AesGcm.X86_64.GhashName := .scalar def vpclmul : Proof.AesGcm.X86_64.GhashName := .vpclmul /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [vpclmul, pclmul, scalar] end VG.Proof.Variants.Ghash.X86_64 diff --git a/lean/VerifiedGarbage/Proof/Variants/Keccak/AArch64.lean b/lean/VerifiedGarbage/Proof/Variants/Keccak/AArch64.lean index 37835df9040..d63dca516be 100644 --- a/lean/VerifiedGarbage/Proof/Variants/Keccak/AArch64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/Keccak/AArch64.lean @@ -22,7 +22,8 @@ def sha3 : Proof.Sha3.AArch64.Permutation := { Proof.Sha3.AArch64.Sha3.ResidentBackend.backend with vendor := some "apple" } /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [sha3, scalar] end VG.Proof.Variants.Keccak.AArch64 diff --git a/lean/VerifiedGarbage/Proof/Variants/KeccakX2/AArch64.lean b/lean/VerifiedGarbage/Proof/Variants/KeccakX2/AArch64.lean index a6d16ae8ef9..1d69f73e8ce 100644 --- a/lean/VerifiedGarbage/Proof/Variants/KeccakX2/AArch64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/KeccakX2/AArch64.lean @@ -14,7 +14,8 @@ def neon : Proof.Sha3.AArch64.X2Permutation := Proof.Variants.KeccakX2.AArch64.N def sha3 : Proof.Sha3.AArch64.X2Permutation := Proof.Variants.KeccakX2.AArch64.Sha3.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [sha3, neon] end VG.Proof.Variants.KeccakX2.AArch64 diff --git a/lean/VerifiedGarbage/Proof/Variants/MdHash/AArch64.lean b/lean/VerifiedGarbage/Proof/Variants/MdHash/AArch64.lean index 66ec7cb9008..26646f19148 100644 --- a/lean/VerifiedGarbage/Proof/Variants/MdHash/AArch64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/MdHash/AArch64.lean @@ -55,7 +55,8 @@ def sha512_256 : Proof.Pbkdf2.Md.AArch64.MdHash := Proof.Pbkdf2.Md.AArch64.Sha51 def sha512_256Sha3 : Proof.Pbkdf2.Md.AArch64.MdHash := Proof.Pbkdf2.Md.AArch64.Sha512.sha512_256 Proof.Sha512.AArch64.Sha3.backend /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [md5, sha1Sha2, sha1, sha224Sha2, sha224, sha256Sha2, sha256, sha384Sha3, sha384, sha512Sha3, sha512, sha512_224Sha3, sha512_224, sha512_256Sha3, sha512_256] end VG.Proof.Variants.MdHash.AArch64 diff --git a/lean/VerifiedGarbage/Proof/Variants/MdHash/RISCV64.lean b/lean/VerifiedGarbage/Proof/Variants/MdHash/RISCV64.lean index cb31ca5b554..3ec94b396aa 100644 --- a/lean/VerifiedGarbage/Proof/Variants/MdHash/RISCV64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/MdHash/RISCV64.lean @@ -25,7 +25,8 @@ def sha512_224 : Proof.HmacTable.RISCV64.HVar := Proof.HmacTable.RISCV64.sha512_ def sha512_256 : Proof.HmacTable.RISCV64.HVar := Proof.HmacTable.RISCV64.sha512_256W /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [md5, sha1, sha224, sha256, sha384, sha512, sha512_224, sha512_256] end VG.Proof.Variants.MdHash.RISCV64 diff --git a/lean/VerifiedGarbage/Proof/Variants/MdHash/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/MdHash/X86_64.lean index 18decec87ea..26af32f8e3d 100644 --- a/lean/VerifiedGarbage/Proof/Variants/MdHash/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/MdHash/X86_64.lean @@ -77,7 +77,8 @@ def sha512_256ShaNi : Proof.Pbkdf2.Md.X86_64.MdHash := Proof.Pbkdf2.Md.X86_64.Sha512.sha512_256 .shani (Proof.Pbkdf2.Md.X86_64.Sha512.stream512_256 .shani) /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [md5, sha1ShaNi, sha1AvxBmi, sha1, sha224ShaNi, sha224Avx2, sha224, sha256ShaNi, sha256Avx2, sha256, sha384ShaNi, sha384Avx2, sha384, sha512ShaNi, sha512Avx2, sha512, sha512_224ShaNi, sha512_224Avx2, sha512_224, sha512_256ShaNi, sha512_256Avx2, sha512_256] end VG.Proof.Variants.MdHash.X86_64 diff --git a/lean/VerifiedGarbage/Proof/Variants/MillerRabin/AArch64.lean b/lean/VerifiedGarbage/Proof/Variants/MillerRabin/AArch64.lean index fb9cb01e6a0..7ee4996fd3c 100644 --- a/lean/VerifiedGarbage/Proof/Variants/MillerRabin/AArch64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/MillerRabin/AArch64.lean @@ -12,7 +12,8 @@ open VG.Proof.MillerRabin.AArch64 VG.Proof.Bignum.AArch64 in def base : RoundImpl := Proof.Variants.MillerRabin.AArch64.Base.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [base] end VG.Proof.Variants.MillerRabin.AArch64 diff --git a/lean/VerifiedGarbage/Proof/Variants/MillerRabin/Arm.lean b/lean/VerifiedGarbage/Proof/Variants/MillerRabin/Arm.lean index 5d71d83c3bb..7747924b600 100644 --- a/lean/VerifiedGarbage/Proof/Variants/MillerRabin/Arm.lean +++ b/lean/VerifiedGarbage/Proof/Variants/MillerRabin/Arm.lean @@ -16,7 +16,8 @@ open VG.Proof.MillerRabin.Arm in def umaal : RoundImpl := Proof.Variants.MillerRabin.Arm.Umaal.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [umaal, base] end VG.Proof.Variants.MillerRabin.Arm diff --git a/lean/VerifiedGarbage/Proof/Variants/MillerRabin/PPC64LE.lean b/lean/VerifiedGarbage/Proof/Variants/MillerRabin/PPC64LE.lean index e883f6e3fa3..0e25103896e 100644 --- a/lean/VerifiedGarbage/Proof/Variants/MillerRabin/PPC64LE.lean +++ b/lean/VerifiedGarbage/Proof/Variants/MillerRabin/PPC64LE.lean @@ -11,7 +11,8 @@ namespace VG.Proof.Variants.MillerRabin.PPC64LE def base : Proof.MillerRabin.PPC64LE.RoundImpl := Proof.Variants.MillerRabin.PPC64LE.Base.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [base] end VG.Proof.Variants.MillerRabin.PPC64LE diff --git a/lean/VerifiedGarbage/Proof/Variants/MillerRabin/RISCV64.lean b/lean/VerifiedGarbage/Proof/Variants/MillerRabin/RISCV64.lean index 71f1612a1d0..5e08ab4f7f1 100644 --- a/lean/VerifiedGarbage/Proof/Variants/MillerRabin/RISCV64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/MillerRabin/RISCV64.lean @@ -11,7 +11,8 @@ namespace VG.Proof.Variants.MillerRabin.RISCV64 def base : Proof.MillerRabin.RISCV64.RoundImpl := Proof.Variants.MillerRabin.RISCV64.Base.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [base] end VG.Proof.Variants.MillerRabin.RISCV64 diff --git a/lean/VerifiedGarbage/Proof/Variants/MillerRabin/X86.lean b/lean/VerifiedGarbage/Proof/Variants/MillerRabin/X86.lean index 7ce1f139892..80df24afd74 100644 --- a/lean/VerifiedGarbage/Proof/Variants/MillerRabin/X86.lean +++ b/lean/VerifiedGarbage/Proof/Variants/MillerRabin/X86.lean @@ -11,7 +11,8 @@ namespace VG.Proof.Variants.MillerRabin.X86 def base : Proof.MillerRabin.X86.RoundImpl := Proof.Variants.MillerRabin.X86.Base.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [base] end VG.Proof.Variants.MillerRabin.X86 diff --git a/lean/VerifiedGarbage/Proof/Variants/MillerRabin/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/MillerRabin/X86_64.lean index 18b4724afa3..c2122b28d3e 100644 --- a/lean/VerifiedGarbage/Proof/Variants/MillerRabin/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/MillerRabin/X86_64.lean @@ -20,7 +20,8 @@ open VG VG.X86_64 VG.Proof.Bignum.X86_64 VG.Proof.MillerRabin.X86_64 in def ifma : RoundImpl := Proof.Variants.MillerRabin.X86_64.Ifma.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [ifma, adx, base] end VG.Proof.Variants.MillerRabin.X86_64 diff --git a/lean/VerifiedGarbage/Proof/Variants/MlDsaArith/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/MlDsaArith/X86_64.lean index 6bc34fac45e..63f1985231d 100644 --- a/lean/VerifiedGarbage/Proof/Variants/MlDsaArith/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/MlDsaArith/X86_64.lean @@ -16,7 +16,8 @@ def avx512 : Proof.MlDsa.X86_64.ArithImpl := .avx512 def sse2 : Proof.MlDsa.X86_64.ArithImpl := .sse2 /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [avx512, avx2, sse2] end VG.Proof.Variants.MlDsaArith.X86_64 diff --git a/lean/VerifiedGarbage/Proof/Variants/MlKemSample4/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/MlKemSample4/X86_64.lean index 2aa1f842f63..abe884cae7f 100644 --- a/lean/VerifiedGarbage/Proof/Variants/MlKemSample4/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/MlKemSample4/X86_64.lean @@ -15,7 +15,8 @@ def avx512 : Proof.MlKem.X86_64.Sample4Impl := .avx512 def scalar : Proof.MlKem.X86_64.Sample4Impl := .scalar /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [avx512, avx2, scalar] end VG.Proof.Variants.MlKemSample4.X86_64 diff --git a/lean/VerifiedGarbage/Proof/Variants/Poly1305Blocks/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/Poly1305Blocks/X86_64.lean index ea59a20a874..15ca5e463cc 100644 --- a/lean/VerifiedGarbage/Proof/Variants/Poly1305Blocks/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/Poly1305Blocks/X86_64.lean @@ -22,7 +22,8 @@ def ifma : Proof.Poly1305.X86_64.BlocksImpl := .ifma def scalar : Proof.Poly1305.X86_64.BlocksImpl := .scalar /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [ifma, avx512, avx2, scalar] end VG.Proof.Variants.Poly1305Blocks.X86_64 diff --git a/lean/VerifiedGarbage/Proof/Variants/RsaMont/Arm.lean b/lean/VerifiedGarbage/Proof/Variants/RsaMont/Arm.lean index f69ff1ea0d5..b36448fc950 100644 --- a/lean/VerifiedGarbage/Proof/Variants/RsaMont/Arm.lean +++ b/lean/VerifiedGarbage/Proof/Variants/RsaMont/Arm.lean @@ -14,7 +14,8 @@ def base : Proof.Bignum.Arm.MontBackend := Proof.Variants.RsaMont.Arm.Base.varia def umaal : Proof.Bignum.Arm.MontBackend := Proof.Variants.RsaMont.Arm.Umaal.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [umaal, base] end VG.Proof.Variants.RsaMont.Arm diff --git a/lean/VerifiedGarbage/Proof/Variants/RsaPrivateCrt/AArch64.lean b/lean/VerifiedGarbage/Proof/Variants/RsaPrivateCrt/AArch64.lean index 40c85431c3e..9c7e52d8fd9 100644 --- a/lean/VerifiedGarbage/Proof/Variants/RsaPrivateCrt/AArch64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/RsaPrivateCrt/AArch64.lean @@ -12,7 +12,8 @@ open VG.AArch64 VG.Proof.Bignum VG.Proof.Bignum.AArch64 in def base : Proof.Rsa.AArch64.CrtImpl := Proof.Variants.RsaPrivateCrt.AArch64.Base.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [base] end VG.Proof.Variants.RsaPrivateCrt.AArch64 diff --git a/lean/VerifiedGarbage/Proof/Variants/RsaPrivateCrt/Arm.lean b/lean/VerifiedGarbage/Proof/Variants/RsaPrivateCrt/Arm.lean index 95056ba87b9..608d5eddc15 100644 --- a/lean/VerifiedGarbage/Proof/Variants/RsaPrivateCrt/Arm.lean +++ b/lean/VerifiedGarbage/Proof/Variants/RsaPrivateCrt/Arm.lean @@ -14,7 +14,8 @@ def base : Proof.Rsa.Arm.CrtImpl := Proof.Variants.RsaPrivateCrt.Arm.Base.varian def umaal : Proof.Rsa.Arm.CrtImpl := Proof.Variants.RsaPrivateCrt.Arm.Umaal.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [umaal, base] end VG.Proof.Variants.RsaPrivateCrt.Arm diff --git a/lean/VerifiedGarbage/Proof/Variants/RsaPrivateCrt/X86.lean b/lean/VerifiedGarbage/Proof/Variants/RsaPrivateCrt/X86.lean index a799781c0e5..534df936549 100644 --- a/lean/VerifiedGarbage/Proof/Variants/RsaPrivateCrt/X86.lean +++ b/lean/VerifiedGarbage/Proof/Variants/RsaPrivateCrt/X86.lean @@ -11,7 +11,8 @@ namespace VG.Proof.Variants.RsaPrivateCrt.X86 def base : Proof.Rsa.X86.PrivChecked.CrtImpl := Proof.Variants.RsaPrivateCrt.X86.Base.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [base] end VG.Proof.Variants.RsaPrivateCrt.X86 diff --git a/lean/VerifiedGarbage/Proof/Variants/RsaPrivateCrt/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/RsaPrivateCrt/X86_64.lean index 1aa57f30bd3..4333fac8532 100644 --- a/lean/VerifiedGarbage/Proof/Variants/RsaPrivateCrt/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/RsaPrivateCrt/X86_64.lean @@ -20,7 +20,8 @@ open VG.X86_64 VG.Proof.Bignum VG.Proof.Bignum.X86_64 VG.Proof.Rsa.X86_64 in def ifma : CrtImpl := Proof.Variants.RsaPrivateCrt.X86_64.Ifma.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [ifma, adx, base] end VG.Proof.Variants.RsaPrivateCrt.X86_64 diff --git a/lean/VerifiedGarbage/Proof/Variants/RsaPublicPrecomputed/AArch64.lean b/lean/VerifiedGarbage/Proof/Variants/RsaPublicPrecomputed/AArch64.lean index 5c84b20771d..f85048da97a 100644 --- a/lean/VerifiedGarbage/Proof/Variants/RsaPublicPrecomputed/AArch64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/RsaPublicPrecomputed/AArch64.lean @@ -12,7 +12,8 @@ open VG.AArch64 VG.Proof.Bignum VG.Proof.Bignum.AArch64 VG.Proof.Rsa.AArch64 in def base : PublicImpl := Proof.Variants.RsaPublicPrecomputed.AArch64.Base.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [base] end VG.Proof.Variants.RsaPublicPrecomputed.AArch64 diff --git a/lean/VerifiedGarbage/Proof/Variants/RsaPublicPrecomputed/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/RsaPublicPrecomputed/X86_64.lean index 9824ac84f21..08bc6aa2ea3 100644 --- a/lean/VerifiedGarbage/Proof/Variants/RsaPublicPrecomputed/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/RsaPublicPrecomputed/X86_64.lean @@ -20,7 +20,8 @@ open VG.X86_64 VG.Proof.Bignum VG.Proof.Bignum.X86_64 VG.Proof.Rsa.X86_64 in def ifma : PublicImpl := Proof.Variants.RsaPublicPrecomputed.X86_64.Ifma.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [ifma, adx, base] end VG.Proof.Variants.RsaPublicPrecomputed.X86_64 diff --git a/lean/VerifiedGarbage/Proof/Variants/Sha1/X86.lean b/lean/VerifiedGarbage/Proof/Variants/Sha1/X86.lean index a42f294c1b0..cc97133b02f 100644 --- a/lean/VerifiedGarbage/Proof/Variants/Sha1/X86.lean +++ b/lean/VerifiedGarbage/Proof/Variants/Sha1/X86.lean @@ -14,7 +14,8 @@ def scalar : Proof.Sha1.X86.Variants.Backend := Proof.Variants.Sha1.X86.Scalar.v def shaNi : Proof.Sha1.X86.Variants.Backend := Proof.Variants.Sha1.X86.ShaNi.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [shaNi, scalar] end VG.Proof.Variants.Sha1.X86 diff --git a/lean/VerifiedGarbage/Proof/Variants/Sha256/X86.lean b/lean/VerifiedGarbage/Proof/Variants/Sha256/X86.lean index bb8d63948f8..f84b050b66c 100644 --- a/lean/VerifiedGarbage/Proof/Variants/Sha256/X86.lean +++ b/lean/VerifiedGarbage/Proof/Variants/Sha256/X86.lean @@ -14,7 +14,8 @@ def scalar : Proof.Sha256.X86.Variants.Backend := Proof.Variants.Sha256.X86.Scal def shaNi : Proof.Sha256.X86.Variants.Backend := Proof.Variants.Sha256.X86.ShaNi.variant /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [shaNi, scalar] end VG.Proof.Variants.Sha256.X86 diff --git a/lean/VerifiedGarbage/Proof/Variants/Sm4Gcm/AArch64.lean b/lean/VerifiedGarbage/Proof/Variants/Sm4Gcm/AArch64.lean index f4927bd2284..440c64597d0 100644 --- a/lean/VerifiedGarbage/Proof/Variants/Sm4Gcm/AArch64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/Sm4Gcm/AArch64.lean @@ -13,7 +13,8 @@ def aes : Proof.Sm4Gcm.AArch64.GcmVariant := ⟨.aes⟩ def scalar : Proof.Sm4Gcm.AArch64.GcmVariant := ⟨.scalar⟩ /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [aes, scalar] end VG.Proof.Variants.Sm4Gcm.AArch64 diff --git a/lean/VerifiedGarbage/Proof/Variants/TripleDesEcb/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/TripleDesEcb/X86_64.lean index 8850687e274..8717aeb9334 100644 --- a/lean/VerifiedGarbage/Proof/Variants/TripleDesEcb/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/TripleDesEcb/X86_64.lean @@ -15,7 +15,8 @@ def avx512 : Proof.TripleDes.X86_64.Batch.EcbImpl := Proof.TripleDes.X86_64.Batc def sse2 : Proof.TripleDes.X86_64.Batch.EcbImpl := Proof.TripleDes.X86_64.Batch.sse /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [avx512, avx2, sse2] end VG.Proof.Variants.TripleDesEcb.X86_64 diff --git a/lean/VerifiedGarbage/Proof/Variants/Zeroize/X86_64.lean b/lean/VerifiedGarbage/Proof/Variants/Zeroize/X86_64.lean index e312ac54b49..d62c93e48c4 100644 --- a/lean/VerifiedGarbage/Proof/Variants/Zeroize/X86_64.lean +++ b/lean/VerifiedGarbage/Proof/Variants/Zeroize/X86_64.lean @@ -13,7 +13,8 @@ def avx : Proof.Zeroize.X86_64.ZeroizeImpl := .avx def scalar : Proof.Zeroize.X86_64.ZeroizeImpl := .scalar /-- Every implementation, best first: each function generic over them is -emitted with each (the registration files that import this). -/ +emitted with each (the registration files that import this), and its +dispatcher tries them in this order. -/ def variants := [avx, scalar] end VG.Proof.Variants.Zeroize.X86_64 diff --git a/lean/VerifiedGarbage/TCB/Emit.lean b/lean/VerifiedGarbage/TCB/Emit.lean index e18b62cffd4..b63f24a4fd1 100644 --- a/lean/VerifiedGarbage/TCB/Emit.lean +++ b/lean/VerifiedGarbage/TCB/Emit.lean @@ -14,64 +14,29 @@ say `VerifiedGarbage/Artifacts/Sha3/X86_64.lean`, must define the list an algorithm or a target adds such a file, so that parallel changes don't all edit one list. -## Variants and generic callers +## Variants Some functions have several implementations with the same contract (e.g. a compression function in scalar code and with the SHA extensions), and other functions call them. A caller whose proof holds for any implementation is *generic*: it is emitted once for each implementation, each a direct call -of that implementation. Adding an implementation must not edit its callers, -so both are found by their files: - -* a *variant* file, `VerifiedGarbage/Variants///.lean`, - defines `VG.Variants....variant`, one implementation - of the interface `` on ``; -* a *generic* file, `VerifiedGarbage/Generic///.lean`, - defines `VG.Generic....artifacts`, a function from a - variant of `` on `` to a list of artifacts. - -A caller of functions of several interfaces (e.g. a key derivation calling -both a hash's compression function and a block function, each with its own -implementations) is generic over all of them: its file is -`VerifiedGarbage/Generic//…///.lean`, and its -`artifacts` takes a variant of each interface on ``, in that order. -Variants of different interfaces may have the same suffix (e.g. `_avx2`), so -the instances of such a caller are named by `qualifiedName`: its name, then -for each interface whose variant has a non-empty suffix, in that order, a tag -for the interface and the suffix (`vg_argon2_blake2b_avx2_g_avx512`; -the instance of the baseline variants keeps the plain name, `vg_argon2`). -Callers of one interface append the suffix alone (`vg_sha256_update_shani`). - -A caller may instead take every variant of an interface at once, as a list: -one that calls whichever of them it is given at run time, through a table -of functions (`Abi.withTables`), whose contract the emitter checks lists -every table (`Rust.checkFnTables`). Its file names the interface `All` -(`allOf`), e.g. `VerifiedGarbage/Generic/MdHash/RsaPrivateCrt/AllMdHash/X86_64/RsaOaep.lean`, -and its `artifacts` takes, in that place, the list of every variant of -`` on ``, in order of their module names: it is emitted once -for them all, rather than once for each. So a new variant reaches it, as it -reaches the callers of one variant, without anyone listing it. - -The artifacts emitted include each generic function applied to each variant -of its interface and target (each combination of a variant of each of its -interfaces, and the list of every variant of each `All` interface), in order -of their module names. What a variant is (its type) -is up to the interface, as long as every variant and every generic function -of it agree, which elaboration checks. Nothing here needs trusting beyond -the files found: every artifact carries its own proof, and -the emitter checks its calls and features (`Rust.files`) as for any other. -A variant's own functions (e.g. the implementation itself) are listed in a -registration file as usual, if the Rust code calls them; if only its callers -do, they are internal functions, which the emitter writes from the code of -their calls (`Rust.Internal`). - -`Emit.lean` finds the registration, variant and generic files -(`registrations`, `grouped`, `genericGroups`), writes a program that -imports them all and the ranking of the dispatchers of every target -(`VerifiedGarbage/Dispatch.lean`, `VG.Dispatch.ranking`, which only orders -the implementations a dispatcher tries: see `Rust.Rank`), checks that the whole list depends only on the standard -axioms, that what it runs is what the kernel checked and that the `Spec/` -names it uses come from `Spec/` (`TCB/Audit.lean`), and emits it +of that implementation. Its registration file says so in plain Lean, by +applying it to each element of the list of the implementations (the +*variants*, `Proof/Variants//.lean`), which is not trusted: +every artifact carries its own proof, and the emitter checks its calls and +features (`Rust.files`) as for any other. So adding an implementation adds +an element to that list, and edits none of its callers. + +Each function's dispatcher tries its implementations in the order their +registration file lists them (`Rust.dispatcher`): a function's +implementations are those of its `Api` (`Artifact.base`), which must all be +in one registration file (`checkSources`). Which comes first is a matter of +speed, not of correctness or safety, so the order is not trusted either. + +`Emit.lean` finds the registration files (`registrations`), writes a +program that imports them all, checks that the whole list depends only on +the standard axioms, that what it runs is what the kernel checked and that +the `Spec/` names it uses come from `Spec/` (`TCB/Audit.lean`), and emits it (`driver`), and runs that program, whose `main` is `run`. -/ @@ -89,12 +54,6 @@ def registrationDir : FilePath := "VerifiedGarbage" / "Artifacts" def moduleOf (path : FilePath) : String := ".".intercalate ((path.withExtension "").components.filter (· != ".")) -/-- The directory of the variant files, relative to `lean/`. -/ -def variantDir : FilePath := "VerifiedGarbage" / "Variants" - -/-- The directory of the generic files, relative to `lean/`. -/ -def genericDir : FilePath := "VerifiedGarbage" / "Generic" - /-- The modules of the `.lean` files under `dir`, in order of their names. -/ def modulesUnder (dir : FilePath) : IO (List String) := do unless ← dir.pathExists do return [] @@ -106,177 +65,87 @@ def modulesUnder (dir : FilePath) : IO (List String) := do order of their module names. -/ def registrations : IO (List String) := modulesUnder registrationDir -/-- The interface and target of a variant module `mod`, e.g. -`Sha256Compress.X86_64` for `VerifiedGarbage.Variants.Sha256Compress.X86_64.ShaNi`; -`none` unless it is exactly three levels below the directory. -/ -def groupOf (mod : String) : Option String := - match mod.splitOn "." with - | [_, _, i, t, _] => some s!"{i}.{t}" - | _ => none - -/-- The modules under `dir`, grouped by interface and target (`groupOf`), in -order of the groups' names; fails on a module at any other depth. -/ -def grouped (dir : FilePath) : IO (List (String × List String)) := do - let mods ← modulesUnder dir - let mut groups : List (String × List String) := [] - for m in mods do - let some g := groupOf m - | throw <| IO.userError s!"{m} must be at {dir}///.lean" - groups := match groups.find? (·.1 == g) with - | some _ => groups.map fun (h, ms) => if h == g then (h, ms ++ [m]) else (h, ms) - | none => groups ++ [(g, [m])] - return groups.mergeSort (·.1 ≤ ·.1) - /-- The name of the list a registration module defines: `VerifiedGarbage.Artifacts.Sha3.X86_64` defines `VG.Artifacts.Sha3.X86_64.artifacts`. -/ def listOf (mod : String) : String := "VG." ++ (mod.dropPrefix "VerifiedGarbage.").toString ++ ".artifacts" -/-- The variant a variant module defines: -`VerifiedGarbage.Variants.Sha256Compress.X86_64.ShaNi` defines -`VG.Variants.Sha256Compress.X86_64.ShaNi.variant`. -/ -def variantOf (mod : String) : String := - "VG." ++ (mod.dropPrefix "VerifiedGarbage.").toString ++ ".variant" - -/-- The interfaces of a generic module `mod`, each with its target: -`["Sha256Compress.X86_64"]` for -`VerifiedGarbage.Generic.Sha256Compress.X86_64.Pbkdf2`, and -`["Blake2b.X86_64", "Argon2Compress.X86_64"]` for -`VerifiedGarbage.Generic.Blake2b.Argon2Compress.X86_64.Argon2`; `none` -unless there is at least one interface, a target and a name below the -directory. -/ -def interfacesOf (mod : String) : Option (List String) := - match mod.splitOn "." with - | _ :: _ :: rest => - match rest.reverse with - | _ :: t :: i :: is => some ((i :: is).reverse.map (· ++ "." ++ t)) - | _ => none - | _ => none - -/-- The modules under `dir` (generic files), grouped by their interfaces -(`interfacesOf`), in order of the groups' names; fails on a module with no -interface. -/ -def genericGroups (dir : FilePath) : IO (List (List String × List String)) := do - let mods ← modulesUnder dir - let mut groups : List (List String × List String) := [] - for m in mods do - let some g := interfacesOf m - | throw <| IO.userError s!"{m} must be at {dir}//…//.lean" - groups := match groups.find? (·.1 == g) with - | some _ => groups.map fun (h, ms) => if h == g then (h, ms ++ [m]) else (h, ms) - | none => groups ++ [(g, [m])] - return groups.mergeSort fun a b => ".".intercalate a.1 ≤ ".".intercalate b.1 - -/-- The name of an instance of a generic function of several interfaces -(see above): `base`, then `_` for each `(tag, suffix)` of -`parts` (one per interface, in order) whose suffix is not empty. Suffixes -start with `_`, so `qualifiedName "vg_argon2" [("blake2b", "_avx2"), -("g", "")]` is `vg_argon2_blake2b_avx2`. Two variants with the same -suffix in different interfaces give different names, as long as the tags -differ. -/ +/-- The name of an instance of a generic function of several interfaces: +`base`, then `_` for each `(tag, suffix)` of `parts` (one per +interface, in order) whose suffix is not empty. Suffixes start with `_`, so +`qualifiedName "vg_argon2" [("blake2b", "_avx2"), ("g", "")]` is +`vg_argon2_blake2b_avx2`. Two variants with the same suffix in different +interfaces give different names, as long as the tags differ. It only names +them: the emitter checks that names are unique (`Rust.checkUnique`). -/ def qualifiedName (base : String) (parts : List (String × String)) : String := base ++ String.join (parts.map fun (tag, suffix) => if suffix.isEmpty then "" else "_" ++ tag ++ suffix) -/-- The interface whose variants the interface `i` of a generic function -takes (`.`), and whether it takes all of them at once, as a -list: an interface `All.` takes every variant of -`.`. -/ -def allOf (i : String) : String × Bool := - if i.startsWith "All" then ((i.drop 3).toString, true) else (i, false) - -/-- The name of the variable bound to a variant of the `i`th interface of a -generic function: `v`, then `v2`, `v3`, …. -/ -def varName (i : Nat) : String := if i == 0 then "v" else s!"v{i + 1}" - -/-- The artifacts of the generic modules `gens` of the same interfaces and -target, for each combination of a variant module of each interface, in -`vars` (one list per interface, in order, with whether the interface takes -them all, `allOf`, as one list): one term of type `List Artifact`. -/ -def instances (gens : List String) (vars : List (Bool × List String)) : String := - let names := (List.range vars.length).map varName - let apply (g : String) : String := listOf g ++ String.join (names.map (" " ++ ·)) - let body := "List.flatten [" ++ ", ".intercalate (gens.map apply) ++ "]" - (names.zip vars).foldr (fun (v, all, ms) acc => - let list := "[" ++ ", ".intercalate (ms.map variantOf) ++ "]" - if all then s!"((fun {v} => {acc}) {list})" - else s!"(List.flatMap (fun {v} => {acc}) {list})") body - /-- The program that emits every artifact: those of each registration -module `mods`, in order, then each generic function of `gens` applied to -each variant of its interface and target in `vars` (each combination of -variants of its interfaces). It fails to elaborate unless each module defines its list -(or variant), with types that agree, unless everything emitted depends -only on the standard axioms (`#assert_standard_axioms`), unless the compiled -code of everything emitted, of the ranking of the dispatchers -(`VerifiedGarbage/Dispatch.lean`, `Rust.Rank`) and of `run` is what the kernel checked +module `mods`, in order, each with the name of its module. It fails to +elaborate unless each module defines its list, unless everything emitted +depends only on the standard axioms (`#assert_standard_axioms`), unless the +compiled code of everything emitted and of `run` is what the kernel checked (`#assert_no_compiler_overrides`) and unless every `VG.Spec` definition is declared in `Spec/` (`#assert_spec_origin`). It is a module, which does not load the private data (`.olean.private`) of what it imports: run with Lake's setup for it (`lake setup-file`), Lean reads only their `.olean`, `.olean.server` and IR, and the audits take the rest from the audit records (`TCB/AuditRecords.lean`). -/ -def driver (mods : List String) (gens : List (List String × List String) := []) - (vars : List (String × List String) := []) : String := - let groups := gens.map fun (is, gs) => - (gs, is.map fun i => let (b, all) := allOf i; (all, ((vars.find? (·.1 == b)).map (·.2)).getD [])) +def driver (mods : List String) : String := -- Each module both ways: `public import` for its declarations, `public meta import` -- for its compiled code, which `main` runs. let imp (m : String) := s!"public import {m}\npublic meta import {m}\n" "-- Generated by Emit.lean, which runs it. DO NOT EDIT.\nmodule\n\n" ++ imp "VerifiedGarbage.TCB.Audit" ++ imp "VerifiedGarbage.TCB.Axioms" ++ imp "VerifiedGarbage.TCB.Emit" ++ - imp "VerifiedGarbage.Dispatch" ++ - String.join ((mods ++ gens.flatMap (·.2) ++ vars.flatMap (·.2)).map imp) ++ + String.join (mods.map imp) ++ "\npublic section\n\nnamespace VG.Emit\n\n" ++ - "def all : List Artifact := List.flatten [\n" ++ - String.join (mods.map fun m => s!" {listOf m},\n") ++ - String.join (groups.map fun (gs, vs) => s!" {instances gs vs},\n") ++ - " []]\n\n" ++ + "def all : List (String × List Artifact) := [" ++ + ",".intercalate (mods.map fun m => s!"\n (\"{m}\", {listOf m})") ++ "]\n\n" ++ "#assert_standard_axioms all\n" ++ - "#assert_no_compiler_overrides all VG.Dispatch.ranking run\n" ++ + "#assert_no_compiler_overrides all run\n" ++ "#assert_spec_origin\n\n" ++ "end VG.Emit\n\n" ++ - "def main (args : List String) : IO UInt32 :=\n VG.Emit.run VG.Emit.all VG.Dispatch.ranking args\n\nend\n" + "def main (args : List String) : IO UInt32 :=\n VG.Emit.run VG.Emit.all args\n\nend\n" -/-- The target directory a registration, generic or variant module's path -names: `X86_64` for `VerifiedGarbage.Artifacts.Sha3.X86_64`, -`VerifiedGarbage.Generic.Sha256Compress.X86_64.Pbkdf2` and -`VerifiedGarbage.Variants.Sha256Compress.X86_64.ShaNi`. -/ +/-- The target directory a registration module's path names: `X86_64` for +`VerifiedGarbage.Artifacts.Sha3.X86_64`. -/ def targetDirOf (mod : String) : Option String := match mod.splitOn "." with | ["VerifiedGarbage", "Artifacts", _, t] => some t - | "VerifiedGarbage" :: d :: rest => - if (d == "Generic" || d == "Variants") && rest.length ≥ 3 then rest[rest.length - 2]? - else none | _ => none -/-- The registration modules `mods`, generic modules `gens` and variant -modules `vars` (as `driver` takes them) of each target directory -(`targetDirOf`), in order of its name; fails on a module without one. -`Emit.lean` runs a driver for each (a *part*): where they are split only -decides how much each process does, as `finish` checks that no target's -artifacts are in two parts. -/ -def parts (mods : List String) (gens : List (List String × List String)) - (vars : List (String × List String)) : - Except String (List (String × List String × List (List String × List String) × - List (String × List String))) := do +/-- The registration modules `mods` of each target directory (`targetDirOf`), +in order of its name; fails on a module without one. `Emit.lean` runs a +driver for each (a *part*): where they are split only decides how much each +process does, as `finish` checks that no target's artifacts are in two +parts. -/ +def parts (mods : List String) : Except String (List (String × List String)) := do let dirOf (m : String) : Except String String := match targetDirOf m with | some t => pure t | none => throw s!"{m} names no target directory" let mut dirs : List String := [] - for m in mods ++ gens.flatMap (·.2) ++ vars.flatMap (·.2) do + for m in mods do let t ← dirOf m unless dirs.contains t do dirs := dirs ++ [t] dirs.mergeSort (· ≤ ·) |>.mapM fun t => do - let ms ← mods.filterM fun m => return (← dirOf m) == t - let gs ← gens.filterMapM fun (is, gs) => do - let gs ← gs.filterM fun g => return (← dirOf g) == t - return if gs.isEmpty then none else some (is, gs) - let vs ← vars.filterMapM fun (i, vs) => do - let vs ← vs.filterM fun v => return (← dirOf v) == t - return if vs.isEmpty then none else some (i, vs) - return (t, ms, gs, vs) + return (t, ← mods.filterM fun m => return (← dirOf m) == t) + +/-- Every implementation of a function on a target (every artifact of its +`Api`, `Artifact.base`) is in the list of one registration module of `all`, +so that the order that list gives them is the one its dispatcher tries +them in (`Rust.dispatcher`). -/ +def checkSources (all : List (String × List Artifact)) : Except String Unit := do + let mut seen : List ((String × String) × String) := [] + for (m, as) in all do + for a in as do + match seen.lookup (a.target.name, a.base) with + | some m' => + unless m' == m do + throw s!"{a.target.name}: the implementations of {a.base} are in two registration \ + files, {m'} and {m}: its dispatcher tries them in the order one lists them" + | none => seen := ((a.target.name, a.base), m) :: seen def usage : String := "usage: lake env lean --run Emit.lean [--check] [DIR]" @@ -345,21 +214,25 @@ def codeDirs (dir : FilePath) : IO (List String) := do ds := ds ++ (Rust.generatedDirs (e.fileName.drop (Rust.crateDir "").length).toString).toArray return (ds.qsort (· < ·)).toList -/-- Writes the files generated from `as` (with the dispatchers that `ranking` -orders, `Rust.Rank`) under the directory in `args` (by +/-- Writes the files generated from the artifacts of `all` (those of each +registration module, `checkSources`) under the directory in `args` (by default `../crates`), deleting any other generated file in the directories of generated files of a crate of generated code there (`codeDirs`, and nothing else: the rest of those crates, and the other crates, are not the emitter's); or, if `args` starts with `--check`, writes nothing and fails if the files on disk differ from what would be generated. -With `--part FILE`, `as` are the artifacts of a part (`parts`): only the +With `--part FILE`, `all` are the registration modules of a part (`parts`): only the files of their targets' crates are written or checked, and the other generated files in those crates' directories of generated files; then `FILE` gets their targets, for `finish`, which checks the rest. -/ -def run (as : List Artifact) (ranking : Rust.Ranking) (args : List String) : IO UInt32 := do +def run (all : List (String × List Artifact)) (args : List String) : IO UInt32 := do let some (check, part, dir) := parseArgs args | do IO.eprintln usage; return 2 - let files ← match Rust.files as ranking with + if let .error e := checkSources all then + IO.eprintln e + return 1 + let as := all.flatMap (·.2) + let files ← match Rust.files as with | .ok files => pure files | .error e => do IO.eprintln e; return 1 let targets := Rust.targetsOf as diff --git a/lean/VerifiedGarbage/TCB/Registration.lean b/lean/VerifiedGarbage/TCB/Registration.lean index 439fb96c3ba..856144972b0 100644 --- a/lean/VerifiedGarbage/TCB/Registration.lean +++ b/lean/VerifiedGarbage/TCB/Registration.lean @@ -169,6 +169,13 @@ structure Artifact where module : String /-- The Rust function name. Must be unique within the target. -/ name : String + /-- The name of the function it implements, its `Api`'s (`Api.artifact`), + which `name` extends with the implementation's suffix: the artifacts of a + target with the same `base` are the implementations of one function, which + its dispatcher may call in each other's place (`Rust.groupsOf`). By + default, its own name: an artifact not made from an `Api` is the only + implementation of its function. -/ + base : String := name /-- The Rust signature, rendered as the parameter list and return type (`Sig.rust`). -/ sig : Sig @@ -344,7 +351,7 @@ structure Registration (api : Api) (target : Target) where /-- The artifact of the implementation `r` of `api` on `target`: its module, signature and `writeArgs` are `api`'s, its name `api`'s followed by -`r.suffix`, its documentation `api`'s (`Api.doc`), and its +`r.suffix` (its `base` `api`'s), its documentation `api`'s (`Api.doc`), and its contract `api`'s on `target`, for `r.consts`, `r.tables` and `r.stack` (`Api.contract`), which makes the mode public if `api.pubMode`; its code is printed and documented with `target`'s ISA's `Emitter` (`EmitTarget.of`). -/ @@ -353,6 +360,7 @@ def Api.artifact (api : Api) (target : Target) [Emitter target.isa] (r : Registr target := .of target module := api.module name := api.name ++ r.suffix + base := api.name sig := api.sig doc := api.doc consts := r.consts diff --git a/lean/VerifiedGarbage/TCB/Rust.lean b/lean/VerifiedGarbage/TCB/Rust.lean index 22f5f6c75a3..d3f2b11040a 100644 --- a/lean/VerifiedGarbage/TCB/Rust.lean +++ b/lean/VerifiedGarbage/TCB/Rust.lean @@ -52,25 +52,6 @@ parses all of a codegen unit's module-level assembly again each time it imports a function from that unit, which it does for the Rust functions it shares a unit with. In a crate of their own they share a unit with none. -Artifacts of a module can render to the same function but for its name -(and documentation and constants): the same `cfg` attribute, calling -convention, signature and body, calls and tables included (`sameKey`), as -when a generic caller's instances for two variants of an interface call -none of what tells the variants apart. Only the one with the shortest name -is then emitted as a function, and each other as another name of it -(`alias`), keeping its own documentation, saying so, and its own constants: - -```rust -#[allow(unused_imports)] -pub use self:: as ; -``` - -Calling it runs that function, whose text is the text the artifact's own -function would have: the same machine code, which its proof verified -against its own contract. Only artifacts of the same module are -compared, so that every file is a function of its own artifacts alone -(which `EmitOne.lean` relies on). - A call (`Code.call name body`) is a call instruction whose operand is the symbol of the Rust function `name` (a `sym` operand). The model runs `body` for it, so `files` checks that the function emitted as `name` has printed @@ -838,31 +819,6 @@ where "#[unsafe(naked)]\n" ++ s!"{vis} unsafe extern \"{a.target.rustAbi}\" fn {a.name}" ++ code -/-- What makes two functions of a module the same function: their `cfg` -attribute, calling convention, signature and body (`gateOf`, `codeOf`), -everything of `function`'s text but the name, the documentation and the -constants. -/ -def sameKey (a : Artifact) (code : String) : String := - gateOf a ++ "\n" ++ a.target.rustAbi ++ "\n" ++ code - -/-- `a`'s function as another name of the function `canon` of the same -module, whose text but for its name, documentation and constants is the -text `a`'s would have (`sameKey`): a `use` of it as `a`'s name, under `a`'s -`cfg` attribute, with `a`'s documentation (saying so) and constants. -/ -def alias (a : Artifact) (canon : String) (vis : String := "pub") : String := - let gate := gateOf a - let note := s!"The machine code of this function is that of `{canon}`, which is emitted once \ - and named both ways: the two are the same function." - let doc := fullDoc a - let doc := if (doc.splitOn "\n\n# Safety\n\n").length > 1 then insertNotes doc [note] - else doc ++ "\n\n" ++ note - featuresConst a.name gate a.features ++ - writtenConsts a.name gate a.written ++ - docComment "" doc ++ - gate ++ - "#[allow(unused_imports)]\n" ++ - s!"{vis} use self::{canon} as {a.name};\n" - /-- The tables of constants of `a`: those its contract gives it (`Artifact.consts`) and those its instructions carry (`Printer.codeTables`, x86-64's `aluTbl`). -/ @@ -952,40 +908,23 @@ structure Prepared extends Shared where def Prepared.of (s : Shared) : Prepared := ⟨s, _, _, rfl, rfl⟩ /-- The functions of a module, the artifacts of `ps` in order: each with the -text of its function after its name (`codeOf`) and the name of the function -it is emitted as: of those with the same `sameKey`, the one with the -shortest name (the first of them, if several are as short). -/ +text of its function after its name (`codeOf`). -/ def moduleItems (moduleOf : Artifact → String → String) (ps : List Prepared) : - List (Prepared × String × String) := - let items := ps.map fun p => - let code := codeOf p.art p.lines (moduleOf p.art) - let key := sameKey p.art code - (p, code, hash key, key) - -- The function each key's artifacts are emitted as. - let canon (h : UInt64) (key : String) : String := - (items.filter fun (_, _, h', k) => h' == h && k == key).foldl (init := none) - (fun best (p, _) => match best with - | some b => if p.art.name.length < b.length then some p.art.name else some b - | none => some p.art.name) |>.getD "" - items.map fun (p, code, h, key) => (p, code, canon h key) - -/-- The text of the functions of a module (`moduleItems`): each as `function` -writes it, if it is emitted as itself, else the `alias` of the function it -is emitted as; each with the visibility `vis` gives its name. -/ -def moduleText (items : List (Prepared × String × String)) - (vis : String → String := fun _ => "pub") : String := - String.join (items.map fun (p, code, c) => - "\n" ++ if c == p.art.name then function.withCode p.art code (vis c) - else alias p.art c (vis p.art.name)) + List (Prepared × String) := + ps.map fun p => (p, codeOf p.art p.lines (moduleOf p.art)) + +/-- The text of the functions of a module (`moduleItems`), each as `function` +writes it, with the visibility `vis` gives its name. -/ +def moduleText (items : List (Prepared × String)) (vis : String → String := fun _ => "pub") : + String := + String.join (items.map fun (p, code) => "\n" ++ function.withCode p.art code (vis p.art.name)) /-- The assembly file of the functions of a module (`moduleItems`), which the -crate does not compile: each function emitted as itself as its name, then -its lines (`assembly`), each indented but its labels; each other as a line -naming the function it is. -/ -def moduleAssembly (items : List (Prepared × String × String)) : String := - String.join (items.map fun (p, _, c) => - if c == p.art.name then functionAssembly p.art.target p.art.name (bodyOf p.art p.lines) - else s!"\n// {p.art.name}: the function `{c}`\n") +crate does not compile: each function's name, then its lines (`assembly`), +each indented but its labels. -/ +def moduleAssembly (items : List (Prepared × String)) : String := + String.join (items.map fun (p, _) => + functionAssembly p.art.target p.art.name (bodyOf p.art p.lines)) where /-- The function `name` of the target `T`, whose lines are `body`, in an assembly file: its name, then its lines (`assembly`), each indented but its @@ -1279,10 +1218,11 @@ def checkCalls (ps : List Prepared) (hs : List Internal) (p : Prepared) : Except /-! ## Tables of functions -/ /-- The tables of functions the artifacts `as` take (`Artifact.tables`), -each once, in order of their names. -/ +each once, in the order they first list them: the order a table's +dispatcher tries its implementations in (`tableDispatcher`). -/ def fnTables (as : List Artifact) : List FnTable := let all := as.flatMap fun a => a.tables.flatMap (·.2) - ((dedup (all.map (·.name))).mergeSort (· ≤ ·)).filterMap fun n => all.find? (·.name == n) + (dedup (all.map (·.name))).filterMap fun n => all.find? (·.name == n) /-- An interface's name is a Rust type's: an uppercase ASCII letter, then ASCII letters and digits; so it is never a keyword, and is not `Table` (the @@ -1305,11 +1245,6 @@ def fnTableFeatures (ps : List Prepared) (hs : List Internal) (t : String) (T : | some q => q.art.features | none => ((internal? hs t f).map (·.features)).getD []) -/-- What `sameKey` compares of `p`'s artifact, with every function it calls -in the same module (`codeOf`'s `moduleOf`): artifacts emitted as one -function (`alias`) have the same `sameKey`, and so the same `aliasKey`. -/ -def aliasKey (p : Prepared) : String := sameKey p.art (codeOf p.art p.lines fun _ => "") - /-- The tables of functions `p`'s artifact takes (`Artifact.tables`), checked against those of the artifacts of `ps` and the internal functions `hs`: its target's code can name a `static`, and has pointers of 32 or 64 bits, @@ -1324,10 +1259,8 @@ of a name, of any artifact of the target, is the same table, and every table of an interface has as many functions and as many words of data; and every function a table names is an artifact or an internal function of the target, emitted with the code the model runs for each call of it -(`checkCalls`), and, if an artifact, not one that may be emitted as another -name of a function (`alias`, which an artifact of its module with the same -`aliasKey` may be): `Abi.withTables` says the functions of distinct names -are at distinct addresses, as distinct naked functions are. -/ +(`checkCalls`): `Abi.withTables` says the functions of distinct names are +at distinct addresses, as distinct naked functions are. -/ def checkFnTables (ps : List Prepared) (hs : List Internal) (p : Prepared) : Except String Unit := do let a := p.art @@ -1361,16 +1294,10 @@ def checkFnTables (ps : List Prepared) (hs : List Internal) (p : Prepared) : throw s!"{t}: {a.name}'s table of functions {T.name} has a word of data wider than \ its target's {w}-bit pointers" for f in T.fns do - match ps.find? fun q => q.art.target.name == t && q.art.name == f with - | some q => - if ps.any fun q' => q'.art.target.name == t && q'.art.module == q.art.module && - q'.art.name != f && aliasKey q' == aliasKey q then - throw s!"{t}: the table of functions {T.name} names {f}, which may be emitted as \ - another name of a function of the same code (`alias`), at that function's address" - | none => - unless (internal? hs t f).isSome do - throw s!"{t}: the table of functions {T.name} names {f}, which is neither an \ - artifact nor an internal function" + unless ps.any (fun q => q.art.target.name == t && q.art.name == f) || + (internal? hs t f).isSome do + throw s!"{t}: the table of functions {T.name} names {f}, which is neither an \ + artifact nor an internal function" for U in others do if U.iface == T₀.iface && !names.contains U.name then throw s!"{t}: {a.name}'s parameter `{x}` does not list the table of functions {U.name} \ @@ -1628,10 +1555,10 @@ A function with several implementations (its baseline and its variants, each of which may need CPU features) gets a *dispatcher*, an ordinary Rust function that runs the best of them the CPU supports, so that the Rust code calling it never names a variant or its features. The implementations of a -function are the artifacts of a module with the same signature and -documentation (`Artifact.sig`, `Artifact.doc`: `Api.artifact` gives every -artifact of an `Api` the `Api`'s) whose names are that of one of them, the -*base*, followed by `_` (`groupsOf`). Whichever of them it calls, the +function are the artifacts of a module made from the same `Api` +(`Artifact.base`, its name: `groupsOf`), of which the one with that name is +the *base*; each has the `Api`'s signature and documentation +(`checkGroups`). Whichever of them it calls, the dispatcher keeps the promise of their common documentation, which is therefore its own, but for what depends on the implementation: the stack its calls and frames use (it documents the largest, `Sig.layoutDoc`, whose @@ -1698,17 +1625,19 @@ Where the compilation target enables every feature an implementation's condition asks for, its `const` block is `true`, so the compiler sees that `select` returns it, and nothing refers to the implementations after it, which are then not linked: built for `x86-64-v3`, a function with an AVX2 -implementation links neither its baseline nor anything else ranked after -that implementation, but still detects the features of those ranked before +implementation links neither its baseline nor anything else tried after +that implementation, but still detects the features of those tried before it. This is what the dispatcher would choose anyway, as detection reports every feature of `COMPILED` (`VG_CPU_FEATURES` cannot remove them). Which implementation is best is a matter of speed, never of correctness or safety: each is proven against the contract that their common documentation -states, and each is called only on a CPU with its features. So the ranking -(`Rank`) is not trusted, and comes from outside the trusted base -(`VerifiedGarbage/Dispatch.lean`). The dispatcher tries the implementations -in the order it gives, each where the CPU also has the `extra` features +states, and each is called only on a CPU with its features. So their order +is not trusted: the dispatcher tries the implementations in the order their +registration file lists them (`Emit.checkSources`: one file lists them all; +a generic caller's instances are in the order of the variants of its +callees, `Proof/Variants//.lean`), each where the CPU also +has the `extra` features its registration gives it (`Artifact.extra`) and, if its registration names a `vendor` (`Artifact.vendor`), only where the compilation target's `target_vendor` is that, or where `VG_CPU_FEATURES` @@ -1721,15 +1650,8 @@ every CPU but where `VG_CPU_FEATURES` is `none`), so that the tests run the base everywhere with `VG_CPU_FEATURES=none` (AArch64's `_neon` code needs none: AdvSIMD is part of its baseline), or where the compilation target enables some (`COMPILED`). It falls back on the base, which needs -no feature. An implementation of a function generic over several interfaces -has a suffix of several parts (`Emit.qualifiedName`, `_` for -each interface whose variant is not the baseline): unless its whole suffix -is ranked, each part's suffix is (a part that is all of a ranked suffix -having the tag `""`), and the implementations are ordered by the -rank of their part for the first tag, then for the next, and so on (a -missing part being the baseline, ranked last), which chooses the best variant -of each interface in turn. An implementation that can be chosen only where -one ranked before it can is left out (it would never run). +no feature. An implementation that can be chosen only where one before it +can would never run, and fails the emitter (`armsOf`). A function whose base is its only implementation gets a dispatcher too, as another name of it, so that the Rust code calling the dispatcher is the same @@ -1747,8 +1669,8 @@ back on), and neither does an implementation that is not a base. A table of functions (`FnTable`) is chosen the same way, by `dispatch/fn_tables.rs` (`tableDispatcher`). Its implementations are the tables of the same interface and the same words of data whose names are its -own followed by `_` (`tableGroupsOf`), ranked as the suffix in -lowercase in the module of its first function. A function taking tables +own followed by `_` (`tableGroupsOf`), tried in the order the +artifacts taking them list them. A function taking tables is proven for every table of their interface, and what it computes depends on which only through the table's data (`Abi.withTables`; for instance the index of the hash function, `Spec/HashTable.lean`), so these tables give the @@ -1774,76 +1696,29 @@ A table that needs CPU features gets none (there is nothing to fall back on), and neither does an implementation that is not a base; a table with no other implementation gets one, returning it. -/ -/-- How a dispatcher (see above) ranks an implementation of a function: one -whose name is that of its base followed by `suffix`. -/ -structure Rank where - suffix : String - -/-- The ranking of each target, by its name (`Target.name`): its `Rank`s, -best first. -/ -abbrev Ranking := List (String × List Rank) - -/-- The implementations of a function of a module: its base and each other -(with its suffix: what follows the base's name in its own). -/ +/-- The implementations of a function of a module: its base and each other. -/ structure Group where base : Artifact - members : List (Artifact × String) + members : List Artifact /-- The functions of the artifacts `as` of one module and target, with their -implementations: in order of their names' lengths, an artifact is an -implementation of the first function found whose base has its signature and -documentation and whose base's name, followed by `_`, starts its own, and is -otherwise the base of a function of its own. -/ -def groupsOf (as : List Artifact) : List Group := Id.run do - let sorted := as.mergeSort fun a b => a.name.length ≤ b.name.length - let mut gs : Array Group := #[] - for a in sorted do - match gs.findIdx? fun g => - a.name.startsWith (g.base.name ++ "_") && a.sig == g.base.sig && a.doc == g.base.doc with - | some i => - gs := gs.modify i fun g => - { g with members := g.members ++ [(a, (a.name.drop g.base.name.length).toString)] } - | none => gs := gs.push ⟨a, []⟩ - return gs.toList - -/-- The first `Rank` of `rs` for an implementation with the suffix `s`, with -its index in `rs`. -/ -def rankOf (rs : List Rank) (s : String) : Option (Nat × Rank) := - go rs 0 -where - go : List Rank → Nat → Option (Nat × Rank) - | [], _ => none - | r :: rs, i => - if r.suffix == s then some (i, r) - else go rs (i + 1) - -/-- The suffix `s` of an implementation, as parts -`_` each of whose suffixes `rs` ranks (`Emit.qualifiedName`): -each part's tag, and the index in `rs` and `Rank` of its suffix, taking the -longest ranked suffix after each tag that leaves a suffix of parts; `none` if -there is no such way to read it. `fuel` bounds the number of parts. -/ -def partsOf (rs : List Rank) : Nat → String → Option (List (String × Nat × Rank)) - | _, "" => some [] - | 0, _ => none - | fuel + 1, s => - if !s.startsWith "_" then none else - let rest := (s.drop 1).toString - let tag := (rest.splitOn "_").headD "" - let after := (rest.drop tag.length).toString - let cands := (rs.filter fun r => !r.suffix.isEmpty && after.startsWith r.suffix).mergeSort - fun a b => a.suffix.length ≥ b.suffix.length - cands.findSome? fun r => - let tail := (after.drop r.suffix.length).toString - if tag.isEmpty || !(tail.isEmpty || tail.startsWith "_") then none else - match partsOf rs fuel tail, rankOf rs r.suffix with - | some ps, some (i, r) => some ((tag, i, r) :: ps) - | _, _ => none - -/-- Whether the list of numbers `a` comes before `b`, or is `b`. -/ -def lexLe : List Nat → List Nat → Bool - | [], _ => true - | _ :: _, [] => false - | x :: xs, y :: ys => x < y || (x == y && lexLe xs ys) +implementations: each artifact whose name is its `base` (`Artifact.base`, +its `Api`'s name) is the base of a function, whose other implementations are +the other artifacts of that `base`, in the order of `as`; an artifact whose +base is not among `as` is the base of a function of its own. -/ +def groupsOf (as : List Artifact) : List Group := + let bases := as.filter fun a => a.name == a.base || !as.any (·.name == a.base) + bases.map fun b => + ⟨b, if b.name == b.base then as.filter fun a => a.base == b.base && a.name != b.name else []⟩ + +/-- Every implementation of a function of `as` (`groupsOf`) has its base's +signature and documentation, which are what its dispatcher documents and +calls them with: those of their `Api` (`Api.artifact`). -/ +def checkGroups (as : List Artifact) : Except String Unit := + (groupsOf as).forM fun g => g.members.forM fun a => do + unless a.sig == g.base.sig && a.doc == g.base.doc do + throw s!"{a.target.name}: {a.name}, an implementation of {g.base.name}, has another \ + signature or documentation" /-- An implementation a dispatcher may choose: a function (`Impl.ofArtifact`) or a table of functions (`tableDispatcher`), by its name; the module, from @@ -1874,37 +1749,21 @@ structure Arm where extra : List String vendors : List String -/-- The implementations `members` (each with its suffix) of a function or -table of the target `t`, but its base, that its dispatcher tries, in order -(see above), or why `rs` does not rank them. -/ -def armsOf (rs : List Rank) (t : String) (members : List (Impl × String)) : - Except String (List Arm) := do - let arm (a : Impl) : Arm := ⟨a, a.extra, a.vendor.toList⟩ - let keyed ← if members.all fun (_, s) => (rankOf rs s).isSome then - pure (members.map fun (a, s) => match rankOf rs s with - | some (i, _) => ([i], arm a) - | none => ([], arm a)) - else do - let parsed ← members.mapM fun (a, s) => - match (rankOf rs s).map (fun (i, r) => [("", i, r)]) <|> partsOf rs s.length s with - | some ps => pure (a, ps) - | none => throw s!"{t}: {a.name}: its suffix `{s}` is not ranked \ - (`VG.Dispatch.ranking`), nor made of parts `_` whose suffixes are" - -- Each tag once, in the order of the parts of every suffix that has it. - let tags := parsed.foldl (init := ([] : List String)) fun tags (_, ps) => - (ps.map (·.1)).foldl (init := (tags, 0)) (fun (tags, pos) t => - match tags.idxOf? t with - | some i => (tags, i + 1) - | none => (tags.insertIdx pos t, pos + 1)) |>.1 - pure (parsed.map fun (a, ps) => - (tags.map fun t => ((ps.find? (·.1 == t)).map (·.2.1)).getD rs.length, arm a)) - let sorted := keyed.mergeSort fun a b => lexLe a.1 b.1 - -- Leave out each implementation that can be chosen only where one before it can. +/-- The implementations `members` of a function or table of the target `t`, +but its base, that its dispatcher tries, in their order (see above), or the +first that it would never choose. -/ +def armsOf (t : String) (members : List Impl) : Except String (List Arm) := do + let arms := members.map fun a => (⟨a, a.extra, a.vendor.toList⟩ : Arm) + -- One that can be chosen only where one before it can would never run. let weaker (e l : Arm) : Bool := (e.impl.features ++ e.extra).all (l.impl.features ++ l.extra).contains && e.vendors.all l.vendors.contains && e.impl.gate.all l.impl.gate.contains - return sorted.foldl (init := []) fun acc (_, l) => - if acc.any (weaker · l) then acc else acc ++ [l] + let _ ← arms.foldlM (init := ([] : List Arm)) fun before l => do + if let some e := before.find? (weaker · l) then + throw s!"{t}: {l.impl.name} would never run: its dispatcher tries {e.impl.name} \ + before it, which needs nothing it does not" + return before ++ [l] + return arms /-- The token every dispatcher takes first: proof that the caller has seen to data independent timing on its thread (see the top of @@ -1934,7 +1793,7 @@ def argsOf (sig : Sig) : String := /-- The condition under which a dispatcher calls `a`, given the variable `cpu` it holds `verified_garbage_cpu::detected()` in, whose lines after its first start with `ind` and four spaces, as `rustfmt` would break them. The features it checks are `a`'s (its constant, `featuresConst`) and -its ranking's `extra` ones, first, in a `const` block, among those the +its registration's `extra` ones, first, in a `const` block, among those the compilation target enables (`verified_garbage_cpu::target_contains`), then among those detected; for one that needs none, that the target enables some (`COMPILED`) or detection reports some. -/ @@ -1972,8 +1831,8 @@ def dispatchNote (base : String) (arms : List Arm) : String := match gate ++ vendor ++ needs ++ extra with | [] => s!"`{a.impl.name}`" | cs => s!"`{a.impl.name}` if " ++ englishList "and" cs - s!"It runs the first of these implementations that applies, which `VG.Dispatch.ranking` orders \ - (`VG.Rust.Rank`): " ++ "; ".intercalate (arms.map item) ++ + s!"It runs the first of these implementations that applies, in the order its registration \ + file lists them: " ++ "; ".intercalate (arms.map item) ++ s!"; or else `{base}`, which needs no CPU feature. It chooses on its first call, and calls \ the one it chose from then on without choosing again. Each has this documentation but for its stack (whose requirements here are the largest) \ and what it writes." @@ -1995,10 +1854,10 @@ its implementations' `arms`. -/ def dispatcher (g : Group) (arms : List Arm) : String := let b := g.base let auto := autoName b.name - let all := b :: g.members.map (·.1) + let all := b :: g.members let stack := all.foldl (fun n a => max n a.stack) 0 let written := b.written.filterMap fun (p, n) => - let ns := (g.members.map (·.1)).map fun a => a.written.lookup p + let ns := g.members.map fun a => a.written.lookup p if ns.all (·.isSome) then some (p, ns.foldl (fun m o => max m (o.getD 0)) n) else none let args := argsOf b.sig let abi := b.target.rustAbi @@ -2065,12 +1924,6 @@ def vendorOk (v : String) : Bool := def featureOk (f : String) : Bool := !f.isEmpty && f.all fun c => c.isAlphanum || c == '.' || c == '_' -/-- Each `Rank` of `rs` has a suffix starting with `_`. -/ -def checkRanks (t : String) (rs : List Rank) : Except String Unit := - rs.forM fun r => do - unless r.suffix.startsWith "_" && r.suffix.length > 1 do - throw s!"{t}: the ranked suffix `{r.suffix}` does not start with `_`" - /-- The artifact `a`'s vendor, if it names one, and each of its `extra` features are as `vendorOk` and `featureOk` require. -/ def checkChoice (a : Artifact) : Except String Unit := do @@ -2085,47 +1938,42 @@ def checkChoice (a : Artifact) : Except String Unit := do above), as the text that follows its functions in its file; `names` are the names of every artifact of the target, none of which may be a dispatcher's. -/ -def dispatchers (rs : List Rank) (names : List String) (as : List Artifact) : +def dispatchers (names : List String) (as : List Artifact) : Except String String := do let gs := (groupsOf as).filter (·.base.features.isEmpty) let texts ← gs.mapM fun g => do let auto := autoName g.base.name if names.contains auto then throw s!"{g.base.target.name}: {auto} is both an artifact and the dispatcher of {g.base.name}" - let arms ← if g.members.isEmpty then pure [] else - armsOf rs g.base.target.name (g.members.map fun (a, s) => (Impl.ofArtifact a, s)) + let arms ← armsOf g.base.target.name (g.members.map Impl.ofArtifact) return if g.members.isEmpty then soleDispatcher g else dispatcher g arms return String.join (texts.map ("\n" ++ ·)) -/-- The tables of functions `ts` of a target, with their implementations: in -order of their names' lengths, a table is an implementation of the first -found whose base has its interface and data and whose base's name, followed -by `_`, starts its own, and is otherwise the base of a group of its own. -/ -def tableGroupsOf (ts : List FnTable) : List (FnTable × List (FnTable × String)) := Id.run do - let sorted := ts.mergeSort fun a b => a.name.length ≤ b.name.length - let mut gs : Array (FnTable × List (FnTable × String)) := #[] - for T in sorted do - match gs.findIdx? fun (b, _) => - T.name.startsWith (b.name ++ "_") && T.iface == b.iface && T.data == b.data with - | some i => - gs := gs.modify i fun (b, ms) => (b, ms ++ [(T, (T.name.drop b.name.length).toString)]) - | none => gs := gs.push (T, []) - return gs.toList +/-- The tables of functions `ts` of a target, with their implementations: a +table is an implementation of the shortest other table of its interface and +data whose name, followed by `_`, starts its own, and is otherwise the base +of a group of its own, whose implementations are in the order of `ts`. -/ +def tableGroupsOf (ts : List FnTable) : List (FnTable × List FnTable) := + let baseOf (T : FnTable) : Option FnTable := + ((ts.filter fun b => T.name.startsWith (b.name ++ "_") && T.iface == b.iface && + T.data == b.data).mergeSort fun a b => a.name.length ≤ b.name.length).head? + (ts.filter (baseOf · |>.isNone)).map fun b => + (b, ts.filter fun T => (baseOf T).map (·.name) == some b.name) /-- The name of the dispatcher of the table of functions `name`: its name in lowercase, followed by `_auto`. -/ def tableAutoName (name : String) : String := name.toLower ++ "_auto" /-- The dispatcher of the table of functions `b` of the target `t` (see -above), whose implementations are `members` (each with its suffix), given -the CPU features each table needs (`features`), its `cfg` predicates -(`gate`); or why `rs` does not rank them. -/ -def tableDispatcher (rs : List Rank) (t : String) (features : FnTable → List String) +above), whose implementations are `members`, given the CPU features each +table needs (`features`), its `cfg` predicates (`gate`); or the first it +would never choose. -/ +def tableDispatcher (t : String) (features : FnTable → List String) (gate : List String → List String) (b : FnTable) - (members : List (FnTable × String)) : Except String String := do + (members : List FnTable) : Except String String := do let impl (T : FnTable) : Impl := { name := T.name, module := "fn_tables", features := features T, gate := gate (features T) } - let arms ← armsOf rs t (members.map fun (T, s) => (impl T, s.toLower)) + let arms ← armsOf t (members.map impl) let item (a : Arm) : String := let needs := if a.impl.features.isEmpty then (if a.extra.isEmpty then ["detection reports some CPU feature"] else []) @@ -2144,7 +1992,7 @@ def tableDispatcher (rs : List Rank) (t : String) (features : FnTable → List S else s!"A table of functions of the interface `{b.iface}` with the data of `{b.name}`, which \ every function taking one computes the same with (`VG.Rust.tableDispatcher`): the \ - first of these that applies, which `VG.Dispatch.ranking` orders (`VG.Rust.Rank`): " ++ + first of these that applies, in the order its registration file lists them: " ++ "; ".intercalate (arms.map item) ++ s!"; or else `{b.name}`, which needs no CPU feature. \ Its CPU features, if any, are those of the table returned, which the CPU has." return docComment "" doc ++ @@ -2163,14 +2011,14 @@ def tableDispatcher (rs : List Rank) (t : String) (features : FnTable → List S above), as the text of `dispatch/fn_tables.rs` after its header; `names` are the names of every artifact of the target, none of which may be a dispatcher's. -/ -def tableDispatchers (rs : List Rank) (t : String) (names : List String) (ts : List FnTable) +def tableDispatchers (t : String) (names : List String) (ts : List FnTable) (features : FnTable → List String) (gate : List String → List String) : Except String String := do let gs := (tableGroupsOf ts).filter fun (b, _) => (features b).isEmpty let texts ← gs.mapM fun (b, ms) => do if names.contains (tableAutoName b.name) then throw s!"{t}: {tableAutoName b.name} is both an artifact and the dispatcher of {b.name}" - tableDispatcher rs t features gate b ms + tableDispatcher t features gate b ms return String.join (texts.map ("\n" ++ ·)) /-- The module of the dispatchers of the target `t` (`dispatch/mod.rs` of its @@ -2193,18 +2041,13 @@ def dispatchFile (t m text : String) : String := /-- The visibility of each function of a module (`moduleItems`), by its name: `pub(crate)` if it is an implementation of a function with a -dispatcher (whose base needs no CPU feature, `dispatchers`) and so is every -artifact emitted as it (`alias`), so that the library calls it only through -the dispatcher, which takes the token (see the top of this file); -else `pub`. -/ -def visibilities (items : List (Prepared × String × String)) : String → String := - let as := items.map (·.1.art) - let dispatched := ((groupsOf as).filter (·.base.features.isEmpty)).flatMap fun g => - g.base.name :: g.members.map (·.1.name) - let open_ := (items.filter fun (p, _, _) => !dispatched.contains p.art.name).map (·.2.2) - fun n => - let canon := ((items.find? (·.1.art.name == n)).map (·.2.2)).getD n - if dispatched.contains n && !open_.contains canon then "pub(crate)" else "pub" +dispatcher (whose base needs no CPU feature, `dispatchers`), so that the +library calls it only through the dispatcher, which takes the token (see the +top of this file); else `pub`. -/ +def visibilities (items : List (Prepared × String)) : String → String := + let dispatched := ((groupsOf (items.map (·.1.art))).filter (·.base.features.isEmpty)).flatMap + fun g => g.base.name :: g.members.map (·.name) + fun n => if dispatched.contains n then "pub(crate)" else "pub" /-- The generated files of the artifacts of `ps`, given the module of each function each artifact calls; each artifact's code as `ps` printed it @@ -2274,15 +2117,16 @@ code has an instruction its target's printer cannot encode (`checkEncodable`), its doc has no `# Safety` section at its end for what `fullDoc` adds to it (`checkLayout`), a bound on what it writes is not of a writable array parameter, at most its size (`checkWritten`), or its vendor -or `extra` features are not names (`checkChoice`), or `ranking` -(each target's, by its name) does not rank an implementation a dispatcher -calls (`dispatchers`, `checkRanks`); with each module's dispatchers after its +or `extra` features are not names (`checkChoice`), an implementation of a +function has another signature or documentation than its base's +(`checkGroups`), or a dispatcher would never choose one of its +implementations (`armsOf`); with each module's dispatchers after its functions. Each check of every artifact runs on `tasks` tasks (`forMPar`), one check after the other, so the error is the one checking them in order would give; what they read of an artifact more than once is computed once, on `tasks` tasks (`Prepared`, `prepareAll`). -/ -def files (as : List Artifact) (ranking : Ranking := []) : +def files (as : List Artifact) : Except String (List (String × String)) := do -- The same artifacts, with equal parts the same value in memory -- (`ShareCommon.shareCommon'` returns its argument): the code of every call @@ -2309,17 +2153,17 @@ def files (as : List Artifact) (ranking : Ranking := []) : forMPar as checkWritten forMPar as checkChoice let disp ← (targetsOf as).flatMapM fun (t, _) => do - let rs := (ranking.lookup t).getD [] - checkRanks t rs let arts := as.filter (·.target.name == t) let names := arts.map (·.name) let fns ← (distinct arts (·.module)).mapM fun m => do - return ((t, m), ← dispatchers rs names (arts.filter (·.module == m))) + let ms := arts.filter (·.module == m) + checkGroups ms + return ((t, m), ← dispatchers names ms) let tps := ps.filter (·.art.target.name == t) let gate (fs : List String) : List String := match arts.head? with | some a => fs.filterMap a.target.printer.featureCfg | none => [] - let tabs ← tableDispatchers rs t names (fnTables arts) (fnTableFeatures tps hs t) gate + let tabs ← tableDispatchers t names (fnTables arts) (fnTableFeatures tps hs t) gate return fns ++ (if tabs.isEmpty then [] else [((t, "fn_tables"), tabs)]) return render ps hs (fun t n => match as.find? fun b => b.target.name == t && b.name == n with diff --git a/lean/VerifiedGarbageTest/Dispatch.lean b/lean/VerifiedGarbageTest/Dispatch.lean index 4ea9291215b..56023a7aa84 100644 --- a/lean/VerifiedGarbageTest/Dispatch.lean +++ b/lean/VerifiedGarbageTest/Dispatch.lean @@ -2,16 +2,16 @@ module import VerifiedGarbage.TCB.Rust meta import VerifiedGarbage.TCB.Rust -import VerifiedGarbage.Dispatch -meta import VerifiedGarbage.Dispatch +import VerifiedGarbage.TCB.Emit +meta import VerifiedGarbage.TCB.Emit import VerifiedGarbage.Artifacts.Poly1305.AArch64 meta import VerifiedGarbage.Artifacts.Poly1305.AArch64 /-! # Dispatchers (`Rust.dispatcher`) -How a dispatcher reads and ranks its implementations' suffixes, and what it -is for Poly1305 on AArch64: `vg_poly1305_update` and its `_neon` variant, +Which implementations a dispatcher tries, in what order, and what it is for +Poly1305 on AArch64: `vg_poly1305_update` and its `_neon` variant, which its registration lets the dispatcher choose only on Apple's targets (or where `VG_CPU_FEATURES` names its features), and only where the target enables or detection reports the `neon` feature; `vg_poly1305_init` and @@ -22,25 +22,8 @@ other names of them. namespace VG.Test.Dispatch open VG -def rs : List Rust.Rank := [{ suffix := "_shani" }, { suffix := "_avx2" }, { suffix := "_adx" }] - --- The first rank of a suffix. -#guard (Rust.rankOf rs "_avx2").map (·.1) == some 1 -#guard (Rust.rankOf rs "_aes").isNone - --- A suffix of parts `_` (`Emit.qualifiedName`), each ranked. -#guard (Rust.partsOf rs 40 "_sha256_avx2_mr_adx").map (·.map fun (t, i, _) => (t, i)) == - some [("sha256", 1), ("mr", 2)] -#guard (Rust.partsOf rs 40 "_sha256_avx512").isNone -#guard (Rust.partsOf rs 40 "_avx2").isNone - --- Parts compare by the first tag's rank, then the next. -#guard Rust.lexLe [0, 4] [1, 0] -#guard !Rust.lexLe [1, 0] [0, 4] -#guard Rust.lexLe [1, 2] [1, 2] - def out : List (String × String) := - match Rust.files Artifacts.Poly1305.AArch64.artifacts Dispatch.ranking with + match Rust.files Artifacts.Poly1305.AArch64.artifacts with | .ok fs => fs | .error e => [("error", e)] @@ -104,58 +87,68 @@ def has (text part : String) : Bool := (text.splitOn part).length > 1 /-! ## A registration's `extra` features and `vendor` (`Artifact.extra`, `Artifact.vendor`) -/ -/-- The files of Poly1305 on AArch64 under `rk`, with `f` applied to `_neon` -without its registration's `extra` features and `vendor`. -/ -def neonOut (rk : Rust.Ranking) (f : Artifact → Artifact) : Except String (List (String × String)) := +/-- The files of Poly1305 on AArch64, with `f` applied to `_neon` without its +registration's `extra` features and `vendor`. -/ +def neonOut (f : Artifact → Artifact) : Except String (List (String × String)) := Rust.files (Artifacts.Poly1305.AArch64.artifacts.map fun a => - if a.name == "vg_poly1305_update_neon" then f { a with extra := [], vendor := none } else a) rk + if a.name == "vg_poly1305_update_neon" then f { a with extra := [], vendor := none } else a) -- The registration's choice: the same files. -#guard match neonOut Dispatch.ranking fun a => { a with vendor := some "apple", extra := ["neon"] } with +#guard match neonOut fun a => { a with vendor := some "apple", extra := ["neon"] } with | .ok fs => fs == out | .error _ => false -- Without either, `_neon` is chosen everywhere. -#guard match neonOut Dispatch.ranking id with +#guard match neonOut id with | .ok fs => fs != out && has (((fs.find? (·.1 == "verified-garbage-asm-aarch64/src/dispatch/poly1305.rs")).map (·.2)).getD "") " if const { verified_garbage_cpu::COMPILED.0 != 0 }\n || cpu.0 != 0\n {\n" | .error _ => false -- A vendor or feature that is not a name fails the emitter. -#guard match neonOut Dispatch.ranking fun a => { a with vendor := some "Apple" } with +#guard match neonOut fun a => { a with vendor := some "Apple" } with | .error e => has e "vg_poly1305_update_neon names the vendor `Apple`" | .ok _ => false -#guard match neonOut Dispatch.ranking fun a => { a with extra := ["ne on"] } with +#guard match neonOut fun a => { a with extra := ["ne on"] } with | .error e => has e "vg_poly1305_update_neon needs the feature `ne on`" | .ok _ => false -/-- The position of a suffix in x86-64's ranking. -/ -def x86Rank (suffix : String) : Option Nat := - (Rust.rankOf ((Dispatch.ranking.lookup "x86_64").getD []) suffix).map (·.1) - --- ChaCha20's and ChaCha20-Poly1305's dispatchers try `_avx2` before `_ssse3`, --- so they never choose `_ssse3` with AVX2, and `_ssse3` without it. -#guard match x86Rank "_avx2", x86Rank "_ssse3" with - | some a, some s => a < s - | _, _ => false - --- AES-CMAC's, AES-SIV's and AES-CCM's dispatchers rank `_aesni`, which needs --- only AES-NI and SSSE3, so they never choose the baseline with those. -#guard (x86Rank "_aesni").isSome - --- AES-GCM-SIV's dispatchers try `_aesni_pclmul_avx_avx2` before `_aesni_avx2` --- and `_pclmul_avx2`, and `_vaes_vpclmul_avx512` before `_vaes_avx2` and --- `_vpclmul_avx2`. -#guard [("_aesni_pclmul_avx_avx2", "_aesni_avx2"), ("_aesni_pclmul_avx_avx2", "_pclmul_avx2"), - ("_vaes_vpclmul_avx512", "_vaes_avx2"), ("_vaes_vpclmul_avx512", "_vpclmul_avx2")].all fun (a, b) => - match x86Rank a, x86Rank b with - | some i, some j => i < j - | _, _ => false - --- An unranked suffix fails the emitter. -#guard match Rust.files Artifacts.Poly1305.AArch64.artifacts [("aarch64", [])] with - | .error e => has e "vg_poly1305_update_neon: its suffix `_neon` is not ranked" +/-! ## The order of the implementations -/ + +/-- `_neon` as the registration has it (a list of it), its other +implementations, and another name of `_neon` that any target may choose. -/ +def neon : List Artifact := + Artifacts.Poly1305.AArch64.artifacts.filter (·.name == "vg_poly1305_update_neon") +def others : List Artifact := + Artifacts.Poly1305.AArch64.artifacts.filter (·.name != "vg_poly1305_update_neon") +def neon2 : List Artifact := neon.map fun a => { a with name := "vg_poly1305_update_neon2", vendor := none } + +/-- The dispatchers of Poly1305 with the artifacts `as`. -/ +def dispatchOf (as : List Artifact) : String := + match Rust.files as with + | .ok fs => ((fs.find? (·.1 == "verified-garbage-asm-aarch64/src/dispatch/poly1305.rs")).map + (·.2)).getD "" + | .error e => "error: " ++ e + +-- It tries them in the order the artifacts list them: `_neon`, then `_neon2`. +#guard ((((dispatchOf (others ++ neon ++ neon2)).splitOn + "return crate::poly1305::vg_poly1305_update_neon;").getD 1 "").splitOn + "return crate::poly1305::vg_poly1305_update_neon2;").length == 2 + +-- One that it could choose only where one before it can fails the emitter. +#guard has (dispatchOf (others ++ neon2 ++ neon)) "error: aarch64: vg_poly1305_update_neon would \ + never run: its dispatcher tries vg_poly1305_update_neon2 before it" + +-- An implementation of another signature or documentation than its base's fails it. +#guard has (dispatchOf (others ++ neon.map fun a => { a with doc := "Another function. " ++ a.doc })) + "vg_poly1305_update_neon, an implementation of vg_poly1305_update, has another signature or \ + documentation" + +-- Every implementation of a function is in one registration file. +#guard (Emit.checkSources [("A", Artifacts.Poly1305.AArch64.artifacts)]).toOption.isSome +#guard match Emit.checkSources [("A", others), ("B", neon)] with + | .error e => has e "aarch64: the implementations of vg_poly1305_update are in two registration \ + files, A and B" | .ok _ => false /-! ## Tables of functions (`Rust.tableDispatcher`) -/ @@ -173,20 +166,21 @@ def tU : FnTable := { name := "VG_U_AVX2", iface := "Fns", fns := ["vg_t_avx2"], def tableFeatures (T : FnTable) : List String := if T.name.endsWith "_SHANI" then ["sha"] else if T.name.endsWith "_AVX2" then ["avx2"] else [] -def tableText (rs : List Rust.Rank) (ts : List FnTable) : String := - match Rust.tableDispatchers rs "x86_64" [] ts tableFeatures +def tableText (ts : List FnTable) : String := + match Rust.tableDispatchers "x86_64" [] ts tableFeatures (fun fs => fs.map fun f => s!"target_feature = \"{f}\"") with | .ok s => s | .error e => "error: " ++ e -def tables : String := tableText rs [tU, tAvx2, t2, tT, tShani] +def tables : String := tableText [tU, tShani, tAvx2, t2, tT] --- Implementations of `VG_T`: the tables of its interface and data named after it. -#guard (Rust.tableGroupsOf [tU, tAvx2, t2, tT, tShani]).map (fun (b, ms) => (b.name, ms.map (·.2))) == - [("VG_T", ["_AVX2", "_SHANI"]), ("VG_T_2", []), ("VG_U_AVX2", [])] +-- Implementations of `VG_T`: the tables of its interface and data named after it, +-- in the order of the list. +#guard (Rust.tableGroupsOf [tU, tShani, tAvx2, t2, tT]).map (fun (b, ms) => (b.name, ms.map (·.name))) == + [("VG_U_AVX2", []), ("VG_T_2", []), ("VG_T", ["VG_T_SHANI", "VG_T_AVX2"])] -- `VG_T`'s dispatcher returns `_SHANI` where the CPU has its features, then --- `_AVX2`, as the ranking orders them, else `VG_T`. +-- `_AVX2`, in the order of the list, else `VG_T`. #guard has tables "pub fn vg_t_auto() -> &'static crate::fn_tables::Fns {\n \ let cpu = verified_garbage_cpu::detected();\n \ @@ -206,13 +200,13 @@ def tables : String := tableText rs [tU, tAvx2, t2, tT, tShani] &crate::fn_tables::VG_T_2\n}\n" #guard !has tables "vg_u_avx2_auto" --- An unranked suffix fails the emitter. -#guard tableText [] [tT, tShani] == - "error: x86_64: VG_T_SHANI: its suffix `_shani` is not ranked (`VG.Dispatch.ranking`), nor \ - made of parts `_` whose suffixes are" +-- One that it could choose only where one before it can fails the emitter. +#guard tableText [tT, tShani, { tShani with name := "VG_T_X_SHANI" }] == + "error: x86_64: VG_T_X_SHANI would never run: its dispatcher tries VG_T_SHANI before it, \ + which needs nothing it does not" -- A dispatcher's name may not be an artifact's. -#guard match Rust.tableDispatchers rs "x86_64" ["vg_t_auto"] [tT] tableFeatures (fun _ => []) with +#guard match Rust.tableDispatchers "x86_64" ["vg_t_auto"] [tT] tableFeatures (fun _ => []) with | .error e => has e "vg_t_auto is both an artifact and the dispatcher of VG_T" | .ok _ => false diff --git a/lean/VerifiedGarbageTest/Emit.lean b/lean/VerifiedGarbageTest/Emit.lean index 68ff2591bae..a60f3aa4e4d 100644 --- a/lean/VerifiedGarbageTest/Emit.lean +++ b/lean/VerifiedGarbageTest/Emit.lean @@ -7,28 +7,13 @@ meta import VerifiedGarbage.TCB.Emit # Golden tests for the emitter's driver `Emit.driver` writes the program that emits every artifact: these tests pin -down which modules it imports and how it applies each generic function to -each variant of its interface and target. +down which modules it imports and how it lists their artifacts. -/ namespace VG.Test.Emit open VG.Emit -#guard groupOf "VerifiedGarbage.Variants.Sha256Compress.X86_64.ShaNi" == some "Sha256Compress.X86_64" -#guard groupOf "VerifiedGarbage.Variants.Sha256Compress.ShaNi" == none -#guard groupOf "VerifiedGarbage.Variants.Sha256Compress.X86_64.Sub.ShaNi" == none - -#guard interfacesOf "VerifiedGarbage.Generic.Sha256Compress.X86_64.Pbkdf2" == - some ["Sha256Compress.X86_64"] -#guard interfacesOf "VerifiedGarbage.Generic.Blake2b.Argon2Compress.X86_64.Argon2" == - some ["Blake2b.X86_64", "Argon2Compress.X86_64"] -#guard interfacesOf "VerifiedGarbage.Generic.X86_64.Pbkdf2" == none -#guard interfacesOf "VerifiedGarbage.Generic.Pbkdf2" == none - -#guard variantOf "VerifiedGarbage.Variants.Sha256Compress.X86_64.ShaNi" == - "VG.Variants.Sha256Compress.X86_64.ShaNi.variant" - -- Instances of a caller of several interfaces: one qualified part per -- interface whose variant has a suffix; the baseline instance keeps the name. #guard qualifiedName "vg_argon2" [("blake2b", ""), ("g", "")] == "vg_argon2" @@ -44,8 +29,8 @@ open VG.Emit #guard qualifiedName "vg_argon2" [("blake2b", "_avx2"), ("g", "_avx2")] == "vg_argon2_blake2b_avx2_g_avx2" --- Without variants or generic files. -#guard driver ["VerifiedGarbage.Artifacts.Sha3.X86_64"] == +-- Each registration module's list, with its name. +#guard driver ["VerifiedGarbage.Artifacts.Sha3.X86_64", "VerifiedGarbage.Artifacts.Sha3.AArch64"] == "-- Generated by Emit.lean, which runs it. DO NOT EDIT.\n\ module\n\ \n\ @@ -55,170 +40,42 @@ open VG.Emit public meta import VerifiedGarbage.TCB.Axioms\n\ public import VerifiedGarbage.TCB.Emit\n\ public meta import VerifiedGarbage.TCB.Emit\n\ - public import VerifiedGarbage.Dispatch\n\ - public meta import VerifiedGarbage.Dispatch\n\ public import VerifiedGarbage.Artifacts.Sha3.X86_64\n\ public meta import VerifiedGarbage.Artifacts.Sha3.X86_64\n\ + public import VerifiedGarbage.Artifacts.Sha3.AArch64\n\ + public meta import VerifiedGarbage.Artifacts.Sha3.AArch64\n\ \n\ public section\n\ \n\ namespace VG.Emit\n\ \n\ - def all : List Artifact := List.flatten [\n \ - VG.Artifacts.Sha3.X86_64.artifacts,\n \ - []]\n\ - \n\ - #assert_standard_axioms all\n\ - #assert_no_compiler_overrides all VG.Dispatch.ranking run\n\ - #assert_spec_origin\n\ - \n\ - end VG.Emit\n\ - \n\ - def main (args : List String) : IO UInt32 :=\n \ - VG.Emit.run VG.Emit.all VG.Dispatch.ranking args\n\ - \n\ - end\n" - --- Each generic function of an interface is applied to each of its --- variants; variants of an interface without generic functions add nothing. -#guard driver ["VerifiedGarbage.Artifacts.Sha256.X86_64"] - [(["I.X86_64"], ["VerifiedGarbage.Generic.I.X86_64.A", "VerifiedGarbage.Generic.I.X86_64.B"])] - [("I.X86_64", ["VerifiedGarbage.Variants.I.X86_64.P", "VerifiedGarbage.Variants.I.X86_64.Q"]), - ("J.X86_64", ["VerifiedGarbage.Variants.J.X86_64.R"])] == - "-- Generated by Emit.lean, which runs it. DO NOT EDIT.\n\ - module\n\ - \n\ - public import VerifiedGarbage.TCB.Audit\n\ - public meta import VerifiedGarbage.TCB.Audit\n\ - public import VerifiedGarbage.TCB.Axioms\n\ - public meta import VerifiedGarbage.TCB.Axioms\n\ - public import VerifiedGarbage.TCB.Emit\n\ - public meta import VerifiedGarbage.TCB.Emit\n\ - public import VerifiedGarbage.Dispatch\n\ - public meta import VerifiedGarbage.Dispatch\n\ - public import VerifiedGarbage.Artifacts.Sha256.X86_64\n\ - public meta import VerifiedGarbage.Artifacts.Sha256.X86_64\n\ - public import VerifiedGarbage.Generic.I.X86_64.A\n\ - public meta import VerifiedGarbage.Generic.I.X86_64.A\n\ - public import VerifiedGarbage.Generic.I.X86_64.B\n\ - public meta import VerifiedGarbage.Generic.I.X86_64.B\n\ - public import VerifiedGarbage.Variants.I.X86_64.P\n\ - public meta import VerifiedGarbage.Variants.I.X86_64.P\n\ - public import VerifiedGarbage.Variants.I.X86_64.Q\n\ - public meta import VerifiedGarbage.Variants.I.X86_64.Q\n\ - public import VerifiedGarbage.Variants.J.X86_64.R\n\ - public meta import VerifiedGarbage.Variants.J.X86_64.R\n\ - \n\ - public section\n\ - \n\ - namespace VG.Emit\n\ - \n\ - def all : List Artifact := List.flatten [\n \ - VG.Artifacts.Sha256.X86_64.artifacts,\n \ - (List.flatMap (fun v => List.flatten [VG.Generic.I.X86_64.A.artifacts v, \ - VG.Generic.I.X86_64.B.artifacts v]) \ - [VG.Variants.I.X86_64.P.variant, VG.Variants.I.X86_64.Q.variant]),\n \ - []]\n\ + def all : List (String × List Artifact) := [\n \ + (\"VerifiedGarbage.Artifacts.Sha3.X86_64\", VG.Artifacts.Sha3.X86_64.artifacts),\n \ + (\"VerifiedGarbage.Artifacts.Sha3.AArch64\", VG.Artifacts.Sha3.AArch64.artifacts)]\n\ \n\ #assert_standard_axioms all\n\ - #assert_no_compiler_overrides all VG.Dispatch.ranking run\n\ + #assert_no_compiler_overrides all run\n\ #assert_spec_origin\n\ \n\ end VG.Emit\n\ \n\ def main (args : List String) : IO UInt32 :=\n \ - VG.Emit.run VG.Emit.all VG.Dispatch.ranking args\n\ + VG.Emit.run VG.Emit.all args\n\ \n\ end\n" +#guard ((driver []).splitOn "def all : List (String × List Artifact) := []\n").length == 2 --- An `All` interface takes the list of every variant of its interface, once. -#guard allOf "AllI.X86_64" == ("I.X86_64", true) -#guard allOf "I.X86_64" == ("I.X86_64", false) -#guard interfacesOf "VerifiedGarbage.Generic.I.AllJ.X86_64.A" == some ["I.X86_64", "AllJ.X86_64"] -#guard instances ["VerifiedGarbage.Generic.I.AllJ.X86_64.A"] - [(false, ["VerifiedGarbage.Variants.I.X86_64.P", "VerifiedGarbage.Variants.I.X86_64.Q"]), - (true, ["VerifiedGarbage.Variants.J.X86_64.R", "VerifiedGarbage.Variants.J.X86_64.S"])] == - "(List.flatMap (fun v => ((fun v2 => List.flatten [VG.Generic.I.AllJ.X86_64.A.artifacts v v2]) \ - [VG.Variants.J.X86_64.R.variant, VG.Variants.J.X86_64.S.variant])) \ - [VG.Variants.I.X86_64.P.variant, VG.Variants.I.X86_64.Q.variant])" -#guard ((driver [] [(["I.X86_64", "AllJ.X86_64"], ["VerifiedGarbage.Generic.I.AllJ.X86_64.A"])] - [("I.X86_64", ["VerifiedGarbage.Variants.I.X86_64.P"]), - ("J.X86_64", ["VerifiedGarbage.Variants.J.X86_64.R", "VerifiedGarbage.Variants.J.X86_64.S"])]).splitOn - "((fun v2 => List.flatten [VG.Generic.I.AllJ.X86_64.A.artifacts v v2]) \ - [VG.Variants.J.X86_64.R.variant, VG.Variants.J.X86_64.S.variant])").length == 2 - --- A generic function of two interfaces is applied to each pair of a variant --- of the first and a variant of the second, the first varying slowest. -#guard driver [] - [(["I.X86_64", "J.X86_64"], ["VerifiedGarbage.Generic.I.J.X86_64.A"])] - [("I.X86_64", ["VerifiedGarbage.Variants.I.X86_64.P", "VerifiedGarbage.Variants.I.X86_64.Q"]), - ("J.X86_64", ["VerifiedGarbage.Variants.J.X86_64.R", "VerifiedGarbage.Variants.J.X86_64.S"])] == - "-- Generated by Emit.lean, which runs it. DO NOT EDIT.\n\ - module\n\ - \n\ - public import VerifiedGarbage.TCB.Audit\n\ - public meta import VerifiedGarbage.TCB.Audit\n\ - public import VerifiedGarbage.TCB.Axioms\n\ - public meta import VerifiedGarbage.TCB.Axioms\n\ - public import VerifiedGarbage.TCB.Emit\n\ - public meta import VerifiedGarbage.TCB.Emit\n\ - public import VerifiedGarbage.Dispatch\n\ - public meta import VerifiedGarbage.Dispatch\n\ - public import VerifiedGarbage.Generic.I.J.X86_64.A\n\ - public meta import VerifiedGarbage.Generic.I.J.X86_64.A\n\ - public import VerifiedGarbage.Variants.I.X86_64.P\n\ - public meta import VerifiedGarbage.Variants.I.X86_64.P\n\ - public import VerifiedGarbage.Variants.I.X86_64.Q\n\ - public meta import VerifiedGarbage.Variants.I.X86_64.Q\n\ - public import VerifiedGarbage.Variants.J.X86_64.R\n\ - public meta import VerifiedGarbage.Variants.J.X86_64.R\n\ - public import VerifiedGarbage.Variants.J.X86_64.S\n\ - public meta import VerifiedGarbage.Variants.J.X86_64.S\n\ - \n\ - public section\n\ - \n\ - namespace VG.Emit\n\ - \n\ - def all : List Artifact := List.flatten [\n \ - (List.flatMap (fun v => (List.flatMap (fun v2 => \ - List.flatten [VG.Generic.I.J.X86_64.A.artifacts v v2]) \ - [VG.Variants.J.X86_64.R.variant, VG.Variants.J.X86_64.S.variant])) \ - [VG.Variants.I.X86_64.P.variant, VG.Variants.I.X86_64.Q.variant]),\n \ - []]\n\ - \n\ - #assert_standard_axioms all\n\ - #assert_no_compiler_overrides all VG.Dispatch.ranking run\n\ - #assert_spec_origin\n\ - \n\ - end VG.Emit\n\ - \n\ - def main (args : List String) : IO UInt32 :=\n \ - VG.Emit.run VG.Emit.all VG.Dispatch.ranking args\n\ - \n\ - end\n" - - --- The target directory of each kind of module. +-- The target directory of a registration module. #guard targetDirOf "VerifiedGarbage.Artifacts.Sha3.X86_64" == some "X86_64" -#guard targetDirOf "VerifiedGarbage.Generic.Sha256Compress.AArch64.Pbkdf2" == some "AArch64" -#guard targetDirOf "VerifiedGarbage.Generic.Blake2b.Argon2Compress.X86_64.Argon2" == some "X86_64" -#guard targetDirOf "VerifiedGarbage.Variants.Sha256Compress.Arm.ShaNi" == some "Arm" #guard targetDirOf "VerifiedGarbage.Artifacts.Sha3" == none -#guard targetDirOf "VerifiedGarbage.Variants.Sha256Compress.X86_64" == none +#guard targetDirOf "VerifiedGarbage.Proof.Variants.Sha256Compress.X86_64" == none -- The modules of each target directory, in order of its name. -#guard (parts ["VerifiedGarbage.Artifacts.Sha3.X86_64", "VerifiedGarbage.Artifacts.Sha3.AArch64"] - [(["I.X86_64"], ["VerifiedGarbage.Generic.I.X86_64.A"]), - (["I.AArch64"], ["VerifiedGarbage.Generic.I.AArch64.A"])] - [("I.X86_64", ["VerifiedGarbage.Variants.I.X86_64.P"]), - ("J.Arm", ["VerifiedGarbage.Variants.J.Arm.R"])]).toOption == - some [("AArch64", ["VerifiedGarbage.Artifacts.Sha3.AArch64"], - [(["I.AArch64"], ["VerifiedGarbage.Generic.I.AArch64.A"])], []), - ("Arm", [], [], [("J.Arm", ["VerifiedGarbage.Variants.J.Arm.R"])]), - ("X86_64", ["VerifiedGarbage.Artifacts.Sha3.X86_64"], - [(["I.X86_64"], ["VerifiedGarbage.Generic.I.X86_64.A"])], - [("I.X86_64", ["VerifiedGarbage.Variants.I.X86_64.P"])])] -#guard (parts ["VerifiedGarbage.Artifacts.Sha3"] [] []).toOption.isNone +#guard (parts ["VerifiedGarbage.Artifacts.Sha3.X86_64", "VerifiedGarbage.Artifacts.Sha3.AArch64", + "VerifiedGarbage.Artifacts.Sha256.X86_64"]).toOption == + some [("AArch64", ["VerifiedGarbage.Artifacts.Sha3.AArch64"]), + ("X86_64", ["VerifiedGarbage.Artifacts.Sha3.X86_64", "VerifiedGarbage.Artifacts.Sha256.X86_64"])] +#guard (parts ["VerifiedGarbage.Artifacts.Sha3"]).toOption.isNone /-- `parseArgs`, with the directory as a string. -/ def args (as : List String) : Option (Bool × Option String × String) :=