-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmeasure.c
91 lines (83 loc) · 2.93 KB
/
measure.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <stdlib.h>
#include "kernelrandombytes.h"
#include "cpucycles.h"
#include "crypto_aead.h"
#include "measure.h"
const char *primitiveimplementation = crypto_aead_IMPLEMENTATION;
const char *implementationversion = crypto_aead_VERSION;
const char *sizenames[] = { "keybytes", "nsecbytes", "npubbytes", "abytes", 0 };
const long long sizes[] = { crypto_aead_KEYBYTES, crypto_aead_NSECBYTES, crypto_aead_NPUBBYTES, crypto_aead_ABYTES };
#define MAXTEST_BYTES 2048
static unsigned char *k;
static unsigned char *nsec;
static unsigned char *npub;
static unsigned char *m;
static unsigned char *ad;
static unsigned char *c;
void preallocate(void)
{
}
void allocate(void)
{
k = alignedcalloc(crypto_aead_KEYBYTES);
nsec = alignedcalloc(crypto_aead_NSECBYTES);
npub = alignedcalloc(crypto_aead_NPUBBYTES);
m = alignedcalloc(MAXTEST_BYTES);
ad = alignedcalloc(MAXTEST_BYTES);
c = alignedcalloc(MAXTEST_BYTES + crypto_aead_ABYTES);
}
#define TIMINGS 7
static long long cycles[TIMINGS + 1];
void measure(void)
{
int i;
int loop;
int direction;
unsigned long long mlen;
unsigned long long adlen;
unsigned long long clen;
unsigned long long tlen;
for (loop = 0;loop < LOOPS;++loop) {
for (direction = 0;direction < 3;++direction) {
mlen = 0;
adlen = 0;
for (;;) {
if (direction != 1) ++mlen;
if (direction != 0) ++adlen;
if (mlen > MAXTEST_BYTES) break;
if (adlen > MAXTEST_BYTES) break;
if (mlen > 64) if (mlen & 31) if (adlen != mlen) continue;
if (adlen > 64) if (adlen & 31) if (adlen != mlen) continue;
kernelrandombytes(k,crypto_aead_KEYBYTES);
kernelrandombytes(nsec,crypto_aead_NSECBYTES);
kernelrandombytes(npub,crypto_aead_NPUBBYTES);
kernelrandombytes(m,mlen);
kernelrandombytes(ad,adlen);
kernelrandombytes(c,mlen + crypto_aead_ABYTES);
for (i = 0;i <= TIMINGS;++i) {
cycles[i] = cpucycles();
crypto_aead_encrypt(c,&clen,m,mlen,ad,adlen,nsec,npub,k);
}
for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
printentry(1000000 * adlen + mlen,"encrypt_cycles",cycles,TIMINGS);
if (mlen > 64) if (mlen & 31) continue;
if (adlen > 64) if (adlen & 31) continue;
for (i = 0;i <= TIMINGS;++i) {
cycles[i] = cpucycles();
crypto_aead_decrypt(m,&tlen,nsec,c,clen,ad,adlen,npub,k);
}
for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
printentry(1000000 * adlen + mlen,"decrypt_cycles",cycles,TIMINGS);
if (clen > 0) {
++c[clen/2];
for (i = 0;i <= TIMINGS;++i) {
cycles[i] = cpucycles();
crypto_aead_decrypt(m,&tlen,nsec,c,clen,ad,adlen,npub,k);
}
for (i = 0;i < TIMINGS;++i) cycles[i] = cycles[i + 1] - cycles[i];
printentry(1000000 * adlen + mlen,"forgery_decrypt_cycles",cycles,TIMINGS);
}
}
}
}
}