From 73d1b2ff67f1fca08828beff5443d57aa49be5d4 Mon Sep 17 00:00:00 2001 From: Dylan Conway <35280289+dylan-conway@users.noreply.github.com> Date: Fri, 21 Mar 2025 19:49:44 -0700 Subject: [PATCH] Add benchmark for `Cipheriv` and `Decipheriv` (#18394) --- bench/crypto/aes-gcm-throughput.mjs | 44 +++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 bench/crypto/aes-gcm-throughput.mjs diff --git a/bench/crypto/aes-gcm-throughput.mjs b/bench/crypto/aes-gcm-throughput.mjs new file mode 100644 index 0000000000..5a5962089e --- /dev/null +++ b/bench/crypto/aes-gcm-throughput.mjs @@ -0,0 +1,44 @@ +import { bench, run } from "../runner.mjs"; +import crypto from "node:crypto"; +import { Buffer } from "node:buffer"; + +const keylen = { "aes-128-gcm": 16, "aes-192-gcm": 24, "aes-256-gcm": 32 }; +const sizes = [4 * 1024, 1024 * 1024]; +const ciphers = ["aes-128-gcm", "aes-192-gcm", "aes-256-gcm"]; + +const messages = {}; +sizes.forEach(size => { + messages[size] = Buffer.alloc(size, "b"); +}); + +const keys = {}; +ciphers.forEach(cipher => { + keys[cipher] = crypto.randomBytes(keylen[cipher]); +}); + +// Fixed IV and AAD +const iv = crypto.randomBytes(12); +const associate_data = Buffer.alloc(16, "z"); + +for (const cipher of ciphers) { + for (const size of sizes) { + const message = messages[size]; + const key = keys[cipher]; + + bench(`${cipher} ${size / 1024}KB`, () => { + const alice = crypto.createCipheriv(cipher, key, iv); + alice.setAAD(associate_data); + const enc = alice.update(message); + alice.final(); + const tag = alice.getAuthTag(); + + const bob = crypto.createDecipheriv(cipher, key, iv); + bob.setAuthTag(tag); + bob.setAAD(associate_data); + bob.update(enc); + bob.final(); + }); + } +} + +await run();