• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common.js');
3const crypto = require('crypto');
4const keylen = { 'aes-128-gcm': 16, 'aes-192-gcm': 24, 'aes-256-gcm': 32 };
5const bench = common.createBenchmark(main, {
6  n: [500],
7  cipher: ['aes-128-gcm', 'aes-192-gcm', 'aes-256-gcm'],
8  len: [1024, 4 * 1024, 16 * 1024, 64 * 1024, 256 * 1024, 1024 * 1024]
9});
10
11function main({ n, len, cipher }) {
12  const message = Buffer.alloc(len, 'b');
13  const key = crypto.randomBytes(keylen[cipher]);
14  const iv = crypto.randomBytes(12);
15  const associate_data = Buffer.alloc(16, 'z');
16  bench.start();
17  AEAD_Bench(cipher, message, associate_data, key, iv, n, len);
18}
19
20function AEAD_Bench(cipher, message, associate_data, key, iv, n, len) {
21  const written = n * len;
22  const bits = written * 8;
23  const mbits = bits / (1024 * 1024);
24
25  for (let i = 0; i < n; i++) {
26    const alice = crypto.createCipheriv(cipher, key, iv);
27    alice.setAAD(associate_data);
28    const enc = alice.update(message);
29    alice.final();
30    const tag = alice.getAuthTag();
31    const bob = crypto.createDecipheriv(cipher, key, iv);
32    bob.setAuthTag(tag);
33    bob.setAAD(associate_data);
34    bob.update(enc);
35    bob.final();
36  }
37
38  bench.end(mbits);
39}
40