Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions patches/boringssl/gcm-vaes-requires-hwaes-key-schedule.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
CRYPTO_gcm128_init_aes_key selects the stitched VAES AES-GCM implementations
(gcm_x86_vaes_avx2/avx512) based only on the VPCLMULQDQ/AVX ghash choice and
CRYPTO_is_VAES_capable(), without checking that the AES key schedule was
actually produced by aes_hw_set_encrypt_key. When OPENSSL_ia32cap masks
AES-NI (bit 57, e.g. `~0x200000000000000`) but leaves VAES set,
aes_ctr_set_key falls back to vpaes, whose key schedule is stored in a
transformed basis. The VAES bulk assembly then runs against that vpaes-format
schedule and silently produces wrong ciphertext (and decrypts garbage whose
tag still verifies, since GHASH and EK0 come from the consistent vpaes block
function). The aarch64 branches directly below already gate on |is_hwaes|;
this adds the same gate to the two x86-64 VAES branches.

Upstream BoringSSL fixed this the same way in
997bafbf73886ec19ac3eb70b31db64fe42832c3 ("Gate VAES codepaths on is_hwaes
to fix a theoretical concern",
https://boringssl-review.googlesource.com/c/boringssl/+/101787). Delete
this patch when the pinned BoringSSL commit includes that change. The
patch application fails loudly at dep-fetch time when that happens.
See https://github.com/oven-sh/bun/issues/32126

--- a/crypto/fipsmodule/aes/gcm.cc.inc
+++ b/crypto/fipsmodule/aes/gcm.cc.inc
@@ -276,11 +276,16 @@ void bssl::CRYPTO_gcm128_init_aes_key(GCM128_KEY *gcm_key, const uint8_t *key,

#if !defined(OPENSSL_NO_ASM)
#if defined(OPENSSL_X86_64)
+ // The stitched AES-GCM implementations consume |gcm_key->aes| directly and
+ // expect the standard round-key layout produced by |aes_hw_set_encrypt_key|.
+ // Without |is_hwaes|, masking AES-NI but not VAES via OPENSSL_ia32cap made
+ // |aes_ctr_set_key| fall back to vpaes's transformed key schedule while the
+ // VAES bulk code still ran, silently producing wrong ciphertext.
if (gcm_key->ghash == gcm_ghash_vpclmulqdq_avx512 &&
- CRYPTO_is_VAES_capable()) {
+ CRYPTO_is_VAES_capable() && is_hwaes) {
gcm_key->impl = gcm_x86_vaes_avx512;
} else if (gcm_key->ghash == gcm_ghash_vpclmulqdq_avx2 &&
- CRYPTO_is_VAES_capable()) {
+ CRYPTO_is_VAES_capable() && is_hwaes) {
gcm_key->impl = gcm_x86_vaes_avx2;
} else if (gcm_key->ghash == gcm_ghash_avx && is_hwaes) {
gcm_key->impl = gcm_x86_aesni;
10 changes: 10 additions & 0 deletions scripts/build/deps/boringssl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ export const boringssl: Dependency = {
commit: BORINGSSL_COMMIT,
}),

// AES-GCM picked the stitched VAES implementation based on CPU bits alone,
// without checking that the key schedule came from aes_hw_set_encrypt_key.
// With OPENSSL_ia32cap masking AES-NI but not VAES, the VAES bulk assembly
// ran against a vpaes-format key schedule and silently produced wrong
// output. Upstream fixed this the same way in google/boringssl
// 997bafbf73886ec19ac3eb70b31db64fe42832c3. Delete the patch when the
// pinned commit includes that change (patch application fails loudly then).
// https://github.com/oven-sh/bun/issues/32126
patches: ["patches/boringssl/gcm-vaes-requires-hwaes-key-schedule.patch"],

build: cfg => {
// win-x64 uses NASM-syntax .asm; everything else (including win-aarch64)
// uses gas .S that clang assembles.
Expand Down
73 changes: 73 additions & 0 deletions test/js/node/crypto/node-crypto.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1316,3 +1316,76 @@ describe("Certificate spkac argument validation", () => {
expect(new crypto.Certificate().verifySpkac("not a spkac", "utf8")).toBe(false);
});
});

// https://github.com/oven-sh/bun/issues/32126
it("AES-GCM stays correct when OPENSSL_ia32cap masks AES-NI but not VAES", async () => {
const script = `
const crypto = require("node:crypto");
const assert = require("node:assert");
function run(alg, keyLen, pt) {
const key = Buffer.alloc(keyLen);
const iv = Buffer.alloc(12);
const c = crypto.createCipheriv(alg, key, iv);
const ct = Buffer.concat([c.update(pt), c.final()]);
const tag = c.getAuthTag();
const d = crypto.createDecipheriv(alg, key, iv);
d.setAuthTag(tag);
const rt = Buffer.concat([d.update(ct), d.final()]);
assert(rt.equals(pt), "round-trip failed for " + alg);
return { ct: ct.toString("hex"), tag: tag.toString("hex") };
}
// Decrypt the authentic NIST test-case-2 ciphertext separately: the broken
// dispatch emitted garbage plaintext while the tag still verified.
const d = crypto.createDecipheriv("aes-128-gcm", Buffer.alloc(16), Buffer.alloc(12));
d.setAuthTag(Buffer.from("ab6e47d42cec13bdf53a67b21257bddf", "hex"));
const decNist = Buffer.concat([
d.update(Buffer.from("0388dace60b6a392f328c2b971b2fe78", "hex")),
d.final(),
]).toString("hex");
console.log(JSON.stringify({
gcm128: run("aes-128-gcm", 16, Buffer.alloc(16)),
gcm192: run("aes-192-gcm", 24, Buffer.alloc(16)),
gcm256: run("aes-256-gcm", 32, Buffer.alloc(16)),
big: run("aes-128-gcm", 16, Buffer.alloc(1024, 7)),
decNist,
}));
`;

async function runWithCaps(caps) {
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", script],
env: { ...bunEnv, OPENSSL_ia32cap: caps },
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
if (exitCode !== 0) {
throw new Error(`child (OPENSSL_ia32cap=${caps}) exited with ${exitCode}: ${stderr}`);
}
return JSON.parse(stdout);
}

// "~0x200000000000000" clears AES-NI (CPUID.1:ECX bit 25) from the first
// OPENSSL_ia32cap word; ":~0x60000000000" additionally clears
// VAES/VPCLMULQDQ (CPUID.7:ECX bits 9-10) from the second. On non-x86-64
// platforms or CPUs without VAES, the masked runs dispatch the same as the
// unmasked one and pass trivially.
const [unmasked, aesniMasked, bothMasked] = await Promise.all(
[undefined, "~0x200000000000000", "~0x200000000000000:~0x60000000000"].map(runWithCaps),
);

// GCM spec (McGrew & Viega) test cases 2, 8 and 14: all-zero key, IV and
// 16-byte plaintext.
const expected = {
gcm128: { ct: "0388dace60b6a392f328c2b971b2fe78", tag: "ab6e47d42cec13bdf53a67b21257bddf" },
gcm192: { ct: "98e7247c07f0fe411c267e4384b0f600", tag: "2ff58d80033927ab8ef4d4587514f0fb" },
gcm256: { ct: "cea7403d4d606b6e074ec5d3baf39d18", tag: "d0d1c8a799996bf0265b98b5d48ab919" },
decNist: "00000000000000000000000000000000",
// Multi-block output has no published vector; it must be identical no
// matter which implementation the capability mask selects.
big: unmasked.big,
};
expect(unmasked).toEqual(expected);
expect(aesniMasked).toEqual(expected);
expect(bothMasked).toEqual(expected);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
});
Loading