diff options
Diffstat (limited to 'nacl/crypto_secretbox/measure.c')
-rw-r--r-- | nacl/crypto_secretbox/measure.c | 75 |
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 | |||
6 | extern void printentry(long long,const char *,long long *,long long); | ||
7 | extern unsigned char *alignedcalloc(unsigned long long); | ||
8 | extern const char *primitiveimplementation; | ||
9 | extern const char *implementationversion; | ||
10 | extern const char *sizenames[]; | ||
11 | extern const long long sizes[]; | ||
12 | extern void allocate(void); | ||
13 | extern void measure(void); | ||
14 | |||
15 | const char *primitiveimplementation = crypto_secretbox_IMPLEMENTATION; | ||
16 | const char *implementationversion = crypto_secretbox_VERSION; | ||
17 | const char *sizenames[] = { "keybytes", "noncebytes", "zerobytes", "boxzerobytes", 0 }; | ||
18 | const long long sizes[] = { crypto_secretbox_KEYBYTES, crypto_secretbox_NONCEBYTES, crypto_secretbox_ZEROBYTES, crypto_secretbox_BOXZEROBYTES }; | ||
19 | |||
20 | #define MAXTEST_BYTES 4096 | ||
21 | |||
22 | static unsigned char *k; | ||
23 | static unsigned char *n; | ||
24 | static unsigned char *m; | ||
25 | static unsigned char *c; | ||
26 | |||
27 | void preallocate(void) | ||
28 | { | ||
29 | } | ||
30 | |||
31 | void 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 | ||
40 | static long long cycles[TIMINGS + 1]; | ||
41 | |||
42 | void 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 | } | ||