-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack.c
119 lines (100 loc) · 3.62 KB
/
stack.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "api.h"
#include "randombytes.h"
#include "hal.h"
#include "sendfn.h"
#include <string.h>
#ifndef MAX_STACK_SIZE
#define MAX_STACK_SIZE hal_get_stack_size()
#endif
#ifndef STACK_SIZE_INCR
#define STACK_SIZE_INCR 0x1000
#endif
// https://stackoverflow.com/a/1489985/1711232
#define PASTER(x, y) x##y
#define EVALUATOR(x, y) PASTER(x, y)
#define NAMESPACE(fun) EVALUATOR(MUPQ_NAMESPACE, fun)
// use different names so we can have empty namespaces
#define MUPQ_CRYPTO_BYTES NAMESPACE(CRYPTO_BYTES)
#define MUPQ_CRYPTO_PUBLICKEYBYTES NAMESPACE(CRYPTO_PUBLICKEYBYTES)
#define MUPQ_CRYPTO_SECRETKEYBYTES NAMESPACE(CRYPTO_SECRETKEYBYTES)
#define MUPQ_CRYPTO_CIPHERTEXTBYTES NAMESPACE(CRYPTO_CIPHERTEXTBYTES)
#define MUPQ_CRYPTO_ALGNAME NAMESPACE(CRYPTO_ALGNAME)
#define MUPQ_crypto_kem_keypair NAMESPACE(crypto_kem_keypair)
#define MUPQ_crypto_kem_enc NAMESPACE(crypto_kem_enc)
#define MUPQ_crypto_kem_dec NAMESPACE(crypto_kem_dec)
#define send_stack_usage(S, U) send_unsigned((S), (U))
unsigned int canary_size;
volatile unsigned char *p;
unsigned int c;
uint8_t canary = 0x42;
#if defined(STM32F407VG) //write key pair into CCM
unsigned char * sk_a = 0x10000000 + 8192;
unsigned char * pk = 0x10000000 + 8192 + MUPQ_CRYPTO_SECRETKEYBYTES;
#else
unsigned char sk_a[MUPQ_CRYPTO_SECRETKEYBYTES];
unsigned char pk[MUPQ_CRYPTO_PUBLICKEYBYTES];
#endif
unsigned char sendb[MUPQ_CRYPTO_CIPHERTEXTBYTES];
unsigned int stack_key_gen, stack_encaps, stack_decaps;
unsigned char key_a[MUPQ_CRYPTO_BYTES], key_b[MUPQ_CRYPTO_BYTES];
#define FILL_STACK() \
p = &a; \
while (p > &a - canary_size) \
*(p--) = canary;
#define CHECK_STACK() \
c = canary_size; \
p = &a - canary_size + 1; \
while (*p == canary && p < &a) { \
p++; \
c--; \
}
static int test_keys(void) {
volatile unsigned char a;
// Alice generates a public key
FILL_STACK()
MUPQ_crypto_kem_keypair(pk, sk_a);
CHECK_STACK()
if(c >= canary_size) return -1;
stack_key_gen = c;
// Bob derives a secret key and creates a response
FILL_STACK()
MUPQ_crypto_kem_enc(sendb, key_b, pk);
CHECK_STACK()
if(c >= canary_size) return -1;
stack_encaps = c;
// Alice uses Bobs response to get her secret key
FILL_STACK()
MUPQ_crypto_kem_dec(key_a, sendb, sk_a);
CHECK_STACK()
if(c >= canary_size) return -1;
stack_decaps = c;
if (memcmp(key_a, key_b, MUPQ_CRYPTO_BYTES)){
return -1;
} else {
send_stack_usage("keypair stack usage:", stack_key_gen);
send_stack_usage("encaps stack usage:", stack_encaps);
send_stack_usage("decaps stack usage:", stack_decaps);
hal_send_str("OK KEYS\n");
return 0;
}
}
int main(void) {
hal_setup(CLOCK_FAST);
// marker for automated benchmarks
hal_send_str("==========================");
//canary_size = STACK_SIZE_INCR;
canary_size=110000;
while(test_keys()){
if(canary_size == MAX_STACK_SIZE) {
hal_send_str("failed to measure stack usage.\n");
break;
}
canary_size += STACK_SIZE_INCR;
if(canary_size >= MAX_STACK_SIZE) {
canary_size = MAX_STACK_SIZE;
}
}
// marker for automated benchmarks
hal_send_str("#");
return 0;
}