5 #include <mach/mach_time.h>
6 #include <sys/sysctl.h>
9 aes_encrypt_ctx encrypt_ctx
;
10 aes_decrypt_ctx decrypt_ctx
;
18 len
= sizeof(cpufreq
);
20 sysctl(mib
, 2, &cpufreq
, &len
, NULL
, 0);
28 main(int argc
, char **argv
)
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
;
49 fprintf(stderr
, "this appears to be an iPhone device, where cpu_freq can not be detected. set to 800MHz.\n");
52 fprintf(stderr
, "device max CPU clock rate = %.2f MHz\n", cpu_freq
/1.e6
);
55 mach_timebase_info_data_t info
;
56 kern_return_t err
= mach_timebase_info( &info
);
59 fprintf(stderr
, "usage : %s iterations num_16bytes_block\n", argv
[0]);
62 ITERATIONS
= atoi(argv
[1]);
63 NUM_BLOCKS
= atoi(argv
[2]);
64 data_size
= 16*NUM_BLOCKS
;
66 plain
= malloc(data_size
);
67 cipher
= malloc(data_size
);
68 decrypt
= malloc(data_size
);
70 if ((plain
==NULL
) || (cipher
==NULL
) || (decrypt
==NULL
)) {
71 fprintf(stderr
,"malloc error.\n");
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();
79 aes_encrypt_key128(key
, &encrypt_ctx
);
80 aes_decrypt_key128(key
, &decrypt_ctx
);
82 for (iterations
=0;iterations
<ITERATIONS
;iterations
++) {
83 t0
= mach_absolute_time();
86 aes_encrypt_cbc(plain
, iv
, NUM_BLOCKS
, cipher
, &encrypt_ctx
);
88 t1
= mach_absolute_time();
91 aes_decrypt_cbc(cipher
, iv
, NUM_BLOCKS
, decrypt
, &decrypt_ctx
);
93 t2
= mach_absolute_time();
95 for (i
=0;i
<(16*NUM_BLOCKS
);i
++) if (plain
[i
]!=decrypt
[i
]) {
96 fprintf(stderr
,"error : decrypt != plain. i = %d\n", i
);
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
;
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
);
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
);
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
);
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
);