]> git.saurik.com Git - apple/xnu.git/blob - bsd/crypto/aes/test/tstaes.c
xnu-1699.24.8.tar.gz
[apple/xnu.git] / bsd / crypto / aes / test / tstaes.c
1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "../aes.h"
5 #include <mach/mach_time.h>
6 #include <sys/sysctl.h>
7
8
9 aes_encrypt_ctx encrypt_ctx;
10 aes_decrypt_ctx decrypt_ctx;
11
12 size_t getFreq()
13 {
14 int mib[2];
15 size_t cpufreq, len;
16 mib[0] = CTL_HW;
17 mib[1] = HW_CPU_FREQ;
18 len = sizeof(cpufreq);
19
20 sysctl(mib, 2, &cpufreq, &len, NULL, 0);
21
22 return cpufreq;
23 }
24
25
26 uint32_t cpu_freq;
27
28 main(int argc, char **argv)
29 {
30
31 char *plain;
32 char *cipher;
33 char *decrypt;
34
35 uint32_t ITERATIONS;
36 uint32_t NUM_BLOCKS;
37 uint32_t data_size;
38
39 char key[32];
40 char iv[16];
41 int checksum=0;
42 int i, j, iterations;
43 uint64_t t0, t1, t2, sum=0, max_time=0, min_time=-1, sum1=0, max_time1=0, min_time1=-1;
44 float time, time_max, time_min, time1, time_max1, time_min1;
45
46 cpu_freq = getFreq();
47
48 if (cpu_freq == 0) {
49 fprintf(stderr, "this appears to be an iPhone device, where cpu_freq can not be detected. set to 800MHz.\n");
50 cpu_freq = 800000000;
51 } else {
52 fprintf(stderr, "device max CPU clock rate = %.2f MHz\n", cpu_freq/1.e6);
53 }
54
55 mach_timebase_info_data_t info;
56 kern_return_t err = mach_timebase_info( &info );
57
58 if (argc!=3) {
59 fprintf(stderr, "usage : %s iterations num_16bytes_block\n", argv[0]);
60 exit(1);
61 }
62 ITERATIONS = atoi(argv[1]);
63 NUM_BLOCKS = atoi(argv[2]);
64 data_size = 16*NUM_BLOCKS;
65
66 plain = malloc(data_size);
67 cipher = malloc(data_size);
68 decrypt = malloc(data_size);
69
70 if ((plain==NULL) || (cipher==NULL) || (decrypt==NULL)) {
71 fprintf(stderr,"malloc error.\n");
72 exit(1);
73 }
74
75 for (i=0;i<data_size;i++) plain[i] = random();
76 for (i=0;i<32;i++) key[i] = random();
77 for (i=0;i<16;i++) iv[i] = random();
78
79 aes_encrypt_key128(key, &encrypt_ctx);
80 aes_decrypt_key128(key, &decrypt_ctx);
81
82 for (iterations=0;iterations<ITERATIONS;iterations++) {
83 t0 = mach_absolute_time();
84
85 // encrypt
86 aes_encrypt_cbc(plain, iv, NUM_BLOCKS, cipher, &encrypt_ctx);
87
88 t1 = mach_absolute_time();
89
90 // decrypt
91 aes_decrypt_cbc(cipher, iv, NUM_BLOCKS, decrypt, &decrypt_ctx);
92
93 t2 = mach_absolute_time();
94
95 for (i=0;i<(16*NUM_BLOCKS);i++) if (plain[i]!=decrypt[i]) {
96 fprintf(stderr,"error : decrypt != plain. i = %d\n", i);
97 exit(1);
98 }
99 sum += (t1-t0);
100 sum1 += (t2-t1);
101 t2-=t1;
102 t1-=t0;
103 if (t1>max_time) max_time = t1;
104 if (t1<min_time) min_time = t1;
105 if (t2>max_time1) max_time1 = t2;
106 if (t2<min_time1) min_time1 = t2;
107 }
108
109 time = sum * 1e-9* ((double) info.numer)/((double) info.denom);
110 time_max = max_time * 1e-9* ((double) info.numer)/((double) info.denom);
111 time_min = min_time * 1e-9* ((double) info.numer)/((double) info.denom);
112
113 time1 = sum1 * 1e-9* ((double) info.numer)/((double) info.denom);
114 time_max1 = max_time1 * 1e-9* ((double) info.numer)/((double) info.denom);
115 time_min1 = min_time1 * 1e-9* ((double) info.numer)/((double) info.denom);
116
117 printf("%d bytes per cbc call\n", data_size);
118 printf(" aes_encrypt_cbc : time elapsed = %8.2f usecs, %7.2f MBytes/sec, %8.2f cycles/byte\n", 1.e6*time/ITERATIONS,data_size*ITERATIONS/1024./1024./time, time*1.*cpu_freq/ITERATIONS/data_size);
119 printf(" best iteration : time elapsed = %8.2f usecs, %7.2f MBytes/sec, %8.2f cycles/byte\n", 1.e6*time_min,data_size/1024./1024./time_min, time_min*1.*cpu_freq/data_size);
120 printf(" worst iteration : time elapsed = %8.2f usecs, %7.2f MBytes/sec, %8.2f cycles/byte\n", 1.e6*time_max,data_size/1024./1024./time_max, time_max*1.*cpu_freq/data_size);
121
122 printf("\n");
123
124 printf(" aes_decrypt_cbc : time elapsed = %8.2f usecs, %7.2f MBytes/sec, %8.2f cycles/byte\n", 1.e6*time1/ITERATIONS,data_size*ITERATIONS/1024./1024./time1, time1*1.*cpu_freq/ITERATIONS/data_size);
125 printf(" best iteration : time elapsed = %8.2f usecs, %7.2f MBytes/sec, %8.2f cycles/byte\n", 1.e6*time_min1,data_size/1024./1024./time_min1, time_min1*1.*cpu_freq/data_size);
126 printf(" worst iteration : time elapsed = %8.2f usecs, %7.2f MBytes/sec, %8.2f cycles/byte\n", 1.e6*time_max1,data_size/1024./1024./time_max1, time_max1*1.*cpu_freq/data_size);
127
128 free(plain);
129 free(cipher);
130 free(decrypt);
131 }