diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 8d7fc1b..cfd883f 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -13,4 +13,4 @@ target_link_libraries(nfc_simulator ${PRODUCT_NAME}) ####################################### # Test applications -add_subdirectory(measurements) +add_subdirectory(measurements/atmega) diff --git a/examples/esp32/components/clock/CMakeLists.txt b/examples/esp32/components/clock/CMakeLists.txt new file mode 100644 index 0000000..ee15a5b --- /dev/null +++ b/examples/esp32/components/clock/CMakeLists.txt @@ -0,0 +1,5 @@ +idf_component_register( + SRCS "clock_cycles.c" + INCLUDE_DIRS "." + PRIV_REQUIRES esp_hw_support esp_rom +) diff --git a/examples/esp32/components/clock/clock_cycles.c b/examples/esp32/components/clock/clock_cycles.c new file mode 100644 index 0000000..90d12df --- /dev/null +++ b/examples/esp32/components/clock/clock_cycles.c @@ -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 + +// Buffer variable used for measuring elapsed clock cycles. +esp_cpu_cycle_count_t clock_cycle_start; diff --git a/examples/esp32/components/clock/clock_cycles.h b/examples/esp32/components/clock/clock_cycles.h new file mode 100644 index 0000000..af9b983 --- /dev/null +++ b/examples/esp32/components/clock/clock_cycles.h @@ -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 +#include +#include + +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(); +} diff --git a/examples/esp32/components/hw_crypto/CMakeLists.txt b/examples/esp32/components/hw_crypto/CMakeLists.txt new file mode 100644 index 0000000..a242b23 --- /dev/null +++ b/examples/esp32/components/hw_crypto/CMakeLists.txt @@ -0,0 +1,5 @@ +idf_component_register( + SRCS "hw_crypto.c" + INCLUDE_DIRS "." + PRIV_REQUIRES libmicrofido2 mbedtls +) diff --git a/examples/esp32/main/Kconfig.projbuild b/examples/esp32/components/hw_crypto/Kconfig.projbuild similarity index 100% rename from examples/esp32/main/Kconfig.projbuild rename to examples/esp32/components/hw_crypto/Kconfig.projbuild diff --git a/examples/esp32/components/hw_crypto/hw_crypto.c b/examples/esp32/components/hw_crypto/hw_crypto.c new file mode 100644 index 0000000..3956a4c --- /dev/null +++ b/examples/esp32/components/hw_crypto/hw_crypto.c @@ -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 + +#ifdef CONFIG_USE_HW_CRYPTO +#include "fido.h" + +#include +#include +#include +#include +#include + +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 diff --git a/examples/nrf52/src/hw_crypto.h b/examples/esp32/components/hw_crypto/hw_crypto.h similarity index 100% rename from examples/nrf52/src/hw_crypto.h rename to examples/esp32/components/hw_crypto/hw_crypto.h diff --git a/examples/esp32/main/CMakeLists.txt b/examples/esp32/main/CMakeLists.txt index 7f1d753..174d102 100644 --- a/examples/esp32/main/CMakeLists.txt +++ b/examples/esp32/main/CMakeLists.txt @@ -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 ) diff --git a/examples/esp32/main/esp32-libmicrofido2.c b/examples/esp32/main/esp32-libmicrofido2.c index fecf360..1044c4a 100644 --- a/examples/esp32/main/esp32-libmicrofido2.c +++ b/examples/esp32/main/esp32-libmicrofido2.c @@ -6,135 +6,31 @@ * license that can be found in the LICENSE file. */ -#include -#include -#include -#include - -#include - -#ifdef CONFIG_USE_HW_CRYPTO -#include -#include -#include -#include -#endif - -#ifdef CONFIG_USE_HW_CRYPTO -int 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); - } - return r; -} - -int 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); - } - return r; -} - -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, - ciphertext, plaintext, - 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; -} - -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_crypt_and_tag( - &ctx, - MBEDTLS_DECRYPT, - ciphertext_len, - iv, iv_len, - aad, aad_len, - ciphertext, plaintext, - 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; -} - -void init_crypto() { - fido_sha256 = &sha256; - fido_sha512 = &sha512; - fido_aes_gcm_encrypt = &aes_gcm_encrypt; - fido_aes_gcm_decrypt = &aes_gcm_decrypt; -} -#endif - #include +#include +#include "hw_crypto.h" +#include "clock_cycles.h" #include "stateless_rp/stateless_rp.h" #include "stateless_rp/stateless_rp_nfc_simulator.h" int app_main(void) { - #ifdef CONFIG_USE_HW_CRYPTO - init_crypto(); - #endif + clock_init(); + if (init_hw_crypto() != 0) { + return -1; + } else { + printf("Initialized cryptography.\n"); + } fido_dev_t dev; - if (prepare_stateless_rp_nfc_simulator_device(&dev) != 0) { - return 1; + printf("Could not setup simulator device.\n"); + return -1; } - 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}; - return stateless_assert(&dev, "example.com", updater_public_key); + clock_start_counting(); + const int ret = stateless_assert(&dev, "example.com", updater_public_key); + uint64_t elapsed_cycles = clock_stop_counting(); + printf("Elapsed cycles for stateless assertion: %zu\n", elapsed_cycles); + printf("Elapsed nanoseconds for stateless assertion: %zu\n", clock_cyles_to_ns(elapsed_cycles)); + return ret; } diff --git a/examples/measurements/README.md b/examples/measurements/README.md index ec6b881..509d2aa 100644 --- a/examples/measurements/README.md +++ b/examples/measurements/README.md @@ -15,3 +15,12 @@ Then, make sure to select the algorithm you want to measure in the `CMakeLists.t Finally, the build procedure is similar to the one in the [nrf52 example](../nrf52/README.md). To initially build the program, execute: `sudo docker run --rm -v $PWD/../../../:/libmicrofido2 nrf52-sdk west -v build -b nrf52840dk_nrf52840 -d /libmicrofido2/examples/measurements/nrf52/build /libmicrofido2/examples/measurements/nrf52`. To build the program and flash it, execute: `docker run --rm -v $PWD/../../../:/libmicrofido2 --privileged --device=/dev/ttyACM? nrf52-sdk west flash -d /libmicrofido2/examples/measurements/nrf52/build`. + +## Compiling for ESP32 + +The examples must be compiled explicitly for the ESP32. + +First, navigate to the [esp32](./esp32/) folder. +Then, make sure to select the algorithm you want to measure in the `sdkconfig` (or using `idf.py menuconfig`). +Finally, the build procedure is similar to the one in the [esp32 example](../esp32/README.md). +Therefore, to build and flash the program execute the following command from inside the [esp32](./esp32/) folder: `docker run --rm -v $PWD/../../../:/project -w /project/examples/measurements/esp32 --device /dev/ttyUSB0 espressif/idf idf.py flash`. diff --git a/examples/measurements/CMakeLists.txt b/examples/measurements/atmega/CMakeLists.txt similarity index 65% rename from examples/measurements/CMakeLists.txt rename to examples/measurements/atmega/CMakeLists.txt index 34fb92c..6312033 100644 --- a/examples/measurements/CMakeLists.txt +++ b/examples/measurements/atmega/CMakeLists.txt @@ -1,8 +1,8 @@ -include(../../cmake/linker-map.cmake) +include(../../../cmake/linker-map.cmake) function(add_measurement name) - add_executable(${name} "common/${name}.c" common/gpio.c) - target_include_directories(${name} PRIVATE common/) + add_executable(${name} "common/${name}.c" gpio.c hw_crypto.c) + target_include_directories(${name} PRIVATE common/ .) add_linker_map_for_target(${name}) target_link_libraries(${name} ${PRODUCT_NAME}) endfunction() diff --git a/examples/measurements/atmega/common b/examples/measurements/atmega/common new file mode 120000 index 0000000..60d3b0a --- /dev/null +++ b/examples/measurements/atmega/common @@ -0,0 +1 @@ +../common \ No newline at end of file diff --git a/examples/measurements/atmega/gpio.c b/examples/measurements/atmega/gpio.c new file mode 100644 index 0000000..99ac595 --- /dev/null +++ b/examples/measurements/atmega/gpio.c @@ -0,0 +1,30 @@ +/* + * 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 +#include + +#define PIN PB5 + +void setup_pin() { + DDRB |= _BV(PIN); +} + +void pin_on() { + PORTB |= _BV(PIN); +} + +void pin_off() { + PORTB &= ~_BV(PIN); +} + +void delay(double ms) { + for (double i = 0; i < ms; i++) { + _delay_ms(1); + } +} diff --git a/examples/measurements/atmega/hw_crypto.c b/examples/measurements/atmega/hw_crypto.c new file mode 100644 index 0000000..8b3e602 --- /dev/null +++ b/examples/measurements/atmega/hw_crypto.c @@ -0,0 +1,11 @@ +/* + * 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. + */ + +int init_hw_crypto() { + return 0; +} diff --git a/examples/measurements/atmega/hw_crypto.h b/examples/measurements/atmega/hw_crypto.h new file mode 100644 index 0000000..c684d14 --- /dev/null +++ b/examples/measurements/atmega/hw_crypto.h @@ -0,0 +1,14 @@ +/* + * 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. + */ + +/** + * @brief Initialize cryptography module (if hardware cryptography is enabled). + * + * @return int 0 on success. + */ +int init_hw_crypto(); diff --git a/examples/measurements/common/aes_gcm_measure.c b/examples/measurements/common/aes_gcm_measure.c index e999f95..d6a86cc 100644 --- a/examples/measurements/common/aes_gcm_measure.c +++ b/examples/measurements/common/aes_gcm_measure.c @@ -8,6 +8,11 @@ #include "fido.h" #include "gpio.h" +#include "hw_crypto.h" + +#ifdef ESP_PLATFORM + #include "sdkconfig.h" +#endif #define SAMPLES 20 @@ -18,12 +23,18 @@ static const uint8_t nonce[] = { 0xb7, 0x3a, 0x1b, 0x33, 0x1e, 0xe7, 0xef, 0x59, static const uint8_t ciphertext[] = { 0x20, 0x21, 0x93, 0x59, 0x35, 0x51, 0xec, 0xaa, 0x5c, 0x63, 0xe2, 0x31, 0xbb, 0x1b, 0x12, 0x29, 0x1d, 0x12, 0x24, 0xe5, 0x63, 0x28, 0xfd, 0xcd, 0x8a, 0x3a, 0xee, 0x45, 0x9c, 0x22, 0x32, 0xcb, 0xba, 0x34, 0x2e, 0x18, 0xad, 0xe5, 0xd0, 0xfa, 0x8f, 0x4f, 0xfe, 0x3d, 0xfb, 0xe6, 0xd0, 0x84, 0x4f, 0xcd, 0x1d, 0xfd, 0x34, 0xec, 0x75, 0x7f, 0xef, 0x86, 0x71, 0x37, 0x3c, 0x4b, 0xf1, 0x68, 0x64, 0x21, 0x70, 0x65, 0x82, 0x2c, 0xe6, 0xfb, 0x81, 0x55, 0xfa, 0x87, 0x12, 0xd5, 0x43, 0xfa, 0x41, 0x75, 0x36, 0x47, 0xf3, 0xfe, 0x94, 0x8e, 0xb9, 0x6d, 0xa4, 0x9d, 0x84, 0x7e, 0xb0, 0x81, 0x45, 0x19, 0x78, 0xa2, 0x79, 0x91, 0x41, 0x45, 0x08, 0x04, 0x37, 0xf5, 0xa2, 0x1c, 0xdd, 0x24, 0x90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 0xa4, 0xa3, 0xa3, 0x63, 0x49, 0xe5, 0x15, 0x23, 0x0d, 0x55, 0xb0, 0xf2, 0x28, 0xb3, 0xb2, 0x81, 0x47, 0x57, 0xea, 0x66, 0x08, 0x4e, 0xd0, 0x09, 0x1e, 0xf0, 0x21, 0xc2, 0x4f, 0x8b, 0x2b, 0x1f, 0x30, 0x2a, 0x48, 0x9f, 0xd5, 0x55, 0xb9, 0xde, 0x4d, 0xf6, 0xe2, 0x3a, 0xc9, 0x9b, 0xbf, 0xbe, 0xb1, 0xd4, 0x3c, 0x74, 0x09, 0x8d, 0x98, 0xa5, 0x51, 0x98, 0x5f, 0x19, 0x39, 0x82, 0xde, 0x19, 0x97, 0xa2, 0x95, 0x2a, 0x2e, 0x2f, 0x0e, 0xe3, 0x3a, 0xdb, 0x54, 0x37, 0x17, 0x84, 0xba, 0xb5, 0x44, 0xbb, 0xed, 0xfb, 0x57, 0xb4, 0xe6, 0xc4, 0x8d, 0xe4, 0x01, 0xb7, 0x84, 0x5f }; static const uint8_t tag[] = { 0x65, 0xc0, 0xe6, 0x07, 0x14, 0x30, 0x93, 0xde, 0x16, 0xc4, 0xa3, 0x28, 0x59, 0x54, 0x47, 0x09 }; +#ifdef ESP_PLATFORM +int app_main(void) { +#else int main(void) { +#endif // Wait until the microcontroller booted up to remove increased power consumption in the measurements at the beginning. delay(3000); uint8_t plain[576] = {0}; + init_hw_crypto(); + setup_pin(); pin_off(); diff --git a/examples/measurements/common/ed25519_measure.c b/examples/measurements/common/ed25519_measure.c index f27360d..9eb54c7 100644 --- a/examples/measurements/common/ed25519_measure.c +++ b/examples/measurements/common/ed25519_measure.c @@ -8,7 +8,9 @@ #include "fido.h" #include "gpio.h" +#include "hw_crypto.h" +#include #include #define SAMPLES 20 @@ -22,10 +24,16 @@ static const uint8_t public_key[] = { 0x5e, 0x51, 0x78, 0x80, 0xf9, 0x31, 0x4f, static const uint8_t message[] = { 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66 }; static const uint8_t signature[] = { 0x2e, 0x55, 0x1a, 0x75, 0xf6, 0x48, 0x01, 0xc0, 0x1f, 0x56, 0x81, 0x22, 0xb3, 0x7a, 0xc2, 0xf5, 0x43, 0xa7, 0x9a, 0x5e, 0x0a, 0xc0, 0xba, 0x9c, 0xbe, 0xdd, 0xc8, 0x71, 0x95, 0xc6, 0x74, 0x82, 0x11, 0x66, 0xea, 0xaf, 0x17, 0x30, 0xb5, 0xbc, 0xf3, 0xef, 0x2b, 0x2c, 0xa1, 0xf0, 0xe7, 0xa4, 0x0c, 0x8a, 0x9b, 0xad, 0x55, 0x06, 0x28, 0x46, 0x2f, 0xdf, 0x51, 0x56, 0xc7, 0x51, 0x88, 0x08 }; +#ifdef ESP_PLATFORM +int app_main(void) { +#else int main(void) { +#endif // Wait until the microcontroller booted up to remove increased power consumption in the measurements at the beginning. delay(3000); + init_hw_crypto(); + setup_pin(); pin_off(); diff --git a/examples/measurements/common/gpio.h b/examples/measurements/common/gpio.h index 730c665..49c8fd9 100644 --- a/examples/measurements/common/gpio.h +++ b/examples/measurements/common/gpio.h @@ -6,6 +6,8 @@ * license that can be found in the LICENSE file. */ +#pragma once + /** * @brief Setup the logical PPK2 output pin. */ diff --git a/examples/measurements/common/inflate_measure.c b/examples/measurements/common/inflate_measure.c index 2150efa..4e95277 100644 --- a/examples/measurements/common/inflate_measure.c +++ b/examples/measurements/common/inflate_measure.c @@ -17,7 +17,11 @@ static const uint8_t uncompressed[] = { 0x0f, 0x12, 0xdc, 0x0d, 0xd9, 0xab, 0x0e, 0x00, 0x96, 0x34, 0xdc, 0xec, 0xc2, 0xcd, 0x27, 0x38, 0x29, 0x18, 0x6e, 0x0a, 0xcf, 0x90, 0xd6, 0xc2, 0xc2, 0xd1, 0x11, 0x06, 0x9c, 0x68, 0xf3, 0xbd, 0x03, 0xdf, 0xe3, 0x4a, 0x7b, 0x6c, 0x40, 0x96, 0x89, 0x00, 0x11, 0xb5, 0x6f, 0xf4, 0xb9, 0xb1, 0x28, 0x9b, 0xd8, 0xbc, 0x40, 0x5e, 0xd6, 0x3e, 0x98, 0xcd, 0xed, 0x0c, 0xea, 0x0d, 0x8f, 0x7d, 0x31, 0xf6, 0xc8, 0x46, 0x11, 0xef, 0x10, 0x69, 0x36, 0xc5, 0x1e, 0x96, 0x52, 0xf4, 0x58, 0x6d, 0xa8, 0x43, 0xf3, 0x64, 0x04, 0xcb, 0x83, 0x2c, 0x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xd2, 0x21, 0x51, 0x1c, 0x43, 0xaf, 0xf2, 0xa9, 0xf6, 0xa5, 0x47, 0x30, 0x28, 0xc6, 0x24, 0x07, 0xed, 0x5e, 0xff, 0x1f, 0xfa, 0x43, 0x16, 0x10, 0x50, 0xbb, 0xef, 0x24, 0x0a, 0xfb, 0xda, 0xce, 0x05, 0xf7, 0xad, 0x67, 0x54, 0x78, 0xaa, 0xf2, 0xea, 0x5f, 0xf7, 0x4d, 0x80, 0x57, 0xec, 0x82, 0x3f, 0xf3, 0x47, 0x14, 0xf2, 0xe9, 0x56, 0x82, 0x23, 0x18, 0xea, 0xe1, 0xce, 0xed, 0x54, 0x88, 0xda, 0x37, 0x9a, 0x31, 0xa7, 0xc0, 0xd5, 0x75, 0xa6, 0xf7, 0x33, 0x28, 0x82, 0x54, 0x80, 0x6b, 0x1b, 0xcb, 0x35, 0xab, 0x12, 0x9a, 0x25, 0x0c, 0x7e, 0x45, 0x16, 0x62, 0x54, 0x43, 0x09, 0xf0, 0x7b, 0xef, 0x4b, 0xaf, 0xac, 0x88, 0x31, 0x7b, 0xb8, 0xa0, 0x55, 0xa7, 0xff, 0xb5, 0x7a, 0x51, 0x52, 0x5c, 0x9a, 0xdc, 0x1a, 0xd7, 0xf0 }; static const uint8_t source[] = { 0x01, 0x40, 0x02, 0xbf, 0xfd, 0x0f, 0x12, 0xdc, 0x0d, 0xd9, 0xab, 0x0e, 0x00, 0x96, 0x34, 0xdc, 0xec, 0xc2, 0xcd, 0x27, 0x38, 0x29, 0x18, 0x6e, 0x0a, 0xcf, 0x90, 0xd6, 0xc2, 0xc2, 0xd1, 0x11, 0x06, 0x9c, 0x68, 0xf3, 0xbd, 0x03, 0xdf, 0xe3, 0x4a, 0x7b, 0x6c, 0x40, 0x96, 0x89, 0x00, 0x11, 0xb5, 0x6f, 0xf4, 0xb9, 0xb1, 0x28, 0x9b, 0xd8, 0xbc, 0x40, 0x5e, 0xd6, 0x3e, 0x98, 0xcd, 0xed, 0x0c, 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x9e, 0xf9, 0x09, 0xe0, 0xbd, 0xca, 0xcd, 0xa5, 0x84, 0xe8, 0xde, 0xea, 0x11, 0x7d, 0xa6, 0xe2, 0xd8, 0x3f, 0x09, 0x81, 0x4c, 0x32, 0x73, 0xca, 0x6d, 0x87, 0x6b, 0x93, 0xd2, 0x21, 0x51, 0x1c, 0x43, 0xaf, 0xf2, 0xa9, 0xf6, 0xa5, 0x47, 0x30, 0x28, 0xc6, 0x24, 0x07, 0xed, 0x5e, 0xff, 0x1f, 0xfa, 0x43, 0x16, 0x10, 0x50, 0xbb, 0xef, 0x24, 0x0a, 0xfb, 0xda, 0xce, 0x05, 0xf7, 0xad, 0x67, 0x54, 0x78, 0xaa, 0xf2, 0xea, 0x5f, 0xf7, 0x4d, 0x80, 0x57, 0xec, 0x82, 0x3f, 0xf3, 0x47, 0x14, 0xf2, 0xe9, 0x56, 0x82, 0x23, 0x18, 0xea, 0xe1, 0xce, 0xed, 0x54, 0x88, 0xda, 0x37, 0x9a, 0x31, 0xa7, 0xc0, 0xd5, 0x75, 0xa6, 0xf7, 0x33, 0x28, 0x82, 0x54, 0x80, 0x6b, 0x1b, 0xcb, 0x35, 0xab, 0x12, 0x9a, 0x25, 0x0c, 0x7e, 0x45, 0x16, 0x62, 0x54, 0x43, 0x09, 0xf0, 0x7b, 0xef, 0x4b, 0xaf, 0xac, 0x88, 0x31, 0x7b, 0xb8, 0xa0, 0x55, 0xa7, 0xff, 0xb5, 0x7a, 0x51, 0x52, 0x5c, 0x9a, 0xdc, 0x1a, 0xd7, 0xf0 }; +#ifdef ESP_PLATFORM +int app_main(void) { +#else int main(void) { +#endif // Wait until the microcontroller booted up to remove increased power consumption in the measurements at the beginning. delay(3000); diff --git a/examples/measurements/common/sha256_measure.c b/examples/measurements/common/sha256_measure.c index 3976df4..374061d 100644 --- a/examples/measurements/common/sha256_measure.c +++ b/examples/measurements/common/sha256_measure.c @@ -8,6 +8,11 @@ #include "fido.h" #include "gpio.h" +#include "hw_crypto.h" + +#ifdef ESP_PLATFORM + #include "sdkconfig.h" +#endif #define SAMPLES 20 @@ -15,12 +20,18 @@ static const uint8_t data[] = { 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66 }; // static const uint8_t hash[] = { 0xe4, 0xe1, 0x98, 0x53, 0x7a, 0xfb, 0x1f, 0x3b, 0xb9, 0x3f, 0xae, 0xba, 0x3c, 0x07, 0x7f, 0x8a, 0xfa, 0x1a, 0xac, 0xc9, 0x56, 0xac, 0xb6, 0x58, 0x1c, 0xa4, 0x9a, 0x03, 0xcb, 0x23, 0x3b, 0xaf }; +#ifdef ESP_PLATFORM +int app_main(void) { +#else int main(void) { +#endif // Wait until the microcontroller booted up to remove increased power consumption in the measurements at the beginning. delay(3000); uint8_t hash[32] = {0}; + init_hw_crypto(); + setup_pin(); pin_off(); diff --git a/examples/measurements/common/sha512_measure.c b/examples/measurements/common/sha512_measure.c index f0107e5..18fbdc7 100644 --- a/examples/measurements/common/sha512_measure.c +++ b/examples/measurements/common/sha512_measure.c @@ -8,6 +8,11 @@ #include "fido.h" #include "gpio.h" +#include "hw_crypto.h" + +#ifdef ESP_PLATFORM + #include "sdkconfig.h" +#endif #define SAMPLES 20 @@ -15,12 +20,18 @@ static const uint8_t data[] = { 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66 }; // static const uint8_t hash[] = { 0xb5, 0xe3, 0x6d, 0x53, 0xed, 0x80, 0x78, 0xdb, 0xda, 0xfa, 0xde, 0xe3, 0x97, 0x04, 0xbf, 0x09, 0xf9, 0xb6, 0x1e, 0xc5, 0xee, 0xd6, 0xd0, 0x3a, 0xda, 0x60, 0x32, 0x73, 0x62, 0x6a, 0x03, 0xcd, 0x4f, 0x5b, 0x52, 0x16, 0xea, 0x71, 0x87, 0xa6, 0x3f, 0x0a, 0x48, 0xa4, 0x31, 0x42, 0x6b, 0xff, 0xb9, 0xbc, 0x91, 0xeb, 0x3e, 0xf6, 0xe6, 0xa1, 0xd1, 0xab, 0xb8, 0x47, 0x49, 0xba, 0x30, 0x8b }; +#ifdef ESP_PLATFORM +int app_main(void) { +#else int main(void) { +#endif // Wait until the microcontroller booted up to remove increased power consumption in the measurements at the beginning. delay(3000); uint8_t hash[64] = {0}; + init_hw_crypto(); + setup_pin(); pin_off(); diff --git a/examples/measurements/esp32/.gitignore b/examples/measurements/esp32/.gitignore new file mode 100644 index 0000000..899c0e7 --- /dev/null +++ b/examples/measurements/esp32/.gitignore @@ -0,0 +1,3 @@ +# CMake build directory +build/ +sdkconfig diff --git a/examples/measurements/esp32/CMakeLists.txt b/examples/measurements/esp32/CMakeLists.txt new file mode 100644 index 0000000..956ea2e --- /dev/null +++ b/examples/measurements/esp32/CMakeLists.txt @@ -0,0 +1,8 @@ +# For more information about build system see +# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html +# The following five lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.16) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(esp32-measure) diff --git a/examples/measurements/esp32/components/clock b/examples/measurements/esp32/components/clock new file mode 120000 index 0000000..2af0457 --- /dev/null +++ b/examples/measurements/esp32/components/clock @@ -0,0 +1 @@ +../../../esp32/components/clock \ No newline at end of file diff --git a/examples/measurements/esp32/components/hw_crypto b/examples/measurements/esp32/components/hw_crypto new file mode 120000 index 0000000..6f5fb9d --- /dev/null +++ b/examples/measurements/esp32/components/hw_crypto @@ -0,0 +1 @@ +../../../esp32/components/hw_crypto \ No newline at end of file diff --git a/examples/measurements/esp32/components/libmicrofido2 b/examples/measurements/esp32/components/libmicrofido2 new file mode 120000 index 0000000..11a54ed --- /dev/null +++ b/examples/measurements/esp32/components/libmicrofido2 @@ -0,0 +1 @@ +../../../../ \ No newline at end of file diff --git a/examples/measurements/esp32/main/CMakeLists.txt b/examples/measurements/esp32/main/CMakeLists.txt new file mode 100644 index 0000000..01249c7 --- /dev/null +++ b/examples/measurements/esp32/main/CMakeLists.txt @@ -0,0 +1,5 @@ +idf_component_register( + SRCS "src/${CONFIG_MEASURE_ALGORITHM}_measure.c" "gpio.c" + INCLUDE_DIRS "." + PRIV_REQUIRES libmicrofido2 clock hw_crypto mbedtls driver +) diff --git a/examples/measurements/esp32/main/Kconfig.projbuild b/examples/measurements/esp32/main/Kconfig.projbuild new file mode 100644 index 0000000..39414ed --- /dev/null +++ b/examples/measurements/esp32/main/Kconfig.projbuild @@ -0,0 +1,17 @@ +config MEASURE_ALGORITHM + string "Algorithm to measure (aes_gcm, ed25519, inflate, sha256, sha512)" + default n + help + Decides which of the algorithms aes_gcm, ed25519, inflate, sha256, sha512 should be measured. + +config LOG_CYCLE_COUNT + bool "Log the amount of CPU cycles it took to Serial" + default n + help + Decides, whether the amount of CPU cycles the cryptographic/hash operation took should be measured and printed to Serial out. + +config USE_HW_CRYPTO + bool "Use hardware acceleration for cryptography and hashing" + default n + help + Decides whether FIDO operations will use hardware acceleration for cryptography and hashing (if possible) diff --git a/examples/measurements/esp32/main/gpio.c b/examples/measurements/esp32/main/gpio.c new file mode 100644 index 0000000..c20b93c --- /dev/null +++ b/examples/measurements/esp32/main/gpio.c @@ -0,0 +1,72 @@ +/* + * 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 +#include +#include +#include +#include +#include + +#define PIN 7 +#define LED_PIN 19 +#define LED_PIN_MASK (1ULL << LED_PIN) +#define PIN_MASK (1ULL << PIN) + +#ifdef CONFIG_LOG_CYCLE_COUNT +esp_cpu_cycle_count_t clock_cycle_start; +#endif + +void setup_pin() { + // zero-initialize the config structure. + gpio_config_t io_conf = { + .intr_type = GPIO_INTR_DISABLE, + .mode = GPIO_MODE_OUTPUT, + // set pin mask to only configure these specific pins + .pin_bit_mask = PIN_MASK, + .pull_down_en = 0, + .pull_up_en = 0 + }; + gpio_config(&io_conf); + + // Disable on-board LED. Apparently, this seems to increase power consumption, so we leave it on. + // gpio_config_t led_io_conf = { + // .intr_type = GPIO_INTR_DISABLE, + // .mode = GPIO_MODE_OUTPUT, + // // set pin mask to only configure these specific pins + // .pin_bit_mask = LED_PIN_MASK, + // .pull_down_en = 0, + // .pull_up_en = 0 + // }; + // gpio_config(&led_io_conf); + // gpio_set_level(LED_PIN, 0); +} + +void pin_on() { + gpio_set_level(PIN, 1); + #ifdef CONFIG_LOG_CYCLE_COUNT + clock_cycle_start = esp_cpu_get_cycle_count(); + #endif + // We could disable the FreeRTOS interrupts for every run, but that seems to have no effect on the overall performance. + // taskDISABLE_INTERRUPTS(); +} + +void pin_off() { + #ifdef CONFIG_LOG_CYCLE_COUNT + esp_cpu_cycle_count_t end = esp_cpu_get_cycle_count(); + uint64_t took_cycles = end - clock_cycle_start; + printf("took %lld cycles\n", took_cycles); + #endif + + // taskENABLE_INTERRUPTS(); + gpio_set_level(PIN, 0); +} + +void delay(double ms) { + vTaskDelay(ms / portTICK_PERIOD_MS); +} diff --git a/examples/measurements/esp32/main/src b/examples/measurements/esp32/main/src new file mode 120000 index 0000000..248927d --- /dev/null +++ b/examples/measurements/esp32/main/src @@ -0,0 +1 @@ +../../common/ \ No newline at end of file diff --git a/examples/measurements/esp32/sdkconfig.defaults b/examples/measurements/esp32/sdkconfig.defaults new file mode 100644 index 0000000..8c37597 --- /dev/null +++ b/examples/measurements/esp32/sdkconfig.defaults @@ -0,0 +1,15 @@ +# Increase stack size to not make it crash +CONFIG_ESP_MAIN_TASK_STACK_SIZE=10240 + +# Set target to ESP32-C3 +CONFIG_IDF_TARGET="esp32c3" +CONFIG_IDF_TARGET_ESP32C3=y + +# Disable interrupt watchdog for second loop +CONFIG_ESP_INT_WDT=n + +# Algorithm to measure +CONFIG_MEASURE_ALGORITHM="ed25519" +CONFIG_USE_HW_CRYPTO=n + +CONFIG_LOG_CYCLE_COUNT=n diff --git a/examples/measurements/nrf52/CMakeLists.txt b/examples/measurements/nrf52/CMakeLists.txt index fff0372..916058f 100644 --- a/examples/measurements/nrf52/CMakeLists.txt +++ b/examples/measurements/nrf52/CMakeLists.txt @@ -8,8 +8,8 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(nrf52-libmicrofido C) -target_sources(app PRIVATE "src/${measure_algorithm}_measure.c" src/gpio.c) -target_include_directories(app PRIVATE src/) +target_sources(app PRIVATE "src/${measure_algorithm}_measure.c" gpio/gpio.c hw_crypto/hw_crypto.c) +target_include_directories(app PRIVATE src/ hw_crypto/ clock/ gpio/) ####################################### # libmicrofido2 diff --git a/examples/measurements/nrf52/clock b/examples/measurements/nrf52/clock new file mode 120000 index 0000000..67b7f27 --- /dev/null +++ b/examples/measurements/nrf52/clock @@ -0,0 +1 @@ +../../nrf52/clock \ No newline at end of file diff --git a/examples/measurements/common/gpio.c b/examples/measurements/nrf52/gpio/gpio.c similarity index 53% rename from examples/measurements/common/gpio.c rename to examples/measurements/nrf52/gpio/gpio.c index 1d4ba8a..7193c6b 100644 --- a/examples/measurements/common/gpio.c +++ b/examples/measurements/nrf52/gpio/gpio.c @@ -6,33 +6,6 @@ * license that can be found in the LICENSE file. */ -#if defined(__AVR__) - -#include -#include - -#define PIN PB5 - -void setup_pin() { - DDRB |= _BV(PIN); -} - -void pin_on() { - PORTB |= _BV(PIN); -} - -void pin_off() { - PORTB &= ~_BV(PIN); -} - -void delay(double ms) { - for (double i = 0; i < ms; i++) { - _delay_ms(1); - } -} - -#elif defined(__ZEPHYR__) - #include #include #include @@ -55,12 +28,3 @@ void pin_off() { void delay(double ms) { k_sleep(K_MSEC(ms)); } - -#else -// To make the examples compile, default to NOOP. -#warning GPIO does not work on this system, defaulting to nop -void setup_pin() {} -void pin_on() {} -void pin_off() {} -void delay(double ms) {} -#endif diff --git a/examples/measurements/nrf52/hw_crypto b/examples/measurements/nrf52/hw_crypto new file mode 120000 index 0000000..f0e509d --- /dev/null +++ b/examples/measurements/nrf52/hw_crypto @@ -0,0 +1 @@ +../../nrf52/hw_crypto \ No newline at end of file diff --git a/examples/nrf52/CMakeLists.txt b/examples/nrf52/CMakeLists.txt index 3436046..7ab55e4 100644 --- a/examples/nrf52/CMakeLists.txt +++ b/examples/nrf52/CMakeLists.txt @@ -12,8 +12,8 @@ endif() include_directories(${CMAKE_CURRENT_SOURCE_DIR}) target_sources(app PRIVATE src/main.c - src/clock_cycles.c - src/hw_crypto.c + clock/clock_cycles.c + hw_crypto/hw_crypto.c stateless_rp/stateless_rp.c stateless_rp/stateless_rp_nfc_simulator.c ) diff --git a/examples/nrf52/src/clock_cycles.c b/examples/nrf52/clock/clock_cycles.c similarity index 100% rename from examples/nrf52/src/clock_cycles.c rename to examples/nrf52/clock/clock_cycles.c diff --git a/examples/nrf52/src/clock_cycles.h b/examples/nrf52/clock/clock_cycles.h similarity index 100% rename from examples/nrf52/src/clock_cycles.h rename to examples/nrf52/clock/clock_cycles.h diff --git a/examples/nrf52/src/hw_crypto.c b/examples/nrf52/hw_crypto/hw_crypto.c similarity index 97% rename from examples/nrf52/src/hw_crypto.c rename to examples/nrf52/hw_crypto/hw_crypto.c index 4f5f8c5..e8371e0 100644 --- a/examples/nrf52/src/hw_crypto.c +++ b/examples/nrf52/hw_crypto/hw_crypto.c @@ -19,7 +19,7 @@ #ifdef USE_HW_CRYPTO -void sha256(const uint8_t *data, size_t data_len, uint8_t *hash) { +static void sha256(const uint8_t *data, size_t data_len, uint8_t *hash) { size_t olen; // We actually do not do anything with this parameter, but the API requires it. psa_status_t status = psa_hash_compute( PSA_ALG_SHA_256, @@ -32,7 +32,7 @@ void sha256(const uint8_t *data, size_t data_len, uint8_t *hash) { assert(status == PSA_SUCCESS); } -void sha512(const uint8_t *data, size_t data_len, uint8_t *hash) { +static void sha512(const uint8_t *data, size_t data_len, uint8_t *hash) { size_t olen; // We actually do not do anything with this parameter, but the API requires it. psa_status_t status = psa_hash_compute( PSA_ALG_SHA_512, @@ -45,7 +45,7 @@ void sha512(const uint8_t *data, size_t data_len, uint8_t *hash) { assert(status == PSA_SUCCESS); } -int aes_gcm_encrypt( +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, @@ -111,7 +111,7 @@ int aes_gcm_encrypt( return ret; } -int aes_gcm_decrypt( +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, @@ -187,7 +187,7 @@ int aes_gcm_decrypt( return ret; } -void ed25519_sign( +static void ed25519_sign( uint8_t *signature, const uint8_t *secret_key, const uint8_t *message, @@ -243,7 +243,7 @@ void ed25519_sign( assert(ret == 0); } -int ed25519_verify( +static int ed25519_verify( const uint8_t *signature, const uint8_t *public_key, const uint8_t *message, diff --git a/examples/nrf52/hw_crypto/hw_crypto.h b/examples/nrf52/hw_crypto/hw_crypto.h new file mode 100644 index 0000000..c684d14 --- /dev/null +++ b/examples/nrf52/hw_crypto/hw_crypto.h @@ -0,0 +1,14 @@ +/* + * 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. + */ + +/** + * @brief Initialize cryptography module (if hardware cryptography is enabled). + * + * @return int 0 on success. + */ +int init_hw_crypto(); diff --git a/examples/nrf52/src/main.c b/examples/nrf52/src/main.c index 32f0567..16eaa8a 100644 --- a/examples/nrf52/src/main.c +++ b/examples/nrf52/src/main.c @@ -13,8 +13,8 @@ #include -#include "clock_cycles.h" -#include "hw_crypto.h" +#include "clock/clock_cycles.h" +#include "hw_crypto/hw_crypto.h" #include "stateless_rp/stateless_rp_nfc_simulator.h" #include "stateless_rp/stateless_rp.h"