Skip to content

Commit 1916feb

Browse files
authored
Merge pull request #33 from All-Your-Locks-Are-Belong-To-Us/feature/measure-esp32
Make measurements compile for ESP32
2 parents 5789915 + 3f9332f commit 1916feb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+473
-174
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ target_link_libraries(nfc_simulator ${PRODUCT_NAME})
1313
#######################################
1414
# Test applications
1515

16-
add_subdirectory(measurements)
16+
add_subdirectory(measurements/atmega)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
idf_component_register(
2+
SRCS "clock_cycles.c"
3+
INCLUDE_DIRS "."
4+
PRIV_REQUIRES esp_hw_support esp_rom
5+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright (c) 2022 Felix Gohla, Konrad Hanff, Tobias Kantusch,
3+
* Quentin Kuth, Felix Roth. All rights reserved.
4+
*
5+
* Use of this source code is governed by a BSD-style
6+
* license that can be found in the LICENSE file.
7+
*/
8+
9+
#include <esp_cpu.h>
10+
11+
// Buffer variable used for measuring elapsed clock cycles.
12+
esp_cpu_cycle_count_t clock_cycle_start;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2022 Felix Gohla, Konrad Hanff, Tobias Kantusch,
3+
* Quentin Kuth, Felix Roth. All rights reserved.
4+
*
5+
* Use of this source code is governed by a BSD-style
6+
* license that can be found in the LICENSE file.
7+
*/
8+
9+
#pragma once
10+
11+
#include <stdint.h>
12+
#include <esp_cpu.h>
13+
#include <esp_rom_sys.h>
14+
15+
extern volatile esp_cpu_cycle_count_t clock_cycle_start;
16+
17+
inline void clock_init() {}
18+
19+
/**
20+
* @brief Starts counting clock cycles.
21+
*
22+
*/
23+
static inline void clock_start_counting() {
24+
clock_cycle_start = esp_cpu_get_cycle_count();
25+
}
26+
27+
/**
28+
* @brief Stops counting clock cycles and returns the number of elapsed cycles.
29+
*
30+
*/
31+
static inline uint64_t clock_stop_counting() {
32+
esp_cpu_cycle_count_t end = esp_cpu_get_cycle_count();
33+
uint64_t val = end - clock_cycle_start;
34+
return val;
35+
}
36+
37+
/**
38+
* @brief Converts clock cycles to nanoseconds.
39+
*
40+
* @param cycles The number of cycles.
41+
* @return uint64_t The number of nanoseconds.
42+
*/
43+
static inline uint32_t clock_cyles_to_ns(uint64_t cycles) {
44+
return cycles * 1000 / esp_rom_get_cpu_ticks_per_us();
45+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
idf_component_register(
2+
SRCS "hw_crypto.c"
3+
INCLUDE_DIRS "."
4+
PRIV_REQUIRES libmicrofido2 mbedtls
5+
)
File renamed without changes.
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright (c) 2022 Felix Gohla, Konrad Hanff, Tobias Kantusch,
3+
* Quentin Kuth, Felix Roth. All rights reserved.
4+
*
5+
* Use of this source code is governed by a BSD-style
6+
* license that can be found in the LICENSE file.
7+
*/
8+
9+
#include <sdkconfig.h>
10+
11+
#ifdef CONFIG_USE_HW_CRYPTO
12+
#include "fido.h"
13+
14+
#include <stdio.h>
15+
#include <mbedtls/aes.h>
16+
#include <mbedtls/gcm.h>
17+
#include <mbedtls/sha256.h>
18+
#include <mbedtls/sha512.h>
19+
20+
static void sha256(const uint8_t *data, size_t data_len, uint8_t *hash) {
21+
int r = mbedtls_sha256(data, data_len, hash, 0);
22+
if (r != 0) {
23+
printf("sha256 failed with %d\n", r);
24+
}
25+
}
26+
27+
static void sha512(const uint8_t *data, size_t data_len, uint8_t *hash) {
28+
int r = mbedtls_sha512(data, data_len, hash, 0);
29+
if (r != 0) {
30+
printf("sha512 failed with %d\n", r);
31+
}
32+
}
33+
34+
static int aes_gcm_encrypt(
35+
const uint8_t *key, size_t key_len,
36+
const uint8_t *iv, size_t iv_len,
37+
const uint8_t *plaintext, size_t plaintext_len,
38+
const uint8_t *aad, size_t aad_len,
39+
uint8_t *ciphertext, uint8_t *tag
40+
) {
41+
mbedtls_gcm_context ctx;
42+
int r;
43+
44+
mbedtls_gcm_init(&ctx);
45+
46+
r = mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key, key_len * 8);
47+
if (r != 0) {
48+
printf("[%s] mbedtls_gcm_setkey failed with %d\n", __func__, r);
49+
return r;
50+
}
51+
52+
r = mbedtls_gcm_crypt_and_tag(
53+
&ctx,
54+
MBEDTLS_ENCRYPT,
55+
plaintext_len,
56+
iv, iv_len,
57+
aad, aad_len,
58+
plaintext, ciphertext,
59+
16, tag
60+
);
61+
if (r != 0) {
62+
printf("[%s] mbedtls_gcm_crypt_and_tag failed with %d\n", __func__, r);
63+
return r;
64+
}
65+
66+
mbedtls_gcm_free(&ctx);
67+
68+
return 0;
69+
}
70+
71+
static int aes_gcm_decrypt(
72+
const uint8_t *key, size_t key_len,
73+
const uint8_t *iv, size_t iv_len,
74+
const uint8_t *ciphertext, size_t ciphertext_len,
75+
const uint8_t *aad, size_t aad_len,
76+
const uint8_t *tag,
77+
uint8_t *plaintext
78+
) {
79+
mbedtls_gcm_context ctx;
80+
int r;
81+
82+
mbedtls_gcm_init(&ctx);
83+
84+
r = mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key, key_len * 8);
85+
if (r != 0) {
86+
printf("[%s] mbedtls_gcm_setkey failed with %d\n", __func__, r);
87+
return r;
88+
}
89+
90+
r = mbedtls_gcm_auth_decrypt(
91+
&ctx,
92+
ciphertext_len,
93+
iv, iv_len,
94+
aad, aad_len,
95+
tag, 16,
96+
ciphertext, plaintext
97+
);
98+
if (r != 0) {
99+
printf("[%s] mbedtls_gcm_auth_decrypt failed with %d\n", __func__, r);
100+
return r;
101+
}
102+
103+
mbedtls_gcm_free(&ctx);
104+
105+
return 0;
106+
}
107+
108+
int init_hw_crypto() {
109+
fido_sha256 = &sha256;
110+
fido_sha512 = &sha512;
111+
fido_aes_gcm_encrypt = &aes_gcm_encrypt;
112+
fido_aes_gcm_decrypt = &aes_gcm_decrypt;
113+
114+
return 0;
115+
}
116+
#else
117+
int init_hw_crypto() {
118+
return 0;
119+
}
120+
#endif
File renamed without changes.

examples/esp32/main/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
idf_component_register(
22
SRCS "esp32-libmicrofido2.c" "stateless_rp/stateless_rp.c" "stateless_rp/stateless_rp_nfc_simulator.c"
33
INCLUDE_DIRS "."
4-
PRIV_REQUIRES libmicrofido2 mbedtls
4+
PRIV_REQUIRES libmicrofido2 hw_crypto clock
55
)

examples/esp32/main/esp32-libmicrofido2.c

Lines changed: 17 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -6,135 +6,31 @@
66
* license that can be found in the LICENSE file.
77
*/
88

9-
#include <assert.h>
10-
#include <stdio.h>
11-
#include <string.h>
12-
#include <sdkconfig.h>
13-
14-
#include <fido.h>
15-
16-
#ifdef CONFIG_USE_HW_CRYPTO
17-
#include <mbedtls/aes.h>
18-
#include <mbedtls/gcm.h>
19-
#include <mbedtls/sha256.h>
20-
#include <mbedtls/sha512.h>
21-
#endif
22-
23-
#ifdef CONFIG_USE_HW_CRYPTO
24-
int sha256(const uint8_t *data, size_t data_len, uint8_t *hash) {
25-
int r = mbedtls_sha256(data, data_len, hash, 0);
26-
if (r != 0) {
27-
printf("sha256 failed with %d\n", r);
28-
}
29-
return r;
30-
}
31-
32-
int sha512(const uint8_t *data, size_t data_len, uint8_t *hash) {
33-
int r = mbedtls_sha512(data, data_len, hash, 0);
34-
if (r != 0) {
35-
printf("sha512 failed with %d\n", r);
36-
}
37-
return r;
38-
}
39-
40-
int aes_gcm_encrypt(
41-
const uint8_t *key, size_t key_len,
42-
const uint8_t *iv, size_t iv_len,
43-
const uint8_t *plaintext, size_t plaintext_len,
44-
const uint8_t *aad, size_t aad_len,
45-
uint8_t *ciphertext, uint8_t *tag
46-
) {
47-
mbedtls_gcm_context ctx;
48-
int r;
49-
50-
mbedtls_gcm_init(&ctx);
51-
52-
r = mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key, key_len * 8);
53-
if (r != 0) {
54-
printf("[%s] mbedtls_gcm_setkey failed with %d\n", __func__, r);
55-
return r;
56-
}
57-
58-
r = mbedtls_gcm_crypt_and_tag(
59-
&ctx,
60-
MBEDTLS_ENCRYPT,
61-
plaintext_len,
62-
iv, iv_len,
63-
aad, aad_len,
64-
ciphertext, plaintext,
65-
16, tag
66-
);
67-
if (r != 0) {
68-
printf("[%s] mbedtls_gcm_crypt_and_tag failed with %d\n", __func__, r);
69-
return r;
70-
}
71-
72-
mbedtls_gcm_free(&ctx);
73-
74-
return 0;
75-
}
76-
77-
int aes_gcm_decrypt(
78-
const uint8_t *key, size_t key_len,
79-
const uint8_t *iv, size_t iv_len,
80-
const uint8_t *ciphertext, size_t ciphertext_len,
81-
const uint8_t *aad, size_t aad_len,
82-
const uint8_t *tag,
83-
uint8_t *plaintext
84-
) {
85-
mbedtls_gcm_context ctx;
86-
int r;
87-
88-
mbedtls_gcm_init(&ctx);
89-
90-
r = mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key, key_len * 8);
91-
if (r != 0) {
92-
printf("[%s] mbedtls_gcm_setkey failed with %d\n", __func__, r);
93-
return r;
94-
}
95-
96-
r = mbedtls_gcm_crypt_and_tag(
97-
&ctx,
98-
MBEDTLS_DECRYPT,
99-
ciphertext_len,
100-
iv, iv_len,
101-
aad, aad_len,
102-
ciphertext, plaintext,
103-
16, tag
104-
);
105-
if (r != 0) {
106-
printf("[%s] mbedtls_gcm_crypt_and_tag failed with %d\n", __func__, r);
107-
return r;
108-
}
109-
110-
mbedtls_gcm_free(&ctx);
111-
112-
return 0;
113-
}
114-
115-
void init_crypto() {
116-
fido_sha256 = &sha256;
117-
fido_sha512 = &sha512;
118-
fido_aes_gcm_encrypt = &aes_gcm_encrypt;
119-
fido_aes_gcm_decrypt = &aes_gcm_decrypt;
120-
}
121-
#endif
122-
1239
#include <fido.h>
10+
#include <stdio.h>
11+
#include "hw_crypto.h"
12+
#include "clock_cycles.h"
12413
#include "stateless_rp/stateless_rp.h"
12514
#include "stateless_rp/stateless_rp_nfc_simulator.h"
12615

12716
int app_main(void) {
128-
#ifdef CONFIG_USE_HW_CRYPTO
129-
init_crypto();
130-
#endif
17+
clock_init();
18+
if (init_hw_crypto() != 0) {
19+
return -1;
20+
} else {
21+
printf("Initialized cryptography.\n");
22+
}
13123

13224
fido_dev_t dev;
133-
13425
if (prepare_stateless_rp_nfc_simulator_device(&dev) != 0) {
135-
return 1;
26+
printf("Could not setup simulator device.\n");
27+
return -1;
13628
}
137-
13829
const uint8_t updater_public_key[] = {0xA8, 0xEE, 0x4D, 0x2B, 0xD5, 0xAE, 0x09, 0x0A, 0xBC, 0xA9, 0x8A, 0x06, 0x6C, 0xA5, 0xB3, 0xA6, 0x22, 0x84, 0x89, 0xF5, 0x9E, 0x30, 0x90, 0x87, 0x65, 0x62, 0xB9, 0x79, 0x8A, 0xE7, 0x05, 0x15};
139-
return stateless_assert(&dev, "example.com", updater_public_key);
30+
clock_start_counting();
31+
const int ret = stateless_assert(&dev, "example.com", updater_public_key);
32+
uint64_t elapsed_cycles = clock_stop_counting();
33+
printf("Elapsed cycles for stateless assertion: %zu\n", elapsed_cycles);
34+
printf("Elapsed nanoseconds for stateless assertion: %zu\n", clock_cyles_to_ns(elapsed_cycles));
35+
return ret;
14036
}

0 commit comments

Comments
 (0)