-
Notifications
You must be signed in to change notification settings - Fork 2
/
speed.c
75 lines (63 loc) · 1.7 KB
/
speed.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
#include "api.h"
#include "stm32wrapper.h"
#include <stdio.h>
#include <stdint.h>
#include <string.h>
static unsigned long long overflowcnt = 0;
void sys_tick_handler(void)
{
++overflowcnt;
}
static void printcycles(const char *s, unsigned long long c)
{
char outs[32];
send_USART_str(s);
snprintf(outs,sizeof(outs),"%llu\n",c);
send_USART_str(outs);
}
// hacky workaround to shift everything into another memory segment
// loads/stores seem to be much faster there - no clue what's going on
// TODO: figure out what's going on
#define PAD (1<<16)
int main(void)
{
unsigned char key_a[CRYPTO_BYTES+PAD];
unsigned char key_b[CRYPTO_BYTES];
unsigned char sk[CRYPTO_SECRETKEYBYTES];
unsigned char pk[CRYPTO_PUBLICKEYBYTES];
unsigned char ct[CRYPTO_CIPHERTEXTBYTES];
unsigned int t0, t1;
clock_setup(CLOCK_BENCHMARK);
gpio_setup();
usart_setup(115200);
systick_setup();
rng_enable();
send_USART_str("==========================");
// Key-pair generation
t0 = systick_get_value();
overflowcnt = 0;
crypto_kem_keypair(pk, sk);
t1 = systick_get_value();
printcycles("keypair cycles:", (t0+overflowcnt*2400000llu)-t1);
// Encapsulation
t0 = systick_get_value();
overflowcnt = 0;
crypto_kem_enc(ct, key_a+PAD, pk);
t1 = systick_get_value();
printcycles("encaps cycles: ", (t0+overflowcnt*2400000llu)-t1);
// Decapsulation
t0 = systick_get_value();
overflowcnt = 0;
crypto_kem_dec(key_b, ct, sk);
t1 = systick_get_value();
printcycles("decaps cycles: ", (t0+overflowcnt*2400000llu)-t1);
if (memcmp(key_a+PAD, key_b, CRYPTO_BYTES)) {
send_USART_str("ERROR KEYS\n");
}
else {
send_USART_str("OK KEYS\n");
}
send_USART_str("#");
while(1);
return 0;
}