1 module dcrypt.benchmark.AEADCipherBenchmark;
2 
3 import dcrypt.benchmark.Benchmark;
4 import dcrypt.crypto.modes.aead;
5 import std.conv;
6 import std.algorithm: swap;
7 
8 public class AEADCipherBenchmark: Benchmark {
9 	
10 	private AEADCipher cipher;
11 	 
12 	this (AEADCipher c){
13 		cipher = c;
14 	}
15 
16 	@property
17 	static string[] header() {
18 		return ["algorithm", "AAD MB/s", "encrypt MB/s", "decrypt MB/s"];
19 	}
20 
21 	override string[] benchmark(ulong length) {
22 
23 		cipher.start(true, new ubyte[16], new ubyte[16]);
24 
25 
26 		double aadSpeed = getSpeed(length)*1e-6;
27 
28 		double encrSpeed = getSpeed(length)*1e-6;
29 
30 		cipher.start(true, new ubyte[16], new ubyte[16]);
31 		
32 		//double decrSpeed = getSpeed(length)*1e-6;
33 
34 		return [cipher.name, text(aadSpeed), text(encrSpeed)];
35 			 //decrSpeed);
36 	}
37 
38 	private double getAADSpeed(ulong length) {
39 		ubyte[64] blockA;
40 		StopWatch sw;
41 		sw.reset();
42 		sw.start();
43 		foreach(size_t i; 0 .. length/blockA.length) {
44 			cipher.processAADBytes(blockA);
45 		}
46 		sw.stop();
47 		
48 		double speed = 1e9 * length / sw.peek().nsecs();
49 		return speed;
50 	}
51 	
52 	private double getSpeed(ulong length) {
53 		ubyte[64] blockA;
54 		ubyte[128] blockB;
55 		StopWatch sw;
56 		sw.reset();
57 		sw.start();
58 		foreach(size_t i; 0 .. length/blockA.length) {
59 			cipher.processBytes(blockA, blockB);
60 		}
61 		ubyte[16] macBuf;
62 		cipher.doFinal(macBuf, blockB);
63 		sw.stop();
64 		
65 		double speed = 1e9 * length / sw.peek().nsecs();
66 		return speed;
67 	}
68 	
69 }