summaryrefslogtreecommitdiff
path: root/nacl/crypto_secretbox/measure.c
diff options
context:
space:
mode:
Diffstat (limited to 'nacl/crypto_secretbox/measure.c')
-rw-r--r--nacl/crypto_secretbox/measure.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/nacl/crypto_secretbox/measure.c b/nacl/crypto_secretbox/measure.c
new file mode 100644
index 00000000..6cb0692f
--- /dev/null
+++ b/nacl/crypto_secretbox/measure.c
@@ -0,0 +1,75 @@
1#include <stdlib.h>
2#include "randombytes.h"
3#include "cpucycles.h"
4#include "crypto_secretbox.h"
5
6extern void printentry(long long,const char *,long long *,long long);
7extern unsigned char *alignedcalloc(unsigned long long);
8extern const char *primitiveimplementation;
9extern const char *implementationversion;
10extern const char *sizenames[];
11extern const long long sizes[];
12extern void allocate(void);
13extern void measure(void);
14
15const char *primitiveimplementation = crypto_secretbox_IMPLEMENTATION;
16const char *implementationversion = crypto_secretbox_VERSION;
17const char *sizenames[] = { "keybytes", "noncebytes", "zerobytes", "boxzerobytes", 0 };
18const long long sizes[] = { crypto_secretbox_KEYBYTES, crypto_secretbox_NONCEBYTES, crypto_secretbox_ZEROBYTES, crypto_secretbox_BOXZEROBYTES };
19
20#define MAXTEST_BYTES 4096
21
22static unsigned char *k;
23static unsigned char *n;
24static unsigned char *m;
25static unsigned char *c;
26
27void preallocate(void)
28{
29}
30
31void allocate(void)
32{
33 k = alignedcalloc(crypto_secretbox_KEYBYTES);
34 n = alignedcalloc(crypto_secretbox_NONCEBYTES);
35 m = alignedcalloc(MAXTEST_BYTES + crypto_secretbox_ZEROBYTES);
36 c = alignedcalloc(MAXTEST_BYTES + crypto_secretbox_ZEROBYTES);
37}
38
39#define TIMINGS 15
40static long long cycles[TIMINGS + 1];
41
42void measure(void)
43{
44 int i;
45 int loop;
46 int mlen;
47
48 for (loop = 0;loop < LOOPS;++loop) {
49 for (mlen = 0;mlen <= MAXTEST_BYTES;mlen += 1 + mlen / 8) {
50 randombytes(k,crypto_secretbox_KEYBYTES);
51 randombytes(n,crypto_secretbox_NONCEBYTES);
52 randombytes(m + crypto_secretbox_ZEROBYTES,mlen);
53 randombytes(c,mlen + crypto_secretbox_ZEROBYTES);
54 for (i = 0;i <= TIMINGS;++i) {
55 cycles[i] = cpucycles();
56 crypto_secretbox(c,m,mlen + crypto_secretbox_ZEROBYTES,n,k);
57 }
58 for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
59 printentry(mlen,"cycles",cycles,TIMINGS);
60 for (i = 0;i <= TIMINGS;++i) {
61 cycles[i] = cpucycles();
62 crypto_secretbox_open(m,c,mlen + crypto_secretbox_ZEROBYTES,n,k);
63 }
64 for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
65 printentry(mlen,"open_cycles",cycles,TIMINGS);
66 ++c[crypto_secretbox_ZEROBYTES];
67 for (i = 0;i <= TIMINGS;++i) {
68 cycles[i] = cpucycles();
69 crypto_secretbox_open(m,c,mlen + crypto_secretbox_ZEROBYTES,n,k);
70 }
71 for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
72 printentry(mlen,"forgery_open_cycles",cycles,TIMINGS);
73 }
74 }
75}