-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from All-Your-Locks-Are-Belong-To-Us/feature/m…
…easure-esp32 Make measurements compile for ESP32
- Loading branch information
Showing
42 changed files
with
473 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
idf_component_register( | ||
SRCS "clock_cycles.c" | ||
INCLUDE_DIRS "." | ||
PRIV_REQUIRES esp_hw_support esp_rom | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright (c) 2022 Felix Gohla, Konrad Hanff, Tobias Kantusch, | ||
* Quentin Kuth, Felix Roth. All rights reserved. | ||
* | ||
* Use of this source code is governed by a BSD-style | ||
* license that can be found in the LICENSE file. | ||
*/ | ||
|
||
#include <esp_cpu.h> | ||
|
||
// Buffer variable used for measuring elapsed clock cycles. | ||
esp_cpu_cycle_count_t clock_cycle_start; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2022 Felix Gohla, Konrad Hanff, Tobias Kantusch, | ||
* Quentin Kuth, Felix Roth. All rights reserved. | ||
* | ||
* Use of this source code is governed by a BSD-style | ||
* license that can be found in the LICENSE file. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <stdint.h> | ||
#include <esp_cpu.h> | ||
#include <esp_rom_sys.h> | ||
|
||
extern volatile esp_cpu_cycle_count_t clock_cycle_start; | ||
|
||
inline void clock_init() {} | ||
|
||
/** | ||
* @brief Starts counting clock cycles. | ||
* | ||
*/ | ||
static inline void clock_start_counting() { | ||
clock_cycle_start = esp_cpu_get_cycle_count(); | ||
} | ||
|
||
/** | ||
* @brief Stops counting clock cycles and returns the number of elapsed cycles. | ||
* | ||
*/ | ||
static inline uint64_t clock_stop_counting() { | ||
esp_cpu_cycle_count_t end = esp_cpu_get_cycle_count(); | ||
uint64_t val = end - clock_cycle_start; | ||
return val; | ||
} | ||
|
||
/** | ||
* @brief Converts clock cycles to nanoseconds. | ||
* | ||
* @param cycles The number of cycles. | ||
* @return uint64_t The number of nanoseconds. | ||
*/ | ||
static inline uint32_t clock_cyles_to_ns(uint64_t cycles) { | ||
return cycles * 1000 / esp_rom_get_cpu_ticks_per_us(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
idf_component_register( | ||
SRCS "hw_crypto.c" | ||
INCLUDE_DIRS "." | ||
PRIV_REQUIRES libmicrofido2 mbedtls | ||
) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* Copyright (c) 2022 Felix Gohla, Konrad Hanff, Tobias Kantusch, | ||
* Quentin Kuth, Felix Roth. All rights reserved. | ||
* | ||
* Use of this source code is governed by a BSD-style | ||
* license that can be found in the LICENSE file. | ||
*/ | ||
|
||
#include <sdkconfig.h> | ||
|
||
#ifdef CONFIG_USE_HW_CRYPTO | ||
#include "fido.h" | ||
|
||
#include <stdio.h> | ||
#include <mbedtls/aes.h> | ||
#include <mbedtls/gcm.h> | ||
#include <mbedtls/sha256.h> | ||
#include <mbedtls/sha512.h> | ||
|
||
static void sha256(const uint8_t *data, size_t data_len, uint8_t *hash) { | ||
int r = mbedtls_sha256(data, data_len, hash, 0); | ||
if (r != 0) { | ||
printf("sha256 failed with %d\n", r); | ||
} | ||
} | ||
|
||
static void sha512(const uint8_t *data, size_t data_len, uint8_t *hash) { | ||
int r = mbedtls_sha512(data, data_len, hash, 0); | ||
if (r != 0) { | ||
printf("sha512 failed with %d\n", r); | ||
} | ||
} | ||
|
||
static int aes_gcm_encrypt( | ||
const uint8_t *key, size_t key_len, | ||
const uint8_t *iv, size_t iv_len, | ||
const uint8_t *plaintext, size_t plaintext_len, | ||
const uint8_t *aad, size_t aad_len, | ||
uint8_t *ciphertext, uint8_t *tag | ||
) { | ||
mbedtls_gcm_context ctx; | ||
int r; | ||
|
||
mbedtls_gcm_init(&ctx); | ||
|
||
r = mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key, key_len * 8); | ||
if (r != 0) { | ||
printf("[%s] mbedtls_gcm_setkey failed with %d\n", __func__, r); | ||
return r; | ||
} | ||
|
||
r = mbedtls_gcm_crypt_and_tag( | ||
&ctx, | ||
MBEDTLS_ENCRYPT, | ||
plaintext_len, | ||
iv, iv_len, | ||
aad, aad_len, | ||
plaintext, ciphertext, | ||
16, tag | ||
); | ||
if (r != 0) { | ||
printf("[%s] mbedtls_gcm_crypt_and_tag failed with %d\n", __func__, r); | ||
return r; | ||
} | ||
|
||
mbedtls_gcm_free(&ctx); | ||
|
||
return 0; | ||
} | ||
|
||
static int aes_gcm_decrypt( | ||
const uint8_t *key, size_t key_len, | ||
const uint8_t *iv, size_t iv_len, | ||
const uint8_t *ciphertext, size_t ciphertext_len, | ||
const uint8_t *aad, size_t aad_len, | ||
const uint8_t *tag, | ||
uint8_t *plaintext | ||
) { | ||
mbedtls_gcm_context ctx; | ||
int r; | ||
|
||
mbedtls_gcm_init(&ctx); | ||
|
||
r = mbedtls_gcm_setkey(&ctx, MBEDTLS_CIPHER_ID_AES, key, key_len * 8); | ||
if (r != 0) { | ||
printf("[%s] mbedtls_gcm_setkey failed with %d\n", __func__, r); | ||
return r; | ||
} | ||
|
||
r = mbedtls_gcm_auth_decrypt( | ||
&ctx, | ||
ciphertext_len, | ||
iv, iv_len, | ||
aad, aad_len, | ||
tag, 16, | ||
ciphertext, plaintext | ||
); | ||
if (r != 0) { | ||
printf("[%s] mbedtls_gcm_auth_decrypt failed with %d\n", __func__, r); | ||
return r; | ||
} | ||
|
||
mbedtls_gcm_free(&ctx); | ||
|
||
return 0; | ||
} | ||
|
||
int init_hw_crypto() { | ||
fido_sha256 = &sha256; | ||
fido_sha512 = &sha512; | ||
fido_aes_gcm_encrypt = &aes_gcm_encrypt; | ||
fido_aes_gcm_decrypt = &aes_gcm_decrypt; | ||
|
||
return 0; | ||
} | ||
#else | ||
int init_hw_crypto() { | ||
return 0; | ||
} | ||
#endif |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
idf_component_register( | ||
SRCS "esp32-libmicrofido2.c" "stateless_rp/stateless_rp.c" "stateless_rp/stateless_rp_nfc_simulator.c" | ||
INCLUDE_DIRS "." | ||
PRIV_REQUIRES libmicrofido2 mbedtls | ||
PRIV_REQUIRES libmicrofido2 hw_crypto clock | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
examples/measurements/CMakeLists.txt → examples/measurements/atmega/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../common |
Oops, something went wrong.