Skip to content

Commit

Permalink
Merge pull request #7505 from gojimmypi/PR-Apple-Homekit-SRP-fix
Browse files Browse the repository at this point in the history
Espressif updates to fix Apple Homekit SHA / SRP
  • Loading branch information
dgarske authored May 14, 2024
2 parents 1c44798 + 44ec470 commit 1ee315b
Show file tree
Hide file tree
Showing 10 changed files with 800 additions and 512 deletions.
83 changes: 59 additions & 24 deletions IDE/Espressif/ESP-IDF/examples/wolfssl_test/testAll.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,88 @@
# Run shell check to ensure this a good script.
shellcheck "$0"

if [[ "$PATH" == *"rtos-sdk"* ]]; then
echo "Error. Detected rtos-sdk in path."
echo "Need to start with clean path (no prior idf.py setup) "
exit 1
fi

# Save the current PATH to a temporary variable
ORIGINAL_PATH="$PATH"
echo "ORIGINAL_PATH=$PATH"

export ESPIDF_PUTTY_MONITOR="TRUE"

THIS_SUFFIX="$1"

# Clear IDF path to ensure it is set by export.sh
IDF_PATH=

# set the path for this workspace IDF path (where export.sh is located)
WRK_IDF_PATH=/mnt/c/SysGCC/esp32/esp-idf/v5.2
echo "Run ESP32 export.sh from ${WRK_IDF_PATH}"
#******************************************************************************
# ESP8266 uses rtos-sdk/v3.4 toolchain. Test this first, as it is slowest.
WRK_IDF_PATH=/mnt/c/SysGCC/esp8266/rtos-sdk/v3.4
#******************************************************************************

# Clear ESP-IDF environment variables to ensure clean start for export.sh
unset ESP_IDF_VERSION
unset ESP_ROM_ELF_DIR
unset IDF_DEACTIVATE_FILE_PATH
unset IDF_PATH
unset IDF_PYTHON_ENV_PATH
unset IDF_TOOLS_EXPORT_CMD
unset IDF_TOOLS_INSTALL_CMD
unset OPENOCD_SCRIPTS

echo "Run ESP8266 export.sh from ${WRK_IDF_PATH}"

# shell check should not follow into the ESP-IDF export.sh
# shellcheck disable=SC1091
. "${WRK_IDF_PATH}"/export.sh

echo "IDF_PATH = $IDF_PATH"
. "$WRK_IDF_PATH"/export.sh

./testMonitor.sh wolfssl_test esp32 "$THIS_SUFFIX" || exit 1
./testMonitor.sh wolfssl_test esp32c2 "$THIS_SUFFIX" || exit 1
./testMonitor.sh wolfssl_test esp32c3 "$THIS_SUFFIX" || exit 1
./testMonitor.sh wolfssl_test esp32c6 "$THIS_SUFFIX" || exit 1
./testMonitor.sh wolfssl_test esp32s2 "$THIS_SUFFIX" || exit 1
./testMonitor.sh wolfssl_test esp32s3 "$THIS_SUFFIX" || exit 1
./testMonitor.sh wolfssl_test esp32h2 "$THIS_SUFFIX" || exit 1
./testMonitor.sh wolfssl_test esp8684 "$THIS_SUFFIX" || exit 1
# Tensilica
./testMonitor.sh wolfssl_test esp8266 "$THIS_SUFFIX" || exit 1 # 2715073

# ESP8266 uses a different toolchain

#******************************************************************************
# ESP32[-N] uses esp-idf/v5.2 toolchain
WRK_IDF_PATH=/mnt/c/SysGCC/esp32/esp-idf/v5.2
#******************************************************************************
# Restore the original PATH
export PATH=$ORIGINAL_PATH
export PATH="$ORIGINAL_PATH"

# Clear ESP-IDF environment variables to ensure clean start
unset ESP_IDF_VERSION
unset ESP_ROM_ELF_DIR
unset IDF_DEACTIVATE_FILE_PATH
unset IDF_PATH
unset IDF_PYTHON_ENV_PATH
unset IDF_TOOLS_EXPORT_CMD
unset IDF_TOOLS_INSTALL_CMD
unset OPENOCD_SCRIPTS

IDF_PATH=
WRK_IDF_PATH=/mnt/c/SysGCC/esp8266/rtos-sdk/v3.4
echo "Run ESP8266 export.sh from ${WRK_IDF_PATH}"
echo "Run ESP32 export.sh from ${WRK_IDF_PATH}"

# shell check should not follow into the ESP-IDF export.sh
# shellcheck disable=SC1091
. "$WRK_IDF_PATH"/export.sh

echo "IDF_PATH = $IDF_PATH"
# Comment numeric values are recently observed runtime durations.
# Different tests may be enabled for each device.
# This list is not indicative of relative performance.

# Limited hardware acceleration, test slowest first:
./testMonitor.sh wolfssl_test esp32h2 "$THIS_SUFFIX" || exit 1 # 1424084 esp32h2 COM31" ok
./testMonitor.sh wolfssl_test esp8684 "$THIS_SUFFIX" || exit 1 # 1065290 esp8684 COM49" ok

# RISC-V
./testMonitor.sh wolfssl_test esp32c2 "$THIS_SUFFIX" || exit 1 # 1133856 esp32c2 COM79" ok
./testMonitor.sh wolfssl_test esp32c3 "$THIS_SUFFIX" || exit 1 # 344677 esp32c3 COM35" NT
./testMonitor.sh wolfssl_test esp32c6 "$THIS_SUFFIX" || exit 1 # 346393 esp32c6 COM36" ok

./testMonitor.sh wolfssl_test esp8266 PR || exit 1
# Xtensa
./testMonitor.sh wolfssl_test esp32 "$THIS_SUFFIX" || exit 1 # 259093 esp32 COM9" NT
./testMonitor.sh wolfssl_test esp32s2 "$THIS_SUFFIX" || exit 1 # 305004 esp32s2 COM30" NT
./testMonitor.sh wolfssl_test esp32s3 "$THIS_SUFFIX" || exit 1 # 267518 esp32s3 COM24" NT

# Restore the original PATH
export PATH=$ORIGINAL_PATH
export PATH="$ORIGINAL_PATH"

echo "Done!"
7 changes: 5 additions & 2 deletions IDE/Espressif/ESP-IDF/examples/wolfssl_test/testMonitor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ BUILD_LOG="${THIS_HOME_DIR}/logs/${THIS_EXAMPLE}_build_IDF_v5.1_${THIS_TARGET}_$
FLASH_LOG="${THIS_HOME_DIR}/logs/${THIS_EXAMPLE}_flash_IDF_v5.1_${THIS_TARGET}_${THIS_KEYWORD}.txt"
THIS_LOG="${THIS_HOME_DIR}/logs/${THIS_EXAMPLE}_output_IDF_v5.1_${THIS_TARGET}_${THIS_KEYWORD}.txt"
THIS_CFG="${THIS_HOME_DIR}/logs/${THIS_EXAMPLE}_user_settings_IDF_v5.1_${THIS_TARGET}_${THIS_KEYWORD}.txt"

THIS_WLOG="logs\\${THIS_TARGET}_output.log"
# cp ./components/wolfssl/include/user_settings.h "${THIS_CFG}"

echo "BUILD_LOG = ${BUILD_LOG}"
Expand Down Expand Up @@ -180,6 +180,7 @@ else
THIS_ERROR_CODE=$?
if [ $THIS_ERROR_CODE -ne 0 ]; then
echo ""
tail -n 5 "${BUILD_LOG}"
echo "Error during set-target"
exit 1
fi
Expand All @@ -193,6 +194,7 @@ idf.py build >> "${BUILD_LOG}" 2>&1
THIS_ERROR_CODE=$?
if [ $THIS_ERROR_CODE -ne 0 ]; then
echo ""
tail -n 5 "${BUILD_LOG}"
echo "Error during build for $THIS_TARGET"
echo ""
echo ""
Expand All @@ -207,6 +209,7 @@ idf.py flash -p "${THIS_TARGET_PORT}" -b 115200 2>&1 | tee -a "${FLASH_LOG}"
THIS_ERROR_CODE=$?
if [ $THIS_ERROR_CODE -ne 0 ]; then
echo ""
tail -n 5 "${FLASH_LOG}"
echo "Error during flash"
exit 1
fi
Expand All @@ -223,5 +226,5 @@ if [ -z "$ESPIDF_PUTTY_MONITOR" ]; then
else
echo "Calling putty..."
echo "$PUTTY_EXE -load \"$THIS_TARGET_PUTTY\""
$PUTTY_EXE -load "$THIS_TARGET_PUTTY" &
$PUTTY_EXE -load "$THIS_TARGET_PUTTY" -logoverwrite -sessionlog "${THIS_WLOG}" &
fi
13 changes: 11 additions & 2 deletions wolfcrypt/src/port/Espressif/esp32_aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ static const char* TAG = "wolf_hw_aes";
/* mutex */
static wolfSSL_Mutex aes_mutex;

/* Maximum time to wait for AES HW in FreeRTOS ticks */
#define WOLFSSL_AES_MUTEX_WAIT 5000

/* keep track as to whether esp aes is initialized */
static int espaes_CryptHwMutexInit = 0;

Expand Down Expand Up @@ -86,7 +89,13 @@ static int esp_aes_hw_InUse(void)
* of esp_CryptHwMutexLock(&aes_mutex ...) in code */
/* TODO - do we really want to wait?
* probably not */
ret = esp_CryptHwMutexLock(&aes_mutex, portMAX_DELAY);
ret = esp_CryptHwMutexLock(&aes_mutex, WOLFSSL_AES_MUTEX_WAIT);
if (ret == ESP_OK) {
ESP_LOGV(TAG, "esp_CryptHwMutexLock aes success");
}
else {
ESP_LOGW(TAG, "esp_CryptHwMutexLock aes timeout! %d", ret);
}
}
else {
ESP_LOGE(TAG, "aes engine lock failed.");
Expand Down Expand Up @@ -597,9 +606,9 @@ int wc_esp32AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)

offset += AES_BLOCK_SIZE;
} /* while (blocks--) */
esp_aes_hw_Leave();
} /* if Set Mode was successful (ret == ESP_OK) */

esp_aes_hw_Leave();
ESP_LOGV(TAG, "leave wc_esp32AesCbcDecrypt");
return ret;
} /* wc_esp32AesCbcDecrypt */
Expand Down
23 changes: 16 additions & 7 deletions wolfcrypt/src/port/Espressif/esp32_mp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1839,8 +1839,12 @@ int esp_mp_mulmod(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* M, MATH_INT_T* Z)
/* do we have an even moduli? */
if ((M->dp[0] & 1) == 0) {
#ifndef NO_ESP_MP_MUL_EVEN_ALT_CALC
/* Z = X * Y mod M in mixed HW & SW*/
/* Z = X * Y mod M in mixed HW & SW */
#if defined(NO_WOLFSSL_ESP32_CRYPT_RSA_PRI_MP_MUL)
ret = mp_mul(X, Y, tmpZ); /* SW X * Y */
#else
ret = esp_mp_mul(X, Y, tmpZ); /* HW X * Y */
#endif
if (ret == MP_OKAY) {
/* z = tmpZ mod M, 0 <= Z < M */
ret = mp_mod(tmpZ, M, Z); /* SW mod M */
Expand Down Expand Up @@ -1940,7 +1944,6 @@ int esp_mp_mulmod(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* M, MATH_INT_T* Z)

/* lock HW for use, enable peripheral clock */
if (ret == MP_OKAY) {
mulmod_lock_called = TRUE; /* Don't try to unlock unless we locked */
#ifdef WOLFSSL_HW_METRICS
{
/* Only track max values when using HW */
Expand All @@ -1954,6 +1957,12 @@ int esp_mp_mulmod(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* M, MATH_INT_T* Z)
#endif

ret = esp_mp_hw_lock();
if (ret == ESP_OK) {
mulmod_lock_called = TRUE; /* Don't try to unlock unless locked */
}
else {
ret = WC_HW_WAIT_E;
}
}

#if defined(CONFIG_IDF_TARGET_ESP32)
Expand Down Expand Up @@ -2441,14 +2450,14 @@ int esp_mp_mulmod(MATH_INT_T* X, MATH_INT_T* Y, MATH_INT_T* M, MATH_INT_T* Z)
esp_mp_mulmod_usage_ct);
ESP_LOGI(TAG, "esp_mp_mulmod_error_ct = %lu failures",
esp_mp_mulmod_error_ct);
ESP_LOGI(TAG, WOLFSSL_ESPIDF_BLANKLINE_MESSAGE);
ESP_LOGI(TAG, WOLFSSL_ESPIDF_BLANKLINE_MESSAGE);
esp_show_mp("HW Z", Z); /* this is the HW result */
esp_show_mp("SW Z2", Z2); /* this is the SW result */
ESP_LOGI(TAG, "esp_mp_mulmod_usage_ct = %lu tries",
esp_mp_mulmod_usage_ct);
ESP_LOGI(TAG, "esp_mp_mulmod_error_ct = %lu failures",
esp_mp_mulmod_error_ct);
ESP_LOGI(TAG, WOLFSSL_ESPIDF_BLANKLINE_MESSAGE);
ESP_LOGI(TAG, WOLFSSL_ESPIDF_BLANKLINE_MESSAGE);


#ifndef NO_RECOVER_SOFTWARE_CALC
Expand Down Expand Up @@ -2991,7 +3000,7 @@ int esp_hw_show_mp_metrics(void)
"NO_WOLFSSL_ESP32_CRYPT_RSA_PRI_MP_MUL");
#else
/* Metrics: esp_mp_mul() */
ESP_LOGI(TAG, WOLFSSL_ESPIDF_BLANKLINE_MESSAGE); /* mul follows */
ESP_LOGI(TAG, WOLFSSL_ESPIDF_BLANKLINE_MESSAGE); /* mul follows */
ESP_LOGI(TAG, "esp_mp_mul HW acceleration enabled.");
ESP_LOGI(TAG, "Number of calls to esp_mp_mul: %lu",
esp_mp_mul_usage_ct);
Expand All @@ -3010,7 +3019,7 @@ int esp_hw_show_mp_metrics(void)
"NO_WOLFSSL_ESP32_CRYPT_RSA_PRI_MULMOD");
#else
/* Metrics: esp_mp_mulmod() */
ESP_LOGI(TAG, WOLFSSL_ESPIDF_BLANKLINE_MESSAGE); /* mulmod follows */
ESP_LOGI(TAG, WOLFSSL_ESPIDF_BLANKLINE_MESSAGE); /* mulmod follows */

ESP_LOGI(TAG, "esp_mp_mulmod HW acceleration enabled.");
/* Metrics: esp_mp_mulmod() */
Expand Down Expand Up @@ -3052,7 +3061,7 @@ int esp_hw_show_mp_metrics(void)
"NO_WOLFSSL_ESP32_CRYPT_RSA_PRI_EXPTMOD");
#else
/* Metrics: sp_mp_exptmod() */
ESP_LOGI(TAG, WOLFSSL_ESPIDF_BLANKLINE_MESSAGE); /* exptmod follows */
ESP_LOGI(TAG, WOLFSSL_ESPIDF_BLANKLINE_MESSAGE); /* exptmod follows */

ESP_LOGI(TAG, "Number of calls to esp_mp_exptmod: %lu",
esp_mp_exptmod_usage_ct);
Expand Down
Loading

0 comments on commit 1ee315b

Please sign in to comment.