2 #include "randombytes.h"
4 #include "crypto_box.h"
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);
15 const char *primitiveimplementation
= crypto_box_IMPLEMENTATION
;
16 const char *implementationversion
= crypto_box_VERSION
;
17 const char *sizenames
[] = { "publickeybytes", "secretkeybytes", "beforenmbytes", "noncebytes", "zerobytes", "boxzerobytes", 0 };
18 const long long sizes
[] = { crypto_box_PUBLICKEYBYTES
, crypto_box_SECRETKEYBYTES
, crypto_box_BEFORENMBYTES
, crypto_box_NONCEBYTES
, crypto_box_ZEROBYTES
, crypto_box_BOXZEROBYTES
};
20 #define MAXTEST_BYTES 4096
22 static unsigned char *ska
;
23 static unsigned char *pka
;
24 static unsigned char *skb
;
25 static unsigned char *pkb
;
26 static unsigned char *n
;
27 static unsigned char *m
;
28 static unsigned char *c
;
29 static unsigned char *sa
;
30 static unsigned char *sb
;
32 void preallocate(void)
38 ska
= alignedcalloc(crypto_box_SECRETKEYBYTES
);
39 pka
= alignedcalloc(crypto_box_PUBLICKEYBYTES
);
40 skb
= alignedcalloc(crypto_box_SECRETKEYBYTES
);
41 pkb
= alignedcalloc(crypto_box_PUBLICKEYBYTES
);
42 n
= alignedcalloc(crypto_box_NONCEBYTES
);
43 m
= alignedcalloc(MAXTEST_BYTES
+ crypto_box_ZEROBYTES
);
44 c
= alignedcalloc(MAXTEST_BYTES
+ crypto_box_ZEROBYTES
);
45 sa
= alignedcalloc(crypto_box_BEFORENMBYTES
);
46 sb
= alignedcalloc(crypto_box_BEFORENMBYTES
);
50 static long long cycles
[TIMINGS
+ 1];
58 for (loop
= 0;loop
< LOOPS
;++loop
) {
59 for (i
= 0;i
<= TIMINGS
;++i
) {
60 cycles
[i
] = cpucycles();
61 crypto_box_keypair(pka
,ska
);
63 for (i
= 0;i
< TIMINGS
;++i
) cycles
[i
] = cycles
[i
+ 1] - cycles
[i
];
64 printentry(-1,"keypair_cycles",cycles
,TIMINGS
);
66 for (i
= 0;i
<= TIMINGS
;++i
) {
67 cycles
[i
] = cpucycles();
68 crypto_box_keypair(pkb
,skb
);
70 for (i
= 0;i
< TIMINGS
;++i
) cycles
[i
] = cycles
[i
+ 1] - cycles
[i
];
71 printentry(-1,"keypair_cycles",cycles
,TIMINGS
);
73 for (i
= 0;i
<= TIMINGS
;++i
) {
74 cycles
[i
] = cpucycles();
75 crypto_box_beforenm(sa
,pkb
,ska
);
77 for (i
= 0;i
< TIMINGS
;++i
) cycles
[i
] = cycles
[i
+ 1] - cycles
[i
];
78 printentry(-1,"beforenm_cycles",cycles
,TIMINGS
);
80 for (i
= 0;i
<= TIMINGS
;++i
) {
81 cycles
[i
] = cpucycles();
82 crypto_box_beforenm(sb
,pka
,skb
);
84 for (i
= 0;i
< TIMINGS
;++i
) cycles
[i
] = cycles
[i
+ 1] - cycles
[i
];
85 printentry(-1,"beforenm_cycles",cycles
,TIMINGS
);
87 for (mlen
= 0;mlen
<= MAXTEST_BYTES
;mlen
+= 1 + mlen
/ 8) {
88 randombytes(n
,crypto_box_NONCEBYTES
);
89 randombytes(m
+ crypto_box_ZEROBYTES
,mlen
);
90 randombytes(c
,mlen
+ crypto_box_ZEROBYTES
);
92 for (i
= 0;i
<= TIMINGS
;++i
) {
93 cycles
[i
] = cpucycles();
94 crypto_box(c
,m
,mlen
+ crypto_box_ZEROBYTES
,n
,pka
,skb
);
96 for (i
= 0;i
< TIMINGS
;++i
) cycles
[i
] = cycles
[i
+ 1] - cycles
[i
];
97 printentry(mlen
,"cycles",cycles
,TIMINGS
);
99 for (i
= 0;i
<= TIMINGS
;++i
) {
100 cycles
[i
] = cpucycles();
101 crypto_box_open(m
,c
,mlen
+ crypto_box_ZEROBYTES
,n
,pkb
,ska
);
103 for (i
= 0;i
< TIMINGS
;++i
) cycles
[i
] = cycles
[i
+ 1] - cycles
[i
];
104 printentry(mlen
,"open_cycles",cycles
,TIMINGS
);
106 ++c
[crypto_box_ZEROBYTES
];
107 for (i
= 0;i
<= TIMINGS
;++i
) {
108 cycles
[i
] = cpucycles();
109 crypto_box_open(m
,c
,mlen
+ crypto_box_ZEROBYTES
,n
,pkb
,ska
);
111 for (i
= 0;i
< TIMINGS
;++i
) cycles
[i
] = cycles
[i
+ 1] - cycles
[i
];
112 printentry(mlen
,"forgery_open_cycles",cycles
,TIMINGS
);
114 for (i
= 0;i
<= TIMINGS
;++i
) {
115 cycles
[i
] = cpucycles();
116 crypto_box_afternm(c
,m
,mlen
+ crypto_box_ZEROBYTES
,n
,sb
);
118 for (i
= 0;i
< TIMINGS
;++i
) cycles
[i
] = cycles
[i
+ 1] - cycles
[i
];
119 printentry(mlen
,"afternm_cycles",cycles
,TIMINGS
);
121 for (i
= 0;i
<= TIMINGS
;++i
) {
122 cycles
[i
] = cpucycles();
123 crypto_box_open_afternm(m
,c
,mlen
+ crypto_box_ZEROBYTES
,n
,sa
);
125 for (i
= 0;i
< TIMINGS
;++i
) cycles
[i
] = cycles
[i
+ 1] - cycles
[i
];
126 printentry(mlen
,"open_afternm_cycles",cycles
,TIMINGS
);
128 ++c
[crypto_box_ZEROBYTES
];
129 for (i
= 0;i
<= TIMINGS
;++i
) {
130 cycles
[i
] = cpucycles();
131 crypto_box_open_afternm(m
,c
,mlen
+ crypto_box_ZEROBYTES
,n
,sa
);
133 for (i
= 0;i
< TIMINGS
;++i
) cycles
[i
] = cycles
[i
+ 1] - cycles
[i
];
134 printentry(mlen
,"forgery_open_afternm_cycles",cycles
,TIMINGS
);