11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common.js');
31cb0ef41Sopenharmony_ciconst crypto = require('crypto');
41cb0ef41Sopenharmony_ciconst keylen = { 'aes-128-gcm': 16, 'aes-192-gcm': 24, 'aes-256-gcm': 32 };
51cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, {
61cb0ef41Sopenharmony_ci  n: [500],
71cb0ef41Sopenharmony_ci  cipher: ['aes-128-gcm', 'aes-192-gcm', 'aes-256-gcm'],
81cb0ef41Sopenharmony_ci  len: [1024, 4 * 1024, 16 * 1024, 64 * 1024, 256 * 1024, 1024 * 1024],
91cb0ef41Sopenharmony_ci});
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_cifunction main({ n, len, cipher }) {
121cb0ef41Sopenharmony_ci  const message = Buffer.alloc(len, 'b');
131cb0ef41Sopenharmony_ci  const key = crypto.randomBytes(keylen[cipher]);
141cb0ef41Sopenharmony_ci  const iv = crypto.randomBytes(12);
151cb0ef41Sopenharmony_ci  const associate_data = Buffer.alloc(16, 'z');
161cb0ef41Sopenharmony_ci  bench.start();
171cb0ef41Sopenharmony_ci  AEAD_Bench(cipher, message, associate_data, key, iv, n, len);
181cb0ef41Sopenharmony_ci}
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cifunction AEAD_Bench(cipher, message, associate_data, key, iv, n, len) {
211cb0ef41Sopenharmony_ci  const written = n * len;
221cb0ef41Sopenharmony_ci  const bits = written * 8;
231cb0ef41Sopenharmony_ci  const mbits = bits / (1024 * 1024);
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  for (let i = 0; i < n; i++) {
261cb0ef41Sopenharmony_ci    const alice = crypto.createCipheriv(cipher, key, iv);
271cb0ef41Sopenharmony_ci    alice.setAAD(associate_data);
281cb0ef41Sopenharmony_ci    const enc = alice.update(message);
291cb0ef41Sopenharmony_ci    alice.final();
301cb0ef41Sopenharmony_ci    const tag = alice.getAuthTag();
311cb0ef41Sopenharmony_ci    const bob = crypto.createDecipheriv(cipher, key, iv);
321cb0ef41Sopenharmony_ci    bob.setAuthTag(tag);
331cb0ef41Sopenharmony_ci    bob.setAAD(associate_data);
341cb0ef41Sopenharmony_ci    bob.update(enc);
351cb0ef41Sopenharmony_ci    bob.final();
361cb0ef41Sopenharmony_ci  }
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  bench.end(mbits);
391cb0ef41Sopenharmony_ci}
40