From 62cf2e8e9f62db18514da3392a04cd842515990b Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 27 Mar 2020 16:35:23 +0100 Subject: [PATCH 001/120] Switch all.sh to bash This will let us use bash features that are not found in some other sh implementations, such as DEBUG and ERR traps, "set -o pipefail", etc. Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index f8e43c871441..b6f39e96fa6f 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -1,4 +1,4 @@ -#! /usr/bin/env sh +#! /usr/bin/env bash # all.sh # @@ -175,8 +175,8 @@ pre_initialize_variables () { # Gather the list of available components. These are the functions # defined in this script whose name starts with "component_". - # Parse the script with sed, because in sh there is no way to list - # defined functions. + # Parse the script with sed. This way we get the functions in the order + # they are defined. ALL_COMPONENTS=$(sed -n 's/^ *component_\([0-9A-Z_a-z]*\) *().*/\1/p' <"$0") # Exclude components that are not supported on this platform. From 5d99682a8c701cff581ee3f33e4e7fbb541010f3 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Sat, 28 Mar 2020 21:09:21 +0100 Subject: [PATCH 002/120] Add --error-test option to test error detection and reporting Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index b6f39e96fa6f..20a20a3151ed 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -231,6 +231,8 @@ General options: Prefix for a cross-compiler for arm-none-eabi (default: "${ARM_NONE_EABI_GCC_PREFIX}") --armcc Run ARM Compiler builds (on by default). + --error-test Error test mode: run a failing function in addition + to any specified component. --except Exclude the COMPONENTs listed on the command line, instead of running only those. --no-append-outcome Write a new outcome file and analyze it (default). @@ -378,6 +380,7 @@ check_headers_in_cpp () { pre_parse_command_line () { COMMAND_LINE_COMPONENTS= all_except=0 + error_test=0 no_armcc= # Note that legacy options are ignored instead of being omitted from this @@ -390,6 +393,7 @@ pre_parse_command_line () { --armcc) no_armcc=;; --armc5-bin-dir) shift; ARMC5_BIN_DIR="$1";; --armc6-bin-dir) shift; ARMC6_BIN_DIR="$1";; + --error-test) error_test=$((error_test + 1));; --except) all_except=1;; --force|-f) FORCE=1;; --gnutls-cli) shift; GNUTLS_CLI="$1";; @@ -2636,6 +2640,19 @@ post_report () { #### Run all the things ################################################################ +# Function invoked by --error-test to test error reporting. +pseudo_component_error_test () { + msg "Testing error reporting $error_test" + if [ $KEEP_GOING -ne 0 ]; then + echo "Expect three failing commands." + fi + error_test='this should not be used since the component runs in a subshell' + grep non_existent /dev/null + not grep -q . "$0" + make unknown_target + false "this should not be executed" +} + # Run one component and clean up afterwards. run_component () { # Back up the configuration in case the component modifies it. @@ -2685,6 +2702,10 @@ cleanup pre_generate_files # Run the requested tests. +while [ $error_test -gt 0 ]; do + run_component pseudo_component_error_test + error_test=$((error_test - 1)) +done for component in $RUN_COMPONENTS; do run_component "component_$component" done From ce266c48bb8214a061c906392b8fbcdf53ac6617 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Sat, 28 Mar 2020 18:50:43 +0100 Subject: [PATCH 003/120] Run each component in a subshell and handle errors more robustly This commit completely rewrites keep-going mode. Instead of relying solely on "set -e", which has some subtle limitations (such as being off anywhere inside a conditional), use an ERR trap to record errors. Run each component in a subshell. This way a component can set environment variables, change the current directory, etc., without affecting other components. Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 149 ++++++++++++++++++++++++++----------------- 1 file changed, 92 insertions(+), 57 deletions(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 20a20a3151ed..f3494bdd97ba 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -59,6 +59,15 @@ # This script must be invoked from the toplevel directory of a git # working copy of Mbed TLS. # +# The behavior on an error depends on whether --keep-going (alias -k) +# is in effect. +# * Without --keep-going: the script stops on the first error without +# cleaning up. This lets you work in the configuration of the failing +# component. +# * With --keep-going: the script runs all requested components and +# reports failures at the end. In particular the script always cleans +# up on exit. +# # Note that the output is not saved. You may want to run # script -c tests/scripts/all.sh # or @@ -81,6 +90,9 @@ # # Each component must start by invoking `msg` with a short informative message. # +# Each component is executed in a separate shell process. The component +# fails if any command in it returns a non-zero status. +# # The framework performs some cleanup tasks after each component. This # means that components can assume that the working directory is in a # cleaned-up state, and don't need to perform the cleanup themselves. @@ -91,19 +103,6 @@ # `tests/Makefile` and `programs/fuzz/Makefile` from git. # This cleans up after an in-tree use of CMake. # -# Any command that is expected to fail must be protected so that the -# script keeps running in --keep-going mode despite `set -e`. In keep-going -# mode, if a protected command fails, this is logged as a failure and the -# script will exit with a failure status once it has run all components. -# Commands can be protected in any of the following ways: -# * `make` is a function which runs the `make` command with protection. -# Note that you must write `make VAR=value`, not `VAR=value make`, -# because the `VAR=value make` syntax doesn't work with functions. -# * Put `report_status` before the command to protect it. -# * Put `if_build_successful` before a command. This protects it, and -# additionally skips it if a prior invocation of `make` in the same -# component failed. -# # The tests are roughly in order from fastest to slowest. This doesn't # have to be exact, but in general you should add slower tests towards # the end and fast checks near the beginning. @@ -477,8 +476,9 @@ pre_check_git () { } pre_setup_keep_going () { - failure_summary= - failure_count=0 + failure_count=0 # Number of failed components + last_failure_status=0 # Last failure status in this component + start_red= end_color= if [ -t 1 ]; then @@ -489,57 +489,73 @@ pre_setup_keep_going () { ;; esac fi - record_status () { - if "$@"; then - last_status=0 - else - last_status=$? - text="$current_section: $* -> $last_status" - failure_summary="$failure_summary -$text" - failure_count=$((failure_count + 1)) - echo "${start_red}^^^^$text^^^^${end_color}" >&2 - fi - } - make () { - case "$*" in - *test|*check) - if [ $build_status -eq 0 ]; then - record_status command make "$@" - else - echo "(skipped because the build failed)" - fi - ;; - *) - record_status command make "$@" - build_status=$last_status - ;; + + # Keep a summary of failures in a file. We'll print it out at the end. + failure_summary_file=$PWD/all-sh-failures-$$.log + : >"$failure_summary_file" + + # Whether it makes sense to keep a component going after the specified + # command fails (test command) or not (configure or build). + # This doesn't have to be 100% accurate: all failures are recorded anyway. + can_keep_going_after_failure () { + case "$1" in + "msg "*) false;; + *[!A-Za-z]"test"|*[!A-Za-z]"test"[!A-Za-z]*) true;; + "tests/"*) true;; + "grep "*|"not grep "*) true;; + *) false;; esac } + + # This function runs if there is any error in a component. + # It must either exit with a nonzero status, or set + # last_failure_status to a nonzero value. + err_trap () { + # Save $? (status of the failing command). This must be the very + # first thing, before $? is overridden. + last_failure_status=$? + failed_command=$BASH_COMMAND + + text="$current_section: $failed_command -> $last_failure_status" + echo "${start_red}^^^^$text^^^^${end_color}" >&2 + echo "$text" >>"$failure_summary_file" + + # If the command is fatal (configure or build command), stop this + # component. Otherwise (test command) keep the component running + # (run more tests from the same build). + if ! can_keep_going_after_failure "$failed_command"; then + exit $last_failure_status + fi + } + final_report () { if [ $failure_count -gt 0 ]; then echo echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" - echo "${start_red}FAILED: $failure_count${end_color}$failure_summary" + echo "${start_red}FAILED: $failure_count components${end_color}" + cat "$failure_summary_file" echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" - exit 1 elif [ -z "${1-}" ]; then echo "SUCCESS :)" fi if [ -n "${1-}" ]; then echo "Killed by SIG$1." fi + rm -f "$failure_summary_file" + if [ $failure_count -gt 0 ]; then + exit 1 + fi } } +# These functions are kept temporarily for backward compatibility. +# Don't use them in new components. +record_status () { + "$@" +} if_build_succeeded () { - if [ $build_status -eq 0 ]; then - record_status "$@" - fi + "$@" } - -# to be used instead of ! for commands run with -# record_status or if_build_succeeded not() { ! "$@" } @@ -2667,12 +2683,35 @@ run_component () { # have messed it up or shortened it. redirect_err dd if=/dev/urandom of=./tests/seedfile bs=64 count=1 - # Run the component code. - if [ $QUIET -eq 1 ]; then - # msg() is silenced, so just print the component name here - echo "${current_component#component_}" + # Run the component in a subshell + if [ $KEEP_GOING -eq 1 ]; then + set +e + fi + ( + if [ $QUIET -eq 1 ]; then + # msg() will be silenced, so just print the component name here. + echo "${current_component#component_}" + exec >/dev/null + fi + if [ $KEEP_GOING -eq 1 ]; then + # Keep "set -e" off, and run an ERR trap instead to record failures. + set -E + trap err_trap ERR + fi + # The next line is what runs the component + "$@" + if [ $KEEP_GOING -eq 1 ]; then + trap - ERR + exit $last_failure_status + fi + ) + component_status=$? + if [ $KEEP_GOING -eq 1 ]; then + set -e + if [ $component_status -ne 0 ]; then + failure_count=$((failure_count + 1)) + fi fi - redirect_out "$@" # Restore the build tree to a clean state. cleanup @@ -2689,10 +2728,6 @@ pre_check_git build_status=0 if [ $KEEP_GOING -eq 1 ]; then pre_setup_keep_going -else - record_status () { - "$@" - } fi pre_setup_quiet_redirect pre_prepare_outcome_file From 3664780f98357b211fe6fafb4fb090c6fe630c84 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Sat, 28 Mar 2020 18:50:49 +0100 Subject: [PATCH 004/120] Detect errors on the left-hand side of a pipeline Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index f3494bdd97ba..f80b8ff1eaa9 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -113,8 +113,9 @@ #### Initialization and command line parsing ################################################################ -# Abort on errors (and uninitialised variables) -set -eu +# Abort on errors (even on the left-hand side of a pipe). +# Treat uninitialised variables as errors. +set -e -o pipefail -u pre_check_environment () { if [ -d library -a -d include -a -d tests ]; then :; else From f7e956c85c5204d253db043128ea4260040e158d Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Sat, 28 Mar 2020 18:56:09 +0100 Subject: [PATCH 005/120] component_test_cmake_out_of_source: simplify and fix error handling Remove ssl-opt.err even if it's empty. Call cat unconditionally: it'll have no visible effect if the file is empty. Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index f80b8ff1eaa9..5a0ae4b0fa35 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -2548,11 +2548,10 @@ component_test_cmake_out_of_source () { # file is missing (ssl-opt.sh tolerates the absence of some files so # may exit with status 0 but emit errors). if_build_succeeded ./tests/ssl-opt.sh -f 'Fallback SCSV: beginning of list' 2>ssl-opt.err - if [ -s ssl-opt.err ]; then - cat ssl-opt.err >&2 - record_status [ ! -s ssl-opt.err ] - rm ssl-opt.err - fi + cat ssl-opt.err >&2 + # If ssl-opt.err is non-empty, record an error and keep going. + record_status [ ! -s ssl-opt.err ] + rm ssl-opt.err cd "$MBEDTLS_ROOT_DIR" rm -rf "$OUT_OF_SOURCE_DIR" unset MBEDTLS_ROOT_DIR From 1f0cdaf3af3dbedeb0ae9dd7094df375472d129f Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 8 Jul 2021 18:41:16 +0200 Subject: [PATCH 006/120] Stop dispatching through obsolete functions Remove the obsolete functions record_status and if_build_succeeded. They didn't affect error detection, but they made error reporting worse since $BASH_COMMAND would be the unexpanded "$@". Keep the function definitions for the sake of pull requests using them that may still be in flight. Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 209 ++++++++++++++++++++++--------------------- 1 file changed, 105 insertions(+), 104 deletions(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 5a0ae4b0fa35..8b8e3dd59096 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -549,14 +549,15 @@ pre_setup_keep_going () { } } -# These functions are kept temporarily for backward compatibility. -# Don't use them in new components. +# record_status() and if_build_succeeded() are kept temporarily for backward +# compatibility. Don't use them in new components. record_status () { "$@" } if_build_succeeded () { "$@" } + not() { ! "$@" } @@ -707,24 +708,24 @@ pre_generate_files() { component_check_recursion () { msg "Check: recursion.pl" # < 1s - record_status tests/scripts/recursion.pl library/*.c + tests/scripts/recursion.pl library/*.c } component_check_generated_files () { msg "Check: check-generated-files, files generated with make" # 2s make generated_files - record_status tests/scripts/check-generated-files.sh + tests/scripts/check-generated-files.sh msg "Check: check-generated-files -u, files present" # 2s - record_status tests/scripts/check-generated-files.sh -u + tests/scripts/check-generated-files.sh -u # Check that the generated files are considered up to date. - record_status tests/scripts/check-generated-files.sh + tests/scripts/check-generated-files.sh msg "Check: check-generated-files -u, files absent" # 2s command make neat - record_status tests/scripts/check-generated-files.sh -u + tests/scripts/check-generated-files.sh -u # Check that the generated files are considered up to date. - record_status tests/scripts/check-generated-files.sh + tests/scripts/check-generated-files.sh # This component ends with the generated files present in the source tree. # This is necessary for subsequent components! @@ -732,18 +733,18 @@ component_check_generated_files () { component_check_doxy_blocks () { msg "Check: doxygen markup outside doxygen blocks" # < 1s - record_status tests/scripts/check-doxy-blocks.pl + tests/scripts/check-doxy-blocks.pl } component_check_files () { msg "Check: file sanity checks (permissions, encodings)" # < 1s - record_status tests/scripts/check_files.py + tests/scripts/check_files.py } component_check_changelog () { msg "Check: changelog entries" # < 1s rm -f ChangeLog.new - record_status scripts/assemble_changelog.py -o ChangeLog.new + scripts/assemble_changelog.py -o ChangeLog.new if [ -e ChangeLog.new ]; then # Show the diff for information. It isn't an error if the diff is # non-empty. @@ -754,7 +755,7 @@ component_check_changelog () { component_check_names () { msg "Check: declared and exported names (builds the library)" # < 3s - record_status tests/scripts/check-names.sh -v + tests/scripts/check-names.sh -v } component_check_test_cases () { @@ -764,13 +765,13 @@ component_check_test_cases () { else opt='' fi - record_status tests/scripts/check_test_cases.py $opt + tests/scripts/check_test_cases.py $opt unset opt } component_check_doxygen_warnings () { msg "Check: doxygen warnings (builds the documentation)" # ~ 3s - record_status tests/scripts/doxygen.sh + tests/scripts/doxygen.sh } @@ -790,7 +791,7 @@ component_test_default_out_of_box () { make test msg "selftest: make, default config (out-of-box)" # ~10s - if_build_succeeded programs/test/selftest + programs/test/selftest export MBEDTLS_TEST_OUTCOME_FILE="$SAVE_MBEDTLS_TEST_OUTCOME_FILE" unset SAVE_MBEDTLS_TEST_OUTCOME_FILE @@ -805,16 +806,16 @@ component_test_default_cmake_gcc_asan () { make test msg "test: selftest (ASan build)" # ~ 10s - if_build_succeeded programs/test/selftest + programs/test/selftest msg "test: ssl-opt.sh (ASan build)" # ~ 1 min - if_build_succeeded tests/ssl-opt.sh + tests/ssl-opt.sh msg "test: compat.sh (ASan build)" # ~ 6 min - if_build_succeeded tests/compat.sh + tests/compat.sh msg "test: context-info.sh (ASan build)" # ~ 15 sec - if_build_succeeded tests/context-info.sh + tests/context-info.sh } component_test_full_cmake_gcc_asan () { @@ -827,16 +828,16 @@ component_test_full_cmake_gcc_asan () { make test msg "test: selftest (ASan build)" # ~ 10s - if_build_succeeded programs/test/selftest + programs/test/selftest msg "test: ssl-opt.sh (full config, ASan build)" - if_build_succeeded tests/ssl-opt.sh + tests/ssl-opt.sh msg "test: compat.sh (full config, ASan build)" - if_build_succeeded tests/compat.sh + tests/compat.sh msg "test: context-info.sh (full config, ASan build)" # ~ 15 sec - if_build_succeeded tests/context-info.sh + tests/context-info.sh } component_test_psa_crypto_key_id_encodes_owner () { @@ -874,7 +875,7 @@ component_build_psa_crypto_spm () { # Check that if a symbol is renamed by crypto_spe.h, the non-renamed # version is not present. echo "Checking for renamed symbols in the library" - if_build_succeeded check_renamed_symbols tests/include/spe/crypto_spe.h library/libmbedcrypto.a + check_renamed_symbols tests/include/spe/crypto_spe.h library/libmbedcrypto.a } component_test_psa_crypto_client () { @@ -900,7 +901,7 @@ component_test_psa_crypto_rsa_no_genprime() { component_test_ref_configs () { msg "test/build: ref-configs (ASan build)" # ~ 6 min 20s CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan . - record_status tests/scripts/test-ref-configs.pl + tests/scripts/test-ref-configs.pl } component_test_no_renegotiation () { @@ -913,7 +914,7 @@ component_test_no_renegotiation () { make test msg "test: !MBEDTLS_SSL_RENEGOTIATION - ssl-opt.sh (ASan build)" # ~ 6 min - if_build_succeeded tests/ssl-opt.sh + tests/ssl-opt.sh } component_test_no_pem_no_fs () { @@ -929,7 +930,7 @@ component_test_no_pem_no_fs () { make test msg "test: !MBEDTLS_PEM_PARSE_C !MBEDTLS_FS_IO - ssl-opt.sh (ASan build)" # ~ 6 min - if_build_succeeded tests/ssl-opt.sh + tests/ssl-opt.sh } component_test_rsa_no_crt () { @@ -942,13 +943,13 @@ component_test_rsa_no_crt () { make test msg "test: RSA_NO_CRT - RSA-related part of ssl-opt.sh (ASan build)" # ~ 5s - if_build_succeeded tests/ssl-opt.sh -f RSA + tests/ssl-opt.sh -f RSA msg "test: RSA_NO_CRT - RSA-related part of compat.sh (ASan build)" # ~ 3 min - if_build_succeeded tests/compat.sh -t RSA + tests/compat.sh -t RSA msg "test: RSA_NO_CRT - RSA-related part of context-info.sh (ASan build)" # ~ 15 sec - if_build_succeeded tests/context-info.sh + tests/context-info.sh } component_test_no_ctr_drbg_classic () { @@ -967,10 +968,10 @@ component_test_no_ctr_drbg_classic () { # The SSL tests are slow, so run a small subset, just enough to get # confidence that the SSL code copes with HMAC_DRBG. msg "test: Full minus CTR_DRBG, classic crypto - ssl-opt.sh (subset)" - if_build_succeeded tests/ssl-opt.sh -f 'Default\|SSL async private.*delay=\|tickets enabled on server' + tests/ssl-opt.sh -f 'Default\|SSL async private.*delay=\|tickets enabled on server' msg "test: Full minus CTR_DRBG, classic crypto - compat.sh (subset)" - if_build_succeeded tests/compat.sh -m tls1_2 -t 'ECDSA PSK' -V NO -p OpenSSL + tests/compat.sh -m tls1_2 -t 'ECDSA PSK' -V NO -p OpenSSL } component_test_no_ctr_drbg_use_psa () { @@ -989,10 +990,10 @@ component_test_no_ctr_drbg_use_psa () { # The SSL tests are slow, so run a small subset, just enough to get # confidence that the SSL code copes with HMAC_DRBG. msg "test: Full minus CTR_DRBG, USE_PSA_CRYPTO - ssl-opt.sh (subset)" - if_build_succeeded tests/ssl-opt.sh -f 'Default\|SSL async private.*delay=\|tickets enabled on server' + tests/ssl-opt.sh -f 'Default\|SSL async private.*delay=\|tickets enabled on server' msg "test: Full minus CTR_DRBG, USE_PSA_CRYPTO - compat.sh (subset)" - if_build_succeeded tests/compat.sh -m tls1_2 -t 'ECDSA PSK' -V NO -p OpenSSL + tests/compat.sh -m tls1_2 -t 'ECDSA PSK' -V NO -p OpenSSL } component_test_no_hmac_drbg_classic () { @@ -1014,12 +1015,12 @@ component_test_no_hmac_drbg_classic () { # Test SSL with non-deterministic ECDSA. Only test features that # might be affected by how ECDSA signature is performed. msg "test: Full minus HMAC_DRBG, classic crypto - ssl-opt.sh (subset)" - if_build_succeeded tests/ssl-opt.sh -f 'Default\|SSL async private: sign' + tests/ssl-opt.sh -f 'Default\|SSL async private: sign' # To save time, only test one protocol version, since this part of # the protocol is identical in (D)TLS up to 1.2. msg "test: Full minus HMAC_DRBG, classic crypto - compat.sh (ECDSA)" - if_build_succeeded tests/compat.sh -m tls1_2 -t 'ECDSA' + tests/compat.sh -m tls1_2 -t 'ECDSA' } component_test_no_hmac_drbg_use_psa () { @@ -1041,12 +1042,12 @@ component_test_no_hmac_drbg_use_psa () { # Test SSL with non-deterministic ECDSA. Only test features that # might be affected by how ECDSA signature is performed. msg "test: Full minus HMAC_DRBG, USE_PSA_CRYPTO - ssl-opt.sh (subset)" - if_build_succeeded tests/ssl-opt.sh -f 'Default\|SSL async private: sign' + tests/ssl-opt.sh -f 'Default\|SSL async private: sign' # To save time, only test one protocol version, since this part of # the protocol is identical in (D)TLS up to 1.2. msg "test: Full minus HMAC_DRBG, USE_PSA_CRYPTO - compat.sh (ECDSA)" - if_build_succeeded tests/compat.sh -m tls1_2 -t 'ECDSA' + tests/compat.sh -m tls1_2 -t 'ECDSA' } component_test_psa_external_rng_no_drbg_classic () { @@ -1069,7 +1070,7 @@ component_test_psa_external_rng_no_drbg_classic () { make test msg "test: PSA_CRYPTO_EXTERNAL_RNG minus *_DRBG, classic crypto - ssl-opt.sh (subset)" - if_build_succeeded tests/ssl-opt.sh -f 'Default' + tests/ssl-opt.sh -f 'Default' } component_test_psa_external_rng_no_drbg_use_psa () { @@ -1088,7 +1089,7 @@ component_test_psa_external_rng_no_drbg_use_psa () { make test msg "test: PSA_CRYPTO_EXTERNAL_RNG minus *_DRBG, PSA crypto - ssl-opt.sh (subset)" - if_build_succeeded tests/ssl-opt.sh -f 'Default\|opaque' + tests/ssl-opt.sh -f 'Default\|opaque' } component_test_psa_external_rng_use_psa_crypto () { @@ -1103,7 +1104,7 @@ component_test_psa_external_rng_use_psa_crypto () { make test msg "test: full + PSA_CRYPTO_EXTERNAL_RNG + USE_PSA_CRYPTO minus CTR_DRBG" - if_build_succeeded tests/ssl-opt.sh -f 'Default\|opaque' + tests/ssl-opt.sh -f 'Default\|opaque' } component_test_everest () { @@ -1116,11 +1117,11 @@ component_test_everest () { make test msg "test: Everest ECDH context - ECDH-related part of ssl-opt.sh (ASan build)" # ~ 5s - if_build_succeeded tests/ssl-opt.sh -f ECDH + tests/ssl-opt.sh -f ECDH msg "test: Everest ECDH context - compat.sh with some ECDH ciphersuites (ASan build)" # ~ 3 min # Exclude some symmetric ciphers that are redundant here to gain time. - if_build_succeeded tests/compat.sh -f ECDH -V NO -e 'ARIA\|CAMELLIA\|CHACHA\|DES' + tests/compat.sh -f ECDH -V NO -e 'ARIA\|CAMELLIA\|CHACHA\|DES' } component_test_everest_curve25519_only () { @@ -1150,7 +1151,7 @@ component_test_small_ssl_out_content_len () { make msg "test: small SSL_OUT_CONTENT_LEN - ssl-opt.sh MFL and large packet tests" - if_build_succeeded tests/ssl-opt.sh -f "Max fragment\|Large packet" + tests/ssl-opt.sh -f "Max fragment\|Large packet" } component_test_small_ssl_in_content_len () { @@ -1161,7 +1162,7 @@ component_test_small_ssl_in_content_len () { make msg "test: small SSL_IN_CONTENT_LEN - ssl-opt.sh MFL tests" - if_build_succeeded tests/ssl-opt.sh -f "Max fragment" + tests/ssl-opt.sh -f "Max fragment" } component_test_small_ssl_dtls_max_buffering () { @@ -1171,7 +1172,7 @@ component_test_small_ssl_dtls_max_buffering () { make msg "test: small MBEDTLS_SSL_DTLS_MAX_BUFFERING #0 - ssl-opt.sh specific reordering test" - if_build_succeeded tests/ssl-opt.sh -f "DTLS reordering: Buffer out-of-order hs msg before reassembling next, free buffered msg" + tests/ssl-opt.sh -f "DTLS reordering: Buffer out-of-order hs msg before reassembling next, free buffered msg" } component_test_small_mbedtls_ssl_dtls_max_buffering () { @@ -1181,15 +1182,15 @@ component_test_small_mbedtls_ssl_dtls_max_buffering () { make msg "test: small MBEDTLS_SSL_DTLS_MAX_BUFFERING #1 - ssl-opt.sh specific reordering test" - if_build_succeeded tests/ssl-opt.sh -f "DTLS reordering: Buffer encrypted Finished message, drop for fragmented NewSessionTicket" + tests/ssl-opt.sh -f "DTLS reordering: Buffer encrypted Finished message, drop for fragmented NewSessionTicket" } component_test_psa_collect_statuses () { msg "build+test: psa_collect_statuses" # ~30s scripts/config.py full - record_status tests/scripts/psa_collect_statuses.py + tests/scripts/psa_collect_statuses.py # Check that psa_crypto_init() succeeded at least once - record_status grep -q '^0:psa_crypto_init:' tests/statuses.log + grep -q '^0:psa_crypto_init:' tests/statuses.log rm -f tests/statuses.log } @@ -1203,16 +1204,16 @@ component_test_full_cmake_clang () { make test msg "test: psa_constant_names (full config, clang)" # ~ 1s - record_status tests/scripts/test_psa_constant_names.py + tests/scripts/test_psa_constant_names.py msg "test: ssl-opt.sh default, ECJPAKE, SSL async (full config)" # ~ 1s - if_build_succeeded tests/ssl-opt.sh -f 'Default\|ECJPAKE\|SSL async private' + tests/ssl-opt.sh -f 'Default\|ECJPAKE\|SSL async private' msg "test: compat.sh DES, 3DES & NULL (full config)" # ~ 2 min - if_build_succeeded env OPENSSL_CMD="$OPENSSL_LEGACY" GNUTLS_CLI="$GNUTLS_LEGACY_CLI" GNUTLS_SERV="$GNUTLS_LEGACY_SERV" tests/compat.sh -e '^$' -f 'NULL\|DES' + env OPENSSL_CMD="$OPENSSL_LEGACY" GNUTLS_CLI="$GNUTLS_LEGACY_CLI" GNUTLS_SERV="$GNUTLS_LEGACY_SERV" tests/compat.sh -e '^$' -f 'NULL\|DES' msg "test: compat.sh ARIA + ChachaPoly" - if_build_succeeded env OPENSSL_CMD="$OPENSSL_NEXT" tests/compat.sh -e '^$' -f 'ARIA\|CHACHA' + env OPENSSL_CMD="$OPENSSL_NEXT" tests/compat.sh -e '^$' -f 'ARIA\|CHACHA' } component_test_memsan_constant_flow () { @@ -1319,59 +1320,59 @@ component_build_crypto_default () { msg "build: make, crypto only" scripts/config.py crypto make CFLAGS='-O1 -Werror' - if_build_succeeded are_empty_libraries library/libmbedx509.* library/libmbedtls.* + are_empty_libraries library/libmbedx509.* library/libmbedtls.* } component_build_crypto_full () { msg "build: make, crypto only, full config" scripts/config.py crypto_full make CFLAGS='-O1 -Werror' - if_build_succeeded are_empty_libraries library/libmbedx509.* library/libmbedtls.* + are_empty_libraries library/libmbedx509.* library/libmbedtls.* } component_build_crypto_baremetal () { msg "build: make, crypto only, baremetal config" scripts/config.py crypto_baremetal make CFLAGS='-O1 -Werror' - if_build_succeeded are_empty_libraries library/libmbedx509.* library/libmbedtls.* + are_empty_libraries library/libmbedx509.* library/libmbedtls.* } component_test_depends_curves () { msg "test/build: curves.pl (gcc)" # ~ 4 min - record_status tests/scripts/curves.pl + tests/scripts/curves.pl } component_test_depends_curves_psa () { msg "test/build: curves.pl with MBEDTLS_USE_PSA_CRYPTO defined (gcc)" scripts/config.py set MBEDTLS_USE_PSA_CRYPTO - record_status tests/scripts/curves.pl + tests/scripts/curves.pl } component_test_depends_hashes () { msg "test/build: depends-hashes.pl (gcc)" # ~ 2 min - record_status tests/scripts/depends-hashes.pl + tests/scripts/depends-hashes.pl } component_test_depends_hashes_psa () { msg "test/build: depends-hashes.pl with MBEDTLS_USE_PSA_CRYPTO defined (gcc)" scripts/config.py set MBEDTLS_USE_PSA_CRYPTO - record_status tests/scripts/depends-hashes.pl + tests/scripts/depends-hashes.pl } component_test_depends_pkalgs () { msg "test/build: depends-pkalgs.pl (gcc)" # ~ 2 min - record_status tests/scripts/depends-pkalgs.pl + tests/scripts/depends-pkalgs.pl } component_test_depends_pkalgs_psa () { msg "test/build: depends-pkalgs.pl with MBEDTLS_USE_PSA_CRYPTO defined (gcc)" scripts/config.py set MBEDTLS_USE_PSA_CRYPTO - record_status tests/scripts/depends-pkalgs.pl + tests/scripts/depends-pkalgs.pl } component_build_key_exchanges () { msg "test/build: key-exchanges (gcc)" # ~ 1 min - record_status tests/scripts/key-exchanges.pl + tests/scripts/key-exchanges.pl } component_build_default_make_gcc_and_cxx () { @@ -1379,7 +1380,7 @@ component_build_default_make_gcc_and_cxx () { make CC=gcc CFLAGS='-Werror -Wall -Wextra -Os' msg "test: verify header list in cpp_dummy_build.cpp" - record_status check_headers_in_cpp + check_headers_in_cpp msg "build: Unix make, incremental g++" make TEST_CPP=1 @@ -1434,16 +1435,16 @@ component_test_no_use_psa_crypto_full_cmake_asan() { make test msg "test: ssl-opt.sh (full minus MBEDTLS_USE_PSA_CRYPTO)" - if_build_succeeded tests/ssl-opt.sh + tests/ssl-opt.sh msg "test: compat.sh default (full minus MBEDTLS_USE_PSA_CRYPTO)" - if_build_succeeded tests/compat.sh + tests/compat.sh msg "test: compat.sh DES & NULL (full minus MBEDTLS_USE_PSA_CRYPTO)" - if_build_succeeded env OPENSSL_CMD="$OPENSSL_LEGACY" GNUTLS_CLI="$GNUTLS_LEGACY_CLI" GNUTLS_SERV="$GNUTLS_LEGACY_SERV" tests/compat.sh -e '3DES\|DES-CBC3' -f 'NULL\|DES' + env OPENSSL_CMD="$OPENSSL_LEGACY" GNUTLS_CLI="$GNUTLS_LEGACY_CLI" GNUTLS_SERV="$GNUTLS_LEGACY_SERV" tests/compat.sh -e '3DES\|DES-CBC3' -f 'NULL\|DES' msg "test: compat.sh ARIA + ChachaPoly (full minus MBEDTLS_USE_PSA_CRYPTO)" - if_build_succeeded env OPENSSL_CMD="$OPENSSL_NEXT" tests/compat.sh -e '^$' -f 'ARIA\|CHACHA' + env OPENSSL_CMD="$OPENSSL_NEXT" tests/compat.sh -e '^$' -f 'ARIA\|CHACHA' } component_test_psa_crypto_config_basic() { @@ -1916,7 +1917,7 @@ component_test_memory_buffer_allocator () { msg "test: ssl-opt.sh, MBEDTLS_MEMORY_BUFFER_ALLOC_C" # MBEDTLS_MEMORY_BUFFER_ALLOC is slow. Skip tests that tend to time out. - if_build_succeeded tests/ssl-opt.sh -e '^DTLS proxy' + tests/ssl-opt.sh -e '^DTLS proxy' } component_test_no_max_fragment_length () { @@ -1927,7 +1928,7 @@ component_test_no_max_fragment_length () { make msg "test: ssl-opt.sh, MFL-related tests" - if_build_succeeded tests/ssl-opt.sh -f "Max fragment length" + tests/ssl-opt.sh -f "Max fragment length" } component_test_asan_remove_peer_certificate () { @@ -1940,13 +1941,13 @@ component_test_asan_remove_peer_certificate () { make test msg "test: ssl-opt.sh, !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE" - if_build_succeeded tests/ssl-opt.sh + tests/ssl-opt.sh msg "test: compat.sh, !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE" - if_build_succeeded tests/compat.sh + tests/compat.sh msg "test: context-info.sh, !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE" - if_build_succeeded tests/context-info.sh + tests/context-info.sh } component_test_no_max_fragment_length_small_ssl_out_content_len () { @@ -1958,10 +1959,10 @@ component_test_no_max_fragment_length_small_ssl_out_content_len () { make msg "test: MFL tests (disabled MFL extension case) & large packet tests" - if_build_succeeded tests/ssl-opt.sh -f "Max fragment length\|Large buffer" + tests/ssl-opt.sh -f "Max fragment length\|Large buffer" msg "test: context-info.sh (disabled MFL extension case)" - if_build_succeeded tests/context-info.sh + tests/context-info.sh } component_test_variable_ssl_in_out_buffer_len () { @@ -1974,10 +1975,10 @@ component_test_variable_ssl_in_out_buffer_len () { make test msg "test: ssl-opt.sh, MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH enabled" - if_build_succeeded tests/ssl-opt.sh + tests/ssl-opt.sh msg "test: compat.sh, MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH enabled" - if_build_succeeded tests/compat.sh + tests/compat.sh } component_test_variable_ssl_in_out_buffer_len_CID () { @@ -1992,10 +1993,10 @@ component_test_variable_ssl_in_out_buffer_len_CID () { make test msg "test: ssl-opt.sh, MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH and MBEDTLS_SSL_DTLS_CONNECTION_ID enabled" - if_build_succeeded tests/ssl-opt.sh + tests/ssl-opt.sh msg "test: compat.sh, MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH and MBEDTLS_SSL_DTLS_CONNECTION_ID enabled" - if_build_succeeded tests/compat.sh + tests/compat.sh } component_test_ssl_alloc_buffer_and_mfl () { @@ -2012,7 +2013,7 @@ component_test_ssl_alloc_buffer_and_mfl () { make test msg "test: MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH, MBEDTLS_MEMORY_BUFFER_ALLOC_C, MBEDTLS_MEMORY_DEBUG and MBEDTLS_SSL_MAX_FRAGMENT_LENGTH" - if_build_succeeded tests/ssl-opt.sh -f "Handshake memory usage" + tests/ssl-opt.sh -f "Handshake memory usage" } component_test_when_no_ciphersuites_have_mac () { @@ -2026,7 +2027,7 @@ component_test_when_no_ciphersuites_have_mac () { make test msg "test ssl-opt.sh: !MBEDTLS_SSL_SOME_MODES_USE_MAC" - if_build_succeeded tests/ssl-opt.sh -f 'Default\|EtM' -e 'without EtM' + tests/ssl-opt.sh -f 'Default\|EtM' -e 'without EtM' } component_test_no_date_time () { @@ -2062,7 +2063,7 @@ component_test_malloc_0_null () { msg "selftest: malloc(0) returns NULL (ASan+UBSan build)" # Just the calloc selftest. "make test" ran the others as part of the # test suites. - if_build_succeeded programs/test/selftest calloc + programs/test/selftest calloc msg "test ssl-opt.sh: malloc(0) returns NULL (ASan+UBSan build)" # Run a subset of the tests. The choice is a balance between coverage @@ -2070,7 +2071,7 @@ component_test_malloc_0_null () { # The current choice is to skip tests whose description includes # "proxy", which is an approximation of skipping tests that use the # UDP proxy, which tend to be slower and flakier. - if_build_succeeded tests/ssl-opt.sh -e 'proxy' + tests/ssl-opt.sh -e 'proxy' } component_test_aes_fewer_tables () { @@ -2261,7 +2262,7 @@ component_test_m32_o1 () { make test msg "test ssl-opt.sh, i386, make, gcc-O1" - if_build_succeeded tests/ssl-opt.sh + tests/ssl-opt.sh } support_test_m32_o1 () { support_test_m32_o0 "$@" @@ -2276,11 +2277,11 @@ component_test_m32_everest () { make test msg "test: i386, Everest ECDH context - ECDH-related part of ssl-opt.sh (ASan build)" # ~ 5s - if_build_succeeded tests/ssl-opt.sh -f ECDH + tests/ssl-opt.sh -f ECDH msg "test: i386, Everest ECDH context - compat.sh with some ECDH ciphersuites (ASan build)" # ~ 3 min # Exclude some symmetric ciphers that are redundant here to gain time. - if_build_succeeded tests/compat.sh -f ECDH -V NO -e 'ARIA\|CAMELLIA\|CHACHA\|DES' + tests/compat.sh -f ECDH -V NO -e 'ARIA\|CAMELLIA\|CHACHA\|DES' } support_test_m32_everest () { support_test_m32_o0 "$@" @@ -2378,7 +2379,7 @@ component_test_no_x509_info () { make test msg "test: ssl-opt.sh, full + MBEDTLS_X509_REMOVE_INFO" # ~ 1 min - if_build_succeeded tests/ssl-opt.sh + tests/ssl-opt.sh } component_build_arm_none_eabi_gcc () { @@ -2419,7 +2420,7 @@ component_build_arm_none_eabi_gcc_no_udbl_division () { scripts/config.py set MBEDTLS_NO_UDBL_DIVISION make CC="${ARM_NONE_EABI_GCC_PREFIX}gcc" AR="${ARM_NONE_EABI_GCC_PREFIX}ar" LD="${ARM_NONE_EABI_GCC_PREFIX}ld" CFLAGS='-std=c99 -Werror -Wall -Wextra' lib echo "Checking that software 64-bit division is not required" - if_build_succeeded not grep __aeabi_uldiv library/*.o + not grep __aeabi_uldiv library/*.o } component_build_arm_none_eabi_gcc_no_64bit_multiplication () { @@ -2428,7 +2429,7 @@ component_build_arm_none_eabi_gcc_no_64bit_multiplication () { scripts/config.py set MBEDTLS_NO_64BIT_MULTIPLICATION make CC="${ARM_NONE_EABI_GCC_PREFIX}gcc" AR="${ARM_NONE_EABI_GCC_PREFIX}ar" LD="${ARM_NONE_EABI_GCC_PREFIX}ld" CFLAGS='-std=c99 -Werror -O1 -march=armv6-m -mthumb' lib echo "Checking that software 64-bit multiplication is not required" - if_build_succeeded not grep __aeabi_lmul library/*.o + not grep __aeabi_lmul library/*.o } component_build_armcc () { @@ -2496,13 +2497,13 @@ component_test_memsan () { make test msg "test: ssl-opt.sh (MSan)" # ~ 1 min - if_build_succeeded tests/ssl-opt.sh + tests/ssl-opt.sh # Optional part(s) if [ "$MEMORY" -gt 0 ]; then msg "test: compat.sh (MSan)" # ~ 6 min 20s - if_build_succeeded tests/compat.sh + tests/compat.sh fi } @@ -2518,17 +2519,17 @@ component_test_valgrind () { # seem to receive signals under valgrind on OS X). if [ "$MEMORY" -gt 0 ]; then msg "test: ssl-opt.sh --memcheck (Release)" - if_build_succeeded tests/ssl-opt.sh --memcheck + tests/ssl-opt.sh --memcheck fi if [ "$MEMORY" -gt 1 ]; then msg "test: compat.sh --memcheck (Release)" - if_build_succeeded tests/compat.sh --memcheck + tests/compat.sh --memcheck fi if [ "$MEMORY" -gt 0 ]; then msg "test: context-info.sh --memcheck (Release)" - if_build_succeeded tests/context-info.sh --memcheck + tests/context-info.sh --memcheck fi } @@ -2547,10 +2548,10 @@ component_test_cmake_out_of_source () { # "No such file or directory", which would indicate that some required # file is missing (ssl-opt.sh tolerates the absence of some files so # may exit with status 0 but emit errors). - if_build_succeeded ./tests/ssl-opt.sh -f 'Fallback SCSV: beginning of list' 2>ssl-opt.err + ./tests/ssl-opt.sh -f 'Fallback SCSV: beginning of list' 2>ssl-opt.err cat ssl-opt.err >&2 # If ssl-opt.err is non-empty, record an error and keep going. - record_status [ ! -s ssl-opt.err ] + [ ! -s ssl-opt.err ] rm ssl-opt.err cd "$MBEDTLS_ROOT_DIR" rm -rf "$OUT_OF_SOURCE_DIR" @@ -2564,7 +2565,7 @@ component_test_cmake_as_subdirectory () { cd programs/test/cmake_subproject cmake . make - if_build_succeeded ./cmake_subproject + ./cmake_subproject cd "$MBEDTLS_ROOT_DIR" unset MBEDTLS_ROOT_DIR @@ -2577,7 +2578,7 @@ component_test_cmake_as_package () { cd programs/test/cmake_package cmake . make - if_build_succeeded ./cmake_package + ./cmake_package cd "$MBEDTLS_ROOT_DIR" unset MBEDTLS_ROOT_DIR @@ -2590,7 +2591,7 @@ component_test_cmake_as_package_install () { cd programs/test/cmake_package_install cmake . make - if_build_succeeded ./cmake_package_install + ./cmake_package_install cd "$MBEDTLS_ROOT_DIR" unset MBEDTLS_ROOT_DIR @@ -2615,9 +2616,9 @@ component_test_zeroize () { for compiler in clang gcc; do msg "test: $compiler $optimization_flag, mbedtls_platform_zeroize()" make programs CC="$compiler" DEBUG=1 CFLAGS="$optimization_flag" - if_build_succeeded gdb -ex "$gdb_disable_aslr" -x tests/scripts/test_zeroize.gdb -nw -batch -nx 2>&1 | tee test_zeroize.log - if_build_succeeded grep "The buffer was correctly zeroized" test_zeroize.log - if_build_succeeded not grep -i "error" test_zeroize.log + gdb -ex "$gdb_disable_aslr" -x tests/scripts/test_zeroize.gdb -nw -batch -nx 2>&1 | tee test_zeroize.log + grep "The buffer was correctly zeroized" test_zeroize.log + not grep -i "error" test_zeroize.log rm -f test_zeroize.log make clean done @@ -2628,7 +2629,7 @@ component_test_zeroize () { component_check_python_files () { msg "Lint: Python scripts" - record_status tests/scripts/check-python-files.sh + tests/scripts/check-python-files.sh } component_check_generate_test_code () { @@ -2636,7 +2637,7 @@ component_check_generate_test_code () { # unittest writes out mundane stuff like number or tests run on stderr. # Our convention is to reserve stderr for actual errors, and write # harmless info on stdout so it can be suppress with --quiet. - record_status ./tests/scripts/test_generate_test_code.py 2>&1 + ./tests/scripts/test_generate_test_code.py 2>&1 } ################################################################ From fec30cbe8c800dcc3615601eb2d6859de8d78da5 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Sat, 28 Mar 2020 19:34:23 +0100 Subject: [PATCH 007/120] Fix double reporting when the last command of a function fails Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 8b8e3dd59096..cb3c8f2fb974 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -480,6 +480,11 @@ pre_setup_keep_going () { failure_count=0 # Number of failed components last_failure_status=0 # Last failure status in this component + # See err_trap + previous_failure_status=0 + previous_failed_command= + previous_failure_funcall_depth=0 + start_red= end_color= if [ -t 1 ]; then @@ -517,6 +522,21 @@ pre_setup_keep_going () { last_failure_status=$? failed_command=$BASH_COMMAND + if [[ $last_failure_status -eq $previous_failure_status && + "$failed_command" == "$previous_failed_command" && + ${#FUNCNAME[@]} == $((previous_failure_funcall_depth - 1)) ]] + then + # The same command failed twice in a row, but this time one level + # less deep in the function call stack. This happens when the last + # command of a function returns a nonzero status, and the function + # returns that same status. Ignore the second failure. + previous_failure_funcall_depth=${#FUNCNAME[@]} + return + fi + previous_failure_status=$last_failure_status + previous_failed_command=$failed_command + previous_failure_funcall_depth=${#FUNCNAME[@]} + text="$current_section: $failed_command -> $last_failure_status" echo "${start_red}^^^^$text^^^^${end_color}" >&2 echo "$text" >>"$failure_summary_file" From a681c59d348f362bd18fcff72c694fc8ede98546 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Sat, 28 Mar 2020 21:27:40 +0100 Subject: [PATCH 008/120] Better not function In the `not` function, in keep-going mode, arrange to report the failing command (rather than `"$@"`). Note that the `!` keyword should not be used, because failures with `!` are not reported properly. Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index cb3c8f2fb974..d95ca7b897b9 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -90,6 +90,9 @@ # # Each component must start by invoking `msg` with a short informative message. # +# Warning: due to the way bash detects errors, the failure of a command +# inside 'if' or '!' is not detected. Use the 'not' function instead of '!'. +# # Each component is executed in a separate shell process. The component # fails if any command in it returns a non-zero status. # @@ -484,6 +487,7 @@ pre_setup_keep_going () { previous_failure_status=0 previous_failed_command= previous_failure_funcall_depth=0 + unset report_failed_command start_red= end_color= @@ -508,7 +512,7 @@ pre_setup_keep_going () { "msg "*) false;; *[!A-Za-z]"test"|*[!A-Za-z]"test"[!A-Za-z]*) true;; "tests/"*) true;; - "grep "*|"not grep "*) true;; + "grep "*|"! grep "*) true;; *) false;; esac } @@ -520,7 +524,7 @@ pre_setup_keep_going () { # Save $? (status of the failing command). This must be the very # first thing, before $? is overridden. last_failure_status=$? - failed_command=$BASH_COMMAND + failed_command=${report_failed_command-$BASH_COMMAND} if [[ $last_failure_status -eq $previous_failure_status && "$failed_command" == "$previous_failed_command" && @@ -578,8 +582,14 @@ if_build_succeeded () { "$@" } -not() { - ! "$@" +# '! true' does not trigger the ERR trap. Arrange to trigger it, with +# a reasonably informative error message (not just "$@"). +not () { + if "$@"; then + report_failed_command="! $*" + false + unset report_failed_command + fi } pre_setup_quiet_redirect () { From b80f0d20ea063ff0a87ac52e58c8155faf53f8bd Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Sat, 28 Mar 2020 21:37:59 +0100 Subject: [PATCH 009/120] Complain if an unsupported component is explicitly requested In all.sh, when an explicit list of components is specified, error out if one of the components is not known or not supported. Patterns that happen to match zero components are still effectively ignored. Fix #2783 Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index d95ca7b897b9..61b17e102260 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -444,6 +444,24 @@ pre_parse_command_line () { COMMAND_LINE_COMPONENTS="$COMMAND_LINE_COMPONENTS *_armcc*" fi + if [ $all_except -eq 0 ]; then + unsupported=0 + for component in $COMMAND_LINE_COMPONENTS; do + case $component in + *[*?\[]*) continue;; + esac + case " $SUPPORTED_COMPONENTS " in + *" $component "*) :;; + *) + echo >&2 "Component $component was explicitly requested, but is not known or not supported." + unsupported=$((unsupported + 1));; + esac + done + if [ $unsupported -ne 0 ]; then + exit 2 + fi + fi + # Build the list of components to run. RUN_COMPONENTS= for component in $SUPPORTED_COMPONENTS; do From c2e22ee27177c2041cdb047325db20df500796d4 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Sat, 28 Mar 2020 22:02:50 +0100 Subject: [PATCH 010/120] Remove code that is useless now that components run in a subshell Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 61b17e102260..6249977c8262 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -267,10 +267,6 @@ EOF # remove built files as well as the cmake cache/config cleanup() { - if [ -n "${MBEDTLS_ROOT_DIR+set}" ]; then - cd "$MBEDTLS_ROOT_DIR" - fi - command make clean # Remove CMake artefacts @@ -2603,7 +2599,6 @@ component_test_cmake_out_of_source () { rm ssl-opt.err cd "$MBEDTLS_ROOT_DIR" rm -rf "$OUT_OF_SOURCE_DIR" - unset MBEDTLS_ROOT_DIR } component_test_cmake_as_subdirectory () { From aca0b32132533acec4cbb9c64b5e64338bb04145 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Mon, 20 Apr 2020 13:21:27 +0200 Subject: [PATCH 011/120] Keep going after a shell "[" a.k.a. "test" fails This is necessary to actually keep going and finish the component-specific cleanup in component_test_cmake_out_of_source if ssl-opt.err is non-empty. Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 6249977c8262..afc1a4918553 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -527,6 +527,7 @@ pre_setup_keep_going () { *[!A-Za-z]"test"|*[!A-Za-z]"test"[!A-Za-z]*) true;; "tests/"*) true;; "grep "*|"! grep "*) true;; + "test "*|"[ "*) true;; *) false;; esac } From 88a07457c7e6dbe0c170a75110f84bad39f156c6 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 8 Jul 2021 19:03:50 +0200 Subject: [PATCH 012/120] Remove barely-used redirect functions redirect_out was no longer used and redirect_err was only used to quiet dd. Change the dd invocation to only print diagnostics on error (on platforms where this is possible). Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index afc1a4918553..4614029ad06a 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -607,24 +607,6 @@ not () { fi } -pre_setup_quiet_redirect () { - if [ $QUIET -ne 1 ]; then - redirect_out () { - "$@" - } - redirect_err () { - "$@" - } - else - redirect_out () { - "$@" >/dev/null - } - redirect_err () { - "$@" 2>/dev/null - } - fi -} - pre_prepare_outcome_file () { case "$MBEDTLS_TEST_OUTCOME_FILE" in [!/]*) MBEDTLS_TEST_OUTCOME_FILE="$PWD/$MBEDTLS_TEST_OUTCOME_FILE";; @@ -2726,7 +2708,12 @@ run_component () { # Unconditionally create a seedfile that's sufficiently long. # Do this before each component, because a previous component may # have messed it up or shortened it. - redirect_err dd if=/dev/urandom of=./tests/seedfile bs=64 count=1 + local dd_cmd + dd_cmd=(dd if=/dev/urandom of=./tests/seedfile bs=64 count=1) + case $OSTYPE in + linux*|freebsd*|openbsd*|darwin*) dd_cmd+=(status=none) + esac + "${dd_cmd[@]}" # Run the component in a subshell if [ $KEEP_GOING -eq 1 ]; then @@ -2774,7 +2761,6 @@ build_status=0 if [ $KEEP_GOING -eq 1 ]; then pre_setup_keep_going fi -pre_setup_quiet_redirect pre_prepare_outcome_file pre_print_configuration pre_check_tools From 72385036420e9a072c0588ccd685f8f72cf6458d Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 8 Jul 2021 19:07:07 +0200 Subject: [PATCH 013/120] Heed --quiet when running make generated_files Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 4614029ad06a..c3df05b72429 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -713,7 +713,11 @@ pre_generate_files() { # since make doesn't have proper dependencies, remove any possibly outdate # file that might be around before generating fresh ones make neat - make generated_files + if [ $QUIET -eq 1 ]; then + make -s generated_files + else + make generated_files + fi } From 03ab544832fceeb1d618fef1d4f2e44641a9eadc Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 9 Jul 2021 15:19:28 +0200 Subject: [PATCH 014/120] Generate cpp_cummy_build.cpp dynamically Generate programs/test/cpp_dummy_build.cpp dynamically instead of maintaining it manually. This removes the need to update it when the list of headers changes. Include all the headers unconditionally except for the ones that cannot be included directly. Support this dynamic generation both with make and with cmake. Adapt all.sh accordingly. Remove the redundant C build from component_build_default_make_gcc_and_cxx (it was also done in component_test_default_out_of_box), leaving a component_test_make_cxx. Also run the C++ program, because why not. Do this in the full configuration which may catch a bit more problems in headers. Fixes #2570 for good. Signed-off-by: Gilles Peskine --- programs/.gitignore | 1 + programs/Makefile | 7 +- programs/test/CMakeLists.txt | 12 ++- programs/test/cpp_dummy_build.cpp | 98 ----------------------- programs/test/generate_cpp_dummy_build.sh | 85 ++++++++++++++++++++ tests/scripts/all.sh | 22 ++--- 6 files changed, 109 insertions(+), 116 deletions(-) delete mode 100644 programs/test/cpp_dummy_build.cpp create mode 100755 programs/test/generate_cpp_dummy_build.sh diff --git a/programs/.gitignore b/programs/.gitignore index 83521a792b0e..d8eb6baa03dd 100644 --- a/programs/.gitignore +++ b/programs/.gitignore @@ -56,6 +56,7 @@ ssl/ssl_server ssl/ssl_server2 test/benchmark test/cpp_dummy_build +test/cpp_dummy_build.cpp test/ecp-bench test/query_compile_time_config test/selftest diff --git a/programs/Makefile b/programs/Makefile index 997c198716ab..977ae7e8b468 100644 --- a/programs/Makefile +++ b/programs/Makefile @@ -331,6 +331,10 @@ test/benchmark$(EXEXT): test/benchmark.c $(DEP) echo " CC test/benchmark.c" $(CC) $(LOCAL_CFLAGS) $(CFLAGS) test/benchmark.c $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ +test/cpp_dummy_build.cpp: test/generate_cpp_dummy_build.sh + echo " Gen test/cpp_dummy_build.cpp" + test/generate_cpp_dummy_build.sh + test/cpp_dummy_build$(EXEXT): test/cpp_dummy_build.cpp $(DEP) echo " CXX test/cpp_dummy_build.cpp" $(CXX) $(LOCAL_CXXFLAGS) $(CXXFLAGS) test/cpp_dummy_build.cpp $(LOCAL_LDFLAGS) $(LDFLAGS) -o $@ @@ -391,10 +395,11 @@ clean: ifndef WINDOWS rm -f $(EXES) -rm -f ssl/ssl_pthread_server$(EXEXT) - -rm -f test/cpp_dummy_build$(EXEXT) + -rm -f test/cpp_dummy_build.cpp test/cpp_dummy_build$(EXEXT) else if exist *.o del /Q /F *.o if exist *.exe del /Q /F *.exe + if exist test\cpp_dummy_build.cpp del /Q /F test\cpp_dummy_build.cpp endif $(MAKE) -C fuzz clean diff --git a/programs/test/CMakeLists.txt b/programs/test/CMakeLists.txt index 807d1bc10b74..a0a1b763cca2 100644 --- a/programs/test/CMakeLists.txt +++ b/programs/test/CMakeLists.txt @@ -14,7 +14,17 @@ set(executables_mbedcrypto ) if(TEST_CPP) - list(APPEND executables_mbedcrypto cpp_dummy_build) + set(cpp_dummy_build_cpp "${CMAKE_CURRENT_BINARY_DIR}/cpp_dummy_build.cpp") + set(generate_cpp_dummy_build "${CMAKE_CURRENT_SOURCE_DIR}/generate_cpp_dummy_build.sh") + add_custom_command( + OUTPUT "${cpp_dummy_build_cpp}" + COMMAND "${generate_cpp_dummy_build}" "${cpp_dummy_build_cpp}" + DEPENDS "${generate_cpp_dummy_build}" + WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}" + ) + add_executable(cpp_dummy_build "${cpp_dummy_build_cpp}") + target_include_directories(cpp_dummy_build PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../include) + target_link_libraries(cpp_dummy_build ${mbedcrypto_target}) endif() foreach(exe IN LISTS executables_libs executables_mbedcrypto) diff --git a/programs/test/cpp_dummy_build.cpp b/programs/test/cpp_dummy_build.cpp deleted file mode 100644 index 7f1efe8dba87..000000000000 --- a/programs/test/cpp_dummy_build.cpp +++ /dev/null @@ -1,98 +0,0 @@ -/* - * This program is a dummy C++ program to ensure Mbed TLS library header files - * can be included and built with a C++ compiler. - * - * Copyright The Mbed TLS Contributors - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "mbedtls/build_info.h" - -#include "mbedtls/aes.h" -#include "mbedtls/aria.h" -#include "mbedtls/asn1.h" -#include "mbedtls/asn1write.h" -#include "mbedtls/base64.h" -#include "mbedtls/bignum.h" -#include "mbedtls/camellia.h" -#include "mbedtls/ccm.h" -#include "mbedtls/chacha20.h" -#include "mbedtls/chachapoly.h" -#include "mbedtls/check_config.h" -#include "mbedtls/cipher.h" -#include "mbedtls/cmac.h" -#include "mbedtls/config_psa.h" -#include "mbedtls/ctr_drbg.h" -#include "mbedtls/debug.h" -#include "mbedtls/des.h" -#include "mbedtls/dhm.h" -#include "mbedtls/ecdh.h" -#include "mbedtls/ecdsa.h" -#include "mbedtls/ecjpake.h" -#include "mbedtls/ecp.h" -#include "mbedtls/entropy.h" -#include "mbedtls/error.h" -#include "mbedtls/gcm.h" -#include "mbedtls/hkdf.h" -#include "mbedtls/hmac_drbg.h" -#include "mbedtls/md.h" -#include "mbedtls/md5.h" -#include "mbedtls/net_sockets.h" -#include "mbedtls/nist_kw.h" -#include "mbedtls/oid.h" -#include "mbedtls/pem.h" -#include "mbedtls/pk.h" -#include "mbedtls/pkcs12.h" -#include "mbedtls/pkcs5.h" -#include "mbedtls/platform_time.h" -#include "mbedtls/platform_util.h" -#include "mbedtls/poly1305.h" -#include "mbedtls/psa_util.h" -#include "mbedtls/ripemd160.h" -#include "mbedtls/rsa.h" -#include "mbedtls/sha1.h" -#include "mbedtls/sha256.h" -#include "mbedtls/sha512.h" -#include "mbedtls/ssl.h" -#include "mbedtls/ssl_cache.h" -#include "mbedtls/ssl_ciphersuites.h" -#include "mbedtls/ssl_cookie.h" -#include "mbedtls/ssl_ticket.h" -#include "mbedtls/threading.h" -#include "mbedtls/timing.h" -#include "mbedtls/version.h" -#include "mbedtls/x509.h" -#include "mbedtls/x509_crl.h" -#include "mbedtls/x509_crt.h" -#include "mbedtls/x509_csr.h" - -#if defined(MBEDTLS_PLATFORM_C) -#include "mbedtls/platform.h" -#endif - -#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) -#include "mbedtls/memory_buffer_alloc.h" -#endif - -#include "psa/crypto.h" -#include "psa/crypto_se_driver.h" - -int main() -{ - mbedtls_platform_context *ctx = NULL; - mbedtls_platform_setup(ctx); - mbedtls_printf("CPP Build test\n"); - mbedtls_platform_teardown(ctx); -} diff --git a/programs/test/generate_cpp_dummy_build.sh b/programs/test/generate_cpp_dummy_build.sh new file mode 100755 index 000000000000..41adf149ebe8 --- /dev/null +++ b/programs/test/generate_cpp_dummy_build.sh @@ -0,0 +1,85 @@ +#!/bin/sh + +# Copyright The Mbed TLS Contributors +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -e + +# Ensure a reproducible order for *.h +export LC_ALL=C + +print_cpp () { + cat <<'EOF' +/* Automatically generated file. Do not edit. + * + * This program is a dummy C++ program to ensure Mbed TLS library header files + * can be included and built with a C++ compiler. + * + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbedtls/build_info.h" + +EOF + + for header in include/mbedtls/*.h include/psa/*.h; do + case ${header#include/} in + mbedtls/mbedtls_config.h) :;; # not meant for direct inclusion + psa/crypto_config.h) :;; # not meant for direct inclusion + # Some of the psa/crypto_*.h headers are not meant to be included directly. + # They do have include guards that make them no-ops if psa/crypto.h + # has been included before. Since psa/crypto.h comes before psa/crypto_*.h + # in the wildcard enumeration, we don't need to skip those headers. + *) echo "#include \"${header#include/}\"";; + esac + done + + cat <<'EOF' + +int main() +{ + mbedtls_platform_context *ctx = NULL; + mbedtls_platform_setup(ctx); + mbedtls_printf("CPP Build test passed\n"); + mbedtls_platform_teardown(ctx); +} +EOF +} + +if [ -d include/mbedtls ]; then + : +elif [ -d ../include/mbedtls ]; then + cd .. +elif [ -d ../../include/mbedtls ]; then + cd ../.. +else + echo >&2 "This script must be run from an Mbed TLS source tree." + exit 3 +fi + +print_cpp >"${1:-programs/test/cpp_dummy_build.cpp}" diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index c3df05b72429..8c75c9f052bc 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -368,14 +368,6 @@ check_tools() done } -check_headers_in_cpp () { - ls include/mbedtls | grep "\.h$" >headers.txt - Date: Mon, 30 Mar 2020 20:11:39 +0200 Subject: [PATCH 015/120] Don't restore *config.h before backing it up Back up the config files at the beginning of all.sh, rather than before each component. In particular, create the backup before running cleanup for the first time. This fixes #3139 (all.sh using a config.h.bak from a previous job), and makes all.sh more robust against accidentally using a modified config.h midway through because a component messed with the backup. Use a different extension (*.all.bak rather than *.bak) for the backups. This is necessary to ensure that auxiliary scripts such as depends*.pl that make their own backup don't remove all.sh's backup, which the code from this commit does not support. Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 8c75c9f052bc..5c2ab2778de0 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -129,9 +129,14 @@ pre_check_environment () { pre_initialize_variables () { CONFIG_H='include/mbedtls/mbedtls_config.h' - CONFIG_BAK="$CONFIG_H.bak" CRYPTO_CONFIG_H='include/psa/crypto_config.h' - CRYPTO_CONFIG_BAK="$CRYPTO_CONFIG_H.bak" + + # Files that are clobbered by some jobs will be backed up. Use a different + # suffix from auxiliary scripts so that all.sh and auxiliary scripts can + # independently decide when to remove the backup file. + backup_suffix='.all.bak' + # Files clobbered by config.py + files_to_back_up="$CONFIG_H $CRYPTO_CONFIG_H" append_outcome=0 MEMORY=0 @@ -295,13 +300,18 @@ cleanup() rm -f programs/test/cmake_package_install/Makefile rm -f programs/test/cmake_package_install/cmake_package_install - if [ -f "$CONFIG_BAK" ]; then - mv "$CONFIG_BAK" "$CONFIG_H" - fi + # Restore files that may have been clobbered by the job + for x in $files_to_back_up; do + cp -p "$x$backup_suffix" "$x" + done +} - if [ -f "$CRYPTO_CONFIG_BAK" ]; then - mv "$CRYPTO_CONFIG_BAK" "$CRYPTO_CONFIG_H" - fi +final_cleanup () { + cleanup + + for x in $files_to_back_up; do + rm -f "$x$backup_suffix" + done } # Executed on exit. May be redefined depending on command line options. @@ -310,7 +320,7 @@ final_report () { } fatal_signal () { - cleanup + final_cleanup final_report $1 trap - $1 kill -$1 $$ @@ -485,6 +495,12 @@ pre_check_git () { fi } +pre_back_up () { + for x in $files_to_back_up; do + cp -p "$x" "$x$backup_suffix" + done +} + pre_setup_keep_going () { failure_count=0 # Number of failed components last_failure_status=0 # Last failure status in this component @@ -2666,7 +2682,7 @@ component_check_generate_test_code () { post_report () { msg "Done, cleaning up" - cleanup + final_cleanup final_report } @@ -2692,10 +2708,6 @@ pseudo_component_error_test () { # Run one component and clean up afterwards. run_component () { - # Back up the configuration in case the component modifies it. - # The cleanup function will restore it. - cp -p "$CONFIG_H" "$CONFIG_BAK" - cp -p "$CRYPTO_CONFIG_H" "$CRYPTO_CONFIG_BAK" current_component="$1" export MBEDTLS_TEST_CONFIGURATION="$current_component" @@ -2750,6 +2762,7 @@ pre_initialize_variables pre_parse_command_line "$@" pre_check_git +pre_back_up build_status=0 if [ $KEEP_GOING -eq 1 ]; then From 568f53a9d8734f8dc28f2a9c8fe86ef5267df0af Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Mon, 12 Jul 2021 18:16:01 +0200 Subject: [PATCH 016/120] Don't unconditionally restore **/Makefile all.sh restores **/Makefile from git in case the version in the worktree was from doing a cmake in-tree build. Instead of doing this unconditionally, do it only if the toplevel Makefile seems to have been automatically generated (by cmake or otherwise, e.g. by mbedtls-prepare-build). This way all.sh no longer silently wipes changes made to Makefile but not committed yet. Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 5c2ab2778de0..7db9488eee21 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -137,6 +137,8 @@ pre_initialize_variables () { backup_suffix='.all.bak' # Files clobbered by config.py files_to_back_up="$CONFIG_H $CRYPTO_CONFIG_H" + # Files clobbered by in-tree cmake + files_to_back_up="$files_to_back_up Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile" append_outcome=0 MEMORY=0 @@ -282,8 +284,6 @@ cleanup() -iname CMakeCache.txt \) -exec rm -f {} \+ # Recover files overwritten by in-tree CMake builds rm -f include/Makefile include/mbedtls/Makefile programs/*/Makefile - git update-index --no-skip-worktree Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile - git checkout -- Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile # Remove any artifacts from the component_test_cmake_as_subdirectory test. rm -rf programs/test/cmake_subproject/build @@ -495,6 +495,20 @@ pre_check_git () { fi } +pre_restore_files () { + # If the makefiles have been generated by a framework such as cmake, + # restore them from git. If the makefiles look like modifications from + # the ones checked into git, take care not to modify them. Whatever + # this function leaves behind is what the script will restore before + # each component. + case "$(head -n1 Makefile)" in + *[Gg]enerated*) + git update-index --no-skip-worktree Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile + git checkout -- Makefile library/Makefile programs/Makefile tests/Makefile programs/fuzz/Makefile + ;; + esac +} + pre_back_up () { for x in $files_to_back_up; do cp -p "$x" "$x$backup_suffix" @@ -2762,6 +2776,7 @@ pre_initialize_variables pre_parse_command_line "$@" pre_check_git +pre_restore_files pre_back_up build_status=0 From 218dec824e6de919f229afa00731d185651aae09 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Fri, 23 Jul 2021 07:23:47 +0100 Subject: [PATCH 017/120] Document that returning 0 from the recv callback means EOF Signed-off-by: Hanno Becker --- include/mbedtls/ssl.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/mbedtls/ssl.h b/include/mbedtls/ssl.h index 3a14a5830751..bf44975c35c5 100644 --- a/include/mbedtls/ssl.h +++ b/include/mbedtls/ssl.h @@ -493,10 +493,11 @@ typedef int mbedtls_ssl_send_t( void *ctx, * \param buf Buffer to write the received data to * \param len Length of the receive buffer * - * \return The callback must return the number of bytes received, - * or a non-zero error code. - * If performing non-blocking I/O, \c MBEDTLS_ERR_SSL_WANT_READ + * \returns If data has been received, the positive number of bytes received. + * \returns \c 0 if the connection has been closed. + * \returns If performing non-blocking I/O, \c MBEDTLS_ERR_SSL_WANT_READ * must be returned when the operation would block. + * \returns Another negative error code on other kinds of failures. * * \note The callback may receive fewer bytes than the length of the * buffer. It must always return the number of bytes actually From ec135544c81aa4c292f8b715244ac1b38682580b Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Mon, 2 Aug 2021 23:14:03 +0200 Subject: [PATCH 018/120] Clarify some comments Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 7db9488eee21..73630c122e07 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -2714,8 +2714,11 @@ pseudo_component_error_test () { echo "Expect three failing commands." fi error_test='this should not be used since the component runs in a subshell' + # Expected error: 'grep non_existent /dev/null -> 1' grep non_existent /dev/null + # Expected error: '! grep -q . tests/scripts/all.sh -> 1' not grep -q . "$0" + # Expected error: 'make unknown_target -> 2' make unknown_target false "this should not be executed" } @@ -2735,8 +2738,11 @@ run_component () { esac "${dd_cmd[@]}" - # Run the component in a subshell + # Run the component in a subshell, with error trapping and output + # redirection set up based on the relevant options. if [ $KEEP_GOING -eq 1 ]; then + # We want to keep running if the subshell fails, so 'set -e' must + # be off when the subshell runs. set +e fi ( From 88a7c2b32e6cf289d8287186a20370aead8e6eb1 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Mon, 2 Aug 2021 23:28:00 +0200 Subject: [PATCH 019/120] Improve --error-test reporting Count invocations from 1 to n instead of n to 1. Explain how changing the loop variable would cause an error if the function was not executed in a subshell. Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 73630c122e07..54b28b870fcb 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -2709,11 +2709,13 @@ post_report () { # Function invoked by --error-test to test error reporting. pseudo_component_error_test () { - msg "Testing error reporting $error_test" + msg "Testing error reporting $error_test_i" if [ $KEEP_GOING -ne 0 ]; then echo "Expect three failing commands." fi - error_test='this should not be used since the component runs in a subshell' + # If the component doesn't run in a subshell, changing error_test_i to an + # invalid integer will cause an error in the loop that runs this function. + error_test_i=this_should_not_be_used_since_the_component_runs_in_a_subshell # Expected error: 'grep non_existent /dev/null -> 1' grep non_existent /dev/null # Expected error: '! grep -q . tests/scripts/all.sh -> 1' @@ -2796,10 +2798,10 @@ cleanup pre_generate_files # Run the requested tests. -while [ $error_test -gt 0 ]; do +for ((error_test_i=1; error_test_i <= error_test; error_test_i++)); do run_component pseudo_component_error_test - error_test=$((error_test - 1)) done +unset error_test_i for component in $RUN_COMPONENTS; do run_component "component_$component" done From c111e2429287ba5f20708a4853541fd3cb4f0ae1 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Mon, 2 Aug 2021 23:29:53 +0200 Subject: [PATCH 020/120] Improve the detection of keep-going commands Have simpler patterns related to 'test' (the central objective being to keep going if 'make test' or 'tests/...' fails, but not if 'make tests' fails). Add 'cd' as a can't-keep-going command. Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 54b28b870fcb..6cdc922fe313 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -543,13 +543,19 @@ pre_setup_keep_going () { # Whether it makes sense to keep a component going after the specified # command fails (test command) or not (configure or build). # This doesn't have to be 100% accurate: all failures are recorded anyway. + # False positives result in running things that can't be expected to + # work. False negatives result in things not running after something else + # failed even though they might have given useful feedback. can_keep_going_after_failure () { case "$1" in "msg "*) false;; - *[!A-Za-z]"test"|*[!A-Za-z]"test"[!A-Za-z]*) true;; - "tests/"*) true;; - "grep "*|"! grep "*) true;; - "test "*|"[ "*) true;; + "cd "*) false;; + *make*[\ /]tests*) false;; # make tests, make CFLAGS=-I../tests, ... + *test*) true;; # make test, tests/stuff, env V=v tests/stuff, ... + *make*check*) true;; + "grep "*) true;; + "[ "*) true;; + "! "*) true;; *) false;; esac } From 3a6c76937adce329cbc85fe5001cc7e533545b8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Tue, 18 Aug 2020 10:28:51 +0200 Subject: [PATCH 021/120] Add arm-linux-gnueabi-gcc build to all.sh MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently it can't be mandatory, since we can't install the required toolchain on Jenkins right away. Also, while at it, remove `SHELL='sh -x'` from the other arm5vte component; it was a leftover from debugging. Signed-off-by: Manuel Pégourié-Gonnard --- tests/scripts/all.sh | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index f8e43c871441..7570b1128f31 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -161,6 +161,7 @@ pre_initialize_variables () { : ${ARMC5_BIN_DIR:=/usr/bin} : ${ARMC6_BIN_DIR:=/usr/bin} : ${ARM_NONE_EABI_GCC_PREFIX:=arm-none-eabi-} + : ${ARM_LINUX_GNUEABI_GCC_PREFIX:=arm-linux-gnueabi-} # if MAKEFLAGS is not set add the -j option to speed up invocations of make if [ -z "${MAKEFLAGS+set}" ]; then @@ -230,6 +231,9 @@ General options: --arm-none-eabi-gcc-prefix= Prefix for a cross-compiler for arm-none-eabi (default: "${ARM_NONE_EABI_GCC_PREFIX}") + --arm-linux-gnueabi-gcc-prefix= + Prefix for a cross-compiler for arm-linux-gnueabi + (default: "${ARM_LINUX_GNUEABI_GCC_PREFIX}") --armcc Run ARM Compiler builds (on by default). --except Exclude the COMPONENTs listed on the command line, instead of running only those. @@ -387,6 +391,7 @@ pre_parse_command_line () { case "$1" in --append-outcome) append_outcome=1;; --arm-none-eabi-gcc-prefix) shift; ARM_NONE_EABI_GCC_PREFIX="$1";; + --arm-linux-gnueabi-gcc-prefix) shift; ARM_LINUX_GNUEABI_GCC_PREFIX="$1";; --armcc) no_armcc=;; --armc5-bin-dir) shift; ARMC5_BIN_DIR="$1";; --armc6-bin-dir) shift; ARMC6_BIN_DIR="$1";; @@ -2369,14 +2374,29 @@ component_build_arm_none_eabi_gcc () { ${ARM_NONE_EABI_GCC_PREFIX}size library/*.o } -component_build_arm_none_eabi_gcc_arm5vte () { - msg "build: ${ARM_NONE_EABI_GCC_PREFIX}gcc -march=arm5vte" # ~ 10s +component_build_arm_linux_gnueabi_gcc_arm5vte () { + msg "build: ${ARM_LINUX_GNUEABI_GCC_PREFIX}gcc -march=arm5vte" # ~ 10s scripts/config.py baremetal # Build for a target platform that's close to what Debian uses # for its "armel" distribution (https://wiki.debian.org/ArmEabiPort). # See https://github.com/ARMmbed/mbedtls/pull/2169 and comments. - # It would be better to build with arm-linux-gnueabi-gcc but - # we don't have that on our CI at this time. + # Build everything including programs, see for example + # https://github.com/ARMmbed/mbedtls/pull/3449#issuecomment-675313720 + make CC="${ARM_LINUX_GNUEABI_GCC_PREFIX}gcc" AR="${ARM_LINUX_GNUEABI_GCC_PREFIX}ar" CFLAGS='-Werror -Wall -Wextra -march=armv5te -O1' LDFLAGS='-march=armv5te' + + msg "size: ${ARM_LINUX_GNUEABI_GCC_PREFIX}gcc -march=armv5te -O1" + ${ARM_LINUX_GNUEABI_GCC_PREFIX}size library/*.o +} +support_build_arm_linux_gnueabi_gcc_arm5vte () { + type ${ARM_LINUX_GNUEABI_GCC_PREFIX}gcc >/dev/null 2>&1 +} + +component_build_arm_none_eabi_gcc_arm5vte () { + msg "build: ${ARM_NONE_EABI_GCC_PREFIX}gcc -march=arm5vte" # ~ 10s + scripts/config.py baremetal + # This is an imperfect substitute for + # component_build_arm_linux_gnueabi_gcc_arm5vte + # in case the gcc-arm-linux-gnueabihf toolchain is not available make CC="${ARM_NONE_EABI_GCC_PREFIX}gcc" AR="${ARM_NONE_EABI_GCC_PREFIX}ar" CFLAGS='-std=c99 -Werror -Wall -Wextra -march=armv5te -O1' LDFLAGS='-march=armv5te' SHELL='sh -x' lib msg "size: ${ARM_NONE_EABI_GCC_PREFIX}gcc -march=armv5te -O1" From efd14bf9bd5bfa686d6f422a317f946ad472f5bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Tue, 18 Aug 2020 10:31:36 +0200 Subject: [PATCH 022/120] Enable arm-linux-gnueabi-gcc build on Travis MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Manuel Pégourié-Gonnard --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 48faa4846c17..8fa0b343e40a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,7 @@ jobs: - graphviz - gcc-arm-none-eabi - libnewlib-arm-none-eabi + - gcc-arm-linux-gnueabi language: python # Needed to get pip for Python 3 python: 3.5 # version from Ubuntu 16.04 install: @@ -22,7 +23,7 @@ jobs: - tests/scripts/all.sh -k 'check_*' - tests/scripts/all.sh -k test_default_out_of_box - tests/scripts/all.sh -k test_ref_configs - - tests/scripts/all.sh -k build_arm_none_eabi_gcc_arm5vte build_arm_none_eabi_gcc_m0plus + - tests/scripts/all.sh -k build_arm_linux_gnueabi_gcc_arm5vte build_arm_none_eabi_gcc_m0plus - name: full configuration script: From ae505eeeed006c907bf05697eac8c7e8fd38aee4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Tue, 6 Jul 2021 09:44:59 +0200 Subject: [PATCH 023/120] Fix missing dependency on Travis MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Was getting errors like: In file included from /usr/include/limits.h:25:0, from /usr/lib/gcc-cross/arm-linux-gnueabi/5/include-fixed/limits.h:168, from /usr/lib/gcc-cross/arm-linux-gnueabi/5/include-fixed/syslimits.h:7, from /usr/lib/gcc-cross/arm-linux-gnueabi/5/include-fixed/limits.h:34, from ../include/mbedtls/check_config.h:30, from ../include/mbedtls/build_info.h:81, from common.h:26, from asn1write.c:20: /usr/include/features.h:367:25: fatal error: sys/cdefs.h: No such file or directory There are two packages to choose from: armhf or armel. Since the comment in all.sh says we're trying to be close to Debian's "armel" architecture, choose that, and fix a comment that was mentioning gnueabihf for no apparent reason. Signed-off-by: Manuel Pégourié-Gonnard --- .travis.yml | 1 + tests/scripts/all.sh | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 8fa0b343e40a..39ae19c19099 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,6 +15,7 @@ jobs: - gcc-arm-none-eabi - libnewlib-arm-none-eabi - gcc-arm-linux-gnueabi + - libc6-dev-armel-cross language: python # Needed to get pip for Python 3 python: 3.5 # version from Ubuntu 16.04 install: diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 7570b1128f31..e103c9a3de61 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -2396,7 +2396,7 @@ component_build_arm_none_eabi_gcc_arm5vte () { scripts/config.py baremetal # This is an imperfect substitute for # component_build_arm_linux_gnueabi_gcc_arm5vte - # in case the gcc-arm-linux-gnueabihf toolchain is not available + # in case the gcc-arm-linux-gnueabi toolchain is not available make CC="${ARM_NONE_EABI_GCC_PREFIX}gcc" AR="${ARM_NONE_EABI_GCC_PREFIX}ar" CFLAGS='-std=c99 -Werror -Wall -Wextra -march=armv5te -O1' LDFLAGS='-march=armv5te' SHELL='sh -x' lib msg "size: ${ARM_NONE_EABI_GCC_PREFIX}gcc -march=armv5te -O1" From 1d475b63981742afe9a90b2e70330111c8b65c3d Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 3 Aug 2021 13:43:36 +0200 Subject: [PATCH 024/120] Disable wildcards when checking for unsupported components Otherwise $COMMAND_LINE_COMPONENTS would try to expand wildcard patterns based on files in the current directory. Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 6cdc922fe313..4dfbaec668ef 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -444,7 +444,9 @@ pre_parse_command_line () { if [ $all_except -eq 0 ]; then unsupported=0 + set -f for component in $COMMAND_LINE_COMPONENTS; do + set +f case $component in *[*?\[]*) continue;; esac @@ -455,6 +457,7 @@ pre_parse_command_line () { unsupported=$((unsupported + 1));; esac done + set +f if [ $unsupported -ne 0 ]; then exit 2 fi From bf66e2cc8ffdd17c954d10fcc1431e41214aff7f Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 3 Aug 2021 13:44:28 +0200 Subject: [PATCH 025/120] Documentation improvements Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 4dfbaec668ef..2dc13756a8a1 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -271,7 +271,9 @@ Tool path options: EOF } -# remove built files as well as the cmake cache/config +# Cleanup before/after running a component. +# Remove built files as well as the cmake cache/config. +# Does not remove generated source files. cleanup() { command make clean @@ -306,6 +308,8 @@ cleanup() done } +# Final cleanup when this script exits (except when exiting on a failure +# in non-keep-going mode). final_cleanup () { cleanup @@ -442,11 +446,14 @@ pre_parse_command_line () { COMMAND_LINE_COMPONENTS="$COMMAND_LINE_COMPONENTS *_armcc*" fi + # Error out if an explicitly requested component doesn't exist. if [ $all_except -eq 0 ]; then unsupported=0 set -f for component in $COMMAND_LINE_COMPONENTS; do set +f + # If the requested name includes a wildcard character, don't + # check it. Accept wildcard patterns that don't match anything. case $component in *[*?\[]*) continue;; esac From 3cbd69c4d42df872ba3bec1fe511a0183255da74 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 5 Aug 2021 15:10:27 +0200 Subject: [PATCH 026/120] Switch to 4-space indentation Signed-off-by: Gilles Peskine --- programs/test/generate_cpp_dummy_build.sh | 37 ++++++++++++----------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/programs/test/generate_cpp_dummy_build.sh b/programs/test/generate_cpp_dummy_build.sh index 41adf149ebe8..9e16348b529e 100755 --- a/programs/test/generate_cpp_dummy_build.sh +++ b/programs/test/generate_cpp_dummy_build.sh @@ -21,7 +21,7 @@ set -e export LC_ALL=C print_cpp () { - cat <<'EOF' + cat <<'EOF' /* Automatically generated file. Do not edit. * * This program is a dummy C++ program to ensure Mbed TLS library header files @@ -47,19 +47,20 @@ print_cpp () { EOF - for header in include/mbedtls/*.h include/psa/*.h; do - case ${header#include/} in - mbedtls/mbedtls_config.h) :;; # not meant for direct inclusion - psa/crypto_config.h) :;; # not meant for direct inclusion - # Some of the psa/crypto_*.h headers are not meant to be included directly. - # They do have include guards that make them no-ops if psa/crypto.h - # has been included before. Since psa/crypto.h comes before psa/crypto_*.h - # in the wildcard enumeration, we don't need to skip those headers. - *) echo "#include \"${header#include/}\"";; - esac - done + for header in include/mbedtls/*.h include/psa/*.h; do + case ${header#include/} in + mbedtls/mbedtls_config.h) :;; # not meant for direct inclusion + psa/crypto_config.h) :;; # not meant for direct inclusion + # Some of the psa/crypto_*.h headers are not meant to be included + # directly. They do have include guards that make them no-ops if + # psa/crypto.h has been included before. Since psa/crypto.h comes + # before psa/crypto_*.h in the wildcard enumeration, we don't need + # to skip those headers. + *) echo "#include \"${header#include/}\"";; + esac + done - cat <<'EOF' + cat <<'EOF' int main() { @@ -72,14 +73,14 @@ EOF } if [ -d include/mbedtls ]; then - : + : elif [ -d ../include/mbedtls ]; then - cd .. + cd .. elif [ -d ../../include/mbedtls ]; then - cd ../.. + cd ../.. else - echo >&2 "This script must be run from an Mbed TLS source tree." - exit 3 + echo >&2 "This script must be run from an Mbed TLS source tree." + exit 3 fi print_cpp >"${1:-programs/test/cpp_dummy_build.cpp}" From 7530163f3b003e4851a48cb5661b1b1d4dbf6e6a Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 5 Aug 2021 15:10:47 +0200 Subject: [PATCH 027/120] Make --quiet more effective when running make generated_files Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 2dc13756a8a1..7e8b2c3c2004 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -752,7 +752,7 @@ pre_generate_files() { # file that might be around before generating fresh ones make neat if [ $QUIET -eq 1 ]; then - make -s generated_files + make generated_files >/dev/null else make generated_files fi From 86f6129067b7f2f6f6ff7aa1e1af69660e8f2d9a Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 5 Aug 2021 15:11:33 +0200 Subject: [PATCH 028/120] Documentation improvement Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 7e8b2c3c2004..6c322e29fc69 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -242,7 +242,7 @@ General options: (default: "${ARM_NONE_EABI_GCC_PREFIX}") --armcc Run ARM Compiler builds (on by default). --error-test Error test mode: run a failing function in addition - to any specified component. + to any specified component. May be repeated. --except Exclude the COMPONENTs listed on the command line, instead of running only those. --no-append-outcome Write a new outcome file and analyze it (default). From 91e890e2fcefca43836dc174ad37906a6d6c996f Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 5 Aug 2021 15:13:57 +0200 Subject: [PATCH 029/120] Add documentation Signed-off-by: Gilles Peskine --- programs/test/generate_cpp_dummy_build.sh | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/programs/test/generate_cpp_dummy_build.sh b/programs/test/generate_cpp_dummy_build.sh index 9e16348b529e..94e911515d3e 100755 --- a/programs/test/generate_cpp_dummy_build.sh +++ b/programs/test/generate_cpp_dummy_build.sh @@ -1,5 +1,18 @@ #!/bin/sh +DEFAULT_OUTPUT_FILE=programs/test/cpp_dummy_build.cpp + +if [ "$1" = "--help" ]; then + cat <"${1:-programs/test/cpp_dummy_build.cpp}" +print_cpp >"${1:-$DEFAULT_OUTPUT_FILE}" From 03af67891194788791572fe892eb5d49c0ab9202 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 6 Aug 2021 11:35:17 +0200 Subject: [PATCH 030/120] Documentation improvements Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 6c322e29fc69..26a5b7e5deba 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -203,6 +203,8 @@ pre_initialize_variables () { # Test whether the component $1 is included in the command line patterns. is_component_included() { + # Temporarily disable wildcard expansion so that $COMMAND_LINE_COMPONENTS + # only does word splitting. set -f for pattern in $COMMAND_LINE_COMPONENTS; do set +f @@ -449,6 +451,8 @@ pre_parse_command_line () { # Error out if an explicitly requested component doesn't exist. if [ $all_except -eq 0 ]; then unsupported=0 + # Temporarily disable wildcard expansion so that $COMMAND_LINE_COMPONENTS + # only does word splitting. set -f for component in $COMMAND_LINE_COMPONENTS; do set +f @@ -552,6 +556,9 @@ pre_setup_keep_going () { # Whether it makes sense to keep a component going after the specified # command fails (test command) or not (configure or build). + # This function normally receives the failing simple command + # ($BASH_COMMAND) as an argument, but if $report_failed_command is set, + # this is passed instead. # This doesn't have to be 100% accurate: all failures are recorded anyway. # False positives result in running things that can't be expected to # work. False negatives result in things not running after something else From 80ddb991c2eab48bf5ed1d91ac0cb4a05facd143 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Fri, 6 Aug 2021 11:51:59 +0200 Subject: [PATCH 031/120] Add --restore option to clean up but not necessarily run components Signed-off-by: Gilles Peskine --- tests/scripts/all.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 26a5b7e5deba..f963e4bf039f 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -243,6 +243,9 @@ General options: Prefix for a cross-compiler for arm-none-eabi (default: "${ARM_NONE_EABI_GCC_PREFIX}") --armcc Run ARM Compiler builds (on by default). + --restore First clean up the build tree, restoring backed up + files. Do not run any components unless they are + explicitly specified. --error-test Error test mode: run a failing function in addition to any specified component. May be repeated. --except Exclude the COMPONENTs listed on the command line, @@ -388,6 +391,7 @@ pre_parse_command_line () { COMMAND_LINE_COMPONENTS= all_except=0 error_test=0 + restore_first=0 no_armcc= # Note that legacy options are ignored instead of being omitted from this @@ -426,6 +430,7 @@ pre_parse_command_line () { --quiet|-q) QUIET=1;; --random-seed) unset SEED;; --release-test|-r) SEED=$RELEASE_SEED;; + --restore) restore_first=1;; --seed|-s) shift; SEED="$1";; -*) echo >&2 "Unknown option: $1" @@ -438,7 +443,7 @@ pre_parse_command_line () { done # With no list of components, run everything. - if [ -z "$COMMAND_LINE_COMPONENTS" ]; then + if [ -z "$COMMAND_LINE_COMPONENTS" ] && [ $restore_first -eq 0 ]; then all_except=1 fi From 0402979ed39530a894812d9b9361de943bd8530f Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 10 Aug 2021 16:45:37 +0800 Subject: [PATCH 032/120] Add openssl/gnutls tls1.3 feature tests. Add functions and test cases to make sure tls1.3 is available in openssl/gnutls Change-Id: I797d15117a8de96614f392e6bb2ed16b6d71ba69 Signed-off-by: Jerry Yu --- tests/ssl-opt.sh | 77 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/tests/ssl-opt.sh b/tests/ssl-opt.sh index 01265ae9b3af..9ee6b761dbec 100755 --- a/tests/ssl-opt.sh +++ b/tests/ssl-opt.sh @@ -77,6 +77,14 @@ else O_LEGACY_CLI=false fi +if [ -n "${OPENSSL_NEXT:-}" ]; then + O_NEXT_SRV="$OPENSSL_NEXT s_server -www -cert data_files/server5.crt -key data_files/server5.key" + O_NEXT_CLI="echo 'GET / HTTP/1.0' | $OPENSSL_NEXT s_client" +else + O_NEXT_SRV=false + O_NEXT_CLI=false +fi + if [ -n "${GNUTLS_NEXT_SERV:-}" ]; then G_NEXT_SRV="$GNUTLS_NEXT_SERV --x509certfile data_files/server5.crt --x509keyfile data_files/server5.key" else @@ -346,6 +354,57 @@ requires_openssl_legacy() { fi } +requires_openssl_next() { + if [ -z "${OPENSSL_NEXT_AVAILABLE:-}" ]; then + if which "${OPENSSL_NEXT:-}" >/dev/null 2>&1; then + OPENSSL_NEXT_AVAILABLE="YES" + else + OPENSSL_NEXT_AVAILABLE="NO" + fi + fi + if [ "$OPENSSL_NEXT_AVAILABLE" = "NO" ]; then + SKIP_NEXT="YES" + fi +} + +# skip next test if tls1_3 is not available +requires_openssl_tls1_3() { + requires_openssl_next + if [ "$OPENSSL_NEXT_AVAILABLE" = "NO" ]; then + OPENSSL_TLS1_3_AVAILABLE="NO" + fi + if [ -z "${OPENSSL_TLS1_3_AVAILABLE:-}" ]; then + if $OPENSSL_NEXT s_client -help 2>&1 | grep tls1_3 >/dev/null + then + OPENSSL_TLS1_3_AVAILABLE="YES" + else + OPENSSL_TLS1_3_AVAILABLE="NO" + fi + fi + if [ "$OPENSSL_TLS1_3_AVAILABLE" = "NO" ]; then + SKIP_NEXT="YES" + fi +} + +# skip next test if tls1_3 is not available +requires_gnutls_tls1_3() { + requires_gnutls_next + if [ "$GNUTLS_NEXT_AVAILABLE" = "NO" ]; then + GNUTLS_TLS1_3_AVAILABLE="NO" + fi + if [ -z "${GNUTLS_TLS1_3_AVAILABLE:-}" ]; then + if $GNUTLS_NEXT_CLI -l 2>&1 | grep VERS-TLS1.3 >/dev/null + then + GNUTLS_TLS1_3_AVAILABLE="YES" + else + GNUTLS_TLS1_3_AVAILABLE="NO" + fi + fi + if [ "$GNUTLS_TLS1_3_AVAILABLE" = "NO" ]; then + SKIP_NEXT="YES" + fi +} + # skip next test if IPv6 isn't available on this host requires_ipv6() { if [ -z "${HAS_IPV6:-}" ]; then @@ -8487,6 +8546,24 @@ run_test "export keys functionality" \ -c "EAP-TLS IV is:" \ -s "EAP-TLS IV is:" +# openssl feature tests: check if tls1.3 exists. +requires_openssl_tls1_3 +run_test "TLS1.3: Test openssl tls1_3 feature" \ + "$O_NEXT_SRV -tls1_3 -msg" \ + "$O_NEXT_CLI -tls1_3 -msg" \ + 0 \ + -c "TLS 1.3" \ + -s "TLS 1.3" + +# gnutls feature tests: check if tls1.3 exists. +requires_gnutls_tls1_3 +run_test "TLS1.3: Test gnutls tls1_3 feature" \ + "$G_NEXT_SRV --priority=NORMAL:-VERS-ALL:+VERS-TLS1.3" \ + "$G_NEXT_CLI localhost --priority=NORMAL:-VERS-ALL:+VERS-TLS1.3 -V" \ + 0 \ + -s "Version: TLS1.3" \ + -c "Version: TLS1.3" + # TLS1.3 test cases # TODO: remove or rewrite this test case if #4832 is resolved. requires_config_enabled MBEDTLS_SSL_PROTO_TLS1_2 From 7dd2f504b3ebeda92cdf7e80135db2026b0356bd Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Sat, 24 Apr 2021 13:35:41 +0200 Subject: [PATCH 033/120] Allow configuring MBEDTLS_TLS_EXT_CID at compile time The numerical identifier of the CID extension hasn't been settled yet and different implementations use values from different drafts. Allow configuring the value at compile time. Signed-off-by: Gilles Peskine --- ChangeLog.d/tls_ext_cid-config.txt | 3 +++ include/mbedtls/mbedtls_config.h | 11 +++++++++++ include/mbedtls/ssl.h | 8 +++++++- 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 ChangeLog.d/tls_ext_cid-config.txt diff --git a/ChangeLog.d/tls_ext_cid-config.txt b/ChangeLog.d/tls_ext_cid-config.txt new file mode 100644 index 000000000000..b7b1e72443e2 --- /dev/null +++ b/ChangeLog.d/tls_ext_cid-config.txt @@ -0,0 +1,3 @@ +Features + * The identifier of the CID TLS extension can be configured by defining + MBEDTLS_TLS_EXT_CID at compile time. diff --git a/include/mbedtls/mbedtls_config.h b/include/mbedtls/mbedtls_config.h index a60db7e930f7..d470c0054b4c 100644 --- a/include/mbedtls/mbedtls_config.h +++ b/include/mbedtls/mbedtls_config.h @@ -3194,6 +3194,17 @@ //#define MBEDTLS_PSK_MAX_LEN 32 /**< Max size of TLS pre-shared keys, in bytes (default 256 bits) */ //#define MBEDTLS_SSL_COOKIE_TIMEOUT 60 /**< Default expiration delay of DTLS cookies, in seconds if HAVE_TIME, or in number of cookies issued */ +/** \def MBEDTLS_TLS_EXT_CID + * + * At the time of writing, the CID extension has not been assigned its + * final value. Set this configuration option to make Mbed TLS use a + * different value. + * + * A future minor revision of Mbed TLS may change the default value of + * this option to match evolving standards and usage. + */ +//#define MBEDTLS_TLS_EXT_CID 254 + /** * Complete list of ciphersuites to use, in order of preference. * diff --git a/include/mbedtls/ssl.h b/include/mbedtls/ssl.h index 221cee33799f..167d741a036e 100644 --- a/include/mbedtls/ssl.h +++ b/include/mbedtls/ssl.h @@ -425,8 +425,14 @@ /* The value of the CID extension is still TBD as of * draft-ietf-tls-dtls-connection-id-05 - * (https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05) */ + * (https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05). + * + * A future minor revision of Mbed TLS may change the default value of + * this option to match evolving standards and usage. + */ +#if !defined(MBEDTLS_TLS_EXT_CID) #define MBEDTLS_TLS_EXT_CID 254 /* TBD */ +#endif #define MBEDTLS_TLS_EXT_ECJPAKE_KKPP 256 /* experimental */ From 8c51b73c86beca145271acdfa400631e5456fd7d Mon Sep 17 00:00:00 2001 From: lhuang04 Date: Sat, 14 Aug 2021 05:56:07 -0700 Subject: [PATCH 034/120] Update CMake version for CMP0090 Summary: [CMP0090](https://cmake.org/cmake/help/latest/policy/CMP0090.html) was introduced in CMake version 3.15. The CMake version guard should be greater or equal to 3.15. My cmake version is 3.14.5, and run into the following error. ``` cmake --version cmake version 3.14.5 ``` ``` CMake Error at CMakeLists.txt:338 (cmake_policy): Policy "CMP0090" is not known to this version of CMake. -- Configuring incomplete, errors occurred! Test Plan: ``` cmake ``` Signed-off-by: lhuang04 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cf74af53c47b..210aba489306 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -330,7 +330,7 @@ install( DESTINATION "cmake" FILE "MbedTLSTargets.cmake") -if(CMAKE_VERSION VERSION_GREATER 3.14) +if(CMAKE_VERSION VERSION_GREATER 3.15 OR CMAKE_VERSION VERSION_EQUAL 3.15) # Do not export the package by default cmake_policy(SET CMP0090 NEW) From 50dde56543f28fe138f55afea72ce161f61ddf27 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Tue, 22 Jun 2021 15:51:53 +0100 Subject: [PATCH 035/120] Implement byte reading macros into library/ To improve readability by saving horizontal and vertical space. Removed unecessary & 0xFF. Byte reading macros implemented in library/common.h, All files containing "& 0xff" were modified. Comments/Documentation not yet added to the macro definitions. Fixes #4274 Signed-off-by: Joe Subbiani --- library/common.h | 10 ++++++++++ library/ctr_drbg.c | 8 ++++---- library/nist_kw.c | 2 +- library/psa_crypto.c | 4 ++-- library/psa_its_file.c | 16 ++++++++-------- library/ssl_msg.c | 16 ++++++++-------- library/ssl_ticket.c | 4 ++-- 7 files changed, 35 insertions(+), 25 deletions(-) diff --git a/library/common.h b/library/common.h index 9e4b0312b11c..baef72d48020 100644 --- a/library/common.h +++ b/library/common.h @@ -66,4 +66,14 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c */ #define MBEDTLS_ALLOW_PRIVATE_ACCESS +/** Byte Reading Macros + * + * To tidy up code and save horizontal and vertical space, use byte + * reading macros to cast + */ +#define BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) ) +#define BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) ) +#define BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) ) +#define BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) ) + #endif /* MBEDTLS_LIBRARY_COMMON_H */ diff --git a/library/ctr_drbg.c b/library/ctr_drbg.c index b664fb0fc775..d52d9ef25403 100644 --- a/library/ctr_drbg.c +++ b/library/ctr_drbg.c @@ -152,10 +152,10 @@ static int block_cipher_df( unsigned char *output, * (Total is padded to a multiple of 16-bytes with zeroes) */ p = buf + MBEDTLS_CTR_DRBG_BLOCKSIZE; - *p++ = ( data_len >> 24 ) & 0xff; - *p++ = ( data_len >> 16 ) & 0xff; - *p++ = ( data_len >> 8 ) & 0xff; - *p++ = ( data_len ) & 0xff; + *p++ = BYTE_3( data_len ); + *p++ = BYTE_2( data_len ); + *p++ = BYTE_1( data_len ); + *p++ = BYTE_0( data_len ); p += 3; *p++ = MBEDTLS_CTR_DRBG_SEEDLEN; memcpy( p, data, data_len ); diff --git a/library/nist_kw.c b/library/nist_kw.c index 5054ca206b92..3fff2b7f8523 100644 --- a/library/nist_kw.c +++ b/library/nist_kw.c @@ -169,7 +169,7 @@ static void calc_a_xor_t( unsigned char A[KW_SEMIBLOCK_LENGTH], uint64_t t ) size_t i = 0; for( i = 0; i < sizeof( t ); i++ ) { - A[i] ^= ( t >> ( ( sizeof( t ) - 1 - i ) * 8 ) ) & 0xff; + A[i] ^= BYTE_0( t >> ( ( sizeof( t ) - 1 - i ) * 8 ) ); } } diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 3574b9842acd..b46e023e5dbf 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -4506,8 +4506,8 @@ static psa_status_t psa_tls12_prf_psk_to_ms_set_key( * uint16 with the value N, and the PSK itself. */ - *cur++ = ( data_length >> 8 ) & 0xff; - *cur++ = ( data_length >> 0 ) & 0xff; + *cur++ = BYTE_1( data_length ); + *cur++ = BYTE_0( data_length ); memset( cur, 0, data_length ); cur += data_length; *cur++ = pms[0]; diff --git a/library/psa_its_file.c b/library/psa_its_file.c index 492be1c7114a..66043b502e09 100644 --- a/library/psa_its_file.c +++ b/library/psa_its_file.c @@ -191,14 +191,14 @@ psa_status_t psa_its_set( psa_storage_uid_t uid, size_t n; memcpy( header.magic, PSA_ITS_MAGIC_STRING, PSA_ITS_MAGIC_LENGTH ); - header.size[0] = data_length & 0xff; - header.size[1] = ( data_length >> 8 ) & 0xff; - header.size[2] = ( data_length >> 16 ) & 0xff; - header.size[3] = ( data_length >> 24 ) & 0xff; - header.flags[0] = create_flags & 0xff; - header.flags[1] = ( create_flags >> 8 ) & 0xff; - header.flags[2] = ( create_flags >> 16 ) & 0xff; - header.flags[3] = ( create_flags >> 24 ) & 0xff; + header.size[0] = BYTE_0( data_length ); + header.size[1] = BYTE_1( data_length ); + header.size[2] = BYTE_2( data_length ); + header.size[3] = BYTE_3( data_length ); + header.flags[0] = BYTE_0( create_flags ); + header.flags[1] = BYTE_1( create_flags ); + header.flags[2] = BYTE_2( create_flags ); + header.flags[3] = BYTE_3( create_flags ); psa_its_fill_filename( uid, filename ); stream = fopen( PSA_ITS_STORAGE_TEMP, "wb" ); diff --git a/library/ssl_msg.c b/library/ssl_msg.c index a03f19251ded..1fd9420c24bb 100644 --- a/library/ssl_msg.c +++ b/library/ssl_msg.c @@ -2254,14 +2254,14 @@ int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl ) * copy beginning of headers then fill fragmentation fields. * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */ memcpy( ssl->out_msg, cur->p, 6 ); - - ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff ); - ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff ); - ssl->out_msg[8] = ( ( frag_off ) & 0xff ); - - ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff ); - ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff ); - ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff ); + + ssl->out_msg[6] = BYTE_2( frag_off ); + ssl->out_msg[7] = BYTE_1( frag_off ); + ssl->out_msg[8] = BYTE_0( frag_off ); + + ssl->out_msg[ 9] = BYTE_2( cur_hs_frag_len ); + ssl->out_msg[10] = BYTE_1( cur_hs_frag_len ); + ssl->out_msg[11] = BYTE_0( cur_hs_frag_len ); MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 ); diff --git a/library/ssl_ticket.c b/library/ssl_ticket.c index 940e1a67a20d..29d834575053 100644 --- a/library/ssl_ticket.c +++ b/library/ssl_ticket.c @@ -245,8 +245,8 @@ int mbedtls_ssl_ticket_write( void *p_ticket, { goto cleanup; } - state_len_bytes[0] = ( clear_len >> 8 ) & 0xff; - state_len_bytes[1] = ( clear_len ) & 0xff; + state_len_bytes[0] = BYTE_1( clear_len ); + state_len_bytes[1] = BYTE_0( clear_len ); /* Encrypt and authenticate */ if( ( ret = mbedtls_cipher_auth_encrypt_ext( &key->ctx, From 3b394509da07fd7b78a9d9396d838bd588c6ba59 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Wed, 23 Jun 2021 11:23:44 +0100 Subject: [PATCH 036/120] Move BYTES_TO_U32_LE to common.h The macro BYTES_TO_U32_LE appears in poly1305.c and chacha20.c. Removes duplicate code and save vertical space the macro has been moved to common.h. Improves maintainability. Signed-off-by: Joe Subbiani --- library/chacha20.c | 7 ------- library/common.h | 14 ++++++++++++-- library/poly1305.c | 7 ------- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/library/chacha20.c b/library/chacha20.c index 78467d3fc6ef..9862ea535a36 100644 --- a/library/chacha20.c +++ b/library/chacha20.c @@ -54,13 +54,6 @@ #define CHACHA20_VALIDATE( cond ) \ MBEDTLS_INTERNAL_VALIDATE( cond ) -#define BYTES_TO_U32_LE( data, offset ) \ - ( (uint32_t) (data)[offset] \ - | (uint32_t) ( (uint32_t) (data)[( offset ) + 1] << 8 ) \ - | (uint32_t) ( (uint32_t) (data)[( offset ) + 2] << 16 ) \ - | (uint32_t) ( (uint32_t) (data)[( offset ) + 3] << 24 ) \ - ) - #define ROTL32( value, amount ) \ ( (uint32_t) ( (value) << (amount) ) | ( (value) >> ( 32 - (amount) ) ) ) diff --git a/library/common.h b/library/common.h index baef72d48020..500e5d4c7d38 100644 --- a/library/common.h +++ b/library/common.h @@ -71,9 +71,19 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c * To tidy up code and save horizontal and vertical space, use byte * reading macros to cast */ -#define BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) ) -#define BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) ) +#define BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) ) +#define BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) ) #define BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) ) #define BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) ) +/** + * + */ +#define BYTES_TO_U32_LE( data, offset ) \ + ( (uint32_t) (data)[offset] \ + | (uint32_t) ( (uint32_t) (data)[( offset ) + 1] << 8 ) \ + | (uint32_t) ( (uint32_t) (data)[( offset ) + 2] << 16 ) \ + | (uint32_t) ( (uint32_t) (data)[( offset ) + 3] << 24 ) \ + ) + #endif /* MBEDTLS_LIBRARY_COMMON_H */ diff --git a/library/poly1305.c b/library/poly1305.c index 492d1457d417..a30b1707ed16 100644 --- a/library/poly1305.c +++ b/library/poly1305.c @@ -52,13 +52,6 @@ #define POLY1305_BLOCK_SIZE_BYTES ( 16U ) -#define BYTES_TO_U32_LE( data, offset ) \ - ( (uint32_t) (data)[offset] \ - | (uint32_t) ( (uint32_t) (data)[( offset ) + 1] << 8 ) \ - | (uint32_t) ( (uint32_t) (data)[( offset ) + 2] << 16 ) \ - | (uint32_t) ( (uint32_t) (data)[( offset ) + 3] << 24 ) \ - ) - /* * Our implementation is tuned for 32-bit platforms with a 64-bit multiplier. * However we provided an alternative for platforms without such a multiplier. From 30d974c232cfdff6b91856c99ac8c11de3135915 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Wed, 23 Jun 2021 11:49:03 +0100 Subject: [PATCH 037/120] Move UINT32_BE macros to common.h 32-bit integer manipulation macros (big edian): GET_UINT32_BE and PUT_UINT32_BE appear in several files in library/. Removes duplicate code and save vertical space the macro has been moved to common.h. Improves maintainability. Signed-off-by: Joe Subbiani --- library/camellia.c | 23 ----------------------- library/common.h | 23 +++++++++++++++++++++++ library/des.c | 23 ----------------------- library/gcm.c | 23 ----------------------- library/nist_kw.c | 20 -------------------- library/sha1.c | 23 ----------------------- library/sha256.c | 23 ----------------------- 7 files changed, 23 insertions(+), 135 deletions(-) diff --git a/library/camellia.c b/library/camellia.c index f7e013611ba4..0817b1d292de 100644 --- a/library/camellia.c +++ b/library/camellia.c @@ -49,29 +49,6 @@ #define CAMELLIA_VALIDATE( cond ) \ MBEDTLS_INTERNAL_VALIDATE( cond ) -/* - * 32-bit integer manipulation macros (big endian) - */ -#ifndef GET_UINT32_BE -#define GET_UINT32_BE(n,b,i) \ -{ \ - (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ - | ( (uint32_t) (b)[(i) + 1] << 16 ) \ - | ( (uint32_t) (b)[(i) + 2] << 8 ) \ - | ( (uint32_t) (b)[(i) + 3] ); \ -} -#endif - -#ifndef PUT_UINT32_BE -#define PUT_UINT32_BE(n,b,i) \ -{ \ - (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ - (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ - (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ - (b)[(i) + 3] = (unsigned char) ( (n) ); \ -} -#endif - static const unsigned char SIGMA_CHARS[6][8] = { { 0xa0, 0x9e, 0x66, 0x7f, 0x3b, 0xcc, 0x90, 0x8b }, diff --git a/library/common.h b/library/common.h index 500e5d4c7d38..b3b70dc62f6c 100644 --- a/library/common.h +++ b/library/common.h @@ -76,6 +76,29 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c #define BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) ) #define BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) ) +/* + * 32-bit integer manipulation macros (big endian) + */ +#ifndef GET_UINT32_BE +#define GET_UINT32_BE(n,b,i) \ +{ \ + (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ + | ( (uint32_t) (b)[(i) + 1] << 16 ) \ + | ( (uint32_t) (b)[(i) + 2] << 8 ) \ + | ( (uint32_t) (b)[(i) + 3] ); \ +} +#endif + +#ifndef PUT_UINT32_BE +#define PUT_UINT32_BE(n,b,i) \ +{ \ + (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ + (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ + (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ + (b)[(i) + 3] = (unsigned char) ( (n) ); \ +} +#endif + /** * */ diff --git a/library/des.c b/library/des.c index eddf55e78957..36ea27776017 100644 --- a/library/des.c +++ b/library/des.c @@ -43,29 +43,6 @@ #if !defined(MBEDTLS_DES_ALT) -/* - * 32-bit integer manipulation macros (big endian) - */ -#ifndef GET_UINT32_BE -#define GET_UINT32_BE(n,b,i) \ -{ \ - (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ - | ( (uint32_t) (b)[(i) + 1] << 16 ) \ - | ( (uint32_t) (b)[(i) + 2] << 8 ) \ - | ( (uint32_t) (b)[(i) + 3] ); \ -} -#endif - -#ifndef PUT_UINT32_BE -#define PUT_UINT32_BE(n,b,i) \ -{ \ - (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ - (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ - (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ - (b)[(i) + 3] = (unsigned char) ( (n) ); \ -} -#endif - /* * Expanded DES S-boxes */ diff --git a/library/gcm.c b/library/gcm.c index 835b1b285351..c8254876dd84 100644 --- a/library/gcm.c +++ b/library/gcm.c @@ -58,29 +58,6 @@ #define GCM_VALIDATE( cond ) \ MBEDTLS_INTERNAL_VALIDATE( cond ) -/* - * 32-bit integer manipulation macros (big endian) - */ -#ifndef GET_UINT32_BE -#define GET_UINT32_BE(n,b,i) \ -{ \ - (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ - | ( (uint32_t) (b)[(i) + 1] << 16 ) \ - | ( (uint32_t) (b)[(i) + 2] << 8 ) \ - | ( (uint32_t) (b)[(i) + 3] ); \ -} -#endif - -#ifndef PUT_UINT32_BE -#define PUT_UINT32_BE(n,b,i) \ -{ \ - (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ - (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ - (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ - (b)[(i) + 3] = (unsigned char) ( (n) ); \ -} -#endif - /* * Initialize a context */ diff --git a/library/nist_kw.c b/library/nist_kw.c index 3fff2b7f8523..c0eed674c11b 100644 --- a/library/nist_kw.c +++ b/library/nist_kw.c @@ -77,26 +77,6 @@ static const unsigned char NIST_KW_ICV1[] = {0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, /*! The 32-bit default integrity check value (ICV) for KWP mode. */ static const unsigned char NIST_KW_ICV2[] = {0xA6, 0x59, 0x59, 0xA6}; -#ifndef GET_UINT32_BE -#define GET_UINT32_BE(n,b,i) \ -do { \ - (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ - | ( (uint32_t) (b)[(i) + 1] << 16 ) \ - | ( (uint32_t) (b)[(i) + 2] << 8 ) \ - | ( (uint32_t) (b)[(i) + 3] ); \ -} while( 0 ) -#endif - -#ifndef PUT_UINT32_BE -#define PUT_UINT32_BE(n,b,i) \ -do { \ - (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ - (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ - (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ - (b)[(i) + 3] = (unsigned char) ( (n) ); \ -} while( 0 ) -#endif - /* * Initialize context */ diff --git a/library/sha1.c b/library/sha1.c index 545d093109fd..9beaee5a5400 100644 --- a/library/sha1.c +++ b/library/sha1.c @@ -48,29 +48,6 @@ #if !defined(MBEDTLS_SHA1_ALT) -/* - * 32-bit integer manipulation macros (big endian) - */ -#ifndef GET_UINT32_BE -#define GET_UINT32_BE(n,b,i) \ -{ \ - (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ - | ( (uint32_t) (b)[(i) + 1] << 16 ) \ - | ( (uint32_t) (b)[(i) + 2] << 8 ) \ - | ( (uint32_t) (b)[(i) + 3] ); \ -} -#endif - -#ifndef PUT_UINT32_BE -#define PUT_UINT32_BE(n,b,i) \ -{ \ - (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ - (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ - (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ - (b)[(i) + 3] = (unsigned char) ( (n) ); \ -} -#endif - void mbedtls_sha1_init( mbedtls_sha1_context *ctx ) { SHA1_VALIDATE( ctx != NULL ); diff --git a/library/sha256.c b/library/sha256.c index 6ec6da296adf..f548c672ea72 100644 --- a/library/sha256.c +++ b/library/sha256.c @@ -50,29 +50,6 @@ #if !defined(MBEDTLS_SHA256_ALT) -/* - * 32-bit integer manipulation macros (big endian) - */ -#ifndef GET_UINT32_BE -#define GET_UINT32_BE(n,b,i) \ -do { \ - (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ - | ( (uint32_t) (b)[(i) + 1] << 16 ) \ - | ( (uint32_t) (b)[(i) + 2] << 8 ) \ - | ( (uint32_t) (b)[(i) + 3] ); \ -} while( 0 ) -#endif - -#ifndef PUT_UINT32_BE -#define PUT_UINT32_BE(n,b,i) \ -do { \ - (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ - (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ - (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ - (b)[(i) + 3] = (unsigned char) ( (n) ); \ -} while( 0 ) -#endif - void mbedtls_sha256_init( mbedtls_sha256_context *ctx ) { SHA256_VALIDATE( ctx != NULL ); From 54c6134ff7de2ccc5d5cc2ef55f766d9a3a1fbdb Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Wed, 23 Jun 2021 12:16:47 +0100 Subject: [PATCH 038/120] Move UINT32_LE macros to common.h 32-bit integer manipulation macros (little edian): GET_UINT32_LE and PUT_UINT32_LE appear in several files in library/. Removes duplicate code and save vertical space the macro has been moved to common.h. Improves maintainability. Also provided brief comment in common.h for BYTES_TO_U32_LE. comment/documentation will probably need to be edited further for all recent additions to library/common.h Signed-off-by: Joe Subbiani --- library/aes.c | 23 ----------------------- library/aria.c | 23 ----------------------- library/common.h | 25 ++++++++++++++++++++++++- library/md5.c | 23 ----------------------- library/psa_crypto_storage.c | 23 ----------------------- library/ripemd160.c | 23 ----------------------- 6 files changed, 24 insertions(+), 116 deletions(-) diff --git a/library/aes.c b/library/aes.c index a15a80924a3f..7b4fb869e01f 100644 --- a/library/aes.c +++ b/library/aes.c @@ -57,29 +57,6 @@ #define AES_VALIDATE( cond ) \ MBEDTLS_INTERNAL_VALIDATE( cond ) -/* - * 32-bit integer manipulation macros (little endian) - */ -#ifndef GET_UINT32_LE -#define GET_UINT32_LE(n,b,i) \ -{ \ - (n) = ( (uint32_t) (b)[(i) ] ) \ - | ( (uint32_t) (b)[(i) + 1] << 8 ) \ - | ( (uint32_t) (b)[(i) + 2] << 16 ) \ - | ( (uint32_t) (b)[(i) + 3] << 24 ); \ -} -#endif - -#ifndef PUT_UINT32_LE -#define PUT_UINT32_LE(n,b,i) \ -{ \ - (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ - (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ - (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ - (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ -} -#endif - #if defined(MBEDTLS_PADLOCK_C) && \ ( defined(MBEDTLS_HAVE_X86) || defined(MBEDTLS_PADLOCK_ALIGN16) ) static int aes_padlock_ace = -1; diff --git a/library/aria.c b/library/aria.c index a5786b37ab38..d7d2bea7c87f 100644 --- a/library/aria.c +++ b/library/aria.c @@ -55,29 +55,6 @@ #define ARIA_VALIDATE( cond ) \ MBEDTLS_INTERNAL_VALIDATE( cond ) -/* - * 32-bit integer manipulation macros (little endian) - */ -#ifndef GET_UINT32_LE -#define GET_UINT32_LE( n, b, i ) \ -{ \ - (n) = ( (uint32_t) (b)[(i) ] ) \ - | ( (uint32_t) (b)[(i) + 1] << 8 ) \ - | ( (uint32_t) (b)[(i) + 2] << 16 ) \ - | ( (uint32_t) (b)[(i) + 3] << 24 ); \ -} -#endif - -#ifndef PUT_UINT32_LE -#define PUT_UINT32_LE( n, b, i ) \ -{ \ - (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ - (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ - (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ - (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ -} -#endif - /* * modify byte order: ( A B C D ) -> ( B A D C ), i.e. swap pairs of bytes * diff --git a/library/common.h b/library/common.h index b3b70dc62f6c..c2cf633c99b6 100644 --- a/library/common.h +++ b/library/common.h @@ -99,8 +99,31 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c } #endif +/* + * 32-bit integer manipulation macros (little endian) + */ +#ifndef GET_UINT32_LE +#define GET_UINT32_LE(n,b,i) \ +{ \ + (n) = ( (uint32_t) (b)[(i) ] ) \ + | ( (uint32_t) (b)[(i) + 1] << 8 ) \ + | ( (uint32_t) (b)[(i) + 2] << 16 ) \ + | ( (uint32_t) (b)[(i) + 3] << 24 ); \ +} +#endif + +#ifndef PUT_UINT32_LE +#define PUT_UINT32_LE(n,b,i) \ +{ \ + (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ + (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ + (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ + (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ +} +#endif + /** - * + * 32-bit integer conversion from bytes (little endian) */ #define BYTES_TO_U32_LE( data, offset ) \ ( (uint32_t) (data)[offset] \ diff --git a/library/md5.c b/library/md5.c index d7f8cee4681c..d8f637da836e 100644 --- a/library/md5.c +++ b/library/md5.c @@ -43,29 +43,6 @@ #if !defined(MBEDTLS_MD5_ALT) -/* - * 32-bit integer manipulation macros (little endian) - */ -#ifndef GET_UINT32_LE -#define GET_UINT32_LE(n,b,i) \ -{ \ - (n) = ( (uint32_t) (b)[(i) ] ) \ - | ( (uint32_t) (b)[(i) + 1] << 8 ) \ - | ( (uint32_t) (b)[(i) + 2] << 16 ) \ - | ( (uint32_t) (b)[(i) + 3] << 24 ); \ -} -#endif - -#ifndef PUT_UINT32_LE -#define PUT_UINT32_LE(n,b,i) \ -{ \ - (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ - (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ - (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ - (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ -} -#endif - void mbedtls_md5_init( mbedtls_md5_context *ctx ) { memset( ctx, 0, sizeof( mbedtls_md5_context ) ); diff --git a/library/psa_crypto_storage.c b/library/psa_crypto_storage.c index 09cbab4c4de3..36b518350cc7 100644 --- a/library/psa_crypto_storage.c +++ b/library/psa_crypto_storage.c @@ -230,29 +230,6 @@ static psa_status_t psa_crypto_storage_get_data_length( return( PSA_SUCCESS ); } -/* - * 32-bit integer manipulation macros (little endian) - */ -#ifndef GET_UINT32_LE -#define GET_UINT32_LE( n, b, i ) \ -{ \ - (n) = ( (uint32_t) (b)[(i) ] ) \ - | ( (uint32_t) (b)[(i) + 1] << 8 ) \ - | ( (uint32_t) (b)[(i) + 2] << 16 ) \ - | ( (uint32_t) (b)[(i) + 3] << 24 ); \ -} -#endif - -#ifndef PUT_UINT32_LE -#define PUT_UINT32_LE( n, b, i ) \ -{ \ - (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ - (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ - (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ - (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ -} -#endif - /* * 16-bit integer manipulation macros (little endian) */ diff --git a/library/ripemd160.c b/library/ripemd160.c index d2ccbbec47e5..d60654e9529c 100644 --- a/library/ripemd160.c +++ b/library/ripemd160.c @@ -44,29 +44,6 @@ #if !defined(MBEDTLS_RIPEMD160_ALT) -/* - * 32-bit integer manipulation macros (little endian) - */ -#ifndef GET_UINT32_LE -#define GET_UINT32_LE(n,b,i) \ -{ \ - (n) = ( (uint32_t) (b)[(i) ] ) \ - | ( (uint32_t) (b)[(i) + 1] << 8 ) \ - | ( (uint32_t) (b)[(i) + 2] << 16 ) \ - | ( (uint32_t) (b)[(i) + 3] << 24 ); \ -} -#endif - -#ifndef PUT_UINT32_LE -#define PUT_UINT32_LE(n,b,i) \ -{ \ - (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ - (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ - (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ - (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ -} -#endif - void mbedtls_ripemd160_init( mbedtls_ripemd160_context *ctx ) { memset( ctx, 0, sizeof( mbedtls_ripemd160_context ) ); From c4f3d5b38e4e1e88237f67380246c5f2fdeec7fd Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Wed, 23 Jun 2021 17:58:41 +0100 Subject: [PATCH 039/120] Add do-while protection to macros missed do-while around function-like macros (UINT32_BE and UINT_LE macros) originally present in the indivdual files, before being moved to common.h. Signed-off-by: Joe Subbiani --- library/common.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/library/common.h b/library/common.h index c2cf633c99b6..56514b34362e 100644 --- a/library/common.h +++ b/library/common.h @@ -81,22 +81,22 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c */ #ifndef GET_UINT32_BE #define GET_UINT32_BE(n,b,i) \ -{ \ +do { \ (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ | ( (uint32_t) (b)[(i) + 1] << 16 ) \ | ( (uint32_t) (b)[(i) + 2] << 8 ) \ | ( (uint32_t) (b)[(i) + 3] ); \ -} +} while( 0 ) #endif #ifndef PUT_UINT32_BE #define PUT_UINT32_BE(n,b,i) \ -{ \ +do { \ (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ (b)[(i) + 3] = (unsigned char) ( (n) ); \ -} +} while( 0 ) #endif /* @@ -104,22 +104,22 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c */ #ifndef GET_UINT32_LE #define GET_UINT32_LE(n,b,i) \ -{ \ +do { \ (n) = ( (uint32_t) (b)[(i) ] ) \ | ( (uint32_t) (b)[(i) + 1] << 8 ) \ | ( (uint32_t) (b)[(i) + 2] << 16 ) \ | ( (uint32_t) (b)[(i) + 3] << 24 ); \ -} +} while( 0 ) #endif #ifndef PUT_UINT32_LE #define PUT_UINT32_LE(n,b,i) \ -{ \ +do { \ (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ -} +} while( 0 ) #endif /** From 6f2bb0c8efd7fb3a74ececd00b9ca507f5494efe Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Thu, 24 Jun 2021 09:06:23 +0100 Subject: [PATCH 040/120] Remove trailing whitespace Trailing white spaces causing check_files.py to fail Signed-off-by: Joe Subbiani --- library/common.h | 6 +++--- library/ssl_msg.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/library/common.h b/library/common.h index 56514b34362e..b6b7d01d9d0f 100644 --- a/library/common.h +++ b/library/common.h @@ -67,8 +67,8 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c #define MBEDTLS_ALLOW_PRIVATE_ACCESS /** Byte Reading Macros - * - * To tidy up code and save horizontal and vertical space, use byte + * + * To tidy up code and save horizontal and vertical space, use byte * reading macros to cast */ #define BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) ) @@ -122,7 +122,7 @@ do { \ } while( 0 ) #endif -/** +/** * 32-bit integer conversion from bytes (little endian) */ #define BYTES_TO_U32_LE( data, offset ) \ diff --git a/library/ssl_msg.c b/library/ssl_msg.c index 1fd9420c24bb..f3dbba82b979 100644 --- a/library/ssl_msg.c +++ b/library/ssl_msg.c @@ -2254,7 +2254,7 @@ int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl ) * copy beginning of headers then fill fragmentation fields. * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */ memcpy( ssl->out_msg, cur->p, 6 ); - + ssl->out_msg[6] = BYTE_2( frag_off ); ssl->out_msg[7] = BYTE_1( frag_off ); ssl->out_msg[8] = BYTE_0( frag_off ); From 9aaec54e57e3ec869ad47ed0f6b67761b74f17fd Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Thu, 24 Jun 2021 11:00:08 +0100 Subject: [PATCH 041/120] Undo use of BYTE_x macro The use of the BYTE_x macro in nist_kw did not seem appropriate in hind sight as it is working with a character array not an int Signed-off-by: Joe Subbiani --- library/nist_kw.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/nist_kw.c b/library/nist_kw.c index c0eed674c11b..174a1eef1323 100644 --- a/library/nist_kw.c +++ b/library/nist_kw.c @@ -149,7 +149,7 @@ static void calc_a_xor_t( unsigned char A[KW_SEMIBLOCK_LENGTH], uint64_t t ) size_t i = 0; for( i = 0; i < sizeof( t ); i++ ) { - A[i] ^= BYTE_0( t >> ( ( sizeof( t ) - 1 - i ) * 8 ) ); + A[i] ^= ( t >> ( ( sizeof( t ) - 1 - i ) * 8 ) ) & 0xff; } } From 5ecac217f01aee0f73cffcb30e8b32f00921299f Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Thu, 24 Jun 2021 13:00:03 +0100 Subject: [PATCH 042/120] Prefixed macros with MBEDTLS As per tests/scripts/check-names.sh, macros in library/ header files should be prefixed with MBEDTLS_ The macro functions in common.h where also indented to comply with the same test Signed-off-by: Joe Subbiani --- library/aes.c | 34 ++++++++--------- library/aria.c | 34 ++++++++--------- library/camellia.c | 22 +++++------ library/chacha20.c | 22 +++++------ library/common.h | 74 ++++++++++++++++++------------------ library/ctr_drbg.c | 8 ++-- library/des.c | 20 +++++----- library/gcm.c | 34 ++++++++--------- library/md5.c | 44 ++++++++++----------- library/nist_kw.c | 4 +- library/poly1305.c | 26 ++++++------- library/psa_crypto.c | 4 +- library/psa_crypto_storage.c | 24 ++++++------ library/psa_its_file.c | 16 ++++---- library/ripemd160.c | 46 +++++++++++----------- library/sha1.c | 46 +++++++++++----------- library/sha256.c | 24 ++++++------ library/ssl_msg.c | 12 +++--- library/ssl_ticket.c | 4 +- 19 files changed, 249 insertions(+), 249 deletions(-) diff --git a/library/aes.c b/library/aes.c index 7b4fb869e01f..db726fe36b86 100644 --- a/library/aes.c +++ b/library/aes.c @@ -567,7 +567,7 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key, for( i = 0; i < ( keybits >> 5 ); i++ ) { - GET_UINT32_LE( RK[i], key, i << 2 ); + MBEDTLS_GET_UINT32_LE( RK[i], key, i << 2 ); } switch( ctx->nr ) @@ -850,10 +850,10 @@ int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx, uint32_t Y[4]; } t; - GET_UINT32_LE( t.X[0], input, 0 ); t.X[0] ^= *RK++; - GET_UINT32_LE( t.X[1], input, 4 ); t.X[1] ^= *RK++; - GET_UINT32_LE( t.X[2], input, 8 ); t.X[2] ^= *RK++; - GET_UINT32_LE( t.X[3], input, 12 ); t.X[3] ^= *RK++; + MBEDTLS_GET_UINT32_LE( t.X[0], input, 0 ); t.X[0] ^= *RK++; + MBEDTLS_GET_UINT32_LE( t.X[1], input, 4 ); t.X[1] ^= *RK++; + MBEDTLS_GET_UINT32_LE( t.X[2], input, 8 ); t.X[2] ^= *RK++; + MBEDTLS_GET_UINT32_LE( t.X[3], input, 12 ); t.X[3] ^= *RK++; for( i = ( ctx->nr >> 1 ) - 1; i > 0; i-- ) { @@ -887,10 +887,10 @@ int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx, ( (uint32_t) FSb[ ( t.Y[1] >> 16 ) & 0xFF ] << 16 ) ^ ( (uint32_t) FSb[ ( t.Y[2] >> 24 ) & 0xFF ] << 24 ); - PUT_UINT32_LE( t.X[0], output, 0 ); - PUT_UINT32_LE( t.X[1], output, 4 ); - PUT_UINT32_LE( t.X[2], output, 8 ); - PUT_UINT32_LE( t.X[3], output, 12 ); + MBEDTLS_PUT_UINT32_LE( t.X[0], output, 0 ); + MBEDTLS_PUT_UINT32_LE( t.X[1], output, 4 ); + MBEDTLS_PUT_UINT32_LE( t.X[2], output, 8 ); + MBEDTLS_PUT_UINT32_LE( t.X[3], output, 12 ); mbedtls_platform_zeroize( &t, sizeof( t ) ); @@ -914,10 +914,10 @@ int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx, uint32_t Y[4]; } t; - GET_UINT32_LE( t.X[0], input, 0 ); t.X[0] ^= *RK++; - GET_UINT32_LE( t.X[1], input, 4 ); t.X[1] ^= *RK++; - GET_UINT32_LE( t.X[2], input, 8 ); t.X[2] ^= *RK++; - GET_UINT32_LE( t.X[3], input, 12 ); t.X[3] ^= *RK++; + MBEDTLS_GET_UINT32_LE( t.X[0], input, 0 ); t.X[0] ^= *RK++; + MBEDTLS_GET_UINT32_LE( t.X[1], input, 4 ); t.X[1] ^= *RK++; + MBEDTLS_GET_UINT32_LE( t.X[2], input, 8 ); t.X[2] ^= *RK++; + MBEDTLS_GET_UINT32_LE( t.X[3], input, 12 ); t.X[3] ^= *RK++; for( i = ( ctx->nr >> 1 ) - 1; i > 0; i-- ) { @@ -951,10 +951,10 @@ int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx, ( (uint32_t) RSb[ ( t.Y[1] >> 16 ) & 0xFF ] << 16 ) ^ ( (uint32_t) RSb[ ( t.Y[0] >> 24 ) & 0xFF ] << 24 ); - PUT_UINT32_LE( t.X[0], output, 0 ); - PUT_UINT32_LE( t.X[1], output, 4 ); - PUT_UINT32_LE( t.X[2], output, 8 ); - PUT_UINT32_LE( t.X[3], output, 12 ); + MBEDTLS_PUT_UINT32_LE( t.X[0], output, 0 ); + MBEDTLS_PUT_UINT32_LE( t.X[1], output, 4 ); + MBEDTLS_PUT_UINT32_LE( t.X[2], output, 8 ); + MBEDTLS_PUT_UINT32_LE( t.X[3], output, 12 ); mbedtls_platform_zeroize( &t, sizeof( t ) ); diff --git a/library/aria.c b/library/aria.c index d7d2bea7c87f..a6319d3e29fa 100644 --- a/library/aria.c +++ b/library/aria.c @@ -385,7 +385,7 @@ static void aria_fe_xor( uint32_t r[4], const uint32_t p[4], * Big endian 128-bit rotation: r = a ^ (b <<< n), used only in key setup. * * We chose to store bytes into 32-bit words in little-endian format (see - * GET/PUT_UINT32_LE) so we need to reverse bytes here. + * GET/MBEDTLS_PUT_UINT32_LE) so we need to reverse bytes here. */ static void aria_rot128( uint32_t r[4], const uint32_t a[4], const uint32_t b[4], uint8_t n ) @@ -433,21 +433,21 @@ int mbedtls_aria_setkey_enc( mbedtls_aria_context *ctx, return( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA ); /* Copy key to W0 (and potential remainder to W1) */ - GET_UINT32_LE( w[0][0], key, 0 ); - GET_UINT32_LE( w[0][1], key, 4 ); - GET_UINT32_LE( w[0][2], key, 8 ); - GET_UINT32_LE( w[0][3], key, 12 ); + MBEDTLS_GET_UINT32_LE( w[0][0], key, 0 ); + MBEDTLS_GET_UINT32_LE( w[0][1], key, 4 ); + MBEDTLS_GET_UINT32_LE( w[0][2], key, 8 ); + MBEDTLS_GET_UINT32_LE( w[0][3], key, 12 ); memset( w[1], 0, 16 ); if( keybits >= 192 ) { - GET_UINT32_LE( w[1][0], key, 16 ); // 192 bit key - GET_UINT32_LE( w[1][1], key, 20 ); + MBEDTLS_GET_UINT32_LE( w[1][0], key, 16 ); // 192 bit key + MBEDTLS_GET_UINT32_LE( w[1][1], key, 20 ); } if( keybits == 256 ) { - GET_UINT32_LE( w[1][2], key, 24 ); // 256 bit key - GET_UINT32_LE( w[1][3], key, 28 ); + MBEDTLS_GET_UINT32_LE( w[1][2], key, 24 ); // 256 bit key + MBEDTLS_GET_UINT32_LE( w[1][3], key, 28 ); } i = ( keybits - 128 ) >> 6; // index: 0, 1, 2 @@ -524,10 +524,10 @@ int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx, ARIA_VALIDATE_RET( input != NULL ); ARIA_VALIDATE_RET( output != NULL ); - GET_UINT32_LE( a, input, 0 ); - GET_UINT32_LE( b, input, 4 ); - GET_UINT32_LE( c, input, 8 ); - GET_UINT32_LE( d, input, 12 ); + MBEDTLS_GET_UINT32_LE( a, input, 0 ); + MBEDTLS_GET_UINT32_LE( b, input, 4 ); + MBEDTLS_GET_UINT32_LE( c, input, 8 ); + MBEDTLS_GET_UINT32_LE( d, input, 12 ); i = 0; while( 1 ) @@ -559,10 +559,10 @@ int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx, c ^= ctx->rk[i][2]; d ^= ctx->rk[i][3]; - PUT_UINT32_LE( a, output, 0 ); - PUT_UINT32_LE( b, output, 4 ); - PUT_UINT32_LE( c, output, 8 ); - PUT_UINT32_LE( d, output, 12 ); + MBEDTLS_PUT_UINT32_LE( a, output, 0 ); + MBEDTLS_PUT_UINT32_LE( b, output, 4 ); + MBEDTLS_PUT_UINT32_LE( c, output, 8 ); + MBEDTLS_PUT_UINT32_LE( d, output, 12 ); return( 0 ); } diff --git a/library/camellia.c b/library/camellia.c index 0817b1d292de..9aab7ab675ef 100644 --- a/library/camellia.c +++ b/library/camellia.c @@ -353,8 +353,8 @@ int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, * Prepare SIGMA values */ for( i = 0; i < 6; i++ ) { - GET_UINT32_BE( SIGMA[i][0], SIGMA_CHARS[i], 0 ); - GET_UINT32_BE( SIGMA[i][1], SIGMA_CHARS[i], 4 ); + MBEDTLS_GET_UINT32_BE( SIGMA[i][0], SIGMA_CHARS[i], 0 ); + MBEDTLS_GET_UINT32_BE( SIGMA[i][1], SIGMA_CHARS[i], 4 ); } /* @@ -365,7 +365,7 @@ int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, /* Store KL, KR */ for( i = 0; i < 8; i++ ) - GET_UINT32_BE( KC[i], t, i * 4 ); + MBEDTLS_GET_UINT32_BE( KC[i], t, i * 4 ); /* Generate KA */ for( i = 0; i < 4; ++i ) @@ -491,10 +491,10 @@ int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx, NR = ctx->nr; RK = ctx->rk; - GET_UINT32_BE( X[0], input, 0 ); - GET_UINT32_BE( X[1], input, 4 ); - GET_UINT32_BE( X[2], input, 8 ); - GET_UINT32_BE( X[3], input, 12 ); + MBEDTLS_GET_UINT32_BE( X[0], input, 0 ); + MBEDTLS_GET_UINT32_BE( X[1], input, 4 ); + MBEDTLS_GET_UINT32_BE( X[2], input, 8 ); + MBEDTLS_GET_UINT32_BE( X[3], input, 12 ); X[0] ^= *RK++; X[1] ^= *RK++; @@ -529,10 +529,10 @@ int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx, X[0] ^= *RK++; X[1] ^= *RK++; - PUT_UINT32_BE( X[2], output, 0 ); - PUT_UINT32_BE( X[3], output, 4 ); - PUT_UINT32_BE( X[0], output, 8 ); - PUT_UINT32_BE( X[1], output, 12 ); + MBEDTLS_PUT_UINT32_BE( X[2], output, 0 ); + MBEDTLS_PUT_UINT32_BE( X[3], output, 4 ); + MBEDTLS_PUT_UINT32_BE( X[0], output, 8 ); + MBEDTLS_PUT_UINT32_BE( X[1], output, 12 ); return( 0 ); } diff --git a/library/chacha20.c b/library/chacha20.c index 9862ea535a36..d0d5741c7b9d 100644 --- a/library/chacha20.c +++ b/library/chacha20.c @@ -205,14 +205,14 @@ int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx, ctx->state[3] = 0x6b206574; /* Set key */ - ctx->state[4] = BYTES_TO_U32_LE( key, 0 ); - ctx->state[5] = BYTES_TO_U32_LE( key, 4 ); - ctx->state[6] = BYTES_TO_U32_LE( key, 8 ); - ctx->state[7] = BYTES_TO_U32_LE( key, 12 ); - ctx->state[8] = BYTES_TO_U32_LE( key, 16 ); - ctx->state[9] = BYTES_TO_U32_LE( key, 20 ); - ctx->state[10] = BYTES_TO_U32_LE( key, 24 ); - ctx->state[11] = BYTES_TO_U32_LE( key, 28 ); + ctx->state[4] = MBEDTLS_BYTES_TO_U32_LE( key, 0 ); + ctx->state[5] = MBEDTLS_BYTES_TO_U32_LE( key, 4 ); + ctx->state[6] = MBEDTLS_BYTES_TO_U32_LE( key, 8 ); + ctx->state[7] = MBEDTLS_BYTES_TO_U32_LE( key, 12 ); + ctx->state[8] = MBEDTLS_BYTES_TO_U32_LE( key, 16 ); + ctx->state[9] = MBEDTLS_BYTES_TO_U32_LE( key, 20 ); + ctx->state[10] = MBEDTLS_BYTES_TO_U32_LE( key, 24 ); + ctx->state[11] = MBEDTLS_BYTES_TO_U32_LE( key, 28 ); return( 0 ); } @@ -228,9 +228,9 @@ int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx, ctx->state[12] = counter; /* Nonce */ - ctx->state[13] = BYTES_TO_U32_LE( nonce, 0 ); - ctx->state[14] = BYTES_TO_U32_LE( nonce, 4 ); - ctx->state[15] = BYTES_TO_U32_LE( nonce, 8 ); + ctx->state[13] = MBEDTLS_BYTES_TO_U32_LE( nonce, 0 ); + ctx->state[14] = MBEDTLS_BYTES_TO_U32_LE( nonce, 4 ); + ctx->state[15] = MBEDTLS_BYTES_TO_U32_LE( nonce, 8 ); mbedtls_platform_zeroize( ctx->keystream8, sizeof( ctx->keystream8 ) ); diff --git a/library/common.h b/library/common.h index b6b7d01d9d0f..6dbc808d3447 100644 --- a/library/common.h +++ b/library/common.h @@ -71,61 +71,61 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c * To tidy up code and save horizontal and vertical space, use byte * reading macros to cast */ -#define BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) ) -#define BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) ) -#define BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) ) -#define BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) ) +#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) ) +#define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) ) +#define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) ) +#define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) ) /* * 32-bit integer manipulation macros (big endian) */ -#ifndef GET_UINT32_BE -#define GET_UINT32_BE(n,b,i) \ -do { \ - (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ - | ( (uint32_t) (b)[(i) + 1] << 16 ) \ - | ( (uint32_t) (b)[(i) + 2] << 8 ) \ - | ( (uint32_t) (b)[(i) + 3] ); \ -} while( 0 ) +#ifndef MBEDTLS_GET_UINT32_BE +#define MBEDTLS_GET_UINT32_BE(n,b,i) \ + do { \ + (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ + | ( (uint32_t) (b)[(i) + 1] << 16 ) \ + | ( (uint32_t) (b)[(i) + 2] << 8 ) \ + | ( (uint32_t) (b)[(i) + 3] ); \ + } while( 0 ) #endif -#ifndef PUT_UINT32_BE -#define PUT_UINT32_BE(n,b,i) \ -do { \ - (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ - (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ - (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ - (b)[(i) + 3] = (unsigned char) ( (n) ); \ -} while( 0 ) +#ifndef MBEDTLS_PUT_UINT32_BE +#define MBEDTLS_PUT_UINT32_BE(n,b,i) \ + do { \ + (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ + (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ + (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ + (b)[(i) + 3] = (unsigned char) ( (n) ); \ + } while( 0 ) #endif /* * 32-bit integer manipulation macros (little endian) */ -#ifndef GET_UINT32_LE -#define GET_UINT32_LE(n,b,i) \ -do { \ - (n) = ( (uint32_t) (b)[(i) ] ) \ - | ( (uint32_t) (b)[(i) + 1] << 8 ) \ - | ( (uint32_t) (b)[(i) + 2] << 16 ) \ - | ( (uint32_t) (b)[(i) + 3] << 24 ); \ -} while( 0 ) +#ifndef MBEDTLS_GET_UINT32_LE +#define MBEDTLS_GET_UINT32_LE(n,b,i) \ + do { \ + (n) = ( (uint32_t) (b)[(i) ] ) \ + | ( (uint32_t) (b)[(i) + 1] << 8 ) \ + | ( (uint32_t) (b)[(i) + 2] << 16 ) \ + | ( (uint32_t) (b)[(i) + 3] << 24 ); \ + } while( 0 ) #endif -#ifndef PUT_UINT32_LE -#define PUT_UINT32_LE(n,b,i) \ -do { \ - (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ - (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ - (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ - (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ -} while( 0 ) +#ifndef MBEDTLS_PUT_UINT32_LE +#define MBEDTLS_PUT_UINT32_LE(n,b,i) \ + do { \ + (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ + (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ + (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ + (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ + } while( 0 ) #endif /** * 32-bit integer conversion from bytes (little endian) */ -#define BYTES_TO_U32_LE( data, offset ) \ +#define MBEDTLS_BYTES_TO_U32_LE( data, offset ) \ ( (uint32_t) (data)[offset] \ | (uint32_t) ( (uint32_t) (data)[( offset ) + 1] << 8 ) \ | (uint32_t) ( (uint32_t) (data)[( offset ) + 2] << 16 ) \ diff --git a/library/ctr_drbg.c b/library/ctr_drbg.c index d52d9ef25403..68b32a366b69 100644 --- a/library/ctr_drbg.c +++ b/library/ctr_drbg.c @@ -152,10 +152,10 @@ static int block_cipher_df( unsigned char *output, * (Total is padded to a multiple of 16-bytes with zeroes) */ p = buf + MBEDTLS_CTR_DRBG_BLOCKSIZE; - *p++ = BYTE_3( data_len ); - *p++ = BYTE_2( data_len ); - *p++ = BYTE_1( data_len ); - *p++ = BYTE_0( data_len ); + *p++ = MBEDTLS_BYTE_3( data_len ); + *p++ = MBEDTLS_BYTE_2( data_len ); + *p++ = MBEDTLS_BYTE_1( data_len ); + *p++ = MBEDTLS_BYTE_0( data_len ); p += 3; *p++ = MBEDTLS_CTR_DRBG_SEEDLEN; memcpy( p, data, data_len ); diff --git a/library/des.c b/library/des.c index 36ea27776017..9281747de8dc 100644 --- a/library/des.c +++ b/library/des.c @@ -400,8 +400,8 @@ void mbedtls_des_setkey( uint32_t SK[32], const unsigned char key[MBEDTLS_DES_KE int i; uint32_t X, Y, T; - GET_UINT32_BE( X, key, 0 ); - GET_UINT32_BE( Y, key, 4 ); + MBEDTLS_GET_UINT32_BE( X, key, 0 ); + MBEDTLS_GET_UINT32_BE( Y, key, 4 ); /* * Permuted Choice 1 @@ -610,8 +610,8 @@ int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx, SK = ctx->sk; - GET_UINT32_BE( X, input, 0 ); - GET_UINT32_BE( Y, input, 4 ); + MBEDTLS_GET_UINT32_BE( X, input, 0 ); + MBEDTLS_GET_UINT32_BE( Y, input, 4 ); DES_IP( X, Y ); @@ -623,8 +623,8 @@ int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx, DES_FP( Y, X ); - PUT_UINT32_BE( Y, output, 0 ); - PUT_UINT32_BE( X, output, 4 ); + MBEDTLS_PUT_UINT32_BE( Y, output, 0 ); + MBEDTLS_PUT_UINT32_BE( X, output, 4 ); return( 0 ); } @@ -697,8 +697,8 @@ int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx, SK = ctx->sk; - GET_UINT32_BE( X, input, 0 ); - GET_UINT32_BE( Y, input, 4 ); + MBEDTLS_GET_UINT32_BE( X, input, 0 ); + MBEDTLS_GET_UINT32_BE( Y, input, 4 ); DES_IP( X, Y ); @@ -722,8 +722,8 @@ int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx, DES_FP( Y, X ); - PUT_UINT32_BE( Y, output, 0 ); - PUT_UINT32_BE( X, output, 4 ); + MBEDTLS_PUT_UINT32_BE( Y, output, 0 ); + MBEDTLS_PUT_UINT32_BE( X, output, 4 ); return( 0 ); } diff --git a/library/gcm.c b/library/gcm.c index c8254876dd84..3caeed26def9 100644 --- a/library/gcm.c +++ b/library/gcm.c @@ -88,12 +88,12 @@ static int gcm_gen_table( mbedtls_gcm_context *ctx ) return( ret ); /* pack h as two 64-bits ints, big-endian */ - GET_UINT32_BE( hi, h, 0 ); - GET_UINT32_BE( lo, h, 4 ); + MBEDTLS_GET_UINT32_BE( hi, h, 0 ); + MBEDTLS_GET_UINT32_BE( lo, h, 4 ); vh = (uint64_t) hi << 32 | lo; - GET_UINT32_BE( hi, h, 8 ); - GET_UINT32_BE( lo, h, 12 ); + MBEDTLS_GET_UINT32_BE( hi, h, 8 ); + MBEDTLS_GET_UINT32_BE( lo, h, 12 ); vl = (uint64_t) hi << 32 | lo; /* 8 = 1000 corresponds to 1 in GF(2^128) */ @@ -200,10 +200,10 @@ static void gcm_mult( mbedtls_gcm_context *ctx, const unsigned char x[16], if( mbedtls_aesni_has_support( MBEDTLS_AESNI_CLMUL ) ) { unsigned char h[16]; - PUT_UINT32_BE( ctx->HH[8] >> 32, h, 0 ); - PUT_UINT32_BE( ctx->HH[8], h, 4 ); - PUT_UINT32_BE( ctx->HL[8] >> 32, h, 8 ); - PUT_UINT32_BE( ctx->HL[8], h, 12 ); + MBEDTLS_PUT_UINT32_BE( ctx->HH[8] >> 32, h, 0 ); + MBEDTLS_PUT_UINT32_BE( ctx->HH[8], h, 4 ); + MBEDTLS_PUT_UINT32_BE( ctx->HL[8] >> 32, h, 8 ); + MBEDTLS_PUT_UINT32_BE( ctx->HL[8], h, 12 ); mbedtls_aesni_gcm_mult( output, x, h ); return; @@ -239,10 +239,10 @@ static void gcm_mult( mbedtls_gcm_context *ctx, const unsigned char x[16], zl ^= ctx->HL[hi]; } - PUT_UINT32_BE( zh >> 32, output, 0 ); - PUT_UINT32_BE( zh, output, 4 ); - PUT_UINT32_BE( zl >> 32, output, 8 ); - PUT_UINT32_BE( zl, output, 12 ); + MBEDTLS_PUT_UINT32_BE( zh >> 32, output, 0 ); + MBEDTLS_PUT_UINT32_BE( zh, output, 4 ); + MBEDTLS_PUT_UINT32_BE( zl >> 32, output, 8 ); + MBEDTLS_PUT_UINT32_BE( zl, output, 12 ); } int mbedtls_gcm_starts( mbedtls_gcm_context *ctx, @@ -278,7 +278,7 @@ int mbedtls_gcm_starts( mbedtls_gcm_context *ctx, else { memset( work_buf, 0x00, 16 ); - PUT_UINT32_BE( iv_len * 8, work_buf, 12 ); + MBEDTLS_PUT_UINT32_BE( iv_len * 8, work_buf, 12 ); p = iv; while( iv_len > 0 ) @@ -546,10 +546,10 @@ int mbedtls_gcm_finish( mbedtls_gcm_context *ctx, { memset( work_buf, 0x00, 16 ); - PUT_UINT32_BE( ( orig_add_len >> 32 ), work_buf, 0 ); - PUT_UINT32_BE( ( orig_add_len ), work_buf, 4 ); - PUT_UINT32_BE( ( orig_len >> 32 ), work_buf, 8 ); - PUT_UINT32_BE( ( orig_len ), work_buf, 12 ); + MBEDTLS_PUT_UINT32_BE( ( orig_add_len >> 32 ), work_buf, 0 ); + MBEDTLS_PUT_UINT32_BE( ( orig_add_len ), work_buf, 4 ); + MBEDTLS_PUT_UINT32_BE( ( orig_len >> 32 ), work_buf, 8 ); + MBEDTLS_PUT_UINT32_BE( ( orig_len ), work_buf, 12 ); for( i = 0; i < 16; i++ ) ctx->buf[i] ^= work_buf[i]; diff --git a/library/md5.c b/library/md5.c index d8f637da836e..e8d00216f0d2 100644 --- a/library/md5.c +++ b/library/md5.c @@ -87,22 +87,22 @@ int mbedtls_internal_md5_process( mbedtls_md5_context *ctx, uint32_t X[16], A, B, C, D; } local; - GET_UINT32_LE( local.X[ 0], data, 0 ); - GET_UINT32_LE( local.X[ 1], data, 4 ); - GET_UINT32_LE( local.X[ 2], data, 8 ); - GET_UINT32_LE( local.X[ 3], data, 12 ); - GET_UINT32_LE( local.X[ 4], data, 16 ); - GET_UINT32_LE( local.X[ 5], data, 20 ); - GET_UINT32_LE( local.X[ 6], data, 24 ); - GET_UINT32_LE( local.X[ 7], data, 28 ); - GET_UINT32_LE( local.X[ 8], data, 32 ); - GET_UINT32_LE( local.X[ 9], data, 36 ); - GET_UINT32_LE( local.X[10], data, 40 ); - GET_UINT32_LE( local.X[11], data, 44 ); - GET_UINT32_LE( local.X[12], data, 48 ); - GET_UINT32_LE( local.X[13], data, 52 ); - GET_UINT32_LE( local.X[14], data, 56 ); - GET_UINT32_LE( local.X[15], data, 60 ); + MBEDTLS_GET_UINT32_LE( local.X[ 0], data, 0 ); + MBEDTLS_GET_UINT32_LE( local.X[ 1], data, 4 ); + MBEDTLS_GET_UINT32_LE( local.X[ 2], data, 8 ); + MBEDTLS_GET_UINT32_LE( local.X[ 3], data, 12 ); + MBEDTLS_GET_UINT32_LE( local.X[ 4], data, 16 ); + MBEDTLS_GET_UINT32_LE( local.X[ 5], data, 20 ); + MBEDTLS_GET_UINT32_LE( local.X[ 6], data, 24 ); + MBEDTLS_GET_UINT32_LE( local.X[ 7], data, 28 ); + MBEDTLS_GET_UINT32_LE( local.X[ 8], data, 32 ); + MBEDTLS_GET_UINT32_LE( local.X[ 9], data, 36 ); + MBEDTLS_GET_UINT32_LE( local.X[10], data, 40 ); + MBEDTLS_GET_UINT32_LE( local.X[11], data, 44 ); + MBEDTLS_GET_UINT32_LE( local.X[12], data, 48 ); + MBEDTLS_GET_UINT32_LE( local.X[13], data, 52 ); + MBEDTLS_GET_UINT32_LE( local.X[14], data, 56 ); + MBEDTLS_GET_UINT32_LE( local.X[15], data, 60 ); #define S(x,n) \ ( ( (x) << (n) ) | ( ( (x) & 0xFFFFFFFF) >> ( 32 - (n) ) ) ) @@ -307,8 +307,8 @@ int mbedtls_md5_finish( mbedtls_md5_context *ctx, | ( ctx->total[1] << 3 ); low = ( ctx->total[0] << 3 ); - PUT_UINT32_LE( low, ctx->buffer, 56 ); - PUT_UINT32_LE( high, ctx->buffer, 60 ); + MBEDTLS_PUT_UINT32_LE( low, ctx->buffer, 56 ); + MBEDTLS_PUT_UINT32_LE( high, ctx->buffer, 60 ); if( ( ret = mbedtls_internal_md5_process( ctx, ctx->buffer ) ) != 0 ) return( ret ); @@ -316,10 +316,10 @@ int mbedtls_md5_finish( mbedtls_md5_context *ctx, /* * Output final state */ - PUT_UINT32_LE( ctx->state[0], output, 0 ); - PUT_UINT32_LE( ctx->state[1], output, 4 ); - PUT_UINT32_LE( ctx->state[2], output, 8 ); - PUT_UINT32_LE( ctx->state[3], output, 12 ); + MBEDTLS_PUT_UINT32_LE( ctx->state[0], output, 0 ); + MBEDTLS_PUT_UINT32_LE( ctx->state[1], output, 4 ); + MBEDTLS_PUT_UINT32_LE( ctx->state[2], output, 8 ); + MBEDTLS_PUT_UINT32_LE( ctx->state[3], output, 12 ); return( 0 ); } diff --git a/library/nist_kw.c b/library/nist_kw.c index 174a1eef1323..b8f923999200 100644 --- a/library/nist_kw.c +++ b/library/nist_kw.c @@ -223,7 +223,7 @@ int mbedtls_nist_kw_wrap( mbedtls_nist_kw_context *ctx, } memcpy( output, NIST_KW_ICV2, KW_SEMIBLOCK_LENGTH / 2 ); - PUT_UINT32_BE( ( in_len & 0xffffffff ), output, + MBEDTLS_PUT_UINT32_BE( ( in_len & 0xffffffff ), output, KW_SEMIBLOCK_LENGTH / 2 ); memcpy( output + KW_SEMIBLOCK_LENGTH, input, in_len ); @@ -454,7 +454,7 @@ int mbedtls_nist_kw_unwrap( mbedtls_nist_kw_context *ctx, ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED; } - GET_UINT32_BE( Plen, A, KW_SEMIBLOCK_LENGTH / 2 ); + MBEDTLS_GET_UINT32_BE( Plen, A, KW_SEMIBLOCK_LENGTH / 2 ); /* * Plen is the length of the plaintext, when the input is valid. diff --git a/library/poly1305.c b/library/poly1305.c index a30b1707ed16..3c0b7c6aa9c5 100644 --- a/library/poly1305.c +++ b/library/poly1305.c @@ -122,10 +122,10 @@ static void poly1305_process( mbedtls_poly1305_context *ctx, for( i = 0U; i < nblocks; i++ ) { /* The input block is treated as a 128-bit little-endian integer */ - d0 = BYTES_TO_U32_LE( input, offset + 0 ); - d1 = BYTES_TO_U32_LE( input, offset + 4 ); - d2 = BYTES_TO_U32_LE( input, offset + 8 ); - d3 = BYTES_TO_U32_LE( input, offset + 12 ); + d0 = MBEDTLS_BYTES_TO_U32_LE( input, offset + 0 ); + d1 = MBEDTLS_BYTES_TO_U32_LE( input, offset + 4 ); + d2 = MBEDTLS_BYTES_TO_U32_LE( input, offset + 8 ); + d3 = MBEDTLS_BYTES_TO_U32_LE( input, offset + 12 ); /* Compute: acc += (padded) block as a 130-bit integer */ d0 += (uint64_t) acc0; @@ -290,15 +290,15 @@ int mbedtls_poly1305_starts( mbedtls_poly1305_context *ctx, POLY1305_VALIDATE_RET( key != NULL ); /* r &= 0x0ffffffc0ffffffc0ffffffc0fffffff */ - ctx->r[0] = BYTES_TO_U32_LE( key, 0 ) & 0x0FFFFFFFU; - ctx->r[1] = BYTES_TO_U32_LE( key, 4 ) & 0x0FFFFFFCU; - ctx->r[2] = BYTES_TO_U32_LE( key, 8 ) & 0x0FFFFFFCU; - ctx->r[3] = BYTES_TO_U32_LE( key, 12 ) & 0x0FFFFFFCU; - - ctx->s[0] = BYTES_TO_U32_LE( key, 16 ); - ctx->s[1] = BYTES_TO_U32_LE( key, 20 ); - ctx->s[2] = BYTES_TO_U32_LE( key, 24 ); - ctx->s[3] = BYTES_TO_U32_LE( key, 28 ); + ctx->r[0] = MBEDTLS_BYTES_TO_U32_LE( key, 0 ) & 0x0FFFFFFFU; + ctx->r[1] = MBEDTLS_BYTES_TO_U32_LE( key, 4 ) & 0x0FFFFFFCU; + ctx->r[2] = MBEDTLS_BYTES_TO_U32_LE( key, 8 ) & 0x0FFFFFFCU; + ctx->r[3] = MBEDTLS_BYTES_TO_U32_LE( key, 12 ) & 0x0FFFFFFCU; + + ctx->s[0] = MBEDTLS_BYTES_TO_U32_LE( key, 16 ); + ctx->s[1] = MBEDTLS_BYTES_TO_U32_LE( key, 20 ); + ctx->s[2] = MBEDTLS_BYTES_TO_U32_LE( key, 24 ); + ctx->s[3] = MBEDTLS_BYTES_TO_U32_LE( key, 28 ); /* Initial accumulator state */ ctx->acc[0] = 0U; diff --git a/library/psa_crypto.c b/library/psa_crypto.c index b46e023e5dbf..95aa0d531676 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -4506,8 +4506,8 @@ static psa_status_t psa_tls12_prf_psk_to_ms_set_key( * uint16 with the value N, and the PSK itself. */ - *cur++ = BYTE_1( data_length ); - *cur++ = BYTE_0( data_length ); + *cur++ = MBEDTLS_BYTE_1( data_length ); + *cur++ = MBEDTLS_BYTE_0( data_length ); memset( cur, 0, data_length ); cur += data_length; *cur++ = pms[0]; diff --git a/library/psa_crypto_storage.c b/library/psa_crypto_storage.c index 36b518350cc7..3646953a57a1 100644 --- a/library/psa_crypto_storage.c +++ b/library/psa_crypto_storage.c @@ -275,14 +275,14 @@ void psa_format_key_data_for_storage( const uint8_t *data, (psa_persistent_key_storage_format *) storage_data; memcpy( storage_format->magic, PSA_KEY_STORAGE_MAGIC_HEADER, PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ); - PUT_UINT32_LE( 0, storage_format->version, 0 ); - PUT_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 ); + MBEDTLS_PUT_UINT32_LE( 0, storage_format->version, 0 ); + MBEDTLS_PUT_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 ); PUT_UINT16_LE( (uint16_t) attr->type, storage_format->type, 0 ); PUT_UINT16_LE( (uint16_t) attr->bits, storage_format->bits, 0 ); - PUT_UINT32_LE( attr->policy.usage, storage_format->policy, 0 ); - PUT_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) ); - PUT_UINT32_LE( attr->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) ); - PUT_UINT32_LE( data_length, storage_format->data_len, 0 ); + MBEDTLS_PUT_UINT32_LE( attr->policy.usage, storage_format->policy, 0 ); + MBEDTLS_PUT_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) ); + MBEDTLS_PUT_UINT32_LE( attr->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) ); + MBEDTLS_PUT_UINT32_LE( data_length, storage_format->data_len, 0 ); memcpy( storage_format->key_data, data, data_length ); } @@ -312,11 +312,11 @@ psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data, if( status != PSA_SUCCESS ) return( status ); - GET_UINT32_LE( version, storage_format->version, 0 ); + MBEDTLS_GET_UINT32_LE( version, storage_format->version, 0 ); if( version != 0 ) return( PSA_ERROR_DATA_INVALID ); - GET_UINT32_LE( *key_data_length, storage_format->data_len, 0 ); + MBEDTLS_GET_UINT32_LE( *key_data_length, storage_format->data_len, 0 ); if( *key_data_length > ( storage_data_length - sizeof(*storage_format) ) || *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE ) return( PSA_ERROR_DATA_INVALID ); @@ -333,12 +333,12 @@ psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data, memcpy( *key_data, storage_format->key_data, *key_data_length ); } - GET_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 ); + MBEDTLS_GET_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 ); GET_UINT16_LE( attr->type, storage_format->type, 0 ); GET_UINT16_LE( attr->bits, storage_format->bits, 0 ); - GET_UINT32_LE( attr->policy.usage, storage_format->policy, 0 ); - GET_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) ); - GET_UINT32_LE( attr->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) ); + MBEDTLS_GET_UINT32_LE( attr->policy.usage, storage_format->policy, 0 ); + MBEDTLS_GET_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) ); + MBEDTLS_GET_UINT32_LE( attr->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) ); return( PSA_SUCCESS ); } diff --git a/library/psa_its_file.c b/library/psa_its_file.c index 66043b502e09..c3b19a74ac8f 100644 --- a/library/psa_its_file.c +++ b/library/psa_its_file.c @@ -191,14 +191,14 @@ psa_status_t psa_its_set( psa_storage_uid_t uid, size_t n; memcpy( header.magic, PSA_ITS_MAGIC_STRING, PSA_ITS_MAGIC_LENGTH ); - header.size[0] = BYTE_0( data_length ); - header.size[1] = BYTE_1( data_length ); - header.size[2] = BYTE_2( data_length ); - header.size[3] = BYTE_3( data_length ); - header.flags[0] = BYTE_0( create_flags ); - header.flags[1] = BYTE_1( create_flags ); - header.flags[2] = BYTE_2( create_flags ); - header.flags[3] = BYTE_3( create_flags ); + header.size[0] = MBEDTLS_BYTE_0( data_length ); + header.size[1] = MBEDTLS_BYTE_1( data_length ); + header.size[2] = MBEDTLS_BYTE_2( data_length ); + header.size[3] = MBEDTLS_BYTE_3( data_length ); + header.flags[0] = MBEDTLS_BYTE_0( create_flags ); + header.flags[1] = MBEDTLS_BYTE_1( create_flags ); + header.flags[2] = MBEDTLS_BYTE_2( create_flags ); + header.flags[3] = MBEDTLS_BYTE_3( create_flags ); psa_its_fill_filename( uid, filename ); stream = fopen( PSA_ITS_STORAGE_TEMP, "wb" ); diff --git a/library/ripemd160.c b/library/ripemd160.c index d60654e9529c..2bed107230a7 100644 --- a/library/ripemd160.c +++ b/library/ripemd160.c @@ -92,22 +92,22 @@ int mbedtls_internal_ripemd160_process( mbedtls_ripemd160_context *ctx, uint32_t A, B, C, D, E, Ap, Bp, Cp, Dp, Ep, X[16]; } local; - GET_UINT32_LE( local.X[ 0], data, 0 ); - GET_UINT32_LE( local.X[ 1], data, 4 ); - GET_UINT32_LE( local.X[ 2], data, 8 ); - GET_UINT32_LE( local.X[ 3], data, 12 ); - GET_UINT32_LE( local.X[ 4], data, 16 ); - GET_UINT32_LE( local.X[ 5], data, 20 ); - GET_UINT32_LE( local.X[ 6], data, 24 ); - GET_UINT32_LE( local.X[ 7], data, 28 ); - GET_UINT32_LE( local.X[ 8], data, 32 ); - GET_UINT32_LE( local.X[ 9], data, 36 ); - GET_UINT32_LE( local.X[10], data, 40 ); - GET_UINT32_LE( local.X[11], data, 44 ); - GET_UINT32_LE( local.X[12], data, 48 ); - GET_UINT32_LE( local.X[13], data, 52 ); - GET_UINT32_LE( local.X[14], data, 56 ); - GET_UINT32_LE( local.X[15], data, 60 ); + MBEDTLS_GET_UINT32_LE( local.X[ 0], data, 0 ); + MBEDTLS_GET_UINT32_LE( local.X[ 1], data, 4 ); + MBEDTLS_GET_UINT32_LE( local.X[ 2], data, 8 ); + MBEDTLS_GET_UINT32_LE( local.X[ 3], data, 12 ); + MBEDTLS_GET_UINT32_LE( local.X[ 4], data, 16 ); + MBEDTLS_GET_UINT32_LE( local.X[ 5], data, 20 ); + MBEDTLS_GET_UINT32_LE( local.X[ 6], data, 24 ); + MBEDTLS_GET_UINT32_LE( local.X[ 7], data, 28 ); + MBEDTLS_GET_UINT32_LE( local.X[ 8], data, 32 ); + MBEDTLS_GET_UINT32_LE( local.X[ 9], data, 36 ); + MBEDTLS_GET_UINT32_LE( local.X[10], data, 40 ); + MBEDTLS_GET_UINT32_LE( local.X[11], data, 44 ); + MBEDTLS_GET_UINT32_LE( local.X[12], data, 48 ); + MBEDTLS_GET_UINT32_LE( local.X[13], data, 52 ); + MBEDTLS_GET_UINT32_LE( local.X[14], data, 56 ); + MBEDTLS_GET_UINT32_LE( local.X[15], data, 60 ); local.A = local.Ap = ctx->state[0]; local.B = local.Bp = ctx->state[1]; @@ -354,8 +354,8 @@ int mbedtls_ripemd160_finish( mbedtls_ripemd160_context *ctx, | ( ctx->total[1] << 3 ); low = ( ctx->total[0] << 3 ); - PUT_UINT32_LE( low, msglen, 0 ); - PUT_UINT32_LE( high, msglen, 4 ); + MBEDTLS_PUT_UINT32_LE( low, msglen, 0 ); + MBEDTLS_PUT_UINT32_LE( high, msglen, 4 ); last = ctx->total[0] & 0x3F; padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); @@ -368,11 +368,11 @@ int mbedtls_ripemd160_finish( mbedtls_ripemd160_context *ctx, if( ret != 0 ) return( ret ); - PUT_UINT32_LE( ctx->state[0], output, 0 ); - PUT_UINT32_LE( ctx->state[1], output, 4 ); - PUT_UINT32_LE( ctx->state[2], output, 8 ); - PUT_UINT32_LE( ctx->state[3], output, 12 ); - PUT_UINT32_LE( ctx->state[4], output, 16 ); + MBEDTLS_PUT_UINT32_LE( ctx->state[0], output, 0 ); + MBEDTLS_PUT_UINT32_LE( ctx->state[1], output, 4 ); + MBEDTLS_PUT_UINT32_LE( ctx->state[2], output, 8 ); + MBEDTLS_PUT_UINT32_LE( ctx->state[3], output, 12 ); + MBEDTLS_PUT_UINT32_LE( ctx->state[4], output, 16 ); return( 0 ); } diff --git a/library/sha1.c b/library/sha1.c index 9beaee5a5400..da61f65fca0c 100644 --- a/library/sha1.c +++ b/library/sha1.c @@ -103,22 +103,22 @@ int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, SHA1_VALIDATE_RET( ctx != NULL ); SHA1_VALIDATE_RET( (const unsigned char *)data != NULL ); - GET_UINT32_BE( local.W[ 0], data, 0 ); - GET_UINT32_BE( local.W[ 1], data, 4 ); - GET_UINT32_BE( local.W[ 2], data, 8 ); - GET_UINT32_BE( local.W[ 3], data, 12 ); - GET_UINT32_BE( local.W[ 4], data, 16 ); - GET_UINT32_BE( local.W[ 5], data, 20 ); - GET_UINT32_BE( local.W[ 6], data, 24 ); - GET_UINT32_BE( local.W[ 7], data, 28 ); - GET_UINT32_BE( local.W[ 8], data, 32 ); - GET_UINT32_BE( local.W[ 9], data, 36 ); - GET_UINT32_BE( local.W[10], data, 40 ); - GET_UINT32_BE( local.W[11], data, 44 ); - GET_UINT32_BE( local.W[12], data, 48 ); - GET_UINT32_BE( local.W[13], data, 52 ); - GET_UINT32_BE( local.W[14], data, 56 ); - GET_UINT32_BE( local.W[15], data, 60 ); + MBEDTLS_GET_UINT32_BE( local.W[ 0], data, 0 ); + MBEDTLS_GET_UINT32_BE( local.W[ 1], data, 4 ); + MBEDTLS_GET_UINT32_BE( local.W[ 2], data, 8 ); + MBEDTLS_GET_UINT32_BE( local.W[ 3], data, 12 ); + MBEDTLS_GET_UINT32_BE( local.W[ 4], data, 16 ); + MBEDTLS_GET_UINT32_BE( local.W[ 5], data, 20 ); + MBEDTLS_GET_UINT32_BE( local.W[ 6], data, 24 ); + MBEDTLS_GET_UINT32_BE( local.W[ 7], data, 28 ); + MBEDTLS_GET_UINT32_BE( local.W[ 8], data, 32 ); + MBEDTLS_GET_UINT32_BE( local.W[ 9], data, 36 ); + MBEDTLS_GET_UINT32_BE( local.W[10], data, 40 ); + MBEDTLS_GET_UINT32_BE( local.W[11], data, 44 ); + MBEDTLS_GET_UINT32_BE( local.W[12], data, 48 ); + MBEDTLS_GET_UINT32_BE( local.W[13], data, 52 ); + MBEDTLS_GET_UINT32_BE( local.W[14], data, 56 ); + MBEDTLS_GET_UINT32_BE( local.W[15], data, 60 ); #define S(x,n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n)))) @@ -362,8 +362,8 @@ int mbedtls_sha1_finish( mbedtls_sha1_context *ctx, | ( ctx->total[1] << 3 ); low = ( ctx->total[0] << 3 ); - PUT_UINT32_BE( high, ctx->buffer, 56 ); - PUT_UINT32_BE( low, ctx->buffer, 60 ); + MBEDTLS_PUT_UINT32_BE( high, ctx->buffer, 56 ); + MBEDTLS_PUT_UINT32_BE( low, ctx->buffer, 60 ); if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 ) return( ret ); @@ -371,11 +371,11 @@ int mbedtls_sha1_finish( mbedtls_sha1_context *ctx, /* * Output final state */ - PUT_UINT32_BE( ctx->state[0], output, 0 ); - PUT_UINT32_BE( ctx->state[1], output, 4 ); - PUT_UINT32_BE( ctx->state[2], output, 8 ); - PUT_UINT32_BE( ctx->state[3], output, 12 ); - PUT_UINT32_BE( ctx->state[4], output, 16 ); + MBEDTLS_PUT_UINT32_BE( ctx->state[0], output, 0 ); + MBEDTLS_PUT_UINT32_BE( ctx->state[1], output, 4 ); + MBEDTLS_PUT_UINT32_BE( ctx->state[2], output, 8 ); + MBEDTLS_PUT_UINT32_BE( ctx->state[3], output, 12 ); + MBEDTLS_PUT_UINT32_BE( ctx->state[4], output, 16 ); return( 0 ); } diff --git a/library/sha256.c b/library/sha256.c index f548c672ea72..fb66340b2081 100644 --- a/library/sha256.c +++ b/library/sha256.c @@ -190,7 +190,7 @@ int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, for( i = 0; i < 64; i++ ) { if( i < 16 ) - GET_UINT32_BE( local.W[i], data, 4 * i ); + MBEDTLS_GET_UINT32_BE( local.W[i], data, 4 * i ); else R( i ); @@ -205,7 +205,7 @@ int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, } #else /* MBEDTLS_SHA256_SMALLER */ for( i = 0; i < 16; i++ ) - GET_UINT32_BE( local.W[i], data, 4 * i ); + MBEDTLS_GET_UINT32_BE( local.W[i], data, 4 * i ); for( i = 0; i < 16; i += 8 ) { @@ -355,8 +355,8 @@ int mbedtls_sha256_finish( mbedtls_sha256_context *ctx, | ( ctx->total[1] << 3 ); low = ( ctx->total[0] << 3 ); - PUT_UINT32_BE( high, ctx->buffer, 56 ); - PUT_UINT32_BE( low, ctx->buffer, 60 ); + MBEDTLS_PUT_UINT32_BE( high, ctx->buffer, 56 ); + MBEDTLS_PUT_UINT32_BE( low, ctx->buffer, 60 ); if( ( ret = mbedtls_internal_sha256_process( ctx, ctx->buffer ) ) != 0 ) return( ret ); @@ -364,18 +364,18 @@ int mbedtls_sha256_finish( mbedtls_sha256_context *ctx, /* * Output final state */ - PUT_UINT32_BE( ctx->state[0], output, 0 ); - PUT_UINT32_BE( ctx->state[1], output, 4 ); - PUT_UINT32_BE( ctx->state[2], output, 8 ); - PUT_UINT32_BE( ctx->state[3], output, 12 ); - PUT_UINT32_BE( ctx->state[4], output, 16 ); - PUT_UINT32_BE( ctx->state[5], output, 20 ); - PUT_UINT32_BE( ctx->state[6], output, 24 ); + MBEDTLS_PUT_UINT32_BE( ctx->state[0], output, 0 ); + MBEDTLS_PUT_UINT32_BE( ctx->state[1], output, 4 ); + MBEDTLS_PUT_UINT32_BE( ctx->state[2], output, 8 ); + MBEDTLS_PUT_UINT32_BE( ctx->state[3], output, 12 ); + MBEDTLS_PUT_UINT32_BE( ctx->state[4], output, 16 ); + MBEDTLS_PUT_UINT32_BE( ctx->state[5], output, 20 ); + MBEDTLS_PUT_UINT32_BE( ctx->state[6], output, 24 ); #if defined(MBEDTLS_SHA224_C) if( ctx->is224 == 0 ) #endif - PUT_UINT32_BE( ctx->state[7], output, 28 ); + MBEDTLS_PUT_UINT32_BE( ctx->state[7], output, 28 ); return( 0 ); } diff --git a/library/ssl_msg.c b/library/ssl_msg.c index f3dbba82b979..77904e0a1baa 100644 --- a/library/ssl_msg.c +++ b/library/ssl_msg.c @@ -2255,13 +2255,13 @@ int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl ) * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */ memcpy( ssl->out_msg, cur->p, 6 ); - ssl->out_msg[6] = BYTE_2( frag_off ); - ssl->out_msg[7] = BYTE_1( frag_off ); - ssl->out_msg[8] = BYTE_0( frag_off ); + ssl->out_msg[6] = MBEDTLS_BYTE_2( frag_off ); + ssl->out_msg[7] = MBEDTLS_BYTE_1( frag_off ); + ssl->out_msg[8] = MBEDTLS_BYTE_0( frag_off ); - ssl->out_msg[ 9] = BYTE_2( cur_hs_frag_len ); - ssl->out_msg[10] = BYTE_1( cur_hs_frag_len ); - ssl->out_msg[11] = BYTE_0( cur_hs_frag_len ); + ssl->out_msg[ 9] = MBEDTLS_BYTE_2( cur_hs_frag_len ); + ssl->out_msg[10] = MBEDTLS_BYTE_1( cur_hs_frag_len ); + ssl->out_msg[11] = MBEDTLS_BYTE_0( cur_hs_frag_len ); MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 ); diff --git a/library/ssl_ticket.c b/library/ssl_ticket.c index 29d834575053..a7a55f1a7517 100644 --- a/library/ssl_ticket.c +++ b/library/ssl_ticket.c @@ -245,8 +245,8 @@ int mbedtls_ssl_ticket_write( void *p_ticket, { goto cleanup; } - state_len_bytes[0] = BYTE_1( clear_len ); - state_len_bytes[1] = BYTE_0( clear_len ); + state_len_bytes[0] = MBEDTLS_BYTE_1( clear_len ); + state_len_bytes[1] = MBEDTLS_BYTE_0( clear_len ); /* Encrypt and authenticate */ if( ( ret = mbedtls_cipher_auth_encrypt_ext( &key->ctx, From 33f953d8104bd0a069fb77d0ecbc13d33d927fe5 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Thu, 24 Jun 2021 16:49:38 +0100 Subject: [PATCH 043/120] Byte Reading Macros Changelog Signed-off-by: Joe Subbiani --- ChangeLog.d/issue4274.txt | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 ChangeLog.d/issue4274.txt diff --git a/ChangeLog.d/issue4274.txt b/ChangeLog.d/issue4274.txt new file mode 100644 index 000000000000..3e59ca5be1b5 --- /dev/null +++ b/ChangeLog.d/issue4274.txt @@ -0,0 +1,9 @@ +Changes + * Create 4 byte reading macros in library/common.h, used in files + within the same directory: MBEDTLS_BYTE_0... MBEDTLS_BYTE_3. + * Move the (PUT and GET) UINT32_ (BE and LE) macro functions into + library/common.h. Rename with the prefix MBEDTLS_ to satisfy + test/scripts/check-names.sh (e.g MBEDTLS_PUT_UINT32_LE). + * Move BYTES_TO_U32_LE macro function to library/common.h, also given + the prefix MBEDTLS_. + Fixes #4274. \ No newline at end of file From c46997377afb35a2fc4881cd202c8c4fe5667479 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Fri, 25 Jun 2021 09:20:07 +0100 Subject: [PATCH 044/120] Fix formatting - Byte reading macros changelog Missing newline at the end of changelog.d/issue4274 Signed-off-by: Joe Subbiani --- ChangeLog.d/issue4274.txt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/ChangeLog.d/issue4274.txt b/ChangeLog.d/issue4274.txt index 3e59ca5be1b5..f0952f43071a 100644 --- a/ChangeLog.d/issue4274.txt +++ b/ChangeLog.d/issue4274.txt @@ -1,9 +1,9 @@ -Changes - * Create 4 byte reading macros in library/common.h, used in files - within the same directory: MBEDTLS_BYTE_0... MBEDTLS_BYTE_3. - * Move the (PUT and GET) UINT32_ (BE and LE) macro functions into - library/common.h. Rename with the prefix MBEDTLS_ to satisfy - test/scripts/check-names.sh (e.g MBEDTLS_PUT_UINT32_LE). - * Move BYTES_TO_U32_LE macro function to library/common.h, also given - the prefix MBEDTLS_. - Fixes #4274. \ No newline at end of file +Changes + * Create 4 byte reading macros in library/common.h, used in files + within the same directory: MBEDTLS_BYTE_0... MBEDTLS_BYTE_3. + * Move the (PUT and GET) UINT32_ (BE and LE) macro functions into + library/common.h. Rename with the prefix MBEDTLS_ to satisfy + test/scripts/check-names.sh (e.g MBEDTLS_PUT_UINT32_LE). + * Move BYTES_TO_U32_LE macro function to library/common.h, also given + the prefix MBEDTLS_. + Fixes #4274. From 9fa9ac3612c0c542fa91c0fae003dcb37c1586a8 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Mon, 5 Jul 2021 15:37:39 +0100 Subject: [PATCH 045/120] Move GET/PUT_UINT16_LE macros to common.h Although these only appear in one file: psa_crypto_storage.c it is tidy to give it the same prefix as the UINT32 macros and to store them in the fame file Signed-off-by: Joe Subbiani --- library/common.h | 31 ++++++++++++++++++++++++++----- library/psa_crypto_storage.c | 27 ++++----------------------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/library/common.h b/library/common.h index 6dbc808d3447..e85cbf28d0f6 100644 --- a/library/common.h +++ b/library/common.h @@ -80,7 +80,7 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c * 32-bit integer manipulation macros (big endian) */ #ifndef MBEDTLS_GET_UINT32_BE -#define MBEDTLS_GET_UINT32_BE(n,b,i) \ +#define MBEDTLS_GET_UINT32_BE(n,b,i) \ do { \ (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ | ( (uint32_t) (b)[(i) + 1] << 16 ) \ @@ -90,7 +90,7 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c #endif #ifndef MBEDTLS_PUT_UINT32_BE -#define MBEDTLS_PUT_UINT32_BE(n,b,i) \ +#define MBEDTLS_PUT_UINT32_BE(n,b,i) \ do { \ (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ @@ -103,7 +103,7 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c * 32-bit integer manipulation macros (little endian) */ #ifndef MBEDTLS_GET_UINT32_LE -#define MBEDTLS_GET_UINT32_LE(n,b,i) \ +#define MBEDTLS_GET_UINT32_LE(n,b,i) \ do { \ (n) = ( (uint32_t) (b)[(i) ] ) \ | ( (uint32_t) (b)[(i) + 1] << 8 ) \ @@ -113,7 +113,7 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c #endif #ifndef MBEDTLS_PUT_UINT32_LE -#define MBEDTLS_PUT_UINT32_LE(n,b,i) \ +#define MBEDTLS_PUT_UINT32_LE(n,b,i) \ do { \ (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ @@ -125,11 +125,32 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c /** * 32-bit integer conversion from bytes (little endian) */ -#define MBEDTLS_BYTES_TO_U32_LE( data, offset ) \ +#define MBEDTLS_BYTES_TO_U32_LE( data, offset ) \ ( (uint32_t) (data)[offset] \ | (uint32_t) ( (uint32_t) (data)[( offset ) + 1] << 8 ) \ | (uint32_t) ( (uint32_t) (data)[( offset ) + 2] << 16 ) \ | (uint32_t) ( (uint32_t) (data)[( offset ) + 3] << 24 ) \ ) + +/* + * 16-bit integer manipulation macros (little endian) + */ +#ifndef MBEDTLS_GET_UINT16_LE +#define MBEDTLS_GET_UINT16_LE( n, b, i ) \ +{ \ + (n) = ( (uint16_t) (b)[(i) ] ) \ + | ( (uint16_t) (b)[(i) + 1] << 8 ); \ +} +#endif + +#ifndef MBEDTLS_PUT_UINT16_LE +#define MBEDTLS_PUT_UINT16_LE( n, b, i ) \ +{ \ + (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ + (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ +} +#endif + + #endif /* MBEDTLS_LIBRARY_COMMON_H */ diff --git a/library/psa_crypto_storage.c b/library/psa_crypto_storage.c index 3646953a57a1..07c2cdf6006c 100644 --- a/library/psa_crypto_storage.c +++ b/library/psa_crypto_storage.c @@ -230,25 +230,6 @@ static psa_status_t psa_crypto_storage_get_data_length( return( PSA_SUCCESS ); } -/* - * 16-bit integer manipulation macros (little endian) - */ -#ifndef GET_UINT16_LE -#define GET_UINT16_LE( n, b, i ) \ -{ \ - (n) = ( (uint16_t) (b)[(i) ] ) \ - | ( (uint16_t) (b)[(i) + 1] << 8 ); \ -} -#endif - -#ifndef PUT_UINT16_LE -#define PUT_UINT16_LE( n, b, i ) \ -{ \ - (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ - (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ -} -#endif - /** * Persistent key storage magic header. */ @@ -277,8 +258,8 @@ void psa_format_key_data_for_storage( const uint8_t *data, memcpy( storage_format->magic, PSA_KEY_STORAGE_MAGIC_HEADER, PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ); MBEDTLS_PUT_UINT32_LE( 0, storage_format->version, 0 ); MBEDTLS_PUT_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 ); - PUT_UINT16_LE( (uint16_t) attr->type, storage_format->type, 0 ); - PUT_UINT16_LE( (uint16_t) attr->bits, storage_format->bits, 0 ); + MBEDTLS_PUT_UINT16_LE( (uint16_t) attr->type, storage_format->type, 0 ); + MBEDTLS_PUT_UINT16_LE( (uint16_t) attr->bits, storage_format->bits, 0 ); MBEDTLS_PUT_UINT32_LE( attr->policy.usage, storage_format->policy, 0 ); MBEDTLS_PUT_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) ); MBEDTLS_PUT_UINT32_LE( attr->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) ); @@ -334,8 +315,8 @@ psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data, } MBEDTLS_GET_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 ); - GET_UINT16_LE( attr->type, storage_format->type, 0 ); - GET_UINT16_LE( attr->bits, storage_format->bits, 0 ); + MBEDTLS_GET_UINT16_LE( attr->type, storage_format->type, 0 ); + MBEDTLS_GET_UINT16_LE( attr->bits, storage_format->bits, 0 ); MBEDTLS_GET_UINT32_LE( attr->policy.usage, storage_format->policy, 0 ); MBEDTLS_GET_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) ); MBEDTLS_GET_UINT32_LE( attr->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) ); From 394bdd662b7facd32cfbb8ccd24cc0593f38ac0d Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Wed, 7 Jul 2021 15:16:56 +0100 Subject: [PATCH 046/120] Document common.h and remove changelog Added documenttion comments to common.h and removed the changelog as it is not really necessary for refactoring. Also modified a comment in aria.c to be clearer Signed-off-by: Joe Subbiani --- ChangeLog.d/issue4274.txt | 9 ------- library/aria.c | 3 ++- library/common.h | 50 +++++++++++++++++++++++++++++++++++---- 3 files changed, 47 insertions(+), 15 deletions(-) delete mode 100644 ChangeLog.d/issue4274.txt diff --git a/ChangeLog.d/issue4274.txt b/ChangeLog.d/issue4274.txt deleted file mode 100644 index f0952f43071a..000000000000 --- a/ChangeLog.d/issue4274.txt +++ /dev/null @@ -1,9 +0,0 @@ -Changes - * Create 4 byte reading macros in library/common.h, used in files - within the same directory: MBEDTLS_BYTE_0... MBEDTLS_BYTE_3. - * Move the (PUT and GET) UINT32_ (BE and LE) macro functions into - library/common.h. Rename with the prefix MBEDTLS_ to satisfy - test/scripts/check-names.sh (e.g MBEDTLS_PUT_UINT32_LE). - * Move BYTES_TO_U32_LE macro function to library/common.h, also given - the prefix MBEDTLS_. - Fixes #4274. diff --git a/library/aria.c b/library/aria.c index a6319d3e29fa..f4aa64107a82 100644 --- a/library/aria.c +++ b/library/aria.c @@ -385,7 +385,8 @@ static void aria_fe_xor( uint32_t r[4], const uint32_t p[4], * Big endian 128-bit rotation: r = a ^ (b <<< n), used only in key setup. * * We chose to store bytes into 32-bit words in little-endian format (see - * GET/MBEDTLS_PUT_UINT32_LE) so we need to reverse bytes here. + * MBEDTLS_GET_UINT32_LE / MBEDTLS_PUT_UINT32_LE ) so we need to reverse + * bytes here. */ static void aria_rot128( uint32_t r[4], const uint32_t a[4], const uint32_t b[4], uint8_t n ) diff --git a/library/common.h b/library/common.h index e85cbf28d0f6..5115465e885b 100644 --- a/library/common.h +++ b/library/common.h @@ -68,15 +68,36 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c /** Byte Reading Macros * - * To tidy up code and save horizontal and vertical space, use byte - * reading macros to cast + * Obtain the most significant byte of x using 0xff + * Using MBEDTLS_BYTE_a will shift a*8 bits + * to retrieve the next byte of information */ #define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) ) #define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) ) #define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) ) #define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) ) -/* +/** + * 32-bit integer manipulation macros + * + * \brief Using GET- + * From input data, take the most significant bytes + * and concatonate them as you shift along + * Using PUT- + * Read from a 32 bit integer and store each byte + * in memory, offset by a byte each, resulting in + * each byte being adjacent in memory. + * + * \param n 32 bit integer where data is accessed via + * PUT or stored using GET + * \param b const unsigned char array of data to be + * manipulated + * \param i offset in bytes, In the case of UINT32, i + * would increment by 4 every use assuming + * the data is being stored in the same location + */ + +/** * 32-bit integer manipulation macros (big endian) */ #ifndef MBEDTLS_GET_UINT32_BE @@ -99,7 +120,7 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c } while( 0 ) #endif -/* +/** * 32-bit integer manipulation macros (little endian) */ #ifndef MBEDTLS_GET_UINT32_LE @@ -132,8 +153,27 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c | (uint32_t) ( (uint32_t) (data)[( offset ) + 3] << 24 ) \ ) +/** + * 16-bit integer manipulation macros + * + * \brief Using GET- + * From input data, take the most significant bytes + * and concatonate them as you shift along + * Using PUT- + * Read from a 16 bit integer and store each byte + * in memory, offset by a byte each, resulting in + * each byte being adjacent in memory. + * + * \param n 16 bit integer where data is accessed via + * PUT or stored using GET + * \param b const unsigned char array of data to be + * manipulated + * \param i offset in bytes, In the case of UINT16, i + * would increment by 2 every use assuming + * the data is being stored in the same location + */ -/* +/** * 16-bit integer manipulation macros (little endian) */ #ifndef MBEDTLS_GET_UINT16_LE From 6a5063149756e1d5d12b0497867f5c22aaa8100e Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Wed, 7 Jul 2021 16:56:29 +0100 Subject: [PATCH 047/120] GET macros use a target variable The GET macros used to write to a macro parameter, but now they can be used to assign a value to the desired variable rather than pass it in as an argument and have it modified in the macro function. Due to this MBEDTLS_BYTES_TO_U32_LE is the same as MBEDTLS_GET_UINT32_LE and was there for replaced in the appropriate files and removed from common.h Signed-off-by: Joe Subbiani --- library/aes.c | 18 ++-- library/aria.c | 24 +++--- library/camellia.c | 14 +-- library/chacha20.c | 22 ++--- library/common.h | 159 +++++++++++++++++++++-------------- library/des.c | 12 +-- library/gcm.c | 8 +- library/md5.c | 32 +++---- library/nist_kw.c | 2 +- library/poly1305.c | 26 +++--- library/psa_crypto_storage.c | 16 ++-- library/ripemd160.c | 32 +++---- library/sha1.c | 32 +++---- library/sha256.c | 4 +- 14 files changed, 216 insertions(+), 185 deletions(-) diff --git a/library/aes.c b/library/aes.c index db726fe36b86..7a44a7840883 100644 --- a/library/aes.c +++ b/library/aes.c @@ -567,7 +567,7 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key, for( i = 0; i < ( keybits >> 5 ); i++ ) { - MBEDTLS_GET_UINT32_LE( RK[i], key, i << 2 ); + RK[i] = MBEDTLS_GET_UINT32_LE( key, i << 2 ); } switch( ctx->nr ) @@ -850,10 +850,10 @@ int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx, uint32_t Y[4]; } t; - MBEDTLS_GET_UINT32_LE( t.X[0], input, 0 ); t.X[0] ^= *RK++; - MBEDTLS_GET_UINT32_LE( t.X[1], input, 4 ); t.X[1] ^= *RK++; - MBEDTLS_GET_UINT32_LE( t.X[2], input, 8 ); t.X[2] ^= *RK++; - MBEDTLS_GET_UINT32_LE( t.X[3], input, 12 ); t.X[3] ^= *RK++; + t.X[0] = MBEDTLS_GET_UINT32_LE( input, 0 ); t.X[0] ^= *RK++; + t.X[1] = MBEDTLS_GET_UINT32_LE( input, 4 ); t.X[1] ^= *RK++; + t.X[2] = MBEDTLS_GET_UINT32_LE( input, 8 ); t.X[2] ^= *RK++; + t.X[3] = MBEDTLS_GET_UINT32_LE( input, 12 ); t.X[3] ^= *RK++; for( i = ( ctx->nr >> 1 ) - 1; i > 0; i-- ) { @@ -914,10 +914,10 @@ int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx, uint32_t Y[4]; } t; - MBEDTLS_GET_UINT32_LE( t.X[0], input, 0 ); t.X[0] ^= *RK++; - MBEDTLS_GET_UINT32_LE( t.X[1], input, 4 ); t.X[1] ^= *RK++; - MBEDTLS_GET_UINT32_LE( t.X[2], input, 8 ); t.X[2] ^= *RK++; - MBEDTLS_GET_UINT32_LE( t.X[3], input, 12 ); t.X[3] ^= *RK++; + t.X[0] = MBEDTLS_GET_UINT32_LE( input, 0 ); t.X[0] ^= *RK++; + t.X[1] = MBEDTLS_GET_UINT32_LE( input, 4 ); t.X[1] ^= *RK++; + t.X[2] = MBEDTLS_GET_UINT32_LE( input, 8 ); t.X[2] ^= *RK++; + t.X[3] = MBEDTLS_GET_UINT32_LE( input, 12 ); t.X[3] ^= *RK++; for( i = ( ctx->nr >> 1 ) - 1; i > 0; i-- ) { diff --git a/library/aria.c b/library/aria.c index f4aa64107a82..320f7758aca0 100644 --- a/library/aria.c +++ b/library/aria.c @@ -434,21 +434,21 @@ int mbedtls_aria_setkey_enc( mbedtls_aria_context *ctx, return( MBEDTLS_ERR_ARIA_BAD_INPUT_DATA ); /* Copy key to W0 (and potential remainder to W1) */ - MBEDTLS_GET_UINT32_LE( w[0][0], key, 0 ); - MBEDTLS_GET_UINT32_LE( w[0][1], key, 4 ); - MBEDTLS_GET_UINT32_LE( w[0][2], key, 8 ); - MBEDTLS_GET_UINT32_LE( w[0][3], key, 12 ); + w[0][0] = MBEDTLS_GET_UINT32_LE( key, 0 ); + w[0][1] = MBEDTLS_GET_UINT32_LE( key, 4 ); + w[0][2] = MBEDTLS_GET_UINT32_LE( key, 8 ); + w[0][3] = MBEDTLS_GET_UINT32_LE( key, 12 ); memset( w[1], 0, 16 ); if( keybits >= 192 ) { - MBEDTLS_GET_UINT32_LE( w[1][0], key, 16 ); // 192 bit key - MBEDTLS_GET_UINT32_LE( w[1][1], key, 20 ); + w[1][0] = MBEDTLS_GET_UINT32_LE( key, 16 ); // 192 bit key + w[1][1] = MBEDTLS_GET_UINT32_LE( key, 20 ); } if( keybits == 256 ) { - MBEDTLS_GET_UINT32_LE( w[1][2], key, 24 ); // 256 bit key - MBEDTLS_GET_UINT32_LE( w[1][3], key, 28 ); + w[1][2] = MBEDTLS_GET_UINT32_LE( key, 24 ); // 256 bit key + w[1][3] = MBEDTLS_GET_UINT32_LE( key, 28 ); } i = ( keybits - 128 ) >> 6; // index: 0, 1, 2 @@ -525,10 +525,10 @@ int mbedtls_aria_crypt_ecb( mbedtls_aria_context *ctx, ARIA_VALIDATE_RET( input != NULL ); ARIA_VALIDATE_RET( output != NULL ); - MBEDTLS_GET_UINT32_LE( a, input, 0 ); - MBEDTLS_GET_UINT32_LE( b, input, 4 ); - MBEDTLS_GET_UINT32_LE( c, input, 8 ); - MBEDTLS_GET_UINT32_LE( d, input, 12 ); + a = MBEDTLS_GET_UINT32_LE( input, 0 ); + b = MBEDTLS_GET_UINT32_LE( input, 4 ); + c = MBEDTLS_GET_UINT32_LE( input, 8 ); + d = MBEDTLS_GET_UINT32_LE( input, 12 ); i = 0; while( 1 ) diff --git a/library/camellia.c b/library/camellia.c index 9aab7ab675ef..4d6b468e5dec 100644 --- a/library/camellia.c +++ b/library/camellia.c @@ -353,8 +353,8 @@ int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, * Prepare SIGMA values */ for( i = 0; i < 6; i++ ) { - MBEDTLS_GET_UINT32_BE( SIGMA[i][0], SIGMA_CHARS[i], 0 ); - MBEDTLS_GET_UINT32_BE( SIGMA[i][1], SIGMA_CHARS[i], 4 ); + SIGMA[i][0] = MBEDTLS_GET_UINT32_BE( SIGMA_CHARS[i], 0 ); + SIGMA[i][1] = MBEDTLS_GET_UINT32_BE( SIGMA_CHARS[i], 4 ); } /* @@ -365,7 +365,7 @@ int mbedtls_camellia_setkey_enc( mbedtls_camellia_context *ctx, /* Store KL, KR */ for( i = 0; i < 8; i++ ) - MBEDTLS_GET_UINT32_BE( KC[i], t, i * 4 ); + KC[i] = MBEDTLS_GET_UINT32_BE( t, i * 4 ); /* Generate KA */ for( i = 0; i < 4; ++i ) @@ -491,10 +491,10 @@ int mbedtls_camellia_crypt_ecb( mbedtls_camellia_context *ctx, NR = ctx->nr; RK = ctx->rk; - MBEDTLS_GET_UINT32_BE( X[0], input, 0 ); - MBEDTLS_GET_UINT32_BE( X[1], input, 4 ); - MBEDTLS_GET_UINT32_BE( X[2], input, 8 ); - MBEDTLS_GET_UINT32_BE( X[3], input, 12 ); + X[0] = MBEDTLS_GET_UINT32_BE( input, 0 ); + X[1] = MBEDTLS_GET_UINT32_BE( input, 4 ); + X[2] = MBEDTLS_GET_UINT32_BE( input, 8 ); + X[3] = MBEDTLS_GET_UINT32_BE( input, 12 ); X[0] ^= *RK++; X[1] ^= *RK++; diff --git a/library/chacha20.c b/library/chacha20.c index d0d5741c7b9d..7015f99d5957 100644 --- a/library/chacha20.c +++ b/library/chacha20.c @@ -205,14 +205,14 @@ int mbedtls_chacha20_setkey( mbedtls_chacha20_context *ctx, ctx->state[3] = 0x6b206574; /* Set key */ - ctx->state[4] = MBEDTLS_BYTES_TO_U32_LE( key, 0 ); - ctx->state[5] = MBEDTLS_BYTES_TO_U32_LE( key, 4 ); - ctx->state[6] = MBEDTLS_BYTES_TO_U32_LE( key, 8 ); - ctx->state[7] = MBEDTLS_BYTES_TO_U32_LE( key, 12 ); - ctx->state[8] = MBEDTLS_BYTES_TO_U32_LE( key, 16 ); - ctx->state[9] = MBEDTLS_BYTES_TO_U32_LE( key, 20 ); - ctx->state[10] = MBEDTLS_BYTES_TO_U32_LE( key, 24 ); - ctx->state[11] = MBEDTLS_BYTES_TO_U32_LE( key, 28 ); + ctx->state[4] = MBEDTLS_GET_UINT32_LE( key, 0 ); + ctx->state[5] = MBEDTLS_GET_UINT32_LE( key, 4 ); + ctx->state[6] = MBEDTLS_GET_UINT32_LE( key, 8 ); + ctx->state[7] = MBEDTLS_GET_UINT32_LE( key, 12 ); + ctx->state[8] = MBEDTLS_GET_UINT32_LE( key, 16 ); + ctx->state[9] = MBEDTLS_GET_UINT32_LE( key, 20 ); + ctx->state[10] = MBEDTLS_GET_UINT32_LE( key, 24 ); + ctx->state[11] = MBEDTLS_GET_UINT32_LE( key, 28 ); return( 0 ); } @@ -228,9 +228,9 @@ int mbedtls_chacha20_starts( mbedtls_chacha20_context* ctx, ctx->state[12] = counter; /* Nonce */ - ctx->state[13] = MBEDTLS_BYTES_TO_U32_LE( nonce, 0 ); - ctx->state[14] = MBEDTLS_BYTES_TO_U32_LE( nonce, 4 ); - ctx->state[15] = MBEDTLS_BYTES_TO_U32_LE( nonce, 8 ); + ctx->state[13] = MBEDTLS_GET_UINT32_LE( nonce, 0 ); + ctx->state[14] = MBEDTLS_GET_UINT32_LE( nonce, 4 ); + ctx->state[15] = MBEDTLS_GET_UINT32_LE( nonce, 8 ); mbedtls_platform_zeroize( ctx->keystream8, sizeof( ctx->keystream8 ) ); diff --git a/library/common.h b/library/common.h index 5115465e885b..4ecc0162bf12 100644 --- a/library/common.h +++ b/library/common.h @@ -78,38 +78,45 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c #define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) ) /** - * 32-bit integer manipulation macros - * - * \brief Using GET- - * From input data, take the most significant bytes - * and concatonate them as you shift along - * Using PUT- - * Read from a 32 bit integer and store each byte - * in memory, offset by a byte each, resulting in - * each byte being adjacent in memory. - * - * \param n 32 bit integer where data is accessed via - * PUT or stored using GET + * 32-bit integer manipulation GET macros (big endian) + * + * \brief Use this to assign an unsigned 32 bit integer + * by taking data stored adjacent in memory that + * can be accessed via on offset + * Big Endian is used when wanting to + * transmit the most signifcant bits first + * + * \param data The data used to translate to a 32 bit + * integer + * \param offset the shift in bytes to access the next byte + * of data + */ +#ifndef MBEDTLS_GET_UINT32_BE +#define MBEDTLS_GET_UINT32_BE( data , offset ) \ + ( \ + ( (uint32_t) ( data )[( offset ) ] << 24 ) \ + | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \ + | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \ + | ( (uint32_t) ( data )[( offset ) + 3] ) \ + ) +#endif + +/** + * 32-bit integer manipulation PUT macros (big endian) + * + * \brief Read from a 32 bit integer and store each byte + * in memory, offset by a specified amount, resulting + * in each byte being adjacent in memory. + * Big Endian is used when wanting to + * transmit the most signifcant bits first + * + * \param n 32 bit integer where data is accessed * \param b const unsigned char array of data to be * manipulated * \param i offset in bytes, In the case of UINT32, i * would increment by 4 every use assuming * the data is being stored in the same location */ - -/** - * 32-bit integer manipulation macros (big endian) - */ -#ifndef MBEDTLS_GET_UINT32_BE -#define MBEDTLS_GET_UINT32_BE(n,b,i) \ - do { \ - (n) = ( (uint32_t) (b)[(i) ] << 24 ) \ - | ( (uint32_t) (b)[(i) + 1] << 16 ) \ - | ( (uint32_t) (b)[(i) + 2] << 8 ) \ - | ( (uint32_t) (b)[(i) + 3] ); \ - } while( 0 ) -#endif - #ifndef MBEDTLS_PUT_UINT32_BE #define MBEDTLS_PUT_UINT32_BE(n,b,i) \ do { \ @@ -121,18 +128,45 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c #endif /** - * 32-bit integer manipulation macros (little endian) + * 32-bit integer manipulation GET macros (little endian) + * + * \brief Use this to assign an unsigned 32 bit integer + * by taking data stored adjacent in memory that + * can be accessed via on offset + * Little Endian is used when wanting to + * transmit the least signifcant bits first + * + * \param data The data used to translate to a 32 bit + * integer + * \param offset the shift in bytes to access the next byte + * of data */ #ifndef MBEDTLS_GET_UINT32_LE -#define MBEDTLS_GET_UINT32_LE(n,b,i) \ - do { \ - (n) = ( (uint32_t) (b)[(i) ] ) \ - | ( (uint32_t) (b)[(i) + 1] << 8 ) \ - | ( (uint32_t) (b)[(i) + 2] << 16 ) \ - | ( (uint32_t) (b)[(i) + 3] << 24 ); \ - } while( 0 ) +#define MBEDTLS_GET_UINT32_LE( data, offset ) \ + ( \ + ( (uint32_t) ( data )[( offset ) ] ) \ + | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \ + | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \ + | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \ + ) #endif +/** + * 32-bit integer manipulation PUT macros (little endian) + * + * \brief Read from a 32 bit integer and store each byte + * in memory, offset by a specified amount, resulting + * in each byte being adjacent in memory. + * Little Endian is used when wanting to + * transmit the least signifcant bits first + * + * \param n 32 bit integer where data is accessed + * \param b const unsigned char array of data to be + * manipulated + * \param i offset in bytes, In the case of UINT32, i + * would increment by 4 every use assuming + * the data is being stored in the same location + */ #ifndef MBEDTLS_PUT_UINT32_LE #define MBEDTLS_PUT_UINT32_LE(n,b,i) \ do { \ @@ -144,46 +178,43 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c #endif /** - * 32-bit integer conversion from bytes (little endian) + * 16-bit integer manipulation GET macros (little endian) + * + * \brief Use this to assign an unsigned 16 bit integer + * by taking data stored adjacent in memory that + * can be accessed via on offset + * Little Endian is used when wanting to + * transmit the least signifcant bits first + * + * \param data The data used to translate to a 16 bit + * integer + * \param offset the shit in bytes to access the next byte + * of data */ -#define MBEDTLS_BYTES_TO_U32_LE( data, offset ) \ - ( (uint32_t) (data)[offset] \ - | (uint32_t) ( (uint32_t) (data)[( offset ) + 1] << 8 ) \ - | (uint32_t) ( (uint32_t) (data)[( offset ) + 2] << 16 ) \ - | (uint32_t) ( (uint32_t) (data)[( offset ) + 3] << 24 ) \ +#ifndef MBEDTLS_GET_UINT16_LE +#define MBEDTLS_GET_UINT16_LE( data, offset ) \ + ( \ + ( (uint16_t) ( data )[( offset ) ] ) \ + | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \ ) +#endif /** - * 16-bit integer manipulation macros - * - * \brief Using GET- - * From input data, take the most significant bytes - * and concatonate them as you shift along - * Using PUT- - * Read from a 16 bit integer and store each byte - * in memory, offset by a byte each, resulting in - * each byte being adjacent in memory. - * - * \param n 16 bit integer where data is accessed via - * PUT or stored using GET + * 16-bit integer manipulation PUT macros (little endian) + * + * \brief Read from a 16 bit integer and store each byte + * in memory, offset by a specified amount, resulting + * in each byte being adjacent in memory. + * Little Endian is used when wanting to + * transmit the least signifcant bits first + * + * \param n 16 bit integer where data is accessed * \param b const unsigned char array of data to be * manipulated * \param i offset in bytes, In the case of UINT16, i * would increment by 2 every use assuming * the data is being stored in the same location */ - -/** - * 16-bit integer manipulation macros (little endian) - */ -#ifndef MBEDTLS_GET_UINT16_LE -#define MBEDTLS_GET_UINT16_LE( n, b, i ) \ -{ \ - (n) = ( (uint16_t) (b)[(i) ] ) \ - | ( (uint16_t) (b)[(i) + 1] << 8 ); \ -} -#endif - #ifndef MBEDTLS_PUT_UINT16_LE #define MBEDTLS_PUT_UINT16_LE( n, b, i ) \ { \ diff --git a/library/des.c b/library/des.c index 9281747de8dc..7f90faa044c1 100644 --- a/library/des.c +++ b/library/des.c @@ -400,8 +400,8 @@ void mbedtls_des_setkey( uint32_t SK[32], const unsigned char key[MBEDTLS_DES_KE int i; uint32_t X, Y, T; - MBEDTLS_GET_UINT32_BE( X, key, 0 ); - MBEDTLS_GET_UINT32_BE( Y, key, 4 ); + X = MBEDTLS_GET_UINT32_BE( key, 0 ); + Y = MBEDTLS_GET_UINT32_BE( key, 4 ); /* * Permuted Choice 1 @@ -610,8 +610,8 @@ int mbedtls_des_crypt_ecb( mbedtls_des_context *ctx, SK = ctx->sk; - MBEDTLS_GET_UINT32_BE( X, input, 0 ); - MBEDTLS_GET_UINT32_BE( Y, input, 4 ); + X = MBEDTLS_GET_UINT32_BE( input, 0 ); + Y = MBEDTLS_GET_UINT32_BE( input, 4 ); DES_IP( X, Y ); @@ -697,8 +697,8 @@ int mbedtls_des3_crypt_ecb( mbedtls_des3_context *ctx, SK = ctx->sk; - MBEDTLS_GET_UINT32_BE( X, input, 0 ); - MBEDTLS_GET_UINT32_BE( Y, input, 4 ); + X = MBEDTLS_GET_UINT32_BE( input, 0 ); + Y = MBEDTLS_GET_UINT32_BE( input, 4 ); DES_IP( X, Y ); diff --git a/library/gcm.c b/library/gcm.c index 3caeed26def9..910646b281cb 100644 --- a/library/gcm.c +++ b/library/gcm.c @@ -88,12 +88,12 @@ static int gcm_gen_table( mbedtls_gcm_context *ctx ) return( ret ); /* pack h as two 64-bits ints, big-endian */ - MBEDTLS_GET_UINT32_BE( hi, h, 0 ); - MBEDTLS_GET_UINT32_BE( lo, h, 4 ); + hi = MBEDTLS_GET_UINT32_BE( h, 0 ); + lo = MBEDTLS_GET_UINT32_BE( h, 4 ); vh = (uint64_t) hi << 32 | lo; - MBEDTLS_GET_UINT32_BE( hi, h, 8 ); - MBEDTLS_GET_UINT32_BE( lo, h, 12 ); + hi = MBEDTLS_GET_UINT32_BE( h, 8 ); + lo = MBEDTLS_GET_UINT32_BE( h, 12 ); vl = (uint64_t) hi << 32 | lo; /* 8 = 1000 corresponds to 1 in GF(2^128) */ diff --git a/library/md5.c b/library/md5.c index e8d00216f0d2..a9bbcb488b88 100644 --- a/library/md5.c +++ b/library/md5.c @@ -87,22 +87,22 @@ int mbedtls_internal_md5_process( mbedtls_md5_context *ctx, uint32_t X[16], A, B, C, D; } local; - MBEDTLS_GET_UINT32_LE( local.X[ 0], data, 0 ); - MBEDTLS_GET_UINT32_LE( local.X[ 1], data, 4 ); - MBEDTLS_GET_UINT32_LE( local.X[ 2], data, 8 ); - MBEDTLS_GET_UINT32_LE( local.X[ 3], data, 12 ); - MBEDTLS_GET_UINT32_LE( local.X[ 4], data, 16 ); - MBEDTLS_GET_UINT32_LE( local.X[ 5], data, 20 ); - MBEDTLS_GET_UINT32_LE( local.X[ 6], data, 24 ); - MBEDTLS_GET_UINT32_LE( local.X[ 7], data, 28 ); - MBEDTLS_GET_UINT32_LE( local.X[ 8], data, 32 ); - MBEDTLS_GET_UINT32_LE( local.X[ 9], data, 36 ); - MBEDTLS_GET_UINT32_LE( local.X[10], data, 40 ); - MBEDTLS_GET_UINT32_LE( local.X[11], data, 44 ); - MBEDTLS_GET_UINT32_LE( local.X[12], data, 48 ); - MBEDTLS_GET_UINT32_LE( local.X[13], data, 52 ); - MBEDTLS_GET_UINT32_LE( local.X[14], data, 56 ); - MBEDTLS_GET_UINT32_LE( local.X[15], data, 60 ); + local.X[ 0] = MBEDTLS_GET_UINT32_LE( data, 0 ); + local.X[ 1] = MBEDTLS_GET_UINT32_LE( data, 4 ); + local.X[ 2] = MBEDTLS_GET_UINT32_LE( data, 8 ); + local.X[ 3] = MBEDTLS_GET_UINT32_LE( data, 12 ); + local.X[ 4] = MBEDTLS_GET_UINT32_LE( data, 16 ); + local.X[ 5] = MBEDTLS_GET_UINT32_LE( data, 20 ); + local.X[ 6] = MBEDTLS_GET_UINT32_LE( data, 24 ); + local.X[ 7] = MBEDTLS_GET_UINT32_LE( data, 28 ); + local.X[ 8] = MBEDTLS_GET_UINT32_LE( data, 32 ); + local.X[ 9] = MBEDTLS_GET_UINT32_LE( data, 36 ); + local.X[10] = MBEDTLS_GET_UINT32_LE( data, 40 ); + local.X[11] = MBEDTLS_GET_UINT32_LE( data, 44 ); + local.X[12] = MBEDTLS_GET_UINT32_LE( data, 48 ); + local.X[13] = MBEDTLS_GET_UINT32_LE( data, 52 ); + local.X[14] = MBEDTLS_GET_UINT32_LE( data, 56 ); + local.X[15] = MBEDTLS_GET_UINT32_LE( data, 60 ); #define S(x,n) \ ( ( (x) << (n) ) | ( ( (x) & 0xFFFFFFFF) >> ( 32 - (n) ) ) ) diff --git a/library/nist_kw.c b/library/nist_kw.c index b8f923999200..e2ab2566f598 100644 --- a/library/nist_kw.c +++ b/library/nist_kw.c @@ -454,7 +454,7 @@ int mbedtls_nist_kw_unwrap( mbedtls_nist_kw_context *ctx, ret = MBEDTLS_ERR_CIPHER_AUTH_FAILED; } - MBEDTLS_GET_UINT32_BE( Plen, A, KW_SEMIBLOCK_LENGTH / 2 ); + Plen = MBEDTLS_GET_UINT32_BE( A, KW_SEMIBLOCK_LENGTH / 2 ); /* * Plen is the length of the plaintext, when the input is valid. diff --git a/library/poly1305.c b/library/poly1305.c index 3c0b7c6aa9c5..f19574253ca2 100644 --- a/library/poly1305.c +++ b/library/poly1305.c @@ -122,10 +122,10 @@ static void poly1305_process( mbedtls_poly1305_context *ctx, for( i = 0U; i < nblocks; i++ ) { /* The input block is treated as a 128-bit little-endian integer */ - d0 = MBEDTLS_BYTES_TO_U32_LE( input, offset + 0 ); - d1 = MBEDTLS_BYTES_TO_U32_LE( input, offset + 4 ); - d2 = MBEDTLS_BYTES_TO_U32_LE( input, offset + 8 ); - d3 = MBEDTLS_BYTES_TO_U32_LE( input, offset + 12 ); + d0 = MBEDTLS_GET_UINT32_LE( input, offset + 0 ); + d1 = MBEDTLS_GET_UINT32_LE( input, offset + 4 ); + d2 = MBEDTLS_GET_UINT32_LE( input, offset + 8 ); + d3 = MBEDTLS_GET_UINT32_LE( input, offset + 12 ); /* Compute: acc += (padded) block as a 130-bit integer */ d0 += (uint64_t) acc0; @@ -290,15 +290,15 @@ int mbedtls_poly1305_starts( mbedtls_poly1305_context *ctx, POLY1305_VALIDATE_RET( key != NULL ); /* r &= 0x0ffffffc0ffffffc0ffffffc0fffffff */ - ctx->r[0] = MBEDTLS_BYTES_TO_U32_LE( key, 0 ) & 0x0FFFFFFFU; - ctx->r[1] = MBEDTLS_BYTES_TO_U32_LE( key, 4 ) & 0x0FFFFFFCU; - ctx->r[2] = MBEDTLS_BYTES_TO_U32_LE( key, 8 ) & 0x0FFFFFFCU; - ctx->r[3] = MBEDTLS_BYTES_TO_U32_LE( key, 12 ) & 0x0FFFFFFCU; - - ctx->s[0] = MBEDTLS_BYTES_TO_U32_LE( key, 16 ); - ctx->s[1] = MBEDTLS_BYTES_TO_U32_LE( key, 20 ); - ctx->s[2] = MBEDTLS_BYTES_TO_U32_LE( key, 24 ); - ctx->s[3] = MBEDTLS_BYTES_TO_U32_LE( key, 28 ); + ctx->r[0] = MBEDTLS_GET_UINT32_LE( key, 0 ) & 0x0FFFFFFFU; + ctx->r[1] = MBEDTLS_GET_UINT32_LE( key, 4 ) & 0x0FFFFFFCU; + ctx->r[2] = MBEDTLS_GET_UINT32_LE( key, 8 ) & 0x0FFFFFFCU; + ctx->r[3] = MBEDTLS_GET_UINT32_LE( key, 12 ) & 0x0FFFFFFCU; + + ctx->s[0] = MBEDTLS_GET_UINT32_LE( key, 16 ); + ctx->s[1] = MBEDTLS_GET_UINT32_LE( key, 20 ); + ctx->s[2] = MBEDTLS_GET_UINT32_LE( key, 24 ); + ctx->s[3] = MBEDTLS_GET_UINT32_LE( key, 28 ); /* Initial accumulator state */ ctx->acc[0] = 0U; diff --git a/library/psa_crypto_storage.c b/library/psa_crypto_storage.c index 07c2cdf6006c..c6660b9553d0 100644 --- a/library/psa_crypto_storage.c +++ b/library/psa_crypto_storage.c @@ -293,11 +293,11 @@ psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data, if( status != PSA_SUCCESS ) return( status ); - MBEDTLS_GET_UINT32_LE( version, storage_format->version, 0 ); + version = MBEDTLS_GET_UINT32_LE( storage_format->version, 0 ); if( version != 0 ) return( PSA_ERROR_DATA_INVALID ); - MBEDTLS_GET_UINT32_LE( *key_data_length, storage_format->data_len, 0 ); + *key_data_length = MBEDTLS_GET_UINT32_LE( storage_format->data_len, 0 ); if( *key_data_length > ( storage_data_length - sizeof(*storage_format) ) || *key_data_length > PSA_CRYPTO_MAX_STORAGE_SIZE ) return( PSA_ERROR_DATA_INVALID ); @@ -314,12 +314,12 @@ psa_status_t psa_parse_key_data_from_storage( const uint8_t *storage_data, memcpy( *key_data, storage_format->key_data, *key_data_length ); } - MBEDTLS_GET_UINT32_LE( attr->lifetime, storage_format->lifetime, 0 ); - MBEDTLS_GET_UINT16_LE( attr->type, storage_format->type, 0 ); - MBEDTLS_GET_UINT16_LE( attr->bits, storage_format->bits, 0 ); - MBEDTLS_GET_UINT32_LE( attr->policy.usage, storage_format->policy, 0 ); - MBEDTLS_GET_UINT32_LE( attr->policy.alg, storage_format->policy, sizeof( uint32_t ) ); - MBEDTLS_GET_UINT32_LE( attr->policy.alg2, storage_format->policy, 2 * sizeof( uint32_t ) ); + attr->lifetime = MBEDTLS_GET_UINT32_LE( storage_format->lifetime, 0 ); + attr->type = MBEDTLS_GET_UINT16_LE( storage_format->type, 0 ); + attr->bits = MBEDTLS_GET_UINT16_LE( storage_format->bits, 0 ); + attr->policy.usage = MBEDTLS_GET_UINT32_LE( storage_format->policy, 0 ); + attr->policy.alg = MBEDTLS_GET_UINT32_LE( storage_format->policy, sizeof( uint32_t ) ); + attr->policy.alg2 = MBEDTLS_GET_UINT32_LE( storage_format->policy, 2 * sizeof( uint32_t ) ); return( PSA_SUCCESS ); } diff --git a/library/ripemd160.c b/library/ripemd160.c index 2bed107230a7..41d838722627 100644 --- a/library/ripemd160.c +++ b/library/ripemd160.c @@ -92,22 +92,22 @@ int mbedtls_internal_ripemd160_process( mbedtls_ripemd160_context *ctx, uint32_t A, B, C, D, E, Ap, Bp, Cp, Dp, Ep, X[16]; } local; - MBEDTLS_GET_UINT32_LE( local.X[ 0], data, 0 ); - MBEDTLS_GET_UINT32_LE( local.X[ 1], data, 4 ); - MBEDTLS_GET_UINT32_LE( local.X[ 2], data, 8 ); - MBEDTLS_GET_UINT32_LE( local.X[ 3], data, 12 ); - MBEDTLS_GET_UINT32_LE( local.X[ 4], data, 16 ); - MBEDTLS_GET_UINT32_LE( local.X[ 5], data, 20 ); - MBEDTLS_GET_UINT32_LE( local.X[ 6], data, 24 ); - MBEDTLS_GET_UINT32_LE( local.X[ 7], data, 28 ); - MBEDTLS_GET_UINT32_LE( local.X[ 8], data, 32 ); - MBEDTLS_GET_UINT32_LE( local.X[ 9], data, 36 ); - MBEDTLS_GET_UINT32_LE( local.X[10], data, 40 ); - MBEDTLS_GET_UINT32_LE( local.X[11], data, 44 ); - MBEDTLS_GET_UINT32_LE( local.X[12], data, 48 ); - MBEDTLS_GET_UINT32_LE( local.X[13], data, 52 ); - MBEDTLS_GET_UINT32_LE( local.X[14], data, 56 ); - MBEDTLS_GET_UINT32_LE( local.X[15], data, 60 ); + local.X[ 0] = MBEDTLS_GET_UINT32_LE( data, 0 ); + local.X[ 1] = MBEDTLS_GET_UINT32_LE( data, 4 ); + local.X[ 2] = MBEDTLS_GET_UINT32_LE( data, 8 ); + local.X[ 3] = MBEDTLS_GET_UINT32_LE( data, 12 ); + local.X[ 4] = MBEDTLS_GET_UINT32_LE( data, 16 ); + local.X[ 5] = MBEDTLS_GET_UINT32_LE( data, 20 ); + local.X[ 6] = MBEDTLS_GET_UINT32_LE( data, 24 ); + local.X[ 7] = MBEDTLS_GET_UINT32_LE( data, 28 ); + local.X[ 8] = MBEDTLS_GET_UINT32_LE( data, 32 ); + local.X[ 9] = MBEDTLS_GET_UINT32_LE( data, 36 ); + local.X[10] = MBEDTLS_GET_UINT32_LE( data, 40 ); + local.X[11] = MBEDTLS_GET_UINT32_LE( data, 44 ); + local.X[12] = MBEDTLS_GET_UINT32_LE( data, 48 ); + local.X[13] = MBEDTLS_GET_UINT32_LE( data, 52 ); + local.X[14] = MBEDTLS_GET_UINT32_LE( data, 56 ); + local.X[15] = MBEDTLS_GET_UINT32_LE( data, 60 ); local.A = local.Ap = ctx->state[0]; local.B = local.Bp = ctx->state[1]; diff --git a/library/sha1.c b/library/sha1.c index da61f65fca0c..6fc937123154 100644 --- a/library/sha1.c +++ b/library/sha1.c @@ -103,22 +103,22 @@ int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx, SHA1_VALIDATE_RET( ctx != NULL ); SHA1_VALIDATE_RET( (const unsigned char *)data != NULL ); - MBEDTLS_GET_UINT32_BE( local.W[ 0], data, 0 ); - MBEDTLS_GET_UINT32_BE( local.W[ 1], data, 4 ); - MBEDTLS_GET_UINT32_BE( local.W[ 2], data, 8 ); - MBEDTLS_GET_UINT32_BE( local.W[ 3], data, 12 ); - MBEDTLS_GET_UINT32_BE( local.W[ 4], data, 16 ); - MBEDTLS_GET_UINT32_BE( local.W[ 5], data, 20 ); - MBEDTLS_GET_UINT32_BE( local.W[ 6], data, 24 ); - MBEDTLS_GET_UINT32_BE( local.W[ 7], data, 28 ); - MBEDTLS_GET_UINT32_BE( local.W[ 8], data, 32 ); - MBEDTLS_GET_UINT32_BE( local.W[ 9], data, 36 ); - MBEDTLS_GET_UINT32_BE( local.W[10], data, 40 ); - MBEDTLS_GET_UINT32_BE( local.W[11], data, 44 ); - MBEDTLS_GET_UINT32_BE( local.W[12], data, 48 ); - MBEDTLS_GET_UINT32_BE( local.W[13], data, 52 ); - MBEDTLS_GET_UINT32_BE( local.W[14], data, 56 ); - MBEDTLS_GET_UINT32_BE( local.W[15], data, 60 ); + local.W[ 0] = MBEDTLS_GET_UINT32_BE( data, 0 ); + local.W[ 1] = MBEDTLS_GET_UINT32_BE( data, 4 ); + local.W[ 2] = MBEDTLS_GET_UINT32_BE( data, 8 ); + local.W[ 3] = MBEDTLS_GET_UINT32_BE( data, 12 ); + local.W[ 4] = MBEDTLS_GET_UINT32_BE( data, 16 ); + local.W[ 5] = MBEDTLS_GET_UINT32_BE( data, 20 ); + local.W[ 6] = MBEDTLS_GET_UINT32_BE( data, 24 ); + local.W[ 7] = MBEDTLS_GET_UINT32_BE( data, 28 ); + local.W[ 8] = MBEDTLS_GET_UINT32_BE( data, 32 ); + local.W[ 9] = MBEDTLS_GET_UINT32_BE( data, 36 ); + local.W[10] = MBEDTLS_GET_UINT32_BE( data, 40 ); + local.W[11] = MBEDTLS_GET_UINT32_BE( data, 44 ); + local.W[12] = MBEDTLS_GET_UINT32_BE( data, 48 ); + local.W[13] = MBEDTLS_GET_UINT32_BE( data, 52 ); + local.W[14] = MBEDTLS_GET_UINT32_BE( data, 56 ); + local.W[15] = MBEDTLS_GET_UINT32_BE( data, 60 ); #define S(x,n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n)))) diff --git a/library/sha256.c b/library/sha256.c index fb66340b2081..c3573f85fd4e 100644 --- a/library/sha256.c +++ b/library/sha256.c @@ -190,7 +190,7 @@ int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, for( i = 0; i < 64; i++ ) { if( i < 16 ) - MBEDTLS_GET_UINT32_BE( local.W[i], data, 4 * i ); + local.W[i] = MBEDTLS_GET_UINT32_BE( data, 4 * i ); else R( i ); @@ -205,7 +205,7 @@ int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, } #else /* MBEDTLS_SHA256_SMALLER */ for( i = 0; i < 16; i++ ) - MBEDTLS_GET_UINT32_BE( local.W[i], data, 4 * i ); + local.W[i] = MBEDTLS_GET_UINT32_BE( data, 4 * i ); for( i = 0; i < 16; i += 8 ) { From cd84d76e9b1a3b2c82d5975561aea493777abd51 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Thu, 8 Jul 2021 14:59:52 +0100 Subject: [PATCH 048/120] Add Character byte reading macros These cast to an unsigned char rather than a uint8_t like with MBEDTLS_BYTE_x These save alot of space and will improve maintence by replacing the appropriate code with MBEDTLS_CHAR_x Signed-off-by: Joe Subbiani --- library/aes.c | 196 +++++++++++++++++++-------------------- library/aria.c | 32 +++---- library/asn1write.c | 18 ++-- library/camellia.c | 16 ++-- library/ccm.c | 6 +- library/common.h | 9 ++ library/ecjpake.c | 18 ++-- library/ssl_cli.c | 131 ++++++++++++-------------- library/ssl_msg.c | 12 +-- library/ssl_srv.c | 94 +++++++++---------- library/ssl_tls.c | 122 ++++++++++++------------ library/ssl_tls13_keys.c | 6 +- 12 files changed, 329 insertions(+), 331 deletions(-) diff --git a/library/aes.c b/library/aes.c index 7a44a7840883..1eb3e204d8a5 100644 --- a/library/aes.c +++ b/library/aes.c @@ -386,7 +386,7 @@ static void aes_gen_tables( void ) { pow[i] = x; log[x] = i; - x = ( x ^ XTIME( x ) ) & 0xFF; + x = MBEDTLS_BYTE_0( x ^ XTIME( x ) ); } /* @@ -395,7 +395,7 @@ static void aes_gen_tables( void ) for( i = 0, x = 1; i < 10; i++ ) { RCON[i] = (uint32_t) x; - x = XTIME( x ) & 0xFF; + x = MBEDTLS_BYTE_0( XTIME( x ) ); } /* @@ -408,10 +408,10 @@ static void aes_gen_tables( void ) { x = pow[255 - log[i]]; - y = x; y = ( ( y << 1 ) | ( y >> 7 ) ) & 0xFF; - x ^= y; y = ( ( y << 1 ) | ( y >> 7 ) ) & 0xFF; - x ^= y; y = ( ( y << 1 ) | ( y >> 7 ) ) & 0xFF; - x ^= y; y = ( ( y << 1 ) | ( y >> 7 ) ) & 0xFF; + y = x; y = MBEDTLS_BYTE_0( ( y << 1 ) | ( y >> 7 ) ); + x ^= y; y = MBEDTLS_BYTE_0( ( y << 1 ) | ( y >> 7 ) ); + x ^= y; y = MBEDTLS_BYTE_0( ( y << 1 ) | ( y >> 7 ) ); + x ^= y; y = MBEDTLS_BYTE_0( ( y << 1 ) | ( y >> 7 ) ); x ^= y ^ 0x63; FSb[i] = (unsigned char) x; @@ -424,8 +424,8 @@ static void aes_gen_tables( void ) for( i = 0; i < 256; i++ ) { x = FSb[i]; - y = XTIME( x ) & 0xFF; - z = ( y ^ x ) & 0xFF; + y = MBEDTLS_BYTE_0( XTIME( x ) ); + z = MBEDTLS_BYTE_0( y ^ x ); FT0[i] = ( (uint32_t) y ) ^ ( (uint32_t) x << 8 ) ^ @@ -577,10 +577,10 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key, for( i = 0; i < 10; i++, RK += 4 ) { RK[4] = RK[0] ^ RCON[i] ^ - ( (uint32_t) FSb[ ( RK[3] >> 8 ) & 0xFF ] ) ^ - ( (uint32_t) FSb[ ( RK[3] >> 16 ) & 0xFF ] << 8 ) ^ - ( (uint32_t) FSb[ ( RK[3] >> 24 ) & 0xFF ] << 16 ) ^ - ( (uint32_t) FSb[ ( RK[3] ) & 0xFF ] << 24 ); + ( (uint32_t) FSb[ MBEDTLS_BYTE_1( RK[3] ) ] ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_2( RK[3] ) ] << 8 ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_3( RK[3] ) ] << 16 ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_0( RK[3] ) ] << 24 ); RK[5] = RK[1] ^ RK[4]; RK[6] = RK[2] ^ RK[5]; @@ -593,10 +593,10 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key, for( i = 0; i < 8; i++, RK += 6 ) { RK[6] = RK[0] ^ RCON[i] ^ - ( (uint32_t) FSb[ ( RK[5] >> 8 ) & 0xFF ] ) ^ - ( (uint32_t) FSb[ ( RK[5] >> 16 ) & 0xFF ] << 8 ) ^ - ( (uint32_t) FSb[ ( RK[5] >> 24 ) & 0xFF ] << 16 ) ^ - ( (uint32_t) FSb[ ( RK[5] ) & 0xFF ] << 24 ); + ( (uint32_t) FSb[ MBEDTLS_BYTE_1( RK[5] ) ] ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_2( RK[5] ) ] << 8 ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_3( RK[5] ) ] << 16 ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_0( RK[5] ) ] << 24 ); RK[7] = RK[1] ^ RK[6]; RK[8] = RK[2] ^ RK[7]; @@ -611,20 +611,20 @@ int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key, for( i = 0; i < 7; i++, RK += 8 ) { RK[8] = RK[0] ^ RCON[i] ^ - ( (uint32_t) FSb[ ( RK[7] >> 8 ) & 0xFF ] ) ^ - ( (uint32_t) FSb[ ( RK[7] >> 16 ) & 0xFF ] << 8 ) ^ - ( (uint32_t) FSb[ ( RK[7] >> 24 ) & 0xFF ] << 16 ) ^ - ( (uint32_t) FSb[ ( RK[7] ) & 0xFF ] << 24 ); + ( (uint32_t) FSb[ MBEDTLS_BYTE_1( RK[7] ) ] ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_2( RK[7] ) ] << 8 ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_3( RK[7] ) ] << 16 ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_0( RK[7] ) ] << 24 ); RK[9] = RK[1] ^ RK[8]; RK[10] = RK[2] ^ RK[9]; RK[11] = RK[3] ^ RK[10]; RK[12] = RK[4] ^ - ( (uint32_t) FSb[ ( RK[11] ) & 0xFF ] ) ^ - ( (uint32_t) FSb[ ( RK[11] >> 8 ) & 0xFF ] << 8 ) ^ - ( (uint32_t) FSb[ ( RK[11] >> 16 ) & 0xFF ] << 16 ) ^ - ( (uint32_t) FSb[ ( RK[11] >> 24 ) & 0xFF ] << 24 ); + ( (uint32_t) FSb[ MBEDTLS_BYTE_0( RK[11] ) ] ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_1( RK[11] ) ] << 8 ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_2( RK[11] ) ] << 16 ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_3( RK[11] ) ] << 24 ); RK[13] = RK[5] ^ RK[12]; RK[14] = RK[6] ^ RK[13]; @@ -690,10 +690,10 @@ int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key, { for( j = 0; j < 4; j++, SK++ ) { - *RK++ = AES_RT0( FSb[ ( *SK ) & 0xFF ] ) ^ - AES_RT1( FSb[ ( *SK >> 8 ) & 0xFF ] ) ^ - AES_RT2( FSb[ ( *SK >> 16 ) & 0xFF ] ) ^ - AES_RT3( FSb[ ( *SK >> 24 ) & 0xFF ] ); + *RK++ = AES_RT0( FSb[ MBEDTLS_BYTE_0( *SK ) ] ) ^ + AES_RT1( FSb[ MBEDTLS_BYTE_1( *SK ) ] ) ^ + AES_RT2( FSb[ MBEDTLS_BYTE_2( *SK ) ] ) ^ + AES_RT3( FSb[ MBEDTLS_BYTE_3( *SK ) ] ); } } @@ -786,52 +786,52 @@ int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx, } #endif /* MBEDTLS_CIPHER_MODE_XTS */ -#define AES_FROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \ - do \ - { \ - (X0) = *RK++ ^ AES_FT0( ( (Y0) ) & 0xFF ) ^ \ - AES_FT1( ( (Y1) >> 8 ) & 0xFF ) ^ \ - AES_FT2( ( (Y2) >> 16 ) & 0xFF ) ^ \ - AES_FT3( ( (Y3) >> 24 ) & 0xFF ); \ - \ - (X1) = *RK++ ^ AES_FT0( ( (Y1) ) & 0xFF ) ^ \ - AES_FT1( ( (Y2) >> 8 ) & 0xFF ) ^ \ - AES_FT2( ( (Y3) >> 16 ) & 0xFF ) ^ \ - AES_FT3( ( (Y0) >> 24 ) & 0xFF ); \ - \ - (X2) = *RK++ ^ AES_FT0( ( (Y2) ) & 0xFF ) ^ \ - AES_FT1( ( (Y3) >> 8 ) & 0xFF ) ^ \ - AES_FT2( ( (Y0) >> 16 ) & 0xFF ) ^ \ - AES_FT3( ( (Y1) >> 24 ) & 0xFF ); \ - \ - (X3) = *RK++ ^ AES_FT0( ( (Y3) ) & 0xFF ) ^ \ - AES_FT1( ( (Y0) >> 8 ) & 0xFF ) ^ \ - AES_FT2( ( (Y1) >> 16 ) & 0xFF ) ^ \ - AES_FT3( ( (Y2) >> 24 ) & 0xFF ); \ +#define AES_FROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \ + do \ + { \ + (X0) = *RK++ ^ AES_FT0( MBEDTLS_BYTE_0( Y0 ) ) ^ \ + AES_FT1( MBEDTLS_BYTE_1( Y1 ) ) ^ \ + AES_FT2( MBEDTLS_BYTE_2( Y2 ) ) ^ \ + AES_FT3( MBEDTLS_BYTE_3( Y3 ) ); \ + \ + (X1) = *RK++ ^ AES_FT0( MBEDTLS_BYTE_0( Y1 ) ) ^ \ + AES_FT1( MBEDTLS_BYTE_1( Y2 ) ) ^ \ + AES_FT2( MBEDTLS_BYTE_2( Y3 ) ) ^ \ + AES_FT3( MBEDTLS_BYTE_3( Y0 ) ); \ + \ + (X2) = *RK++ ^ AES_FT0( MBEDTLS_BYTE_0( Y2 ) ) ^ \ + AES_FT1( MBEDTLS_BYTE_1( Y3 ) ) ^ \ + AES_FT2( MBEDTLS_BYTE_2( Y0 ) ) ^ \ + AES_FT3( MBEDTLS_BYTE_3( Y1 ) ); \ + \ + (X3) = *RK++ ^ AES_FT0( MBEDTLS_BYTE_0( Y3 ) ) ^ \ + AES_FT1( MBEDTLS_BYTE_1( Y0 ) ) ^ \ + AES_FT2( MBEDTLS_BYTE_2( Y1 ) ) ^ \ + AES_FT3( MBEDTLS_BYTE_3( Y2 ) ); \ } while( 0 ) #define AES_RROUND(X0,X1,X2,X3,Y0,Y1,Y2,Y3) \ do \ { \ - (X0) = *RK++ ^ AES_RT0( ( (Y0) ) & 0xFF ) ^ \ - AES_RT1( ( (Y3) >> 8 ) & 0xFF ) ^ \ - AES_RT2( ( (Y2) >> 16 ) & 0xFF ) ^ \ - AES_RT3( ( (Y1) >> 24 ) & 0xFF ); \ + (X0) = *RK++ ^ AES_RT0( MBEDTLS_BYTE_0( Y0 ) ) ^ \ + AES_RT1( MBEDTLS_BYTE_1( Y3 ) ) ^ \ + AES_RT2( MBEDTLS_BYTE_2( Y2 ) ) ^ \ + AES_RT3( MBEDTLS_BYTE_3( Y1 ) ); \ \ - (X1) = *RK++ ^ AES_RT0( ( (Y1) ) & 0xFF ) ^ \ - AES_RT1( ( (Y0) >> 8 ) & 0xFF ) ^ \ - AES_RT2( ( (Y3) >> 16 ) & 0xFF ) ^ \ - AES_RT3( ( (Y2) >> 24 ) & 0xFF ); \ + (X1) = *RK++ ^ AES_RT0( MBEDTLS_BYTE_0( Y1 ) ) ^ \ + AES_RT1( MBEDTLS_BYTE_1( Y0 ) ) ^ \ + AES_RT2( MBEDTLS_BYTE_2( Y3 ) ) ^ \ + AES_RT3( MBEDTLS_BYTE_3( Y2 ) ); \ \ - (X2) = *RK++ ^ AES_RT0( ( (Y2) ) & 0xFF ) ^ \ - AES_RT1( ( (Y1) >> 8 ) & 0xFF ) ^ \ - AES_RT2( ( (Y0) >> 16 ) & 0xFF ) ^ \ - AES_RT3( ( (Y3) >> 24 ) & 0xFF ); \ + (X2) = *RK++ ^ AES_RT0( MBEDTLS_BYTE_0( Y2 ) ) ^ \ + AES_RT1( MBEDTLS_BYTE_1( Y1 ) ) ^ \ + AES_RT2( MBEDTLS_BYTE_2( Y0 ) ) ^ \ + AES_RT3( MBEDTLS_BYTE_3( Y3 ) ); \ \ - (X3) = *RK++ ^ AES_RT0( ( (Y3) ) & 0xFF ) ^ \ - AES_RT1( ( (Y2) >> 8 ) & 0xFF ) ^ \ - AES_RT2( ( (Y1) >> 16 ) & 0xFF ) ^ \ - AES_RT3( ( (Y0) >> 24 ) & 0xFF ); \ + (X3) = *RK++ ^ AES_RT0( MBEDTLS_BYTE_0( Y3 ) ) ^ \ + AES_RT1( MBEDTLS_BYTE_1( Y2 ) ) ^ \ + AES_RT2( MBEDTLS_BYTE_2( Y1 ) ) ^ \ + AES_RT3( MBEDTLS_BYTE_3( Y0 ) ); \ } while( 0 ) /* @@ -864,28 +864,28 @@ int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx, AES_FROUND( t.Y[0], t.Y[1], t.Y[2], t.Y[3], t.X[0], t.X[1], t.X[2], t.X[3] ); t.X[0] = *RK++ ^ \ - ( (uint32_t) FSb[ ( t.Y[0] ) & 0xFF ] ) ^ - ( (uint32_t) FSb[ ( t.Y[1] >> 8 ) & 0xFF ] << 8 ) ^ - ( (uint32_t) FSb[ ( t.Y[2] >> 16 ) & 0xFF ] << 16 ) ^ - ( (uint32_t) FSb[ ( t.Y[3] >> 24 ) & 0xFF ] << 24 ); + ( (uint32_t) FSb[ MBEDTLS_BYTE_0( t.Y[0] ) ] ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_1( t.Y[1] ) ] << 8 ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_2( t.Y[2] ) ] << 16 ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_3( t.Y[3] ) ] << 24 ); t.X[1] = *RK++ ^ \ - ( (uint32_t) FSb[ ( t.Y[1] ) & 0xFF ] ) ^ - ( (uint32_t) FSb[ ( t.Y[2] >> 8 ) & 0xFF ] << 8 ) ^ - ( (uint32_t) FSb[ ( t.Y[3] >> 16 ) & 0xFF ] << 16 ) ^ - ( (uint32_t) FSb[ ( t.Y[0] >> 24 ) & 0xFF ] << 24 ); + ( (uint32_t) FSb[ MBEDTLS_BYTE_0( t.Y[1] ) ] ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_1( t.Y[2] ) ] << 8 ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_2( t.Y[3] ) ] << 16 ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_3( t.Y[0] ) ] << 24 ); t.X[2] = *RK++ ^ \ - ( (uint32_t) FSb[ ( t.Y[2] ) & 0xFF ] ) ^ - ( (uint32_t) FSb[ ( t.Y[3] >> 8 ) & 0xFF ] << 8 ) ^ - ( (uint32_t) FSb[ ( t.Y[0] >> 16 ) & 0xFF ] << 16 ) ^ - ( (uint32_t) FSb[ ( t.Y[1] >> 24 ) & 0xFF ] << 24 ); + ( (uint32_t) FSb[ MBEDTLS_BYTE_0( t.Y[2] ) ] ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_1( t.Y[3] ) ] << 8 ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_2( t.Y[0] ) ] << 16 ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_3( t.Y[1] ) ] << 24 ); t.X[3] = *RK++ ^ \ - ( (uint32_t) FSb[ ( t.Y[3] ) & 0xFF ] ) ^ - ( (uint32_t) FSb[ ( t.Y[0] >> 8 ) & 0xFF ] << 8 ) ^ - ( (uint32_t) FSb[ ( t.Y[1] >> 16 ) & 0xFF ] << 16 ) ^ - ( (uint32_t) FSb[ ( t.Y[2] >> 24 ) & 0xFF ] << 24 ); + ( (uint32_t) FSb[ MBEDTLS_BYTE_0( t.Y[3] ) ] ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_1( t.Y[0] ) ] << 8 ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_2( t.Y[1] ) ] << 16 ) ^ + ( (uint32_t) FSb[ MBEDTLS_BYTE_3( t.Y[2] ) ] << 24 ); MBEDTLS_PUT_UINT32_LE( t.X[0], output, 0 ); MBEDTLS_PUT_UINT32_LE( t.X[1], output, 4 ); @@ -928,28 +928,28 @@ int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx, AES_RROUND( t.Y[0], t.Y[1], t.Y[2], t.Y[3], t.X[0], t.X[1], t.X[2], t.X[3] ); t.X[0] = *RK++ ^ \ - ( (uint32_t) RSb[ ( t.Y[0] ) & 0xFF ] ) ^ - ( (uint32_t) RSb[ ( t.Y[3] >> 8 ) & 0xFF ] << 8 ) ^ - ( (uint32_t) RSb[ ( t.Y[2] >> 16 ) & 0xFF ] << 16 ) ^ - ( (uint32_t) RSb[ ( t.Y[1] >> 24 ) & 0xFF ] << 24 ); + ( (uint32_t) RSb[ MBEDTLS_BYTE_0( t.Y[0] ) ] ) ^ + ( (uint32_t) RSb[ MBEDTLS_BYTE_1( t.Y[3] ) ] << 8 ) ^ + ( (uint32_t) RSb[ MBEDTLS_BYTE_2( t.Y[2] ) ] << 16 ) ^ + ( (uint32_t) RSb[ MBEDTLS_BYTE_3( t.Y[1] ) ] << 24 ); t.X[1] = *RK++ ^ \ - ( (uint32_t) RSb[ ( t.Y[1] ) & 0xFF ] ) ^ - ( (uint32_t) RSb[ ( t.Y[0] >> 8 ) & 0xFF ] << 8 ) ^ - ( (uint32_t) RSb[ ( t.Y[3] >> 16 ) & 0xFF ] << 16 ) ^ - ( (uint32_t) RSb[ ( t.Y[2] >> 24 ) & 0xFF ] << 24 ); + ( (uint32_t) RSb[ MBEDTLS_BYTE_0( t.Y[1] ) ] ) ^ + ( (uint32_t) RSb[ MBEDTLS_BYTE_1( t.Y[0] ) ] << 8 ) ^ + ( (uint32_t) RSb[ MBEDTLS_BYTE_2( t.Y[3] ) ] << 16 ) ^ + ( (uint32_t) RSb[ MBEDTLS_BYTE_3( t.Y[2] ) ] << 24 ); t.X[2] = *RK++ ^ \ - ( (uint32_t) RSb[ ( t.Y[2] ) & 0xFF ] ) ^ - ( (uint32_t) RSb[ ( t.Y[1] >> 8 ) & 0xFF ] << 8 ) ^ - ( (uint32_t) RSb[ ( t.Y[0] >> 16 ) & 0xFF ] << 16 ) ^ - ( (uint32_t) RSb[ ( t.Y[3] >> 24 ) & 0xFF ] << 24 ); + ( (uint32_t) RSb[ MBEDTLS_BYTE_0( t.Y[2] ) ] ) ^ + ( (uint32_t) RSb[ MBEDTLS_BYTE_1( t.Y[1] ) ] << 8 ) ^ + ( (uint32_t) RSb[ MBEDTLS_BYTE_2( t.Y[0] ) ] << 16 ) ^ + ( (uint32_t) RSb[ MBEDTLS_BYTE_3( t.Y[3] ) ] << 24 ); t.X[3] = *RK++ ^ \ - ( (uint32_t) RSb[ ( t.Y[3] ) & 0xFF ] ) ^ - ( (uint32_t) RSb[ ( t.Y[2] >> 8 ) & 0xFF ] << 8 ) ^ - ( (uint32_t) RSb[ ( t.Y[1] >> 16 ) & 0xFF ] << 16 ) ^ - ( (uint32_t) RSb[ ( t.Y[0] >> 24 ) & 0xFF ] << 24 ); + ( (uint32_t) RSb[ MBEDTLS_BYTE_0( t.Y[3] ) ] ) ^ + ( (uint32_t) RSb[ MBEDTLS_BYTE_1( t.Y[2] ) ] << 8 ) ^ + ( (uint32_t) RSb[ MBEDTLS_BYTE_2( t.Y[1] ) ] << 16 ) ^ + ( (uint32_t) RSb[ MBEDTLS_BYTE_3( t.Y[0] ) ] << 24 ); MBEDTLS_PUT_UINT32_LE( t.X[0], output, 0 ); MBEDTLS_PUT_UINT32_LE( t.X[1], output, 4 ); diff --git a/library/aria.c b/library/aria.c index 320f7758aca0..6bfdfbdce2b8 100644 --- a/library/aria.c +++ b/library/aria.c @@ -212,22 +212,22 @@ static inline void aria_sl( uint32_t *a, uint32_t *b, const uint8_t sa[256], const uint8_t sb[256], const uint8_t sc[256], const uint8_t sd[256] ) { - *a = ( (uint32_t) sa[ *a & 0xFF] ) ^ - (((uint32_t) sb[(*a >> 8) & 0xFF]) << 8) ^ - (((uint32_t) sc[(*a >> 16) & 0xFF]) << 16) ^ - (((uint32_t) sd[ *a >> 24 ]) << 24); - *b = ( (uint32_t) sa[ *b & 0xFF] ) ^ - (((uint32_t) sb[(*b >> 8) & 0xFF]) << 8) ^ - (((uint32_t) sc[(*b >> 16) & 0xFF]) << 16) ^ - (((uint32_t) sd[ *b >> 24 ]) << 24); - *c = ( (uint32_t) sa[ *c & 0xFF] ) ^ - (((uint32_t) sb[(*c >> 8) & 0xFF]) << 8) ^ - (((uint32_t) sc[(*c >> 16) & 0xFF]) << 16) ^ - (((uint32_t) sd[ *c >> 24 ]) << 24); - *d = ( (uint32_t) sa[ *d & 0xFF] ) ^ - (((uint32_t) sb[(*d >> 8) & 0xFF]) << 8) ^ - (((uint32_t) sc[(*d >> 16) & 0xFF]) << 16) ^ - (((uint32_t) sd[ *d >> 24 ]) << 24); + *a = ( (uint32_t) sa[ MBEDTLS_BYTE_0( *a ) ] ) ^ + (((uint32_t) sb[ MBEDTLS_BYTE_1( *a ) ]) << 8) ^ + (((uint32_t) sc[ MBEDTLS_BYTE_2( *a ) ]) << 16) ^ + (((uint32_t) sd[ *a >> 24 ]) << 24); + *b = ( (uint32_t) sa[ MBEDTLS_BYTE_0( *b ) ] ) ^ + (((uint32_t) sb[ MBEDTLS_BYTE_1( *b ) ]) << 8) ^ + (((uint32_t) sc[ MBEDTLS_BYTE_2( *b ) ]) << 16) ^ + (((uint32_t) sd[ *b >> 24 ]) << 24); + *c = ( (uint32_t) sa[ MBEDTLS_BYTE_0( *c ) ] ) ^ + (((uint32_t) sb[ MBEDTLS_BYTE_1( *c ) ]) << 8) ^ + (((uint32_t) sc[ MBEDTLS_BYTE_2( *c ) ]) << 16) ^ + (((uint32_t) sd[ *c >> 24 ]) << 24); + *d = ( (uint32_t) sa[ MBEDTLS_BYTE_0( *d ) ] ) ^ + (((uint32_t) sb[ MBEDTLS_BYTE_1( *d ) ]) << 8) ^ + (((uint32_t) sc[ MBEDTLS_BYTE_2( *d ) ]) << 16) ^ + (((uint32_t) sd[ *d >> 24 ]) << 24); } /* diff --git a/library/asn1write.c b/library/asn1write.c index 0289e89491a3..8555767fe26b 100644 --- a/library/asn1write.c +++ b/library/asn1write.c @@ -60,8 +60,8 @@ int mbedtls_asn1_write_len( unsigned char **p, const unsigned char *start, size_ if( *p - start < 3 ) return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); - *--(*p) = ( len ) & 0xFF; - *--(*p) = ( len >> 8 ) & 0xFF; + *--(*p) = MBEDTLS_CHAR_0( len ); + *--(*p) = MBEDTLS_CHAR_1( len ); *--(*p) = 0x82; return( 3 ); } @@ -71,9 +71,9 @@ int mbedtls_asn1_write_len( unsigned char **p, const unsigned char *start, size_ if( *p - start < 4 ) return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); - *--(*p) = ( len ) & 0xFF; - *--(*p) = ( len >> 8 ) & 0xFF; - *--(*p) = ( len >> 16 ) & 0xFF; + *--(*p) = MBEDTLS_CHAR_0( len ); + *--(*p) = MBEDTLS_CHAR_1( len ); + *--(*p) = MBEDTLS_CHAR_2( len ); *--(*p) = 0x83; return( 4 ); } @@ -85,10 +85,10 @@ int mbedtls_asn1_write_len( unsigned char **p, const unsigned char *start, size_ if( *p - start < 5 ) return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); - *--(*p) = ( len ) & 0xFF; - *--(*p) = ( len >> 8 ) & 0xFF; - *--(*p) = ( len >> 16 ) & 0xFF; - *--(*p) = ( len >> 24 ) & 0xFF; + *--(*p) = MBEDTLS_CHAR_0( len ); + *--(*p) = MBEDTLS_CHAR_1( len ); + *--(*p) = MBEDTLS_CHAR_2( len ); + *--(*p) = MBEDTLS_CHAR_3( len ); *--(*p) = 0x84; return( 5 ); } diff --git a/library/camellia.c b/library/camellia.c index 4d6b468e5dec..29d730ab5369 100644 --- a/library/camellia.c +++ b/library/camellia.c @@ -278,14 +278,14 @@ static void camellia_feistel( const uint32_t x[2], const uint32_t k[2], I0 = x[0] ^ k[0]; I1 = x[1] ^ k[1]; - I0 = ((uint32_t) SBOX1((I0 >> 24) & 0xFF) << 24) | - ((uint32_t) SBOX2((I0 >> 16) & 0xFF) << 16) | - ((uint32_t) SBOX3((I0 >> 8) & 0xFF) << 8) | - ((uint32_t) SBOX4((I0 ) & 0xFF) ); - I1 = ((uint32_t) SBOX2((I1 >> 24) & 0xFF) << 24) | - ((uint32_t) SBOX3((I1 >> 16) & 0xFF) << 16) | - ((uint32_t) SBOX4((I1 >> 8) & 0xFF) << 8) | - ((uint32_t) SBOX1((I1 ) & 0xFF) ); + I0 = ((uint32_t) SBOX1( MBEDTLS_BYTE_3( I0 )) << 24) | + ((uint32_t) SBOX2( MBEDTLS_BYTE_2( I0 )) << 16) | + ((uint32_t) SBOX3( MBEDTLS_BYTE_1( I0 )) << 8) | + ((uint32_t) SBOX4( MBEDTLS_BYTE_0( I0 )) ); + I1 = ((uint32_t) SBOX2( MBEDTLS_BYTE_3( I1 )) << 24) | + ((uint32_t) SBOX3( MBEDTLS_BYTE_2( I1 )) << 16) | + ((uint32_t) SBOX4( MBEDTLS_BYTE_1( I1 )) << 8) | + ((uint32_t) SBOX1( MBEDTLS_BYTE_0( I1 )) ); I0 ^= (I1 << 8) | (I1 >> 24); I1 ^= (I0 << 16) | (I0 >> 16); diff --git a/library/ccm.c b/library/ccm.c index 424ee77b6980..95d90dc61a0c 100644 --- a/library/ccm.c +++ b/library/ccm.c @@ -200,7 +200,7 @@ static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length, memcpy( b + 1, iv, iv_len ); for( i = 0, len_left = length; i < q; i++, len_left >>= 8 ) - b[15-i] = (unsigned char)( len_left & 0xFF ); + b[15-i] = MBEDTLS_CHAR_0( len_left ); if( len_left > 0 ) return( MBEDTLS_ERR_CCM_BAD_INPUT ); @@ -221,8 +221,8 @@ static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length, src = add; memset( b, 0, 16 ); - b[0] = (unsigned char)( ( add_len >> 8 ) & 0xFF ); - b[1] = (unsigned char)( ( add_len ) & 0xFF ); + b[0] = MBEDTLS_CHAR_1( add_len ); + b[1] = MBEDTLS_CHAR_0( add_len ); use_len = len_left < 16 - 2 ? len_left : 16 - 2; memcpy( b + 2, src, use_len ); diff --git a/library/common.h b/library/common.h index 4ecc0162bf12..56c300265358 100644 --- a/library/common.h +++ b/library/common.h @@ -77,6 +77,15 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c #define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) ) #define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) ) +#define MBEDTLS_CHAR_0( x ) ( (unsigned char) ( ( x ) & 0xff ) ) +#define MBEDTLS_CHAR_1( x ) ( (unsigned char) ( ( ( x ) >> 8 ) & 0xff ) ) +#define MBEDTLS_CHAR_2( x ) ( (unsigned char) ( ( ( x ) >> 16 ) & 0xff ) ) +#define MBEDTLS_CHAR_3( x ) ( (unsigned char) ( ( ( x ) >> 24 ) & 0xff ) ) +#define MBEDTLS_CHAR_4( x ) ( (unsigned char) ( ( ( x ) >> 32 ) & 0xff ) ) +#define MBEDTLS_CHAR_5( x ) ( (unsigned char) ( ( ( x ) >> 40 ) & 0xff ) ) +#define MBEDTLS_CHAR_6( x ) ( (unsigned char) ( ( ( x ) >> 48 ) & 0xff ) ) +#define MBEDTLS_CHAR_7( x ) ( (unsigned char) ( ( ( x ) >> 56 ) & 0xff ) ) + /** * 32-bit integer manipulation GET macros (big endian) * diff --git a/library/ecjpake.c b/library/ecjpake.c index d22931142044..7305dfeee10f 100644 --- a/library/ecjpake.c +++ b/library/ecjpake.c @@ -180,10 +180,10 @@ static int ecjpake_write_len_point( unsigned char **p, if( ret != 0 ) return( ret ); - (*p)[0] = (unsigned char)( ( len >> 24 ) & 0xFF ); - (*p)[1] = (unsigned char)( ( len >> 16 ) & 0xFF ); - (*p)[2] = (unsigned char)( ( len >> 8 ) & 0xFF ); - (*p)[3] = (unsigned char)( ( len ) & 0xFF ); + (*p)[0] = MBEDTLS_CHAR_3( len ); + (*p)[1] = MBEDTLS_CHAR_2( len ); + (*p)[2] = MBEDTLS_CHAR_1( len ); + (*p)[3] = MBEDTLS_CHAR_0( len ); *p += 4 + len; @@ -223,10 +223,10 @@ static int ecjpake_hash( const mbedtls_md_info_t *md_info, if( end - p < 4 ) return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); - *p++ = (unsigned char)( ( id_len >> 24 ) & 0xFF ); - *p++ = (unsigned char)( ( id_len >> 16 ) & 0xFF ); - *p++ = (unsigned char)( ( id_len >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( id_len ) & 0xFF ); + *p++ = MBEDTLS_CHAR_3( id_len ); + *p++ = MBEDTLS_CHAR_2( id_len ); + *p++ = MBEDTLS_CHAR_1( id_len ); + *p++ = MBEDTLS_CHAR_0( id_len ); if( end < p || (size_t)( end - p ) < id_len ) return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); @@ -366,7 +366,7 @@ static int ecjpake_zkp_write( const mbedtls_md_info_t *md_info, goto cleanup; } - *(*p)++ = (unsigned char)( len & 0xFF ); + *(*p)++ = MBEDTLS_CHAR_0( len ); MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &h, *p, len ) ); /* r */ *p += len; diff --git a/library/ssl_cli.c b/library/ssl_cli.c index 59c546042907..7d65479efba0 100644 --- a/library/ssl_cli.c +++ b/library/ssl_cli.c @@ -136,18 +136,18 @@ static int ssl_write_hostname_ext( mbedtls_ssl_context *ssl, * } ServerNameList; * */ - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_SERVERNAME ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_SERVERNAME ); - *p++ = (unsigned char)( ( (hostname_len + 5) >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( (hostname_len + 5) ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( hostname_len + 5); + *p++ = MBEDTLS_CHAR_0( hostname_len + 5); - *p++ = (unsigned char)( ( (hostname_len + 3) >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( (hostname_len + 3) ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( hostname_len + 3 ); + *p++ = MBEDTLS_CHAR_0( hostname_len + 3 ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ) & 0xFF ); - *p++ = (unsigned char)( ( hostname_len >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( hostname_len ) & 0xFF ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ); + *p++ = MBEDTLS_CHAR_1( hostname_len ); + *p++ = MBEDTLS_CHAR_0( hostname_len ); memcpy( p, ssl->hostname, hostname_len ); @@ -181,14 +181,12 @@ static int ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl, /* * Secure renegotiation */ - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) - & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) - & 0xFF ); + *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ); *p++ = 0x00; - *p++ = ( ssl->verify_data_len + 1 ) & 0xFF; - *p++ = ssl->verify_data_len & 0xFF; + *p++ = MBEDTLS_CHAR_0( ssl->verify_data_len + 1 ); + *p++ = MBEDTLS_CHAR_0( ssl->verify_data_len ); memcpy( p, ssl->own_verify_data, ssl->verify_data_len ); @@ -283,14 +281,14 @@ static int ssl_write_signature_algorithms_ext( mbedtls_ssl_context *ssl, * SignatureAndHashAlgorithm * supported_signature_algorithms<2..2^16-2>; */ - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SIG_ALG >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SIG_ALG ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_SIG_ALG ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_SIG_ALG ); - *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( ( sig_alg_len + 2 ) ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( sig_alg_len + 2 ); + *p++ = MBEDTLS_CHAR_0( sig_alg_len + 2 ); - *p++ = (unsigned char)( ( sig_alg_len >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( sig_alg_len ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( sig_alg_len ); + *p++ = MBEDTLS_CHAR_0( sig_alg_len ); *olen = 6 + sig_alg_len; @@ -358,16 +356,14 @@ static int ssl_write_supported_elliptic_curves_ext( mbedtls_ssl_context *ssl, elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF; } - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES >> 8 ) - & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ) - & 0xFF ); + *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ); - *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( ( elliptic_curve_len + 2 ) ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( elliptic_curve_len + 2 ); + *p++ = MBEDTLS_CHAR_0( elliptic_curve_len + 2 ); - *p++ = (unsigned char)( ( ( elliptic_curve_len ) >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( ( elliptic_curve_len ) ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( elliptic_curve_len ); + *p++ = MBEDTLS_CHAR_0( elliptic_curve_len ); *olen = 6 + elliptic_curve_len; @@ -388,10 +384,8 @@ static int ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl, ( "client hello, adding supported_point_formats extension" ) ); MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) - & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) - & 0xFF ); + *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ); *p++ = 0x00; *p++ = 2; @@ -427,8 +421,8 @@ static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ); /* * We may need to send ClientHello multiple times for Hello verification. @@ -470,8 +464,8 @@ static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len ); } - *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( kkpp_len ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( kkpp_len ); + *p++ = MBEDTLS_CHAR_0( kkpp_len ); *olen = kkpp_len + 4; @@ -510,11 +504,11 @@ static int ssl_write_cid_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) ); /* Add extension ID + size */ - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_CID ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_CID ); ext_len = (size_t) ssl->own_cid_len + 1; - *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( ext_len ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( ext_len ); + *p++ = MBEDTLS_CHAR_0( ext_len ); *p++ = (uint8_t) ssl->own_cid_len; memcpy( p, ssl->own_cid, ssl->own_cid_len ); @@ -543,10 +537,8 @@ static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) - & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) - & 0xFF ); + *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ); *p++ = 0x00; *p++ = 1; @@ -577,8 +569,8 @@ static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ); *p++ = 0x00; *p++ = 0x00; @@ -607,10 +599,8 @@ static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) - & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) - & 0xFF ); + *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ); *p++ = 0x00; *p++ = 0x00; @@ -641,11 +631,11 @@ static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl, /* The addition is safe here since the ticket length is 16 bit. */ MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_SESSION_TICKET ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_SESSION_TICKET ); - *p++ = (unsigned char)( ( tlen >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( tlen ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( tlen ); + *p++ = MBEDTLS_CHAR_0( tlen ); *olen = 4; @@ -685,8 +675,8 @@ static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 + alpnlen ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_ALPN ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_ALPN ); /* * opaque ProtocolName<1..2^8-1>; @@ -713,12 +703,12 @@ static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl, *olen = p - buf; /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */ - buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF ); - buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF ); + buf[4] = MBEDTLS_CHAR_1( *olen - 6 ); + buf[5] = MBEDTLS_CHAR_0( *olen - 6 ); /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */ - buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF ); - buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF ); + buf[2] = MBEDTLS_CHAR_1( *olen - 4 ); + buf[3] = MBEDTLS_CHAR_0( *olen - 4 ); return( 0 ); } @@ -770,12 +760,12 @@ static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, */ MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_USE_SRTP ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_USE_SRTP ); - *p++ = (unsigned char)( ( ( ext_len & 0xFF00 ) >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ext_len & 0xFF ); + *p++ = MBEDTLS_CHAR_1( ext_len & 0xFF00 ); + *p++ = MBEDTLS_CHAR_0( ext_len ); /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */ /* micro-optimization: @@ -786,8 +776,7 @@ static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, * >> 8 ) & 0xFF ); */ *p++ = 0; - *p++ = (unsigned char)( ( 2 * ssl->conf->dtls_srtp_profile_list_len ) - & 0xFF ); + *p++ = MBEDTLS_CHAR_0( 2 * ssl->conf->dtls_srtp_profile_list_len ); for( protection_profiles_index=0; protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len; @@ -799,8 +788,8 @@ static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, { MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x", profile_value ) ); - *p++ = ( ( profile_value >> 8 ) & 0xFF ); - *p++ = ( profile_value & 0xFF ); + *p++ = MBEDTLS_BYTE_1( profile_value ); + *p++ = MBEDTLS_BYTE_0( profile_value ); } else { @@ -1334,8 +1323,8 @@ static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) { /* No need to check for space here, because the extension * writing functions already took care of that. */ - *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( ext_len ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( ext_len ); + *p++ = MBEDTLS_CHAR_0( ext_len ); p += ext_len; } diff --git a/library/ssl_msg.c b/library/ssl_msg.c index 77904e0a1baa..e0c50bcce1d4 100644 --- a/library/ssl_msg.c +++ b/library/ssl_msg.c @@ -454,15 +454,15 @@ static void ssl_extract_add_data_from_record( unsigned char* add_data, *cur = rec->cid_len; cur++; - cur[0] = ( ad_len_field >> 8 ) & 0xFF; - cur[1] = ( ad_len_field >> 0 ) & 0xFF; + cur[0] = MBEDTLS_CHAR_1( ad_len_field ); + cur[1] = MBEDTLS_CHAR_0( ad_len_field ); cur += 2; } else #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ { - cur[0] = ( ad_len_field >> 8 ) & 0xFF; - cur[1] = ( ad_len_field >> 0 ) & 0xFF; + cur[0] = MBEDTLS_CHAR_1( ad_len_field ); + cur[1] = MBEDTLS_CHAR_0( ad_len_field ); cur += 2; } @@ -2481,8 +2481,8 @@ int mbedtls_ssl_write_handshake_msg_ext( mbedtls_ssl_context *ssl, /* Write message_seq and update it, except for HelloRequest */ if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) { - ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF; - ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF; + ssl->out_msg[4] = MBEDTLS_CHAR_1( ssl->handshake->out_msg_seq ); + ssl->out_msg[5] = MBEDTLS_CHAR_0( ssl->handshake->out_msg_seq ); ++( ssl->handshake->out_msg_seq ); } else diff --git a/library/ssl_srv.c b/library/ssl_srv.c index 3d6739342db7..253ab5695902 100644 --- a/library/ssl_srv.c +++ b/library/ssl_srv.c @@ -1848,8 +1848,8 @@ static int ssl_parse_client_hello( mbedtls_ssl_context *ssl ) for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 ) for( i = 0; ciphersuites[i] != 0; i++ ) { - if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) || - p[1] != ( ( ciphersuites[i] ) & 0xFF ) ) + if( p[0] != MBEDTLS_BYTE_1( ciphersuites[i] ) || + p[1] != MBEDTLS_BYTE_0( ciphersuites[i] )) continue; got_common_suite = 1; @@ -1865,8 +1865,8 @@ static int ssl_parse_client_hello( mbedtls_ssl_context *ssl ) for( i = 0; ciphersuites[i] != 0; i++ ) for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 ) { - if( p[0] != ( ( ciphersuites[i] >> 8 ) & 0xFF ) || - p[1] != ( ( ciphersuites[i] ) & 0xFF ) ) + if( p[0] != MBEDTLS_BYTE_1( ciphersuites[i] ) || + p[1] != MBEDTLS_BYTE_0( ciphersuites[i] )) continue; got_common_suite = 1; @@ -1971,11 +1971,11 @@ static void ssl_write_cid_ext( mbedtls_ssl_context *ssl, * } ConnectionId; */ - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_CID ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_CID ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_CID ); ext_len = (size_t) ssl->own_cid_len + 1; - *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( ext_len ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( ext_len ); + *p++ = MBEDTLS_CHAR_0( ext_len ); *p++ = (uint8_t) ssl->own_cid_len; memcpy( p, ssl->own_cid, ssl->own_cid_len ); @@ -2016,8 +2016,8 @@ static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ); *p++ = 0x00; *p++ = 0x00; @@ -2042,8 +2042,8 @@ static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret " "extension" ) ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ); *p++ = 0x00; *p++ = 0x00; @@ -2067,8 +2067,8 @@ static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SESSION_TICKET ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_SESSION_TICKET ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_SESSION_TICKET ); *p++ = 0x00; *p++ = 0x00; @@ -2091,8 +2091,8 @@ static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ); #if defined(MBEDTLS_SSL_RENEGOTIATION) if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) @@ -2132,8 +2132,8 @@ static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ); *p++ = 0x00; *p++ = 1; @@ -2162,8 +2162,8 @@ static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ); *p++ = 0x00; *p++ = 2; @@ -2200,8 +2200,8 @@ static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, return; } - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ); + *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ); ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx, p + 2, end - p - 2, &kkpp_len, @@ -2212,8 +2212,8 @@ static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, return; } - *p++ = (unsigned char)( ( kkpp_len >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( kkpp_len ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( kkpp_len ); + *p++ = MBEDTLS_CHAR_0( kkpp_len ); *olen = kkpp_len + 4; } @@ -2238,18 +2238,18 @@ static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl, * 6 . 6 protocol name length * 7 . 7+n protocol name */ - buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN >> 8 ) & 0xFF ); - buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_ALPN ) & 0xFF ); + buf[0] = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_ALPN ); + buf[1] = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_ALPN ); *olen = 7 + strlen( ssl->alpn_chosen ); - buf[2] = (unsigned char)( ( ( *olen - 4 ) >> 8 ) & 0xFF ); - buf[3] = (unsigned char)( ( ( *olen - 4 ) ) & 0xFF ); + buf[2] = MBEDTLS_CHAR_1( *olen - 4 ); + buf[3] = MBEDTLS_CHAR_0( *olen - 4 ); - buf[4] = (unsigned char)( ( ( *olen - 6 ) >> 8 ) & 0xFF ); - buf[5] = (unsigned char)( ( ( *olen - 6 ) ) & 0xFF ); + buf[4] = MBEDTLS_CHAR_1( *olen - 6 ); + buf[5] = MBEDTLS_CHAR_0( *olen - 6 ); - buf[6] = (unsigned char)( ( ( *olen - 7 ) ) & 0xFF ); + buf[6] = MBEDTLS_CHAR_0( *olen - 7 ); memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 ); } @@ -2294,15 +2294,15 @@ static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, } /* extension */ - buf[0] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP >> 8 ) & 0xFF ); - buf[1] = (unsigned char)( ( MBEDTLS_TLS_EXT_USE_SRTP ) & 0xFF ); + buf[0] = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_USE_SRTP ); + buf[1] = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_USE_SRTP ); /* * total length 5 and mki value: only one profile(2 bytes) * and length(2 bytes) and srtp_mki ) */ ext_len = 5 + mki_len; - buf[2] = (unsigned char)( ( ext_len >> 8 ) & 0xFF ); - buf[3] = (unsigned char)( ext_len & 0xFF ); + buf[2] = MBEDTLS_CHAR_1( ext_len ); + buf[3] = MBEDTLS_CHAR_0( ext_len ); /* protection profile length: 2 */ buf[4] = 0x00; @@ -2311,8 +2311,8 @@ static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, ssl->dtls_srtp_info.chosen_dtls_srtp_profile ); if( profile_value != MBEDTLS_TLS_SRTP_UNSET ) { - buf[6] = (unsigned char)( ( profile_value >> 8 ) & 0xFF ); - buf[7] = (unsigned char)( profile_value & 0xFF ); + buf[6] = MBEDTLS_CHAR_1( profile_value ); + buf[7] = MBEDTLS_CHAR_0( profile_value ); } else { @@ -2648,8 +2648,8 @@ static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) if( ext_len > 0 ) { - *p++ = (unsigned char)( ( ext_len >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( ext_len ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( ext_len ); + *p++ = MBEDTLS_CHAR_0( ext_len ); p += ext_len; } @@ -3478,8 +3478,8 @@ static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); return( MBEDTLS_ERR_SSL_DECODE_ERROR ); } - if( *p++ != ( ( len >> 8 ) & 0xFF ) || - *p++ != ( ( len ) & 0xFF ) ) + if( *p++ != MBEDTLS_CHAR_1( len ) || + *p++ != MBEDTLS_CHAR_0( len ) ) { MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); return( MBEDTLS_ERR_SSL_DECODE_ERROR ); @@ -4223,13 +4223,13 @@ static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl ) tlen = 0; } - ssl->out_msg[4] = ( lifetime >> 24 ) & 0xFF; - ssl->out_msg[5] = ( lifetime >> 16 ) & 0xFF; - ssl->out_msg[6] = ( lifetime >> 8 ) & 0xFF; - ssl->out_msg[7] = ( lifetime ) & 0xFF; + ssl->out_msg[4] = MBEDTLS_CHAR_3( lifetime ); + ssl->out_msg[5] = MBEDTLS_CHAR_2( lifetime ); + ssl->out_msg[6] = MBEDTLS_CHAR_1( lifetime ); + ssl->out_msg[7] = MBEDTLS_CHAR_0( lifetime ); - ssl->out_msg[8] = (unsigned char)( ( tlen >> 8 ) & 0xFF ); - ssl->out_msg[9] = (unsigned char)( ( tlen ) & 0xFF ); + ssl->out_msg[8] = MBEDTLS_CHAR_1( tlen ); + ssl->out_msg[9] = MBEDTLS_CHAR_0( tlen ); ssl->out_msglen = 10 + tlen; diff --git a/library/ssl_tls.c b/library/ssl_tls.c index f8cad4aeca8a..0655dc57ebea 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -4582,8 +4582,8 @@ static unsigned char ssl_serialized_session_header[] = { MBEDTLS_VERSION_MAJOR, MBEDTLS_VERSION_MINOR, MBEDTLS_VERSION_PATCH, - ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF, - ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF, + MBEDTLS_CHAR_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), + MBEDTLS_CHAR_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), }; /* @@ -4664,14 +4664,14 @@ static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, { start = (uint64_t) session->start; - *p++ = (unsigned char)( ( start >> 56 ) & 0xFF ); - *p++ = (unsigned char)( ( start >> 48 ) & 0xFF ); - *p++ = (unsigned char)( ( start >> 40 ) & 0xFF ); - *p++ = (unsigned char)( ( start >> 32 ) & 0xFF ); - *p++ = (unsigned char)( ( start >> 24 ) & 0xFF ); - *p++ = (unsigned char)( ( start >> 16 ) & 0xFF ); - *p++ = (unsigned char)( ( start >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( start ) & 0xFF ); + *p++ = MBEDTLS_CHAR_7( start ); + *p++ = MBEDTLS_CHAR_6( start ); + *p++ = MBEDTLS_CHAR_5( start ); + *p++ = MBEDTLS_CHAR_4( start ); + *p++ = MBEDTLS_CHAR_3( start ); + *p++ = MBEDTLS_CHAR_2( start ); + *p++ = MBEDTLS_CHAR_1( start ); + *p++ = MBEDTLS_CHAR_0( start ); } #endif /* MBEDTLS_HAVE_TIME */ @@ -4687,22 +4687,22 @@ static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, if( used <= buf_len ) { - *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( session->ciphersuite ); + *p++ = MBEDTLS_CHAR_0( session->ciphersuite ); - *p++ = (unsigned char)( session->compression & 0xFF ); + *p++ = MBEDTLS_CHAR_0( session->compression ); - *p++ = (unsigned char)( session->id_len & 0xFF ); + *p++ = MBEDTLS_CHAR_0( session->id_len ); memcpy( p, session->id, 32 ); p += 32; memcpy( p, session->master, 48 ); p += 48; - *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF ); - *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF ); - *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( session->verify_result ) & 0xFF ); + *p++ = MBEDTLS_CHAR_3( session->verify_result ); + *p++ = MBEDTLS_CHAR_2( session->verify_result ); + *p++ = MBEDTLS_CHAR_1( session->verify_result ); + *p++ = MBEDTLS_CHAR_0( session->verify_result ); } /* @@ -4719,9 +4719,9 @@ static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, if( used <= buf_len ) { - *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF ); - *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( cert_len ) & 0xFF ); + *p++ = MBEDTLS_CHAR_2( cert_len ); + *p++ = MBEDTLS_CHAR_1( cert_len ); + *p++ = MBEDTLS_CHAR_0( cert_len ); if( session->peer_cert != NULL ) { @@ -4762,9 +4762,9 @@ static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, if( used <= buf_len ) { - *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF ); - *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF ); + *p++ = MBEDTLS_CHAR_2( session->ticket_len ); + *p++ = MBEDTLS_CHAR_1( session->ticket_len ); + *p++ = MBEDTLS_CHAR_0( session->ticket_len ); if( session->ticket != NULL ) { @@ -4772,10 +4772,10 @@ static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, p += session->ticket_len; } - *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF ); - *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF ); - *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF ); + *p++ = MBEDTLS_CHAR_3( session->ticket_lifetime ); + *p++ = MBEDTLS_CHAR_2( session->ticket_lifetime ); + *p++ = MBEDTLS_CHAR_1( session->ticket_lifetime ); + *p++ = MBEDTLS_CHAR_0( session->ticket_lifetime ); } #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ @@ -4793,7 +4793,7 @@ static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, used += 1; if( used <= buf_len ) - *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF ); + *p++ = MBEDTLS_CHAR_0( session->encrypt_then_mac ); #endif return( used ); @@ -5568,11 +5568,11 @@ static unsigned char ssl_serialized_context_header[] = { MBEDTLS_VERSION_MAJOR, MBEDTLS_VERSION_MINOR, MBEDTLS_VERSION_PATCH, - ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF, - ( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF, - ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF, - ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF, - ( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF, + MBEDTLS_CHAR_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), + MBEDTLS_CHAR_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), + MBEDTLS_CHAR_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), + MBEDTLS_CHAR_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), + MBEDTLS_CHAR_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), }; /* @@ -5713,10 +5713,10 @@ int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, used += 4 + session_len; if( used <= buf_len ) { - *p++ = (unsigned char)( ( session_len >> 24 ) & 0xFF ); - *p++ = (unsigned char)( ( session_len >> 16 ) & 0xFF ); - *p++ = (unsigned char)( ( session_len >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( session_len ) & 0xFF ); + *p++ = MBEDTLS_CHAR_3( session_len ); + *p++ = MBEDTLS_CHAR_2( session_len ); + *p++ = MBEDTLS_CHAR_1( session_len ); + *p++ = MBEDTLS_CHAR_0( session_len ); ret = ssl_session_save( ssl->session, 1, p, session_len, &session_len ); @@ -5757,33 +5757,33 @@ int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, used += 4; if( used <= buf_len ) { - *p++ = (unsigned char)( ( ssl->badmac_seen >> 24 ) & 0xFF ); - *p++ = (unsigned char)( ( ssl->badmac_seen >> 16 ) & 0xFF ); - *p++ = (unsigned char)( ( ssl->badmac_seen >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( ssl->badmac_seen ) & 0xFF ); + *p++ = MBEDTLS_CHAR_3( ssl->badmac_seen ); + *p++ = MBEDTLS_CHAR_2( ssl->badmac_seen ); + *p++ = MBEDTLS_CHAR_1( ssl->badmac_seen ); + *p++ = MBEDTLS_CHAR_0( ssl->badmac_seen ); } #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) used += 16; if( used <= buf_len ) { - *p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF ); - *p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF ); - *p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF ); - *p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF ); - *p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF ); - *p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF ); - *p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF ); - - *p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF ); - *p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF ); - *p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF ); - *p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF ); - *p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF ); - *p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF ); - *p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( ssl->in_window ) & 0xFF ); + *p++ = MBEDTLS_CHAR_7( ssl->in_window_top ); + *p++ = MBEDTLS_CHAR_6( ssl->in_window_top ); + *p++ = MBEDTLS_CHAR_5( ssl->in_window_top ); + *p++ = MBEDTLS_CHAR_4( ssl->in_window_top ); + *p++ = MBEDTLS_CHAR_3( ssl->in_window_top ); + *p++ = MBEDTLS_CHAR_2( ssl->in_window_top ); + *p++ = MBEDTLS_CHAR_1( ssl->in_window_top ); + *p++ = MBEDTLS_CHAR_0( ssl->in_window_top ); + + *p++ = MBEDTLS_CHAR_7( ssl->in_window ); + *p++ = MBEDTLS_CHAR_6( ssl->in_window ); + *p++ = MBEDTLS_CHAR_5( ssl->in_window ); + *p++ = MBEDTLS_CHAR_4( ssl->in_window ); + *p++ = MBEDTLS_CHAR_3( ssl->in_window ); + *p++ = MBEDTLS_CHAR_2( ssl->in_window ); + *p++ = MBEDTLS_CHAR_1( ssl->in_window ); + *p++ = MBEDTLS_CHAR_0( ssl->in_window ); } #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ @@ -5806,8 +5806,8 @@ int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, used += 2; if( used <= buf_len ) { - *p++ = (unsigned char)( ( ssl->mtu >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( ssl->mtu ) & 0xFF ); + *p++ = MBEDTLS_CHAR_1( ssl->mtu ); + *p++ = MBEDTLS_CHAR_0( ssl->mtu ); } #endif /* MBEDTLS_SSL_PROTO_DTLS */ diff --git a/library/ssl_tls13_keys.c b/library/ssl_tls13_keys.c index 902f99ea8147..9e629cb0b3ce 100644 --- a/library/ssl_tls13_keys.c +++ b/library/ssl_tls13_keys.c @@ -113,17 +113,17 @@ static void ssl_tls1_3_hkdf_encode_label( #endif *p++ = 0; - *p++ = (unsigned char)( ( desired_length >> 0 ) & 0xFF ); + *p++ = MBEDTLS_CHAR_0( desired_length ); /* Add label incl. prefix */ - *p++ = (unsigned char)( total_label_len & 0xFF ); + *p++ = MBEDTLS_CHAR_0( total_label_len ); memcpy( p, tls1_3_label_prefix, sizeof(tls1_3_label_prefix) ); p += sizeof(tls1_3_label_prefix); memcpy( p, label, llen ); p += llen; /* Add context value */ - *p++ = (unsigned char)( clen & 0xFF ); + *p++ = MBEDTLS_CHAR_0( clen ); if( clen != 0 ) memcpy( p, ctx, clen ); From 635231a71ef0a055bad5dd46e66bfdcff1027c98 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Wed, 14 Jul 2021 11:53:07 +0100 Subject: [PATCH 049/120] Improve common.h macro documentation Imrpoved the descriptions of the macros and parameters and changing the name of the MBEDTLS_PUT_UINT... macro parameters to be more descriptive Signed-off-by: Joe Subbiani --- library/common.h | 140 ++++++++++++++++++----------------------------- 1 file changed, 52 insertions(+), 88 deletions(-) diff --git a/library/common.h b/library/common.h index 56c300265358..f7a9c9da6d97 100644 --- a/library/common.h +++ b/library/common.h @@ -87,18 +87,13 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c #define MBEDTLS_CHAR_7( x ) ( (unsigned char) ( ( ( x ) >> 56 ) & 0xff ) ) /** - * 32-bit integer manipulation GET macros (big endian) + * Get the unsigned 32 bits integer corresponding to four bytes in + * big-endian order (MSB first). * - * \brief Use this to assign an unsigned 32 bit integer - * by taking data stored adjacent in memory that - * can be accessed via on offset - * Big Endian is used when wanting to - * transmit the most signifcant bits first - * - * \param data The data used to translate to a 32 bit - * integer - * \param offset the shift in bytes to access the next byte - * of data + * \param data Base address of the memory to get the four bytes from. + * \param offset Offset from \p base of the first and most significant + * byte of the four bytes to build the 32 bits unsigned + * integer from. */ #ifndef MBEDTLS_GET_UINT32_BE #define MBEDTLS_GET_UINT32_BE( data , offset ) \ @@ -111,44 +106,32 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c #endif /** - * 32-bit integer manipulation PUT macros (big endian) - * - * \brief Read from a 32 bit integer and store each byte - * in memory, offset by a specified amount, resulting - * in each byte being adjacent in memory. - * Big Endian is used when wanting to - * transmit the most signifcant bits first + * Put in memory a 32 bits unsigned integer in big-endian order. * - * \param n 32 bit integer where data is accessed - * \param b const unsigned char array of data to be - * manipulated - * \param i offset in bytes, In the case of UINT32, i - * would increment by 4 every use assuming - * the data is being stored in the same location + * \param n 32 bits unsigned integer to put in memory + * \param data Base address of the memory where to put the 32 + * bits unsigned integer in. + * \param offset Offset from \p base where to put the most significant + * byte of the 32 bits unsigned integer \p n. */ #ifndef MBEDTLS_PUT_UINT32_BE -#define MBEDTLS_PUT_UINT32_BE(n,b,i) \ - do { \ - (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ - (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ - (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ - (b)[(i) + 3] = (unsigned char) ( (n) ); \ +#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \ + do { \ + ( data )[( offset ) ] = (unsigned char) ( (n) >> 24 ); \ + ( data )[( offset ) + 1] = (unsigned char) ( (n) >> 16 ); \ + ( data )[( offset ) + 2] = (unsigned char) ( (n) >> 8 ); \ + ( data )[( offset ) + 3] = (unsigned char) ( (n) ); \ } while( 0 ) #endif /** - * 32-bit integer manipulation GET macros (little endian) - * - * \brief Use this to assign an unsigned 32 bit integer - * by taking data stored adjacent in memory that - * can be accessed via on offset - * Little Endian is used when wanting to - * transmit the least signifcant bits first + * Get the unsigned 32 bits integer corresponding to four bytes in + * little-endian order (LSB first). * - * \param data The data used to translate to a 32 bit - * integer - * \param offset the shift in bytes to access the next byte - * of data + * \param data Base address of the memory to get the four bytes from. + * \param offset Offset from \p base of the first and least significant + * byte of the four bytes to build the 32 bits unsigned + * integer from. */ #ifndef MBEDTLS_GET_UINT32_LE #define MBEDTLS_GET_UINT32_LE( data, offset ) \ @@ -161,44 +144,32 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c #endif /** - * 32-bit integer manipulation PUT macros (little endian) + * Put in memory a 32 bits unsigned integer in little-endian order. * - * \brief Read from a 32 bit integer and store each byte - * in memory, offset by a specified amount, resulting - * in each byte being adjacent in memory. - * Little Endian is used when wanting to - * transmit the least signifcant bits first - * - * \param n 32 bit integer where data is accessed - * \param b const unsigned char array of data to be - * manipulated - * \param i offset in bytes, In the case of UINT32, i - * would increment by 4 every use assuming - * the data is being stored in the same location + * \param n 32 bits unsigned integer to put in memory + * \param data Base address of the memory where to put the 32 + * bits unsigned integer in. + * \param offset Offset from \p base where to put the least significant + * byte of the 32 bits unsigned integer \p n. */ #ifndef MBEDTLS_PUT_UINT32_LE -#define MBEDTLS_PUT_UINT32_LE(n,b,i) \ - do { \ - (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ - (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ - (b)[(i) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ - (b)[(i) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ +#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \ + do { \ + ( data )[( offset ) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ + ( data )[( offset ) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ + ( data )[( offset ) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ + ( data )[( offset ) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ } while( 0 ) #endif /** - * 16-bit integer manipulation GET macros (little endian) + * Get the unsigned 16 bits integer corresponding to four bytes in + * little-endian order (LSB first). * - * \brief Use this to assign an unsigned 16 bit integer - * by taking data stored adjacent in memory that - * can be accessed via on offset - * Little Endian is used when wanting to - * transmit the least signifcant bits first - * - * \param data The data used to translate to a 16 bit - * integer - * \param offset the shit in bytes to access the next byte - * of data + * \param data Base address of the memory to get the four bytes from. + * \param offset Offset from \p base of the first and least significant + * byte of the four bytes to build the 16 bits unsigned + * integer from. */ #ifndef MBEDTLS_GET_UINT16_LE #define MBEDTLS_GET_UINT16_LE( data, offset ) \ @@ -209,26 +180,19 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c #endif /** - * 16-bit integer manipulation PUT macros (little endian) - * - * \brief Read from a 16 bit integer and store each byte - * in memory, offset by a specified amount, resulting - * in each byte being adjacent in memory. - * Little Endian is used when wanting to - * transmit the least signifcant bits first + * Put in memory a 16 bits unsigned integer in little-endian order. * - * \param n 16 bit integer where data is accessed - * \param b const unsigned char array of data to be - * manipulated - * \param i offset in bytes, In the case of UINT16, i - * would increment by 2 every use assuming - * the data is being stored in the same location + * \param n 16 bits unsigned integer to put in memory + * \param data Base address of the memory where to put the 16 + * bits unsigned integer in. + * \param offset Offset from \p base where to put the least significant + * byte of the 16 bits unsigned integer \p n. */ #ifndef MBEDTLS_PUT_UINT16_LE -#define MBEDTLS_PUT_UINT16_LE( n, b, i ) \ -{ \ - (b)[(i) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ - (b)[(i) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ +#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \ +{ \ + ( data )[( offset ) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ + ( data )[( offset ) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ } #endif From 54550f7fca255769c4a2d9f5e089caddde4b0c6c Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Wed, 14 Jul 2021 11:59:48 +0100 Subject: [PATCH 050/120] Replace 3 byte shift with appropriate macro aria.c has a shift by 3 bytes, but does not use the 0xff masking. aparently this is not a problem and it is tidier to use the maco. Signed-off-by: Joe Subbiani --- library/aria.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/library/aria.c b/library/aria.c index 6bfdfbdce2b8..bc05c4a31962 100644 --- a/library/aria.c +++ b/library/aria.c @@ -215,19 +215,19 @@ static inline void aria_sl( uint32_t *a, uint32_t *b, *a = ( (uint32_t) sa[ MBEDTLS_BYTE_0( *a ) ] ) ^ (((uint32_t) sb[ MBEDTLS_BYTE_1( *a ) ]) << 8) ^ (((uint32_t) sc[ MBEDTLS_BYTE_2( *a ) ]) << 16) ^ - (((uint32_t) sd[ *a >> 24 ]) << 24); + (((uint32_t) sd[ MBEDTLS_BYTE_3( *a ) ]) << 24); *b = ( (uint32_t) sa[ MBEDTLS_BYTE_0( *b ) ] ) ^ (((uint32_t) sb[ MBEDTLS_BYTE_1( *b ) ]) << 8) ^ (((uint32_t) sc[ MBEDTLS_BYTE_2( *b ) ]) << 16) ^ - (((uint32_t) sd[ *b >> 24 ]) << 24); + (((uint32_t) sd[ MBEDTLS_BYTE_3( *b ) ]) << 24); *c = ( (uint32_t) sa[ MBEDTLS_BYTE_0( *c ) ] ) ^ (((uint32_t) sb[ MBEDTLS_BYTE_1( *c ) ]) << 8) ^ (((uint32_t) sc[ MBEDTLS_BYTE_2( *c ) ]) << 16) ^ - (((uint32_t) sd[ *c >> 24 ]) << 24); + (((uint32_t) sd[ MBEDTLS_BYTE_3( *c ) ]) << 24); *d = ( (uint32_t) sa[ MBEDTLS_BYTE_0( *d ) ] ) ^ (((uint32_t) sb[ MBEDTLS_BYTE_1( *d ) ]) << 8) ^ (((uint32_t) sc[ MBEDTLS_BYTE_2( *d ) ]) << 16) ^ - (((uint32_t) sd[ *d >> 24 ]) << 24); + (((uint32_t) sd[ MBEDTLS_BYTE_3( *d ) ]) << 24); } /* From f5462d989c8674805d3566cf4d85029da0f18717 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Tue, 13 Jul 2021 12:13:19 +0100 Subject: [PATCH 051/120] Remove trailing whitespaces Signed-off-by: Joe Subbiani --- library/common.h | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/library/common.h b/library/common.h index f7a9c9da6d97..87f0d66bc76a 100644 --- a/library/common.h +++ b/library/common.h @@ -87,12 +87,12 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c #define MBEDTLS_CHAR_7( x ) ( (unsigned char) ( ( ( x ) >> 56 ) & 0xff ) ) /** - * Get the unsigned 32 bits integer corresponding to four bytes in + * Get the unsigned 32 bits integer corresponding to four bytes in * big-endian order (MSB first). * * \param data Base address of the memory to get the four bytes from. - * \param offset Offset from \p base of the first and most significant - * byte of the four bytes to build the 32 bits unsigned + * \param offset Offset from \p base of the first and most significant + * byte of the four bytes to build the 32 bits unsigned * integer from. */ #ifndef MBEDTLS_GET_UINT32_BE @@ -108,10 +108,10 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c /** * Put in memory a 32 bits unsigned integer in big-endian order. * - * \param n 32 bits unsigned integer to put in memory - * \param data Base address of the memory where to put the 32 + * \param n 32 bits unsigned integer to put in memory. + * \param data Base address of the memory where to put the 32 * bits unsigned integer in. - * \param offset Offset from \p base where to put the most significant + * \param offset Offset from \p base where to put the most significant * byte of the 32 bits unsigned integer \p n. */ #ifndef MBEDTLS_PUT_UINT32_BE @@ -125,12 +125,12 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c #endif /** - * Get the unsigned 32 bits integer corresponding to four bytes in + * Get the unsigned 32 bits integer corresponding to four bytes in * little-endian order (LSB first). * * \param data Base address of the memory to get the four bytes from. - * \param offset Offset from \p base of the first and least significant - * byte of the four bytes to build the 32 bits unsigned + * \param offset Offset from \p base of the first and least significant + * byte of the four bytes to build the 32 bits unsigned * integer from. */ #ifndef MBEDTLS_GET_UINT32_LE @@ -146,10 +146,10 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c /** * Put in memory a 32 bits unsigned integer in little-endian order. * - * \param n 32 bits unsigned integer to put in memory - * \param data Base address of the memory where to put the 32 + * \param n 32 bits unsigned integer to put in memory. + * \param data Base address of the memory where to put the 32 * bits unsigned integer in. - * \param offset Offset from \p base where to put the least significant + * \param offset Offset from \p base where to put the least significant * byte of the 32 bits unsigned integer \p n. */ #ifndef MBEDTLS_PUT_UINT32_LE @@ -163,12 +163,12 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c #endif /** - * Get the unsigned 16 bits integer corresponding to four bytes in + * Get the unsigned 16 bits integer corresponding to four bytes in * little-endian order (LSB first). * * \param data Base address of the memory to get the four bytes from. - * \param offset Offset from \p base of the first and least significant - * byte of the four bytes to build the 16 bits unsigned + * \param offset Offset from \p base of the first and least significant + * byte of the four bytes to build the 16 bits unsigned * integer from. */ #ifndef MBEDTLS_GET_UINT16_LE @@ -182,10 +182,10 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c /** * Put in memory a 16 bits unsigned integer in little-endian order. * - * \param n 16 bits unsigned integer to put in memory - * \param data Base address of the memory where to put the 16 + * \param n 16 bits unsigned integer to put in memory. + * \param data Base address of the memory where to put the 16 * bits unsigned integer in. - * \param offset Offset from \p base where to put the least significant + * \param offset Offset from \p base where to put the least significant * byte of the 16 bits unsigned integer \p n. */ #ifndef MBEDTLS_PUT_UINT16_LE From bf7ea84f83d7fbef2103cae2564fe7258e6847c9 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Wed, 14 Jul 2021 12:05:51 +0100 Subject: [PATCH 052/120] Replace "four bytes" with "two bytes" in macro documentation When writing the documentation 4 bytes was written instead of 2 for MBEDTLS_UINT16_LE Signed-off-by: Joe Subbiani --- library/common.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/common.h b/library/common.h index 87f0d66bc76a..28017f974272 100644 --- a/library/common.h +++ b/library/common.h @@ -163,12 +163,12 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c #endif /** - * Get the unsigned 16 bits integer corresponding to four bytes in + * Get the unsigned 16 bits integer corresponding to two bytes in * little-endian order (LSB first). * - * \param data Base address of the memory to get the four bytes from. + * \param data Base address of the memory to get the two bytes from. * \param offset Offset from \p base of the first and least significant - * byte of the four bytes to build the 16 bits unsigned + * byte of the two bytes to build the 16 bits unsigned * integer from. */ #ifndef MBEDTLS_GET_UINT16_LE From 2194dc477a65c0c18f592168b5e5f765c475bc9c Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Wed, 14 Jul 2021 12:31:31 +0100 Subject: [PATCH 053/120] Replace MBEDTLS_CHAR_x with MBEDTLS_BYTE_x The CHAR macros casted to an unsigned char which in this project is garunteed to be 8 bits - the same as uint8_t (which BYTE casts to) therefore, instances of CHAR have been swapped with BYTE and the number of macros have been cut down Signed-off-by: Joe Subbiani --- library/asn1write.c | 18 +++--- library/ccm.c | 6 +- library/common.h | 17 +++--- library/ecjpake.c | 18 +++--- library/ssl_cli.c | 116 ++++++++++++++++++------------------- library/ssl_msg.c | 4 +- library/ssl_srv.c | 86 +++++++++++++-------------- library/ssl_tls.c | 122 +++++++++++++++++++-------------------- library/ssl_tls13_keys.c | 6 +- 9 files changed, 195 insertions(+), 198 deletions(-) diff --git a/library/asn1write.c b/library/asn1write.c index 8555767fe26b..dc61854137ea 100644 --- a/library/asn1write.c +++ b/library/asn1write.c @@ -60,8 +60,8 @@ int mbedtls_asn1_write_len( unsigned char **p, const unsigned char *start, size_ if( *p - start < 3 ) return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); - *--(*p) = MBEDTLS_CHAR_0( len ); - *--(*p) = MBEDTLS_CHAR_1( len ); + *--(*p) = MBEDTLS_BYTE_0( len ); + *--(*p) = MBEDTLS_BYTE_1( len ); *--(*p) = 0x82; return( 3 ); } @@ -71,9 +71,9 @@ int mbedtls_asn1_write_len( unsigned char **p, const unsigned char *start, size_ if( *p - start < 4 ) return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); - *--(*p) = MBEDTLS_CHAR_0( len ); - *--(*p) = MBEDTLS_CHAR_1( len ); - *--(*p) = MBEDTLS_CHAR_2( len ); + *--(*p) = MBEDTLS_BYTE_0( len ); + *--(*p) = MBEDTLS_BYTE_1( len ); + *--(*p) = MBEDTLS_BYTE_2( len ); *--(*p) = 0x83; return( 4 ); } @@ -85,10 +85,10 @@ int mbedtls_asn1_write_len( unsigned char **p, const unsigned char *start, size_ if( *p - start < 5 ) return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); - *--(*p) = MBEDTLS_CHAR_0( len ); - *--(*p) = MBEDTLS_CHAR_1( len ); - *--(*p) = MBEDTLS_CHAR_2( len ); - *--(*p) = MBEDTLS_CHAR_3( len ); + *--(*p) = MBEDTLS_BYTE_0( len ); + *--(*p) = MBEDTLS_BYTE_1( len ); + *--(*p) = MBEDTLS_BYTE_2( len ); + *--(*p) = MBEDTLS_BYTE_3( len ); *--(*p) = 0x84; return( 5 ); } diff --git a/library/ccm.c b/library/ccm.c index 95d90dc61a0c..0188075f5ea3 100644 --- a/library/ccm.c +++ b/library/ccm.c @@ -200,7 +200,7 @@ static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length, memcpy( b + 1, iv, iv_len ); for( i = 0, len_left = length; i < q; i++, len_left >>= 8 ) - b[15-i] = MBEDTLS_CHAR_0( len_left ); + b[15-i] = MBEDTLS_BYTE_0( len_left ); if( len_left > 0 ) return( MBEDTLS_ERR_CCM_BAD_INPUT ); @@ -221,8 +221,8 @@ static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length, src = add; memset( b, 0, 16 ); - b[0] = MBEDTLS_CHAR_1( add_len ); - b[1] = MBEDTLS_CHAR_0( add_len ); + b[0] = MBEDTLS_BYTE_1( add_len ); + b[1] = MBEDTLS_BYTE_0( add_len ); use_len = len_left < 16 - 2 ? len_left : 16 - 2; memcpy( b + 2, src, use_len ); diff --git a/library/common.h b/library/common.h index 28017f974272..e0f8b99baae3 100644 --- a/library/common.h +++ b/library/common.h @@ -25,6 +25,8 @@ #include "mbedtls/build_info.h" +#include + /** Helper to define a function as static except when building invasive tests. * * If a function is only used inside its own source file and should be @@ -72,19 +74,14 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c * Using MBEDTLS_BYTE_a will shift a*8 bits * to retrieve the next byte of information */ -#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) ) +#define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) ) #define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) ) #define MBEDTLS_BYTE_2( x ) ( (uint8_t) ( ( ( x ) >> 16 ) & 0xff ) ) #define MBEDTLS_BYTE_3( x ) ( (uint8_t) ( ( ( x ) >> 24 ) & 0xff ) ) - -#define MBEDTLS_CHAR_0( x ) ( (unsigned char) ( ( x ) & 0xff ) ) -#define MBEDTLS_CHAR_1( x ) ( (unsigned char) ( ( ( x ) >> 8 ) & 0xff ) ) -#define MBEDTLS_CHAR_2( x ) ( (unsigned char) ( ( ( x ) >> 16 ) & 0xff ) ) -#define MBEDTLS_CHAR_3( x ) ( (unsigned char) ( ( ( x ) >> 24 ) & 0xff ) ) -#define MBEDTLS_CHAR_4( x ) ( (unsigned char) ( ( ( x ) >> 32 ) & 0xff ) ) -#define MBEDTLS_CHAR_5( x ) ( (unsigned char) ( ( ( x ) >> 40 ) & 0xff ) ) -#define MBEDTLS_CHAR_6( x ) ( (unsigned char) ( ( ( x ) >> 48 ) & 0xff ) ) -#define MBEDTLS_CHAR_7( x ) ( (unsigned char) ( ( ( x ) >> 56 ) & 0xff ) ) +#define MBEDTLS_BYTE_4( x ) ( (uint8_t) ( ( ( x ) >> 32 ) & 0xff ) ) +#define MBEDTLS_BYTE_5( x ) ( (uint8_t) ( ( ( x ) >> 40 ) & 0xff ) ) +#define MBEDTLS_BYTE_6( x ) ( (uint8_t) ( ( ( x ) >> 48 ) & 0xff ) ) +#define MBEDTLS_BYTE_7( x ) ( (uint8_t) ( ( ( x ) >> 56 ) & 0xff ) ) /** * Get the unsigned 32 bits integer corresponding to four bytes in diff --git a/library/ecjpake.c b/library/ecjpake.c index 7305dfeee10f..a599b1ba485f 100644 --- a/library/ecjpake.c +++ b/library/ecjpake.c @@ -180,10 +180,10 @@ static int ecjpake_write_len_point( unsigned char **p, if( ret != 0 ) return( ret ); - (*p)[0] = MBEDTLS_CHAR_3( len ); - (*p)[1] = MBEDTLS_CHAR_2( len ); - (*p)[2] = MBEDTLS_CHAR_1( len ); - (*p)[3] = MBEDTLS_CHAR_0( len ); + (*p)[0] = MBEDTLS_BYTE_3( len ); + (*p)[1] = MBEDTLS_BYTE_2( len ); + (*p)[2] = MBEDTLS_BYTE_1( len ); + (*p)[3] = MBEDTLS_BYTE_0( len ); *p += 4 + len; @@ -223,10 +223,10 @@ static int ecjpake_hash( const mbedtls_md_info_t *md_info, if( end - p < 4 ) return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); - *p++ = MBEDTLS_CHAR_3( id_len ); - *p++ = MBEDTLS_CHAR_2( id_len ); - *p++ = MBEDTLS_CHAR_1( id_len ); - *p++ = MBEDTLS_CHAR_0( id_len ); + *p++ = MBEDTLS_BYTE_3( id_len ); + *p++ = MBEDTLS_BYTE_2( id_len ); + *p++ = MBEDTLS_BYTE_1( id_len ); + *p++ = MBEDTLS_BYTE_0( id_len ); if( end < p || (size_t)( end - p ) < id_len ) return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); @@ -366,7 +366,7 @@ static int ecjpake_zkp_write( const mbedtls_md_info_t *md_info, goto cleanup; } - *(*p)++ = MBEDTLS_CHAR_0( len ); + *(*p)++ = MBEDTLS_BYTE_0( len ); MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &h, *p, len ) ); /* r */ *p += len; diff --git a/library/ssl_cli.c b/library/ssl_cli.c index 7d65479efba0..9e0db96c48c8 100644 --- a/library/ssl_cli.c +++ b/library/ssl_cli.c @@ -136,18 +136,18 @@ static int ssl_write_hostname_ext( mbedtls_ssl_context *ssl, * } ServerNameList; * */ - *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_SERVERNAME ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_SERVERNAME ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_SERVERNAME ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME ); - *p++ = MBEDTLS_CHAR_1( hostname_len + 5); - *p++ = MBEDTLS_CHAR_0( hostname_len + 5); + *p++ = MBEDTLS_BYTE_1( hostname_len + 5); + *p++ = MBEDTLS_BYTE_0( hostname_len + 5); - *p++ = MBEDTLS_CHAR_1( hostname_len + 3 ); - *p++ = MBEDTLS_CHAR_0( hostname_len + 3 ); + *p++ = MBEDTLS_BYTE_1( hostname_len + 3 ); + *p++ = MBEDTLS_BYTE_0( hostname_len + 3 ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ); - *p++ = MBEDTLS_CHAR_1( hostname_len ); - *p++ = MBEDTLS_CHAR_0( hostname_len ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ); + *p++ = MBEDTLS_BYTE_1( hostname_len ); + *p++ = MBEDTLS_BYTE_0( hostname_len ); memcpy( p, ssl->hostname, hostname_len ); @@ -181,12 +181,12 @@ static int ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl, /* * Secure renegotiation */ - *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ); *p++ = 0x00; - *p++ = MBEDTLS_CHAR_0( ssl->verify_data_len + 1 ); - *p++ = MBEDTLS_CHAR_0( ssl->verify_data_len ); + *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len + 1 ); + *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len ); memcpy( p, ssl->own_verify_data, ssl->verify_data_len ); @@ -281,14 +281,14 @@ static int ssl_write_signature_algorithms_ext( mbedtls_ssl_context *ssl, * SignatureAndHashAlgorithm * supported_signature_algorithms<2..2^16-2>; */ - *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_SIG_ALG ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_SIG_ALG ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_SIG_ALG ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SIG_ALG ); - *p++ = MBEDTLS_CHAR_1( sig_alg_len + 2 ); - *p++ = MBEDTLS_CHAR_0( sig_alg_len + 2 ); + *p++ = MBEDTLS_BYTE_1( sig_alg_len + 2 ); + *p++ = MBEDTLS_BYTE_0( sig_alg_len + 2 ); - *p++ = MBEDTLS_CHAR_1( sig_alg_len ); - *p++ = MBEDTLS_CHAR_0( sig_alg_len ); + *p++ = MBEDTLS_BYTE_1( sig_alg_len ); + *p++ = MBEDTLS_BYTE_0( sig_alg_len ); *olen = 6 + sig_alg_len; @@ -356,14 +356,14 @@ static int ssl_write_supported_elliptic_curves_ext( mbedtls_ssl_context *ssl, elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF; } - *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ); - *p++ = MBEDTLS_CHAR_1( elliptic_curve_len + 2 ); - *p++ = MBEDTLS_CHAR_0( elliptic_curve_len + 2 ); + *p++ = MBEDTLS_BYTE_1( elliptic_curve_len + 2 ); + *p++ = MBEDTLS_BYTE_0( elliptic_curve_len + 2 ); - *p++ = MBEDTLS_CHAR_1( elliptic_curve_len ); - *p++ = MBEDTLS_CHAR_0( elliptic_curve_len ); + *p++ = MBEDTLS_BYTE_1( elliptic_curve_len ); + *p++ = MBEDTLS_BYTE_0( elliptic_curve_len ); *olen = 6 + elliptic_curve_len; @@ -384,8 +384,8 @@ static int ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl, ( "client hello, adding supported_point_formats extension" ) ); MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); - *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ); *p++ = 0x00; *p++ = 2; @@ -421,8 +421,8 @@ static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 ); - *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ); /* * We may need to send ClientHello multiple times for Hello verification. @@ -464,8 +464,8 @@ static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len ); } - *p++ = MBEDTLS_CHAR_1( kkpp_len ); - *p++ = MBEDTLS_CHAR_0( kkpp_len ); + *p++ = MBEDTLS_BYTE_1( kkpp_len ); + *p++ = MBEDTLS_BYTE_0( kkpp_len ); *olen = kkpp_len + 4; @@ -504,11 +504,11 @@ static int ssl_write_cid_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) ); /* Add extension ID + size */ - *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_CID ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_CID ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_CID ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_CID ); ext_len = (size_t) ssl->own_cid_len + 1; - *p++ = MBEDTLS_CHAR_1( ext_len ); - *p++ = MBEDTLS_CHAR_0( ext_len ); + *p++ = MBEDTLS_BYTE_1( ext_len ); + *p++ = MBEDTLS_BYTE_0( ext_len ); *p++ = (uint8_t) ssl->own_cid_len; memcpy( p, ssl->own_cid, ssl->own_cid_len ); @@ -537,8 +537,8 @@ static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 ); - *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ); *p++ = 0x00; *p++ = 1; @@ -569,8 +569,8 @@ static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 ); - *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ); *p++ = 0x00; *p++ = 0x00; @@ -599,8 +599,8 @@ static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 ); - *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ); *p++ = 0x00; *p++ = 0x00; @@ -631,11 +631,11 @@ static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl, /* The addition is safe here since the ticket length is 16 bit. */ MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen ); - *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_SESSION_TICKET ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_SESSION_TICKET ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_SESSION_TICKET ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SESSION_TICKET ); - *p++ = MBEDTLS_CHAR_1( tlen ); - *p++ = MBEDTLS_CHAR_0( tlen ); + *p++ = MBEDTLS_BYTE_1( tlen ); + *p++ = MBEDTLS_BYTE_0( tlen ); *olen = 4; @@ -675,8 +675,8 @@ static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 + alpnlen ); - *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_ALPN ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_ALPN ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_ALPN ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_ALPN ); /* * opaque ProtocolName<1..2^8-1>; @@ -703,12 +703,12 @@ static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl, *olen = p - buf; /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */ - buf[4] = MBEDTLS_CHAR_1( *olen - 6 ); - buf[5] = MBEDTLS_CHAR_0( *olen - 6 ); + buf[4] = MBEDTLS_BYTE_1( *olen - 6 ); + buf[5] = MBEDTLS_BYTE_0( *olen - 6 ); /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */ - buf[2] = MBEDTLS_CHAR_1( *olen - 4 ); - buf[3] = MBEDTLS_CHAR_0( *olen - 4 ); + buf[2] = MBEDTLS_BYTE_1( *olen - 4 ); + buf[3] = MBEDTLS_BYTE_0( *olen - 4 ); return( 0 ); } @@ -760,12 +760,12 @@ static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, */ MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 ); - *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_USE_SRTP ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_USE_SRTP ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_USE_SRTP ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_USE_SRTP ); - *p++ = MBEDTLS_CHAR_1( ext_len & 0xFF00 ); - *p++ = MBEDTLS_CHAR_0( ext_len ); + *p++ = MBEDTLS_BYTE_1( ext_len & 0xFF00 ); + *p++ = MBEDTLS_BYTE_0( ext_len ); /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */ /* micro-optimization: @@ -776,7 +776,7 @@ static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, * >> 8 ) & 0xFF ); */ *p++ = 0; - *p++ = MBEDTLS_CHAR_0( 2 * ssl->conf->dtls_srtp_profile_list_len ); + *p++ = MBEDTLS_BYTE_0( 2 * ssl->conf->dtls_srtp_profile_list_len ); for( protection_profiles_index=0; protection_profiles_index < ssl->conf->dtls_srtp_profile_list_len; @@ -1323,8 +1323,8 @@ static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) { /* No need to check for space here, because the extension * writing functions already took care of that. */ - *p++ = MBEDTLS_CHAR_1( ext_len ); - *p++ = MBEDTLS_CHAR_0( ext_len ); + *p++ = MBEDTLS_BYTE_1( ext_len ); + *p++ = MBEDTLS_BYTE_0( ext_len ); p += ext_len; } diff --git a/library/ssl_msg.c b/library/ssl_msg.c index e0c50bcce1d4..989c59874f20 100644 --- a/library/ssl_msg.c +++ b/library/ssl_msg.c @@ -2481,8 +2481,8 @@ int mbedtls_ssl_write_handshake_msg_ext( mbedtls_ssl_context *ssl, /* Write message_seq and update it, except for HelloRequest */ if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) { - ssl->out_msg[4] = MBEDTLS_CHAR_1( ssl->handshake->out_msg_seq ); - ssl->out_msg[5] = MBEDTLS_CHAR_0( ssl->handshake->out_msg_seq ); + ssl->out_msg[4] = MBEDTLS_BYTE_1( ssl->handshake->out_msg_seq ); + ssl->out_msg[5] = MBEDTLS_BYTE_0( ssl->handshake->out_msg_seq ); ++( ssl->handshake->out_msg_seq ); } else diff --git a/library/ssl_srv.c b/library/ssl_srv.c index 253ab5695902..96b08ab8f679 100644 --- a/library/ssl_srv.c +++ b/library/ssl_srv.c @@ -1971,11 +1971,11 @@ static void ssl_write_cid_ext( mbedtls_ssl_context *ssl, * } ConnectionId; */ - *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_CID ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_CID ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_CID ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_CID ); ext_len = (size_t) ssl->own_cid_len + 1; - *p++ = MBEDTLS_CHAR_1( ext_len ); - *p++ = MBEDTLS_CHAR_0( ext_len ); + *p++ = MBEDTLS_BYTE_1( ext_len ); + *p++ = MBEDTLS_BYTE_0( ext_len ); *p++ = (uint8_t) ssl->own_cid_len; memcpy( p, ssl->own_cid, ssl->own_cid_len ); @@ -2016,8 +2016,8 @@ static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) ); - *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ); *p++ = 0x00; *p++ = 0x00; @@ -2042,8 +2042,8 @@ static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret " "extension" ) ); - *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ); *p++ = 0x00; *p++ = 0x00; @@ -2067,8 +2067,8 @@ static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) ); - *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_SESSION_TICKET ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_SESSION_TICKET ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_SESSION_TICKET ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SESSION_TICKET ); *p++ = 0x00; *p++ = 0x00; @@ -2091,8 +2091,8 @@ static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) ); - *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ); #if defined(MBEDTLS_SSL_RENEGOTIATION) if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) @@ -2132,8 +2132,8 @@ static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) ); - *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ); *p++ = 0x00; *p++ = 1; @@ -2162,8 +2162,8 @@ static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) ); - *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ); *p++ = 0x00; *p++ = 2; @@ -2200,8 +2200,8 @@ static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, return; } - *p++ = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ); - *p++ = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ); ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx, p + 2, end - p - 2, &kkpp_len, @@ -2212,8 +2212,8 @@ static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, return; } - *p++ = MBEDTLS_CHAR_1( kkpp_len ); - *p++ = MBEDTLS_CHAR_0( kkpp_len ); + *p++ = MBEDTLS_BYTE_1( kkpp_len ); + *p++ = MBEDTLS_BYTE_0( kkpp_len ); *olen = kkpp_len + 4; } @@ -2238,18 +2238,18 @@ static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl, * 6 . 6 protocol name length * 7 . 7+n protocol name */ - buf[0] = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_ALPN ); - buf[1] = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_ALPN ); + buf[0] = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_ALPN ); + buf[1] = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_ALPN ); *olen = 7 + strlen( ssl->alpn_chosen ); - buf[2] = MBEDTLS_CHAR_1( *olen - 4 ); - buf[3] = MBEDTLS_CHAR_0( *olen - 4 ); + buf[2] = MBEDTLS_BYTE_1( *olen - 4 ); + buf[3] = MBEDTLS_BYTE_0( *olen - 4 ); - buf[4] = MBEDTLS_CHAR_1( *olen - 6 ); - buf[5] = MBEDTLS_CHAR_0( *olen - 6 ); + buf[4] = MBEDTLS_BYTE_1( *olen - 6 ); + buf[5] = MBEDTLS_BYTE_0( *olen - 6 ); - buf[6] = MBEDTLS_CHAR_0( *olen - 7 ); + buf[6] = MBEDTLS_BYTE_0( *olen - 7 ); memcpy( buf + 7, ssl->alpn_chosen, *olen - 7 ); } @@ -2294,15 +2294,15 @@ static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, } /* extension */ - buf[0] = MBEDTLS_CHAR_1( MBEDTLS_TLS_EXT_USE_SRTP ); - buf[1] = MBEDTLS_CHAR_0( MBEDTLS_TLS_EXT_USE_SRTP ); + buf[0] = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_USE_SRTP ); + buf[1] = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_USE_SRTP ); /* * total length 5 and mki value: only one profile(2 bytes) * and length(2 bytes) and srtp_mki ) */ ext_len = 5 + mki_len; - buf[2] = MBEDTLS_CHAR_1( ext_len ); - buf[3] = MBEDTLS_CHAR_0( ext_len ); + buf[2] = MBEDTLS_BYTE_1( ext_len ); + buf[3] = MBEDTLS_BYTE_0( ext_len ); /* protection profile length: 2 */ buf[4] = 0x00; @@ -2311,8 +2311,8 @@ static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, ssl->dtls_srtp_info.chosen_dtls_srtp_profile ); if( profile_value != MBEDTLS_TLS_SRTP_UNSET ) { - buf[6] = MBEDTLS_CHAR_1( profile_value ); - buf[7] = MBEDTLS_CHAR_0( profile_value ); + buf[6] = MBEDTLS_BYTE_1( profile_value ); + buf[7] = MBEDTLS_BYTE_0( profile_value ); } else { @@ -2648,8 +2648,8 @@ static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) if( ext_len > 0 ) { - *p++ = MBEDTLS_CHAR_1( ext_len ); - *p++ = MBEDTLS_CHAR_0( ext_len ); + *p++ = MBEDTLS_BYTE_1( ext_len ); + *p++ = MBEDTLS_BYTE_0( ext_len ); p += ext_len; } @@ -3478,8 +3478,8 @@ static int ssl_decrypt_encrypted_pms( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); return( MBEDTLS_ERR_SSL_DECODE_ERROR ); } - if( *p++ != MBEDTLS_CHAR_1( len ) || - *p++ != MBEDTLS_CHAR_0( len ) ) + if( *p++ != MBEDTLS_BYTE_1( len ) || + *p++ != MBEDTLS_BYTE_0( len ) ) { MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad client key exchange message" ) ); return( MBEDTLS_ERR_SSL_DECODE_ERROR ); @@ -4223,13 +4223,13 @@ static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl ) tlen = 0; } - ssl->out_msg[4] = MBEDTLS_CHAR_3( lifetime ); - ssl->out_msg[5] = MBEDTLS_CHAR_2( lifetime ); - ssl->out_msg[6] = MBEDTLS_CHAR_1( lifetime ); - ssl->out_msg[7] = MBEDTLS_CHAR_0( lifetime ); + ssl->out_msg[4] = MBEDTLS_BYTE_3( lifetime ); + ssl->out_msg[5] = MBEDTLS_BYTE_2( lifetime ); + ssl->out_msg[6] = MBEDTLS_BYTE_1( lifetime ); + ssl->out_msg[7] = MBEDTLS_BYTE_0( lifetime ); - ssl->out_msg[8] = MBEDTLS_CHAR_1( tlen ); - ssl->out_msg[9] = MBEDTLS_CHAR_0( tlen ); + ssl->out_msg[8] = MBEDTLS_BYTE_1( tlen ); + ssl->out_msg[9] = MBEDTLS_BYTE_0( tlen ); ssl->out_msglen = 10 + tlen; diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 0655dc57ebea..381eb9e03bb6 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -4582,8 +4582,8 @@ static unsigned char ssl_serialized_session_header[] = { MBEDTLS_VERSION_MAJOR, MBEDTLS_VERSION_MINOR, MBEDTLS_VERSION_PATCH, - MBEDTLS_CHAR_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), - MBEDTLS_CHAR_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), + MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), + MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), }; /* @@ -4664,14 +4664,14 @@ static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, { start = (uint64_t) session->start; - *p++ = MBEDTLS_CHAR_7( start ); - *p++ = MBEDTLS_CHAR_6( start ); - *p++ = MBEDTLS_CHAR_5( start ); - *p++ = MBEDTLS_CHAR_4( start ); - *p++ = MBEDTLS_CHAR_3( start ); - *p++ = MBEDTLS_CHAR_2( start ); - *p++ = MBEDTLS_CHAR_1( start ); - *p++ = MBEDTLS_CHAR_0( start ); + *p++ = MBEDTLS_BYTE_7( start ); + *p++ = MBEDTLS_BYTE_6( start ); + *p++ = MBEDTLS_BYTE_5( start ); + *p++ = MBEDTLS_BYTE_4( start ); + *p++ = MBEDTLS_BYTE_3( start ); + *p++ = MBEDTLS_BYTE_2( start ); + *p++ = MBEDTLS_BYTE_1( start ); + *p++ = MBEDTLS_BYTE_0( start ); } #endif /* MBEDTLS_HAVE_TIME */ @@ -4687,22 +4687,22 @@ static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, if( used <= buf_len ) { - *p++ = MBEDTLS_CHAR_1( session->ciphersuite ); - *p++ = MBEDTLS_CHAR_0( session->ciphersuite ); + *p++ = MBEDTLS_BYTE_1( session->ciphersuite ); + *p++ = MBEDTLS_BYTE_0( session->ciphersuite ); - *p++ = MBEDTLS_CHAR_0( session->compression ); + *p++ = MBEDTLS_BYTE_0( session->compression ); - *p++ = MBEDTLS_CHAR_0( session->id_len ); + *p++ = MBEDTLS_BYTE_0( session->id_len ); memcpy( p, session->id, 32 ); p += 32; memcpy( p, session->master, 48 ); p += 48; - *p++ = MBEDTLS_CHAR_3( session->verify_result ); - *p++ = MBEDTLS_CHAR_2( session->verify_result ); - *p++ = MBEDTLS_CHAR_1( session->verify_result ); - *p++ = MBEDTLS_CHAR_0( session->verify_result ); + *p++ = MBEDTLS_BYTE_3( session->verify_result ); + *p++ = MBEDTLS_BYTE_2( session->verify_result ); + *p++ = MBEDTLS_BYTE_1( session->verify_result ); + *p++ = MBEDTLS_BYTE_0( session->verify_result ); } /* @@ -4719,9 +4719,9 @@ static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, if( used <= buf_len ) { - *p++ = MBEDTLS_CHAR_2( cert_len ); - *p++ = MBEDTLS_CHAR_1( cert_len ); - *p++ = MBEDTLS_CHAR_0( cert_len ); + *p++ = MBEDTLS_BYTE_2( cert_len ); + *p++ = MBEDTLS_BYTE_1( cert_len ); + *p++ = MBEDTLS_BYTE_0( cert_len ); if( session->peer_cert != NULL ) { @@ -4762,9 +4762,9 @@ static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, if( used <= buf_len ) { - *p++ = MBEDTLS_CHAR_2( session->ticket_len ); - *p++ = MBEDTLS_CHAR_1( session->ticket_len ); - *p++ = MBEDTLS_CHAR_0( session->ticket_len ); + *p++ = MBEDTLS_BYTE_2( session->ticket_len ); + *p++ = MBEDTLS_BYTE_1( session->ticket_len ); + *p++ = MBEDTLS_BYTE_0( session->ticket_len ); if( session->ticket != NULL ) { @@ -4772,10 +4772,10 @@ static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, p += session->ticket_len; } - *p++ = MBEDTLS_CHAR_3( session->ticket_lifetime ); - *p++ = MBEDTLS_CHAR_2( session->ticket_lifetime ); - *p++ = MBEDTLS_CHAR_1( session->ticket_lifetime ); - *p++ = MBEDTLS_CHAR_0( session->ticket_lifetime ); + *p++ = MBEDTLS_BYTE_3( session->ticket_lifetime ); + *p++ = MBEDTLS_BYTE_2( session->ticket_lifetime ); + *p++ = MBEDTLS_BYTE_1( session->ticket_lifetime ); + *p++ = MBEDTLS_BYTE_0( session->ticket_lifetime ); } #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ @@ -4793,7 +4793,7 @@ static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, used += 1; if( used <= buf_len ) - *p++ = MBEDTLS_CHAR_0( session->encrypt_then_mac ); + *p++ = MBEDTLS_BYTE_0( session->encrypt_then_mac ); #endif return( used ); @@ -5568,11 +5568,11 @@ static unsigned char ssl_serialized_context_header[] = { MBEDTLS_VERSION_MAJOR, MBEDTLS_VERSION_MINOR, MBEDTLS_VERSION_PATCH, - MBEDTLS_CHAR_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), - MBEDTLS_CHAR_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), - MBEDTLS_CHAR_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), - MBEDTLS_CHAR_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), - MBEDTLS_CHAR_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), + MBEDTLS_BYTE_1( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), + MBEDTLS_BYTE_0( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG ), + MBEDTLS_BYTE_2( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), + MBEDTLS_BYTE_1( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), + MBEDTLS_BYTE_0( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG ), }; /* @@ -5713,10 +5713,10 @@ int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, used += 4 + session_len; if( used <= buf_len ) { - *p++ = MBEDTLS_CHAR_3( session_len ); - *p++ = MBEDTLS_CHAR_2( session_len ); - *p++ = MBEDTLS_CHAR_1( session_len ); - *p++ = MBEDTLS_CHAR_0( session_len ); + *p++ = MBEDTLS_BYTE_3( session_len ); + *p++ = MBEDTLS_BYTE_2( session_len ); + *p++ = MBEDTLS_BYTE_1( session_len ); + *p++ = MBEDTLS_BYTE_0( session_len ); ret = ssl_session_save( ssl->session, 1, p, session_len, &session_len ); @@ -5757,33 +5757,33 @@ int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, used += 4; if( used <= buf_len ) { - *p++ = MBEDTLS_CHAR_3( ssl->badmac_seen ); - *p++ = MBEDTLS_CHAR_2( ssl->badmac_seen ); - *p++ = MBEDTLS_CHAR_1( ssl->badmac_seen ); - *p++ = MBEDTLS_CHAR_0( ssl->badmac_seen ); + *p++ = MBEDTLS_BYTE_3( ssl->badmac_seen ); + *p++ = MBEDTLS_BYTE_2( ssl->badmac_seen ); + *p++ = MBEDTLS_BYTE_1( ssl->badmac_seen ); + *p++ = MBEDTLS_BYTE_0( ssl->badmac_seen ); } #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) used += 16; if( used <= buf_len ) { - *p++ = MBEDTLS_CHAR_7( ssl->in_window_top ); - *p++ = MBEDTLS_CHAR_6( ssl->in_window_top ); - *p++ = MBEDTLS_CHAR_5( ssl->in_window_top ); - *p++ = MBEDTLS_CHAR_4( ssl->in_window_top ); - *p++ = MBEDTLS_CHAR_3( ssl->in_window_top ); - *p++ = MBEDTLS_CHAR_2( ssl->in_window_top ); - *p++ = MBEDTLS_CHAR_1( ssl->in_window_top ); - *p++ = MBEDTLS_CHAR_0( ssl->in_window_top ); - - *p++ = MBEDTLS_CHAR_7( ssl->in_window ); - *p++ = MBEDTLS_CHAR_6( ssl->in_window ); - *p++ = MBEDTLS_CHAR_5( ssl->in_window ); - *p++ = MBEDTLS_CHAR_4( ssl->in_window ); - *p++ = MBEDTLS_CHAR_3( ssl->in_window ); - *p++ = MBEDTLS_CHAR_2( ssl->in_window ); - *p++ = MBEDTLS_CHAR_1( ssl->in_window ); - *p++ = MBEDTLS_CHAR_0( ssl->in_window ); + *p++ = MBEDTLS_BYTE_7( ssl->in_window_top ); + *p++ = MBEDTLS_BYTE_6( ssl->in_window_top ); + *p++ = MBEDTLS_BYTE_5( ssl->in_window_top ); + *p++ = MBEDTLS_BYTE_4( ssl->in_window_top ); + *p++ = MBEDTLS_BYTE_3( ssl->in_window_top ); + *p++ = MBEDTLS_BYTE_2( ssl->in_window_top ); + *p++ = MBEDTLS_BYTE_1( ssl->in_window_top ); + *p++ = MBEDTLS_BYTE_0( ssl->in_window_top ); + + *p++ = MBEDTLS_BYTE_7( ssl->in_window ); + *p++ = MBEDTLS_BYTE_6( ssl->in_window ); + *p++ = MBEDTLS_BYTE_5( ssl->in_window ); + *p++ = MBEDTLS_BYTE_4( ssl->in_window ); + *p++ = MBEDTLS_BYTE_3( ssl->in_window ); + *p++ = MBEDTLS_BYTE_2( ssl->in_window ); + *p++ = MBEDTLS_BYTE_1( ssl->in_window ); + *p++ = MBEDTLS_BYTE_0( ssl->in_window ); } #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ @@ -5806,8 +5806,8 @@ int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, used += 2; if( used <= buf_len ) { - *p++ = MBEDTLS_CHAR_1( ssl->mtu ); - *p++ = MBEDTLS_CHAR_0( ssl->mtu ); + *p++ = MBEDTLS_BYTE_1( ssl->mtu ); + *p++ = MBEDTLS_BYTE_0( ssl->mtu ); } #endif /* MBEDTLS_SSL_PROTO_DTLS */ diff --git a/library/ssl_tls13_keys.c b/library/ssl_tls13_keys.c index 9e629cb0b3ce..7aec21dffe1a 100644 --- a/library/ssl_tls13_keys.c +++ b/library/ssl_tls13_keys.c @@ -113,17 +113,17 @@ static void ssl_tls1_3_hkdf_encode_label( #endif *p++ = 0; - *p++ = MBEDTLS_CHAR_0( desired_length ); + *p++ = MBEDTLS_BYTE_0( desired_length ); /* Add label incl. prefix */ - *p++ = MBEDTLS_CHAR_0( total_label_len ); + *p++ = MBEDTLS_BYTE_0( total_label_len ); memcpy( p, tls1_3_label_prefix, sizeof(tls1_3_label_prefix) ); p += sizeof(tls1_3_label_prefix); memcpy( p, label, llen ); p += llen; /* Add context value */ - *p++ = MBEDTLS_CHAR_0( clen ); + *p++ = MBEDTLS_BYTE_0( clen ); if( clen != 0 ) memcpy( p, ctx, clen ); From 99edd6c8105740b58ad3261da7ed47c42658b36e Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Fri, 16 Jul 2021 12:29:49 +0100 Subject: [PATCH 054/120] Add UINT64 GET and PUT macros Copy over the GET/PUT_UINT64_LE/BE macros from aes.c and sha512.c Add the MBEDTLS_ prefix to all 4 macros. Modify the GET_UINT64 macros to no longer take a target variable as a parameter, so when the macro function is called it must be assigned to a variable in the same statement. Signed-off-by: Joe Subbiani --- library/aes.c | 37 +++----------------- library/common.h | 91 ++++++++++++++++++++++++++++++++++++++++++++++++ library/sha512.c | 39 +++------------------ 3 files changed, 99 insertions(+), 68 deletions(-) diff --git a/library/aes.c b/library/aes.c index 1eb3e204d8a5..8e3358c494f2 100644 --- a/library/aes.c +++ b/library/aes.c @@ -1074,35 +1074,6 @@ int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx, #if defined(MBEDTLS_CIPHER_MODE_XTS) -/* Endianess with 64 bits values */ -#ifndef GET_UINT64_LE -#define GET_UINT64_LE(n,b,i) \ -{ \ - (n) = ( (uint64_t) (b)[(i) + 7] << 56 ) \ - | ( (uint64_t) (b)[(i) + 6] << 48 ) \ - | ( (uint64_t) (b)[(i) + 5] << 40 ) \ - | ( (uint64_t) (b)[(i) + 4] << 32 ) \ - | ( (uint64_t) (b)[(i) + 3] << 24 ) \ - | ( (uint64_t) (b)[(i) + 2] << 16 ) \ - | ( (uint64_t) (b)[(i) + 1] << 8 ) \ - | ( (uint64_t) (b)[(i) ] ); \ -} -#endif - -#ifndef PUT_UINT64_LE -#define PUT_UINT64_LE(n,b,i) \ -{ \ - (b)[(i) + 7] = (unsigned char) ( (n) >> 56 ); \ - (b)[(i) + 6] = (unsigned char) ( (n) >> 48 ); \ - (b)[(i) + 5] = (unsigned char) ( (n) >> 40 ); \ - (b)[(i) + 4] = (unsigned char) ( (n) >> 32 ); \ - (b)[(i) + 3] = (unsigned char) ( (n) >> 24 ); \ - (b)[(i) + 2] = (unsigned char) ( (n) >> 16 ); \ - (b)[(i) + 1] = (unsigned char) ( (n) >> 8 ); \ - (b)[(i) ] = (unsigned char) ( (n) ); \ -} -#endif - typedef unsigned char mbedtls_be128[16]; /* @@ -1118,14 +1089,14 @@ static void mbedtls_gf128mul_x_ble( unsigned char r[16], { uint64_t a, b, ra, rb; - GET_UINT64_LE( a, x, 0 ); - GET_UINT64_LE( b, x, 8 ); + a = MBEDTLS_GET_UINT64_LE( x, 0 ); + b = MBEDTLS_GET_UINT64_LE( x, 8 ); ra = ( a << 1 ) ^ 0x0087 >> ( 8 - ( ( b >> 63 ) << 3 ) ); rb = ( a >> 63 ) | ( b << 1 ); - PUT_UINT64_LE( ra, r, 0 ); - PUT_UINT64_LE( rb, r, 8 ); + MBEDTLS_PUT_UINT64_LE( ra, r, 0 ); + MBEDTLS_PUT_UINT64_LE( rb, r, 8 ); } /* diff --git a/library/common.h b/library/common.h index e0f8b99baae3..ce2f04007ee7 100644 --- a/library/common.h +++ b/library/common.h @@ -193,5 +193,96 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c } #endif +/** + * Get the unsigned 64 bits integer corresponding to eight bytes in + * big-endian order (MSB first). + * + * \param data Base address of the memory to get the eight bytes from. + * \param offset Offset from \p base of the first and most significant + * byte of the eight bytes to build the 64 bits unsigned + * integer from. + */ +#ifndef MBEDTLS_GET_UINT64_BE +#define MBEDTLS_GET_UINT64_BE( data, offset ) \ + ( \ + ( (uint64_t) ( data )[( offset ) ] << 56 ) \ + | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \ + | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \ + | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \ + | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \ + | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \ + | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \ + | ( (uint64_t) ( data )[( offset ) + 7] ) \ + ) +#endif + +/** + * Put in memory a 64 bits unsigned integer in big-endian order. + * + * \param n 64 bits unsigned integer to put in memory. + * \param data Base address of the memory where to put the 64 + * bits unsigned integer in. + * \param offset Offset from \p base where to put the most significant + * byte of the 64 bits unsigned integer \p n. + */ +#ifndef MBEDTLS_PUT_UINT64_BE +#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \ +{ \ + ( data )[( offset ) ] = (unsigned char) ( (n) >> 56 ); \ + ( data )[( offset ) + 1] = (unsigned char) ( (n) >> 48 ); \ + ( data )[( offset ) + 2] = (unsigned char) ( (n) >> 40 ); \ + ( data )[( offset ) + 3] = (unsigned char) ( (n) >> 32 ); \ + ( data )[( offset ) + 4] = (unsigned char) ( (n) >> 24 ); \ + ( data )[( offset ) + 5] = (unsigned char) ( (n) >> 16 ); \ + ( data )[( offset ) + 6] = (unsigned char) ( (n) >> 8 ); \ + ( data )[( offset ) + 7] = (unsigned char) ( (n) ); \ +} +#endif + +/** + * Get the unsigned 64 bits integer corresponding to eight bytes in + * little-endian order (LSB first). + * + * \param data Base address of the memory to get the eight bytes from. + * \param offset Offset from \p base of the first and least significant + * byte of the eight bytes to build the 64 bits unsigned + * integer from. + */ +#ifndef MBEDTLS_GET_UINT64_LE +#define MBEDTLS_GET_UINT64_LE( data, offset ) \ + ( \ + ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \ + | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \ + | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \ + | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \ + | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \ + | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \ + | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \ + | ( (uint64_t) ( data )[( offset ) ] ) \ + ) +#endif + +/** + * Put in memory a 64 bits unsigned integer in little-endian order. + * + * \param n 64 bits unsigned integer to put in memory. + * \param data Base address of the memory where to put the 64 + * bits unsigned integer in. + * \param offset Offset from \p base where to put the least significant + * byte of the 64 bits unsigned integer \p n. + */ +#ifndef MBEDTLS_PUT_UINT64_LE +#define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \ +{ \ + ( data )[( offset ) + 7] = (unsigned char) ( (n) >> 56 ); \ + ( data )[( offset ) + 6] = (unsigned char) ( (n) >> 48 ); \ + ( data )[( offset ) + 5] = (unsigned char) ( (n) >> 40 ); \ + ( data )[( offset ) + 4] = (unsigned char) ( (n) >> 32 ); \ + ( data )[( offset ) + 3] = (unsigned char) ( (n) >> 24 ); \ + ( data )[( offset ) + 2] = (unsigned char) ( (n) >> 16 ); \ + ( data )[( offset ) + 1] = (unsigned char) ( (n) >> 8 ); \ + ( data )[( offset ) ] = (unsigned char) ( (n) ); \ +} +#endif #endif /* MBEDTLS_LIBRARY_COMMON_H */ diff --git a/library/sha512.c b/library/sha512.c index 6511c6e36a8c..2b4cc547e40d 100644 --- a/library/sha512.c +++ b/library/sha512.c @@ -56,44 +56,13 @@ #if !defined(MBEDTLS_SHA512_ALT) -/* - * 64-bit integer manipulation macros (big endian) - */ -#ifndef GET_UINT64_BE -#define GET_UINT64_BE(n,b,i) \ -{ \ - (n) = ( (uint64_t) (b)[(i) ] << 56 ) \ - | ( (uint64_t) (b)[(i) + 1] << 48 ) \ - | ( (uint64_t) (b)[(i) + 2] << 40 ) \ - | ( (uint64_t) (b)[(i) + 3] << 32 ) \ - | ( (uint64_t) (b)[(i) + 4] << 24 ) \ - | ( (uint64_t) (b)[(i) + 5] << 16 ) \ - | ( (uint64_t) (b)[(i) + 6] << 8 ) \ - | ( (uint64_t) (b)[(i) + 7] ); \ -} -#endif /* GET_UINT64_BE */ - -#ifndef PUT_UINT64_BE -#define PUT_UINT64_BE(n,b,i) \ -{ \ - (b)[(i) ] = (unsigned char) ( (n) >> 56 ); \ - (b)[(i) + 1] = (unsigned char) ( (n) >> 48 ); \ - (b)[(i) + 2] = (unsigned char) ( (n) >> 40 ); \ - (b)[(i) + 3] = (unsigned char) ( (n) >> 32 ); \ - (b)[(i) + 4] = (unsigned char) ( (n) >> 24 ); \ - (b)[(i) + 5] = (unsigned char) ( (n) >> 16 ); \ - (b)[(i) + 6] = (unsigned char) ( (n) >> 8 ); \ - (b)[(i) + 7] = (unsigned char) ( (n) ); \ -} -#endif /* PUT_UINT64_BE */ - #if defined(MBEDTLS_SHA512_SMALLER) static void sha512_put_uint64_be( uint64_t n, unsigned char *b, uint8_t i ) { - PUT_UINT64_BE(n, b, i); + MBEDTLS_PUT_UINT64_BE(n, b, i); } #else -#define sha512_put_uint64_be PUT_UINT64_BE +#define sha512_put_uint64_be MBEDTLS_PUT_UINT64_BE #endif /* MBEDTLS_SHA512_SMALLER */ void mbedtls_sha512_init( mbedtls_sha512_context *ctx ) @@ -261,7 +230,7 @@ int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, { if( i < 16 ) { - GET_UINT64_BE( local.W[i], data, i << 3 ); + local.W[i] = MBEDTLS_GET_UINT64_BE( data, i << 3 ); } else { @@ -281,7 +250,7 @@ int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx, #else /* MBEDTLS_SHA512_SMALLER */ for( i = 0; i < 16; i++ ) { - GET_UINT64_BE( local.W[i], data, i << 3 ); + local.W[i] = MBEDTLS_GET_UINT64_BE( data, i << 3 ); } for( ; i < 80; i++ ) From fbeb692dd09efbb5d81304637e490ad1a2208ce5 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Fri, 16 Jul 2021 14:27:50 +0100 Subject: [PATCH 055/120] Use byte reading macros in places not using a byte mask byte shifting opertations throughout library/ were only replaced with the byte reading macros when an 0xff mask was being used. The byte reading macros are now more widley used, however they have not been used in all cases of a byte shift operation, as it detracted from the immediate readability or otherwise did not seem appropriate. Signed-off-by: Joe Subbiani --- library/base64.c | 6 +++--- library/chacha20.c | 8 ++++---- library/chachapoly.c | 32 ++++++++++++++++---------------- library/dhm.c | 4 ++-- library/ecp.c | 4 ++-- library/pkcs12.c | 8 ++++---- library/poly1305.c | 36 ++++++++++++++++++------------------ library/ssl_cli.c | 40 ++++++++++++++++++++-------------------- library/ssl_cookie.c | 8 ++++---- library/ssl_msg.c | 28 ++++++++++++++-------------- library/ssl_srv.c | 30 +++++++++++++++--------------- library/ssl_tls.c | 28 ++++++++++++++-------------- library/x509write_crt.c | 4 ++-- 13 files changed, 118 insertions(+), 118 deletions(-) diff --git a/library/base64.c b/library/base64.c index 1a05226efaab..9cf5dd41d42c 100644 --- a/library/base64.c +++ b/library/base64.c @@ -319,9 +319,9 @@ int mbedtls_base64_decode( unsigned char *dst, size_t dlen, size_t *olen, if( ++n == 4 ) { n = 0; - if( j > 0 ) *p++ = (unsigned char)( x >> 16 ); - if( j > 1 ) *p++ = (unsigned char)( x >> 8 ); - if( j > 2 ) *p++ = (unsigned char)( x ); + if( j > 0 ) *p++ = MBEDTLS_BYTE_2( x ); + if( j > 1 ) *p++ = MBEDTLS_BYTE_1( x ); + if( j > 2 ) *p++ = MBEDTLS_BYTE_0( x ); } } diff --git a/library/chacha20.c b/library/chacha20.c index 7015f99d5957..0e057f0e3c75 100644 --- a/library/chacha20.c +++ b/library/chacha20.c @@ -164,10 +164,10 @@ static void chacha20_block( const uint32_t initial_state[16], { size_t offset = i * 4U; - keystream[offset ] = (unsigned char)( working_state[i] ); - keystream[offset + 1U] = (unsigned char)( working_state[i] >> 8 ); - keystream[offset + 2U] = (unsigned char)( working_state[i] >> 16 ); - keystream[offset + 3U] = (unsigned char)( working_state[i] >> 24 ); + keystream[offset ] = MBEDTLS_BYTE_0( working_state[i] ); + keystream[offset + 1U] = MBEDTLS_BYTE_1( working_state[i] ); + keystream[offset + 2U] = MBEDTLS_BYTE_2( working_state[i] ); + keystream[offset + 3U] = MBEDTLS_BYTE_3( working_state[i] ); } mbedtls_platform_zeroize( working_state, sizeof( working_state ) ); diff --git a/library/chachapoly.c b/library/chachapoly.c index 77d547731cad..696d97bf01bb 100644 --- a/library/chachapoly.c +++ b/library/chachapoly.c @@ -263,22 +263,22 @@ int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx, /* The lengths of the AAD and ciphertext are processed by * Poly1305 as the final 128-bit block, encoded as little-endian integers. */ - len_block[ 0] = (unsigned char)( ctx->aad_len ); - len_block[ 1] = (unsigned char)( ctx->aad_len >> 8 ); - len_block[ 2] = (unsigned char)( ctx->aad_len >> 16 ); - len_block[ 3] = (unsigned char)( ctx->aad_len >> 24 ); - len_block[ 4] = (unsigned char)( ctx->aad_len >> 32 ); - len_block[ 5] = (unsigned char)( ctx->aad_len >> 40 ); - len_block[ 6] = (unsigned char)( ctx->aad_len >> 48 ); - len_block[ 7] = (unsigned char)( ctx->aad_len >> 56 ); - len_block[ 8] = (unsigned char)( ctx->ciphertext_len ); - len_block[ 9] = (unsigned char)( ctx->ciphertext_len >> 8 ); - len_block[10] = (unsigned char)( ctx->ciphertext_len >> 16 ); - len_block[11] = (unsigned char)( ctx->ciphertext_len >> 24 ); - len_block[12] = (unsigned char)( ctx->ciphertext_len >> 32 ); - len_block[13] = (unsigned char)( ctx->ciphertext_len >> 40 ); - len_block[14] = (unsigned char)( ctx->ciphertext_len >> 48 ); - len_block[15] = (unsigned char)( ctx->ciphertext_len >> 56 ); + len_block[ 0] = MBEDTLS_BYTE_0( ctx->aad_len ); + len_block[ 1] = MBEDTLS_BYTE_1( ctx->aad_len ); + len_block[ 2] = MBEDTLS_BYTE_2( ctx->aad_len ); + len_block[ 3] = MBEDTLS_BYTE_3( ctx->aad_len ); + len_block[ 4] = MBEDTLS_BYTE_4( ctx->aad_len ); + len_block[ 5] = MBEDTLS_BYTE_5( ctx->aad_len ); + len_block[ 6] = MBEDTLS_BYTE_6( ctx->aad_len ); + len_block[ 7] = MBEDTLS_BYTE_7( ctx->aad_len ); + len_block[ 8] = MBEDTLS_BYTE_0( ctx->ciphertext_len ); + len_block[ 9] = MBEDTLS_BYTE_1( ctx->ciphertext_len ); + len_block[10] = MBEDTLS_BYTE_2( ctx->ciphertext_len ); + len_block[11] = MBEDTLS_BYTE_3( ctx->ciphertext_len ); + len_block[12] = MBEDTLS_BYTE_4( ctx->ciphertext_len ); + len_block[13] = MBEDTLS_BYTE_5( ctx->ciphertext_len ); + len_block[14] = MBEDTLS_BYTE_6( ctx->ciphertext_len ); + len_block[15] = MBEDTLS_BYTE_7( ctx->ciphertext_len ); ret = mbedtls_poly1305_update( &ctx->poly1305_ctx, len_block, 16U ); if( ret != 0 ) diff --git a/library/dhm.c b/library/dhm.c index 29ce75598ca2..2ce0ed4fde4c 100644 --- a/library/dhm.c +++ b/library/dhm.c @@ -270,8 +270,8 @@ int mbedtls_dhm_make_params( mbedtls_dhm_context *ctx, int x_size, MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( ( X ), \ p + 2, \ ( n ) ) ); \ - *p++ = (unsigned char)( ( n ) >> 8 ); \ - *p++ = (unsigned char)( ( n ) ); \ + *p++ = MBEDTLS_BYTE_1( n ); \ + *p++ = MBEDTLS_BYTE_0( n ); \ p += ( n ); \ } while( 0 ) diff --git a/library/ecp.c b/library/ecp.c index b608ff1bd787..fdfc960069da 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -1012,8 +1012,8 @@ int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen, /* * Next two bytes are the namedcurve value */ - buf[0] = curve_info->tls_id >> 8; - buf[1] = curve_info->tls_id & 0xFF; + buf[0] = MBEDTLS_BYTE_1( curve_info->tls_id ); + buf[1] = MBEDTLS_BYTE_0( curve_info->tls_id ); return( 0 ); } diff --git a/library/pkcs12.c b/library/pkcs12.c index 04b1a6dab365..8f64bc6395f6 100644 --- a/library/pkcs12.c +++ b/library/pkcs12.c @@ -285,8 +285,8 @@ int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen, for( i = v; i > 0; i-- ) { j = salt_block[i - 1] + hash_block[i - 1] + c; - c = (unsigned char) (j >> 8); - salt_block[i - 1] = j & 0xFF; + c = MBEDTLS_BYTE_1( j ); + salt_block[i - 1] = MBEDTLS_BYTE_0( j ); } // pwd_block += B @@ -294,8 +294,8 @@ int mbedtls_pkcs12_derivation( unsigned char *data, size_t datalen, for( i = v; i > 0; i-- ) { j = pwd_block[i - 1] + hash_block[i - 1] + c; - c = (unsigned char) (j >> 8); - pwd_block[i - 1] = j & 0xFF; + c = MBEDTLS_BYTE_1( j ); + pwd_block[i - 1] = MBEDTLS_BYTE_0( j ); } } diff --git a/library/poly1305.c b/library/poly1305.c index f19574253ca2..333aade94adf 100644 --- a/library/poly1305.c +++ b/library/poly1305.c @@ -62,8 +62,8 @@ static uint64_t mul64( uint32_t a, uint32_t b ) /* a = al + 2**16 ah, b = bl + 2**16 bh */ const uint16_t al = (uint16_t) a; const uint16_t bl = (uint16_t) b; - const uint16_t ah = a >> 16; - const uint16_t bh = b >> 16; + const uint16_t ah = MBEDTLS_BYTE_2( a ); + const uint16_t bh = MBEDTLS_BYTE_2( b ); /* ab = al*bl + 2**16 (ah*bl + bl*bh) + 2**32 ah*bh */ const uint32_t lo = (uint32_t) al * bl; @@ -250,22 +250,22 @@ static void poly1305_compute_mac( const mbedtls_poly1305_context *ctx, acc3 += ctx->s[3] + (uint32_t) ( d >> 32U ); /* Compute MAC (128 least significant bits of the accumulator) */ - mac[ 0] = (unsigned char)( acc0 ); - mac[ 1] = (unsigned char)( acc0 >> 8 ); - mac[ 2] = (unsigned char)( acc0 >> 16 ); - mac[ 3] = (unsigned char)( acc0 >> 24 ); - mac[ 4] = (unsigned char)( acc1 ); - mac[ 5] = (unsigned char)( acc1 >> 8 ); - mac[ 6] = (unsigned char)( acc1 >> 16 ); - mac[ 7] = (unsigned char)( acc1 >> 24 ); - mac[ 8] = (unsigned char)( acc2 ); - mac[ 9] = (unsigned char)( acc2 >> 8 ); - mac[10] = (unsigned char)( acc2 >> 16 ); - mac[11] = (unsigned char)( acc2 >> 24 ); - mac[12] = (unsigned char)( acc3 ); - mac[13] = (unsigned char)( acc3 >> 8 ); - mac[14] = (unsigned char)( acc3 >> 16 ); - mac[15] = (unsigned char)( acc3 >> 24 ); + mac[ 0] = MBEDTLS_BYTE_0( acc0 ); + mac[ 1] = MBEDTLS_BYTE_1( acc0 ); + mac[ 2] = MBEDTLS_BYTE_2( acc0 ); + mac[ 3] = MBEDTLS_BYTE_3( acc0 ); + mac[ 4] = MBEDTLS_BYTE_0( acc1 ); + mac[ 5] = MBEDTLS_BYTE_1( acc1 ); + mac[ 6] = MBEDTLS_BYTE_2( acc1 ); + mac[ 7] = MBEDTLS_BYTE_3( acc1 ); + mac[ 8] = MBEDTLS_BYTE_0( acc2 ); + mac[ 9] = MBEDTLS_BYTE_1( acc2 ); + mac[10] = MBEDTLS_BYTE_2( acc2 ); + mac[11] = MBEDTLS_BYTE_3( acc2 ); + mac[12] = MBEDTLS_BYTE_0( acc3 ); + mac[13] = MBEDTLS_BYTE_1( acc3 ); + mac[14] = MBEDTLS_BYTE_2( acc3 ); + mac[15] = MBEDTLS_BYTE_3( acc3 ); } void mbedtls_poly1305_init( mbedtls_poly1305_context *ctx ) diff --git a/library/ssl_cli.c b/library/ssl_cli.c index 9e0db96c48c8..729784a6eb3d 100644 --- a/library/ssl_cli.c +++ b/library/ssl_cli.c @@ -352,8 +352,8 @@ static int ssl_write_supported_elliptic_curves_ext( mbedtls_ssl_context *ssl, grp_id++ ) { info = mbedtls_ecp_curve_info_from_grp_id( *grp_id ); - elliptic_curve_list[elliptic_curve_len++] = info->tls_id >> 8; - elliptic_curve_list[elliptic_curve_len++] = info->tls_id & 0xFF; + elliptic_curve_list[elliptic_curve_len++] = MBEDTLS_BYTE_1( info->tls_id ); + elliptic_curve_list[elliptic_curve_len++] = MBEDTLS_BYTE_0( info->tls_id ); } *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ); @@ -857,10 +857,10 @@ static int ssl_generate_random( mbedtls_ssl_context *ssl ) #if defined(MBEDTLS_HAVE_TIME) t = mbedtls_time( NULL ); - *p++ = (unsigned char)( t >> 24 ); - *p++ = (unsigned char)( t >> 16 ); - *p++ = (unsigned char)( t >> 8 ); - *p++ = (unsigned char)( t ); + *p++ = MBEDTLS_BYTE_3( t ); + *p++ = MBEDTLS_BYTE_2( t ); + *p++ = MBEDTLS_BYTE_1( t ); + *p++ = MBEDTLS_BYTE_0( t ); MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG, (long long) t ) ); @@ -1143,8 +1143,8 @@ static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); n++; - *p++ = (unsigned char)( ciphersuites[i] >> 8 ); - *p++ = (unsigned char)( ciphersuites[i] ); + *p++ = MBEDTLS_BYTE_1( ciphersuites[i] ); + *p++ = MBEDTLS_BYTE_0( ciphersuites[i] ); } MBEDTLS_SSL_DEBUG_MSG( 3, @@ -1159,8 +1159,8 @@ static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) { MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) ); MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); - *p++ = (unsigned char)( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO >> 8 ); - *p++ = (unsigned char)( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO ); + *p++ = MBEDTLS_BYTE_1( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO ); + *p++ = MBEDTLS_BYTE_0( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO ); n++; } @@ -2745,8 +2745,8 @@ static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl, #if defined(MBEDTLS_SSL_PROTO_TLS1_2) if( len_bytes == 2 ) { - ssl->out_msg[offset+0] = (unsigned char)( *olen >> 8 ); - ssl->out_msg[offset+1] = (unsigned char)( *olen ); + ssl->out_msg[offset+0] = MBEDTLS_BYTE_1( *olen ); + ssl->out_msg[offset+1] = MBEDTLS_BYTE_0( *olen ); *olen += 2; } #endif @@ -3503,8 +3503,8 @@ static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl ) */ content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ); - ssl->out_msg[4] = (unsigned char)( content_len >> 8 ); - ssl->out_msg[5] = (unsigned char)( content_len ); + ssl->out_msg[4] = MBEDTLS_BYTE_1( content_len ); + ssl->out_msg[5] = MBEDTLS_BYTE_0( content_len ); header_len = 6; ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx, @@ -3719,8 +3719,8 @@ static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl ) return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); } - ssl->out_msg[header_len++] = (unsigned char)( content_len >> 8 ); - ssl->out_msg[header_len++] = (unsigned char)( content_len ); + ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len ); + ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len ); memcpy( ssl->out_msg + header_len, ssl->conf->psk_identity, @@ -3771,8 +3771,8 @@ static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl ) return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); } - ssl->out_msg[header_len++] = (unsigned char)( content_len >> 8 ); - ssl->out_msg[header_len++] = (unsigned char)( content_len ); + ssl->out_msg[header_len++] = MBEDTLS_BYTE_1( content_len ); + ssl->out_msg[header_len++] = MBEDTLS_BYTE_0( content_len ); ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx, (int) mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ), @@ -4054,8 +4054,8 @@ static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl ) return( ret ); } - ssl->out_msg[4 + offset] = (unsigned char)( n >> 8 ); - ssl->out_msg[5 + offset] = (unsigned char)( n ); + ssl->out_msg[4 + offset] = MBEDTLS_BYTE_1( n ); + ssl->out_msg[5 + offset] = MBEDTLS_BYTE_0( n ); ssl->out_msglen = 6 + n + offset; ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; diff --git a/library/ssl_cookie.c b/library/ssl_cookie.c index 40b8913b8bc4..4f1c07bde6ef 100644 --- a/library/ssl_cookie.c +++ b/library/ssl_cookie.c @@ -165,10 +165,10 @@ int mbedtls_ssl_cookie_write( void *p_ctx, t = ctx->serial++; #endif - (*p)[0] = (unsigned char)( t >> 24 ); - (*p)[1] = (unsigned char)( t >> 16 ); - (*p)[2] = (unsigned char)( t >> 8 ); - (*p)[3] = (unsigned char)( t ); + (*p)[0] = MBEDTLS_BYTE_3( t ); + (*p)[1] = MBEDTLS_BYTE_2( t ); + (*p)[2] = MBEDTLS_BYTE_1( t ); + (*p)[3] = MBEDTLS_BYTE_0( t ); *p += 4; #if defined(MBEDTLS_THREADING_C) diff --git a/library/ssl_msg.c b/library/ssl_msg.c index 989c59874f20..304e7f22b7a1 100644 --- a/library/ssl_msg.c +++ b/library/ssl_msg.c @@ -2451,9 +2451,9 @@ int mbedtls_ssl_write_handshake_msg_ext( mbedtls_ssl_context *ssl, */ if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) { - ssl->out_msg[1] = (unsigned char)( hs_len >> 16 ); - ssl->out_msg[2] = (unsigned char)( hs_len >> 8 ); - ssl->out_msg[3] = (unsigned char)( hs_len ); + ssl->out_msg[1] = MBEDTLS_BYTE_2( hs_len ); + ssl->out_msg[2] = MBEDTLS_BYTE_1( hs_len ); + ssl->out_msg[3] = MBEDTLS_BYTE_0( hs_len ); /* * DTLS has additional fields in the Handshake layer, @@ -2566,8 +2566,8 @@ int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush ) ssl->conf->transport, ssl->out_hdr + 1 ); memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 ); - ssl->out_len[0] = (unsigned char)( len >> 8 ); - ssl->out_len[1] = (unsigned char)( len ); + ssl->out_len[0] = MBEDTLS_BYTE_1( len ); + ssl->out_len[1] = MBEDTLS_BYTE_0( len ); if( ssl->transform_out != NULL ) { @@ -2607,8 +2607,8 @@ int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush ) memcpy( ssl->out_cid, rec.cid, rec.cid_len ); #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ ssl->out_msglen = len = rec.data_len; - ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 ); - ssl->out_len[1] = (unsigned char)( rec.data_len ); + ssl->out_len[0] = MBEDTLS_BYTE_1( rec.data_len ); + ssl->out_len[1] = MBEDTLS_BYTE_0( rec.data_len ); } protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl ); @@ -3180,12 +3180,12 @@ static int ssl_check_dtls_clihlo_cookie( /* Go back and fill length fields */ obuf[27] = (unsigned char)( *olen - 28 ); - obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 ); - obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 ); - obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) ); + obuf[14] = obuf[22] = MBEDTLS_BYTE_2( *olen - 25 ); + obuf[15] = obuf[23] = MBEDTLS_BYTE_1( *olen - 25 ); + obuf[16] = obuf[24] = MBEDTLS_BYTE_0( *olen - 25 ); - obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 ); - obuf[12] = (unsigned char)( ( *olen - 13 ) ); + obuf[11] = MBEDTLS_BYTE_1( *olen - 13 ); + obuf[12] = MBEDTLS_BYTE_0( *olen - 13 ); return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); } @@ -4565,8 +4565,8 @@ static int ssl_get_next_record( mbedtls_ssl_context *ssl ) ssl->in_hdr[0] = rec.type; ssl->in_msg = rec.buf + rec.data_offset; ssl->in_msglen = rec.data_len; - ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 ); - ssl->in_len[1] = (unsigned char)( rec.data_len ); + ssl->in_len[0] = MBEDTLS_BYTE_1( rec.data_len ); + ssl->in_len[1] = MBEDTLS_BYTE_0( rec.data_len ); return( 0 ); } diff --git a/library/ssl_srv.c b/library/ssl_srv.c index 96b08ab8f679..232846ff08d9 100644 --- a/library/ssl_srv.c +++ b/library/ssl_srv.c @@ -2494,10 +2494,10 @@ static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) #if defined(MBEDTLS_HAVE_TIME) t = mbedtls_time( NULL ); - *p++ = (unsigned char)( t >> 24 ); - *p++ = (unsigned char)( t >> 16 ); - *p++ = (unsigned char)( t >> 8 ); - *p++ = (unsigned char)( t ); + *p++ = MBEDTLS_BYTE_3( t ); + *p++ = MBEDTLS_BYTE_2( t ); + *p++ = MBEDTLS_BYTE_1( t ); + *p++ = MBEDTLS_BYTE_0( t ); MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %" MBEDTLS_PRINTF_LONGLONG, (long long) t ) ); @@ -2578,9 +2578,9 @@ static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed", ssl->handshake->resume ? "a" : "no" ) ); - *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite >> 8 ); - *p++ = (unsigned char)( ssl->session_negotiate->ciphersuite ); - *p++ = (unsigned char)( ssl->session_negotiate->compression ); + *p++ = MBEDTLS_BYTE_1( ssl->session_negotiate->ciphersuite ); + *p++ = MBEDTLS_BYTE_0( ssl->session_negotiate->ciphersuite ); + *p++ = MBEDTLS_BYTE_0( ssl->session_negotiate->compression ); MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s", mbedtls_ssl_get_ciphersuite_name( ssl->session_negotiate->ciphersuite ) ) ); @@ -2785,8 +2785,8 @@ static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) #endif } - p[0] = (unsigned char)( sa_len >> 8 ); - p[1] = (unsigned char)( sa_len ); + p[0] = MBEDTLS_BYTE_1( sa_len ); + p[1] = MBEDTLS_BYTE_0( sa_len ); sa_len += 2; p += sa_len; } @@ -2826,8 +2826,8 @@ static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) break; } - *p++ = (unsigned char)( dn_size >> 8 ); - *p++ = (unsigned char)( dn_size ); + *p++ = MBEDTLS_BYTE_1( dn_size ); + *p++ = MBEDTLS_BYTE_0( dn_size ); memcpy( p, crt->subject_raw.p, dn_size ); p += dn_size; @@ -2841,8 +2841,8 @@ static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) ssl->out_msglen = p - buf; ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST; - ssl->out_msg[4 + ct_len + sa_len] = (unsigned char)( total_dn_size >> 8 ); - ssl->out_msg[5 + ct_len + sa_len] = (unsigned char)( total_dn_size ); + ssl->out_msg[4 + ct_len + sa_len] = MBEDTLS_BYTE_1( total_dn_size ); + ssl->out_msg[5 + ct_len + sa_len] = MBEDTLS_BYTE_0( total_dn_size ); ret = mbedtls_ssl_write_handshake_msg( ssl ); @@ -3320,8 +3320,8 @@ static int ssl_write_server_key_exchange( mbedtls_ssl_context *ssl ) #if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED) if( signature_len != 0 ) { - ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len >> 8 ); - ssl->out_msg[ssl->out_msglen++] = (unsigned char)( signature_len ); + ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_1( signature_len ); + ssl->out_msg[ssl->out_msglen++] = MBEDTLS_BYTE_0( signature_len ); MBEDTLS_SSL_DEBUG_BUF( 3, "my signature", ssl->out_msg + ssl->out_msglen, diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 381eb9e03bb6..33f026f4d1d8 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -1487,8 +1487,8 @@ int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exch if( end - p < 2 ) return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); - *(p++) = (unsigned char)( psk_len >> 8 ); - *(p++) = (unsigned char)( psk_len ); + *(p++) = MBEDTLS_BYTE_1( psk_len ); + *(p++) = MBEDTLS_BYTE_0( psk_len ); if( end < p || (size_t)( end - p ) < psk_len ) return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); @@ -1528,8 +1528,8 @@ int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exch MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); return( ret ); } - *(p++) = (unsigned char)( len >> 8 ); - *(p++) = (unsigned char)( len ); + *(p++) = MBEDTLS_BYTE_1( len ); + *(p++) = MBEDTLS_BYTE_0( len ); p += len; MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); @@ -1550,8 +1550,8 @@ int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exch return( ret ); } - *(p++) = (unsigned char)( zlen >> 8 ); - *(p++) = (unsigned char)( zlen ); + *(p++) = MBEDTLS_BYTE_1( zlen ); + *(p++) = MBEDTLS_BYTE_0( zlen ); p += zlen; MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, @@ -1568,8 +1568,8 @@ int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exch if( end - p < 2 ) return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); - *(p++) = (unsigned char)( psk_len >> 8 ); - *(p++) = (unsigned char)( psk_len ); + *(p++) = MBEDTLS_BYTE_1( psk_len ); + *(p++) = MBEDTLS_BYTE_0( psk_len ); if( end < p || (size_t)( end - p ) < psk_len ) return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); @@ -1746,17 +1746,17 @@ int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl ) return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); } - ssl->out_msg[i ] = (unsigned char)( n >> 16 ); - ssl->out_msg[i + 1] = (unsigned char)( n >> 8 ); - ssl->out_msg[i + 2] = (unsigned char)( n ); + ssl->out_msg[i ] = MBEDTLS_BYTE_2( n ); + ssl->out_msg[i + 1] = MBEDTLS_BYTE_1( n ); + ssl->out_msg[i + 2] = MBEDTLS_BYTE_0( n ); i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n ); i += n; crt = crt->next; } - ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 ); - ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 ); - ssl->out_msg[6] = (unsigned char)( ( i - 7 ) ); + ssl->out_msg[4] = MBEDTLS_BYTE_2( i - 7 ); + ssl->out_msg[5] = MBEDTLS_BYTE_1( i - 7 ); + ssl->out_msg[6] = MBEDTLS_BYTE_0( i - 7 ); ssl->out_msglen = i; ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; diff --git a/library/x509write_crt.c b/library/x509write_crt.c index c8169f1feb99..0daf0683e913 100644 --- a/library/x509write_crt.c +++ b/library/x509write_crt.c @@ -251,8 +251,8 @@ int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx, return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE ); c = buf + 5; - ku[0] = (unsigned char)( key_usage ); - ku[1] = (unsigned char)( key_usage >> 8 ); + ku[0] = MBEDTLS_BYTE_0( key_usage ); + ku[1] = MBEDTLS_BYTE_1( key_usage ); ret = mbedtls_asn1_write_named_bitstring( &c, buf, ku, 9 ); if( ret < 0 ) From b6511b04fa8891a92fe45b42315ca9c4ec9439ac Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Fri, 16 Jul 2021 15:02:55 +0100 Subject: [PATCH 056/120] Replace instances of byte reading macros with PUT Instances of a group of byte reading macros which are equivilant to MBEDTLS_PUT_UINTx_yz Signed-off-by: Joe Subbiani --- library/chacha20.c | 5 +---- library/chachapoly.c | 18 ++---------------- library/ssl_cookie.c | 5 +---- 3 files changed, 4 insertions(+), 24 deletions(-) diff --git a/library/chacha20.c b/library/chacha20.c index 0e057f0e3c75..658f04690170 100644 --- a/library/chacha20.c +++ b/library/chacha20.c @@ -164,10 +164,7 @@ static void chacha20_block( const uint32_t initial_state[16], { size_t offset = i * 4U; - keystream[offset ] = MBEDTLS_BYTE_0( working_state[i] ); - keystream[offset + 1U] = MBEDTLS_BYTE_1( working_state[i] ); - keystream[offset + 2U] = MBEDTLS_BYTE_2( working_state[i] ); - keystream[offset + 3U] = MBEDTLS_BYTE_3( working_state[i] ); + MBEDTLS_PUT_UINT32_LE(working_state[i], keystream, offset); } mbedtls_platform_zeroize( working_state, sizeof( working_state ) ); diff --git a/library/chachapoly.c b/library/chachapoly.c index 696d97bf01bb..dc75b2030a48 100644 --- a/library/chachapoly.c +++ b/library/chachapoly.c @@ -263,22 +263,8 @@ int mbedtls_chachapoly_finish( mbedtls_chachapoly_context *ctx, /* The lengths of the AAD and ciphertext are processed by * Poly1305 as the final 128-bit block, encoded as little-endian integers. */ - len_block[ 0] = MBEDTLS_BYTE_0( ctx->aad_len ); - len_block[ 1] = MBEDTLS_BYTE_1( ctx->aad_len ); - len_block[ 2] = MBEDTLS_BYTE_2( ctx->aad_len ); - len_block[ 3] = MBEDTLS_BYTE_3( ctx->aad_len ); - len_block[ 4] = MBEDTLS_BYTE_4( ctx->aad_len ); - len_block[ 5] = MBEDTLS_BYTE_5( ctx->aad_len ); - len_block[ 6] = MBEDTLS_BYTE_6( ctx->aad_len ); - len_block[ 7] = MBEDTLS_BYTE_7( ctx->aad_len ); - len_block[ 8] = MBEDTLS_BYTE_0( ctx->ciphertext_len ); - len_block[ 9] = MBEDTLS_BYTE_1( ctx->ciphertext_len ); - len_block[10] = MBEDTLS_BYTE_2( ctx->ciphertext_len ); - len_block[11] = MBEDTLS_BYTE_3( ctx->ciphertext_len ); - len_block[12] = MBEDTLS_BYTE_4( ctx->ciphertext_len ); - len_block[13] = MBEDTLS_BYTE_5( ctx->ciphertext_len ); - len_block[14] = MBEDTLS_BYTE_6( ctx->ciphertext_len ); - len_block[15] = MBEDTLS_BYTE_7( ctx->ciphertext_len ); + MBEDTLS_PUT_UINT64_LE(ctx->aad_len, len_block, 0); + MBEDTLS_PUT_UINT64_LE(ctx->ciphertext_len, len_block, 8); ret = mbedtls_poly1305_update( &ctx->poly1305_ctx, len_block, 16U ); if( ret != 0 ) diff --git a/library/ssl_cookie.c b/library/ssl_cookie.c index 4f1c07bde6ef..986b1410b005 100644 --- a/library/ssl_cookie.c +++ b/library/ssl_cookie.c @@ -165,10 +165,7 @@ int mbedtls_ssl_cookie_write( void *p_ctx, t = ctx->serial++; #endif - (*p)[0] = MBEDTLS_BYTE_3( t ); - (*p)[1] = MBEDTLS_BYTE_2( t ); - (*p)[2] = MBEDTLS_BYTE_1( t ); - (*p)[3] = MBEDTLS_BYTE_0( t ); + MBEDTLS_PUT_UINT32_BE(t, *p, 0); *p += 4; #if defined(MBEDTLS_THREADING_C) From 4919bb46b02a7032518951d5ff5f9168a8876859 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Fri, 16 Jul 2021 17:14:07 +0100 Subject: [PATCH 057/120] Remove use of byte reading macro for uint16 Accidently used MBEDTLS_BYTE_16 for a uint16 variable Signed-off-by: Joe Subbiani --- library/poly1305.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/poly1305.c b/library/poly1305.c index 333aade94adf..9e90d67b1ac2 100644 --- a/library/poly1305.c +++ b/library/poly1305.c @@ -61,9 +61,9 @@ static uint64_t mul64( uint32_t a, uint32_t b ) { /* a = al + 2**16 ah, b = bl + 2**16 bh */ const uint16_t al = (uint16_t) a; - const uint16_t bl = (uint16_t) b; - const uint16_t ah = MBEDTLS_BYTE_2( a ); - const uint16_t bh = MBEDTLS_BYTE_2( b ); + const uint16_t bl = (uint16_t) b; + const uint16_t ah = a >> 16; + const uint16_t bh = b >> 16; /* ab = al*bl + 2**16 (ah*bl + bl*bh) + 2**32 ah*bh */ const uint32_t lo = (uint32_t) al * bl; From 51b147add0abc84098c680ad7eac86d6dd3e10d6 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Fri, 16 Jul 2021 17:47:17 +0100 Subject: [PATCH 058/120] Remove trailing white space Signed-off-by: Joe Subbiani --- library/poly1305.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/poly1305.c b/library/poly1305.c index 9e90d67b1ac2..1f35f1d5015e 100644 --- a/library/poly1305.c +++ b/library/poly1305.c @@ -61,7 +61,7 @@ static uint64_t mul64( uint32_t a, uint32_t b ) { /* a = al + 2**16 ah, b = bl + 2**16 bh */ const uint16_t al = (uint16_t) a; - const uint16_t bl = (uint16_t) b; + const uint16_t bl = (uint16_t) b; const uint16_t ah = a >> 16; const uint16_t bh = b >> 16; From 6dd73645533dfaf489906c5ee30e9bb816b652a6 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Mon, 19 Jul 2021 11:56:54 +0100 Subject: [PATCH 059/120] Replace instances of byte reading macros with PUT Instances of a group of byte reading macros which are equivilant to MBEDTLS_PUT_UINTx_yz Signed-off-by: Joe Subbiani --- library/common.h | 34 ++++++++++++++++++++++++++++++++++ library/poly1305.c | 20 ++++---------------- library/psa_its_file.c | 10 ++-------- library/ssl_cli.c | 15 +++++---------- library/ssl_msg.c | 21 +++++++-------------- library/ssl_srv.c | 34 ++++++++++------------------------ library/ssl_ticket.c | 3 +-- library/x509write_crt.c | 3 +-- 8 files changed, 64 insertions(+), 76 deletions(-) diff --git a/library/common.h b/library/common.h index ce2f04007ee7..3e8f88bf34b0 100644 --- a/library/common.h +++ b/library/common.h @@ -193,6 +193,40 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c } #endif +/** + * Get the unsigned 16 bits integer corresponding to two bytes in + * big-endian order (LSB first). + * + * \param data Base address of the memory to get the two bytes from. + * \param offset Offset from \p base of the first and most significant + * byte of the two bytes to build the 16 bits unsigned + * integer from. + */ +#ifndef MBEDTLS_GET_UINT16_BE +#define MBEDTLS_GET_UINT16_BE( data, offset ) \ + ( \ + ( (uint16_t) ( data )[( offset ) ] << 8 ) \ + | ( (uint16_t) ( data )[( offset ) + 1] ) \ + ) +#endif + +/** + * Put in memory a 16 bits unsigned integer in big-endian order. + * + * \param n 16 bits unsigned integer to put in memory. + * \param data Base address of the memory where to put the 16 + * bits unsigned integer in. + * \param offset Offset from \p base where to put the most significant + * byte of the 16 bits unsigned integer \p n. + */ +#ifndef MBEDTLS_PUT_UINT16_BE +#define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \ +{ \ + ( data )[( offset ) ] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ + ( data )[( offset ) + 1] = (unsigned char) ( ( (n) ) & 0xFF ); \ +} +#endif + /** * Get the unsigned 64 bits integer corresponding to eight bytes in * big-endian order (MSB first). diff --git a/library/poly1305.c b/library/poly1305.c index 1f35f1d5015e..7375a0c5728f 100644 --- a/library/poly1305.c +++ b/library/poly1305.c @@ -250,22 +250,10 @@ static void poly1305_compute_mac( const mbedtls_poly1305_context *ctx, acc3 += ctx->s[3] + (uint32_t) ( d >> 32U ); /* Compute MAC (128 least significant bits of the accumulator) */ - mac[ 0] = MBEDTLS_BYTE_0( acc0 ); - mac[ 1] = MBEDTLS_BYTE_1( acc0 ); - mac[ 2] = MBEDTLS_BYTE_2( acc0 ); - mac[ 3] = MBEDTLS_BYTE_3( acc0 ); - mac[ 4] = MBEDTLS_BYTE_0( acc1 ); - mac[ 5] = MBEDTLS_BYTE_1( acc1 ); - mac[ 6] = MBEDTLS_BYTE_2( acc1 ); - mac[ 7] = MBEDTLS_BYTE_3( acc1 ); - mac[ 8] = MBEDTLS_BYTE_0( acc2 ); - mac[ 9] = MBEDTLS_BYTE_1( acc2 ); - mac[10] = MBEDTLS_BYTE_2( acc2 ); - mac[11] = MBEDTLS_BYTE_3( acc2 ); - mac[12] = MBEDTLS_BYTE_0( acc3 ); - mac[13] = MBEDTLS_BYTE_1( acc3 ); - mac[14] = MBEDTLS_BYTE_2( acc3 ); - mac[15] = MBEDTLS_BYTE_3( acc3 ); + MBEDTLS_PUT_UINT32_LE( acc0, mac, 0 ); + MBEDTLS_PUT_UINT32_LE( acc1, mac, 4 ); + MBEDTLS_PUT_UINT32_LE( acc2, mac, 8 ); + MBEDTLS_PUT_UINT32_LE( acc3, mac, 12 ); } void mbedtls_poly1305_init( mbedtls_poly1305_context *ctx ) diff --git a/library/psa_its_file.c b/library/psa_its_file.c index c3b19a74ac8f..c4782cdba3fc 100644 --- a/library/psa_its_file.c +++ b/library/psa_its_file.c @@ -191,14 +191,8 @@ psa_status_t psa_its_set( psa_storage_uid_t uid, size_t n; memcpy( header.magic, PSA_ITS_MAGIC_STRING, PSA_ITS_MAGIC_LENGTH ); - header.size[0] = MBEDTLS_BYTE_0( data_length ); - header.size[1] = MBEDTLS_BYTE_1( data_length ); - header.size[2] = MBEDTLS_BYTE_2( data_length ); - header.size[3] = MBEDTLS_BYTE_3( data_length ); - header.flags[0] = MBEDTLS_BYTE_0( create_flags ); - header.flags[1] = MBEDTLS_BYTE_1( create_flags ); - header.flags[2] = MBEDTLS_BYTE_2( create_flags ); - header.flags[3] = MBEDTLS_BYTE_3( create_flags ); + MBEDTLS_PUT_UINT32_LE( data_length, header.size, 0 ); + MBEDTLS_PUT_UINT32_LE( create_flags, header.flags, 0 ); psa_its_fill_filename( uid, filename ); stream = fopen( PSA_ITS_STORAGE_TEMP, "wb" ); diff --git a/library/ssl_cli.c b/library/ssl_cli.c index 729784a6eb3d..3a0e6df6f4c2 100644 --- a/library/ssl_cli.c +++ b/library/ssl_cli.c @@ -703,12 +703,10 @@ static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl, *olen = p - buf; /* List length = olen - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */ - buf[4] = MBEDTLS_BYTE_1( *olen - 6 ); - buf[5] = MBEDTLS_BYTE_0( *olen - 6 ); + MBEDTLS_PUT_UINT16_BE( *olen - 6, buf, 4 ); /* Extension length = olen - 2 (ext_type) - 2 (ext_len) */ - buf[2] = MBEDTLS_BYTE_1( *olen - 4 ); - buf[3] = MBEDTLS_BYTE_0( *olen - 4 ); + MBEDTLS_PUT_UINT16_BE( *olen - 4, buf, 2 ); return( 0 ); } @@ -2745,8 +2743,7 @@ static int ssl_write_encrypted_pms( mbedtls_ssl_context *ssl, #if defined(MBEDTLS_SSL_PROTO_TLS1_2) if( len_bytes == 2 ) { - ssl->out_msg[offset+0] = MBEDTLS_BYTE_1( *olen ); - ssl->out_msg[offset+1] = MBEDTLS_BYTE_0( *olen ); + MBEDTLS_PUT_UINT16_BE( *olen, ssl->out_msg, offset ); *olen += 2; } #endif @@ -3503,8 +3500,7 @@ static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl ) */ content_len = mbedtls_dhm_get_len( &ssl->handshake->dhm_ctx ); - ssl->out_msg[4] = MBEDTLS_BYTE_1( content_len ); - ssl->out_msg[5] = MBEDTLS_BYTE_0( content_len ); + MBEDTLS_PUT_UINT16_BE( content_len, ssl->out_msg, 4 ); header_len = 6; ret = mbedtls_dhm_make_public( &ssl->handshake->dhm_ctx, @@ -4054,8 +4050,7 @@ static int ssl_write_certificate_verify( mbedtls_ssl_context *ssl ) return( ret ); } - ssl->out_msg[4 + offset] = MBEDTLS_BYTE_1( n ); - ssl->out_msg[5 + offset] = MBEDTLS_BYTE_0( n ); + MBEDTLS_PUT_UINT16_BE( n, ssl->out_msg, offset + 4 ); ssl->out_msglen = 6 + n + offset; ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; diff --git a/library/ssl_msg.c b/library/ssl_msg.c index 304e7f22b7a1..2fe801a283d9 100644 --- a/library/ssl_msg.c +++ b/library/ssl_msg.c @@ -454,15 +454,13 @@ static void ssl_extract_add_data_from_record( unsigned char* add_data, *cur = rec->cid_len; cur++; - cur[0] = MBEDTLS_CHAR_1( ad_len_field ); - cur[1] = MBEDTLS_CHAR_0( ad_len_field ); + MBEDTLS_PUT_UINT16_BE( ad_len_field, cur, 0 ); cur += 2; } else #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ { - cur[0] = MBEDTLS_CHAR_1( ad_len_field ); - cur[1] = MBEDTLS_CHAR_0( ad_len_field ); + MBEDTLS_PUT_UINT16_BE( ad_len_field, cur, 0 ); cur += 2; } @@ -2481,8 +2479,7 @@ int mbedtls_ssl_write_handshake_msg_ext( mbedtls_ssl_context *ssl, /* Write message_seq and update it, except for HelloRequest */ if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST ) { - ssl->out_msg[4] = MBEDTLS_BYTE_1( ssl->handshake->out_msg_seq ); - ssl->out_msg[5] = MBEDTLS_BYTE_0( ssl->handshake->out_msg_seq ); + MBEDTLS_PUT_UINT16_BE( ssl->handshake->out_msg_seq, ssl->out_msg, 4 ); ++( ssl->handshake->out_msg_seq ); } else @@ -2566,8 +2563,7 @@ int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush ) ssl->conf->transport, ssl->out_hdr + 1 ); memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 ); - ssl->out_len[0] = MBEDTLS_BYTE_1( len ); - ssl->out_len[1] = MBEDTLS_BYTE_0( len ); + MBEDTLS_PUT_UINT16_BE( len, ssl->out_len, 0); if( ssl->transform_out != NULL ) { @@ -2607,8 +2603,7 @@ int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush ) memcpy( ssl->out_cid, rec.cid, rec.cid_len ); #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ ssl->out_msglen = len = rec.data_len; - ssl->out_len[0] = MBEDTLS_BYTE_1( rec.data_len ); - ssl->out_len[1] = MBEDTLS_BYTE_0( rec.data_len ); + MBEDTLS_PUT_UINT16_BE( rec.data_len, ssl->out_len, 0 ); } protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl ); @@ -3184,8 +3179,7 @@ static int ssl_check_dtls_clihlo_cookie( obuf[15] = obuf[23] = MBEDTLS_BYTE_1( *olen - 25 ); obuf[16] = obuf[24] = MBEDTLS_BYTE_0( *olen - 25 ); - obuf[11] = MBEDTLS_BYTE_1( *olen - 13 ); - obuf[12] = MBEDTLS_BYTE_0( *olen - 13 ); + MBEDTLS_PUT_UINT16_BE( *olen - 13, obuf, 11 ); return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED ); } @@ -4565,8 +4559,7 @@ static int ssl_get_next_record( mbedtls_ssl_context *ssl ) ssl->in_hdr[0] = rec.type; ssl->in_msg = rec.buf + rec.data_offset; ssl->in_msglen = rec.data_len; - ssl->in_len[0] = MBEDTLS_BYTE_1( rec.data_len ); - ssl->in_len[1] = MBEDTLS_BYTE_0( rec.data_len ); + MBEDTLS_PUT_UINT16_BE( rec.data_len, ssl->in_len, 0 ); return( 0 ); } diff --git a/library/ssl_srv.c b/library/ssl_srv.c index 232846ff08d9..a791b807642f 100644 --- a/library/ssl_srv.c +++ b/library/ssl_srv.c @@ -2238,16 +2238,13 @@ static void ssl_write_alpn_ext( mbedtls_ssl_context *ssl, * 6 . 6 protocol name length * 7 . 7+n protocol name */ - buf[0] = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_ALPN ); - buf[1] = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_ALPN ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, buf, 0); *olen = 7 + strlen( ssl->alpn_chosen ); - buf[2] = MBEDTLS_BYTE_1( *olen - 4 ); - buf[3] = MBEDTLS_BYTE_0( *olen - 4 ); + MBEDTLS_PUT_UINT16_BE( *olen - 4, buf, 2 ); - buf[4] = MBEDTLS_BYTE_1( *olen - 6 ); - buf[5] = MBEDTLS_BYTE_0( *olen - 6 ); + MBEDTLS_PUT_UINT16_BE( *olen - 6, buf, 4 ); buf[6] = MBEDTLS_BYTE_0( *olen - 7 ); @@ -2294,15 +2291,13 @@ static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, } /* extension */ - buf[0] = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_USE_SRTP ); - buf[1] = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_USE_SRTP ); + MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_USE_SRTP, buf, 0 ); /* * total length 5 and mki value: only one profile(2 bytes) * and length(2 bytes) and srtp_mki ) */ ext_len = 5 + mki_len; - buf[2] = MBEDTLS_BYTE_1( ext_len ); - buf[3] = MBEDTLS_BYTE_0( ext_len ); + MBEDTLS_PUT_UINT16_BE( ext_len, buf, 2 ); /* protection profile length: 2 */ buf[4] = 0x00; @@ -2311,8 +2306,7 @@ static void ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, ssl->dtls_srtp_info.chosen_dtls_srtp_profile ); if( profile_value != MBEDTLS_TLS_SRTP_UNSET ) { - buf[6] = MBEDTLS_BYTE_1( profile_value ); - buf[7] = MBEDTLS_BYTE_0( profile_value ); + MBEDTLS_PUT_UINT16_BE( profile_value, buf, 6 ); } else { @@ -2785,8 +2779,7 @@ static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) #endif } - p[0] = MBEDTLS_BYTE_1( sa_len ); - p[1] = MBEDTLS_BYTE_0( sa_len ); + MBEDTLS_PUT_UINT16_BE( sa_len, p, 0 ); sa_len += 2; p += sa_len; } @@ -2841,8 +2834,7 @@ static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) ssl->out_msglen = p - buf; ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE_REQUEST; - ssl->out_msg[4 + ct_len + sa_len] = MBEDTLS_BYTE_1( total_dn_size ); - ssl->out_msg[5 + ct_len + sa_len] = MBEDTLS_BYTE_0( total_dn_size ); + MBEDTLS_PUT_UINT16_BE( total_dn_size, ssl->out_msg, 4 + ct_len + sa_len ); ret = mbedtls_ssl_write_handshake_msg( ssl ); @@ -4223,14 +4215,8 @@ static int ssl_write_new_session_ticket( mbedtls_ssl_context *ssl ) tlen = 0; } - ssl->out_msg[4] = MBEDTLS_BYTE_3( lifetime ); - ssl->out_msg[5] = MBEDTLS_BYTE_2( lifetime ); - ssl->out_msg[6] = MBEDTLS_BYTE_1( lifetime ); - ssl->out_msg[7] = MBEDTLS_BYTE_0( lifetime ); - - ssl->out_msg[8] = MBEDTLS_BYTE_1( tlen ); - ssl->out_msg[9] = MBEDTLS_BYTE_0( tlen ); - + MBEDTLS_PUT_UINT32_BE( lifetime, ssl->out_msg, 4 ); + MBEDTLS_PUT_UINT16_BE( tlen, ssl->out_msg, 8 ); ssl->out_msglen = 10 + tlen; /* diff --git a/library/ssl_ticket.c b/library/ssl_ticket.c index a7a55f1a7517..bce9a1cd71a9 100644 --- a/library/ssl_ticket.c +++ b/library/ssl_ticket.c @@ -245,8 +245,7 @@ int mbedtls_ssl_ticket_write( void *p_ticket, { goto cleanup; } - state_len_bytes[0] = MBEDTLS_BYTE_1( clear_len ); - state_len_bytes[1] = MBEDTLS_BYTE_0( clear_len ); + MBEDTLS_PUT_UINT16_BE( clear_len, state_len_bytes, 0 ); /* Encrypt and authenticate */ if( ( ret = mbedtls_cipher_auth_encrypt_ext( &key->ctx, diff --git a/library/x509write_crt.c b/library/x509write_crt.c index 0daf0683e913..17b3e7966ba0 100644 --- a/library/x509write_crt.c +++ b/library/x509write_crt.c @@ -251,8 +251,7 @@ int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx, return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE ); c = buf + 5; - ku[0] = MBEDTLS_BYTE_0( key_usage ); - ku[1] = MBEDTLS_BYTE_1( key_usage ); + MBEDTLS_PUT_UINT16_LE( key_usage, ku, 0 ); ret = mbedtls_asn1_write_named_bitstring( &c, buf, ku, 9 ); if( ret < 0 ) From 5241e343ded7655e4baa95a493002ec93ae458c1 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Mon, 19 Jul 2021 15:29:18 +0100 Subject: [PATCH 060/120] Improve consitency throughout library/common.h Replace the contents of MBEDTLS_PUT_UINTx_yz contained inconsitent but similar/duplicate code to the MBEDTLS_BYTE_x macros. Therefore the contents of the macros now utilise the byte reading macros. MBEDTLS_PUT_UINT64_LE's written order was also not consitent with the other PUT macros, so that was modified. Documentation comment said LSB instead of MSB and that has also been resolved. Signed-off-by: Joe Subbiani --- library/common.h | 166 +++++++++++++++++++++++------------------------ 1 file changed, 83 insertions(+), 83 deletions(-) diff --git a/library/common.h b/library/common.h index 3e8f88bf34b0..ba0396c58cb6 100644 --- a/library/common.h +++ b/library/common.h @@ -93,12 +93,12 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c * integer from. */ #ifndef MBEDTLS_GET_UINT32_BE -#define MBEDTLS_GET_UINT32_BE( data , offset ) \ - ( \ - ( (uint32_t) ( data )[( offset ) ] << 24 ) \ - | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \ - | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \ - | ( (uint32_t) ( data )[( offset ) + 3] ) \ +#define MBEDTLS_GET_UINT32_BE( data , offset ) \ + ( \ + ( (uint32_t) ( data )[( offset ) ] << 24 ) \ + | ( (uint32_t) ( data )[( offset ) + 1] << 16 ) \ + | ( (uint32_t) ( data )[( offset ) + 2] << 8 ) \ + | ( (uint32_t) ( data )[( offset ) + 3] ) \ ) #endif @@ -112,13 +112,13 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c * byte of the 32 bits unsigned integer \p n. */ #ifndef MBEDTLS_PUT_UINT32_BE -#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \ - do { \ - ( data )[( offset ) ] = (unsigned char) ( (n) >> 24 ); \ - ( data )[( offset ) + 1] = (unsigned char) ( (n) >> 16 ); \ - ( data )[( offset ) + 2] = (unsigned char) ( (n) >> 8 ); \ - ( data )[( offset ) + 3] = (unsigned char) ( (n) ); \ - } while( 0 ) +#define MBEDTLS_PUT_UINT32_BE( n, data, offset ) \ +{ \ + ( data )[( offset ) ] = MBEDTLS_BYTE_3( n ); \ + ( data )[( offset ) + 1] = MBEDTLS_BYTE_2( n ); \ + ( data )[( offset ) + 2] = MBEDTLS_BYTE_1( n ); \ + ( data )[( offset ) + 3] = MBEDTLS_BYTE_0( n ); \ +} #endif /** @@ -131,12 +131,12 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c * integer from. */ #ifndef MBEDTLS_GET_UINT32_LE -#define MBEDTLS_GET_UINT32_LE( data, offset ) \ - ( \ - ( (uint32_t) ( data )[( offset ) ] ) \ - | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \ - | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \ - | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \ +#define MBEDTLS_GET_UINT32_LE( data, offset ) \ + ( \ + ( (uint32_t) ( data )[( offset ) ] ) \ + | ( (uint32_t) ( data )[( offset ) + 1] << 8 ) \ + | ( (uint32_t) ( data )[( offset ) + 2] << 16 ) \ + | ( (uint32_t) ( data )[( offset ) + 3] << 24 ) \ ) #endif @@ -150,13 +150,13 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c * byte of the 32 bits unsigned integer \p n. */ #ifndef MBEDTLS_PUT_UINT32_LE -#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \ - do { \ - ( data )[( offset ) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ - ( data )[( offset ) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ - ( data )[( offset ) + 2] = (unsigned char) ( ( (n) >> 16 ) & 0xFF ); \ - ( data )[( offset ) + 3] = (unsigned char) ( ( (n) >> 24 ) & 0xFF ); \ - } while( 0 ) +#define MBEDTLS_PUT_UINT32_LE( n, data, offset ) \ +{ \ + ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \ + ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \ + ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \ + ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \ +} #endif /** @@ -169,10 +169,10 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c * integer from. */ #ifndef MBEDTLS_GET_UINT16_LE -#define MBEDTLS_GET_UINT16_LE( data, offset ) \ - ( \ - ( (uint16_t) ( data )[( offset ) ] ) \ - | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \ +#define MBEDTLS_GET_UINT16_LE( data, offset ) \ + ( \ + ( (uint16_t) ( data )[( offset ) ] ) \ + | ( (uint16_t) ( data )[( offset ) + 1] << 8 ) \ ) #endif @@ -186,16 +186,16 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c * byte of the 16 bits unsigned integer \p n. */ #ifndef MBEDTLS_PUT_UINT16_LE -#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \ -{ \ - ( data )[( offset ) ] = (unsigned char) ( ( (n) ) & 0xFF ); \ - ( data )[( offset ) + 1] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ +#define MBEDTLS_PUT_UINT16_LE( n, data, offset ) \ +{ \ + ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \ + ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \ } #endif /** * Get the unsigned 16 bits integer corresponding to two bytes in - * big-endian order (LSB first). + * big-endian order (MSB first). * * \param data Base address of the memory to get the two bytes from. * \param offset Offset from \p base of the first and most significant @@ -203,10 +203,10 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c * integer from. */ #ifndef MBEDTLS_GET_UINT16_BE -#define MBEDTLS_GET_UINT16_BE( data, offset ) \ - ( \ - ( (uint16_t) ( data )[( offset ) ] << 8 ) \ - | ( (uint16_t) ( data )[( offset ) + 1] ) \ +#define MBEDTLS_GET_UINT16_BE( data, offset ) \ + ( \ + ( (uint16_t) ( data )[( offset ) ] << 8 ) \ + | ( (uint16_t) ( data )[( offset ) + 1] ) \ ) #endif @@ -220,10 +220,10 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c * byte of the 16 bits unsigned integer \p n. */ #ifndef MBEDTLS_PUT_UINT16_BE -#define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \ -{ \ - ( data )[( offset ) ] = (unsigned char) ( ( (n) >> 8 ) & 0xFF ); \ - ( data )[( offset ) + 1] = (unsigned char) ( ( (n) ) & 0xFF ); \ +#define MBEDTLS_PUT_UINT16_BE( n, data, offset ) \ +{ \ + ( data )[( offset ) ] = MBEDTLS_BYTE_1( n ); \ + ( data )[( offset ) + 1] = MBEDTLS_BYTE_0( n ); \ } #endif @@ -237,16 +237,16 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c * integer from. */ #ifndef MBEDTLS_GET_UINT64_BE -#define MBEDTLS_GET_UINT64_BE( data, offset ) \ - ( \ - ( (uint64_t) ( data )[( offset ) ] << 56 ) \ - | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \ - | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \ - | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \ - | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \ - | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \ - | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \ - | ( (uint64_t) ( data )[( offset ) + 7] ) \ +#define MBEDTLS_GET_UINT64_BE( data, offset ) \ + ( \ + ( (uint64_t) ( data )[( offset ) ] << 56 ) \ + | ( (uint64_t) ( data )[( offset ) + 1] << 48 ) \ + | ( (uint64_t) ( data )[( offset ) + 2] << 40 ) \ + | ( (uint64_t) ( data )[( offset ) + 3] << 32 ) \ + | ( (uint64_t) ( data )[( offset ) + 4] << 24 ) \ + | ( (uint64_t) ( data )[( offset ) + 5] << 16 ) \ + | ( (uint64_t) ( data )[( offset ) + 6] << 8 ) \ + | ( (uint64_t) ( data )[( offset ) + 7] ) \ ) #endif @@ -260,16 +260,16 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c * byte of the 64 bits unsigned integer \p n. */ #ifndef MBEDTLS_PUT_UINT64_BE -#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \ -{ \ - ( data )[( offset ) ] = (unsigned char) ( (n) >> 56 ); \ - ( data )[( offset ) + 1] = (unsigned char) ( (n) >> 48 ); \ - ( data )[( offset ) + 2] = (unsigned char) ( (n) >> 40 ); \ - ( data )[( offset ) + 3] = (unsigned char) ( (n) >> 32 ); \ - ( data )[( offset ) + 4] = (unsigned char) ( (n) >> 24 ); \ - ( data )[( offset ) + 5] = (unsigned char) ( (n) >> 16 ); \ - ( data )[( offset ) + 6] = (unsigned char) ( (n) >> 8 ); \ - ( data )[( offset ) + 7] = (unsigned char) ( (n) ); \ +#define MBEDTLS_PUT_UINT64_BE( n, data, offset ) \ +{ \ + ( data )[( offset ) ] = MBEDTLS_BYTE_7( n ); \ + ( data )[( offset ) + 1] = MBEDTLS_BYTE_6( n ); \ + ( data )[( offset ) + 2] = MBEDTLS_BYTE_5( n ); \ + ( data )[( offset ) + 3] = MBEDTLS_BYTE_4( n ); \ + ( data )[( offset ) + 4] = MBEDTLS_BYTE_3( n ); \ + ( data )[( offset ) + 5] = MBEDTLS_BYTE_2( n ); \ + ( data )[( offset ) + 6] = MBEDTLS_BYTE_1( n ); \ + ( data )[( offset ) + 7] = MBEDTLS_BYTE_0( n ); \ } #endif @@ -283,16 +283,16 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c * integer from. */ #ifndef MBEDTLS_GET_UINT64_LE -#define MBEDTLS_GET_UINT64_LE( data, offset ) \ - ( \ - ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \ - | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \ - | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \ - | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \ - | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \ - | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \ - | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \ - | ( (uint64_t) ( data )[( offset ) ] ) \ +#define MBEDTLS_GET_UINT64_LE( data, offset ) \ + ( \ + ( (uint64_t) ( data )[( offset ) + 7] << 56 ) \ + | ( (uint64_t) ( data )[( offset ) + 6] << 48 ) \ + | ( (uint64_t) ( data )[( offset ) + 5] << 40 ) \ + | ( (uint64_t) ( data )[( offset ) + 4] << 32 ) \ + | ( (uint64_t) ( data )[( offset ) + 3] << 24 ) \ + | ( (uint64_t) ( data )[( offset ) + 2] << 16 ) \ + | ( (uint64_t) ( data )[( offset ) + 1] << 8 ) \ + | ( (uint64_t) ( data )[( offset ) ] ) \ ) #endif @@ -306,16 +306,16 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c * byte of the 64 bits unsigned integer \p n. */ #ifndef MBEDTLS_PUT_UINT64_LE -#define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \ -{ \ - ( data )[( offset ) + 7] = (unsigned char) ( (n) >> 56 ); \ - ( data )[( offset ) + 6] = (unsigned char) ( (n) >> 48 ); \ - ( data )[( offset ) + 5] = (unsigned char) ( (n) >> 40 ); \ - ( data )[( offset ) + 4] = (unsigned char) ( (n) >> 32 ); \ - ( data )[( offset ) + 3] = (unsigned char) ( (n) >> 24 ); \ - ( data )[( offset ) + 2] = (unsigned char) ( (n) >> 16 ); \ - ( data )[( offset ) + 1] = (unsigned char) ( (n) >> 8 ); \ - ( data )[( offset ) ] = (unsigned char) ( (n) ); \ +#define MBEDTLS_PUT_UINT64_LE( n, data, offset ) \ +{ \ + ( data )[( offset ) ] = MBEDTLS_BYTE_0( n ); \ + ( data )[( offset ) + 1] = MBEDTLS_BYTE_1( n ); \ + ( data )[( offset ) + 2] = MBEDTLS_BYTE_2( n ); \ + ( data )[( offset ) + 3] = MBEDTLS_BYTE_3( n ); \ + ( data )[( offset ) + 4] = MBEDTLS_BYTE_4( n ); \ + ( data )[( offset ) + 5] = MBEDTLS_BYTE_5( n ); \ + ( data )[( offset ) + 6] = MBEDTLS_BYTE_6( n ); \ + ( data )[( offset ) + 7] = MBEDTLS_BYTE_7( n ); \ } #endif From d0687856afdc603112b39edf5443dfe539eaee40 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Wed, 21 Jul 2021 15:22:47 +0100 Subject: [PATCH 061/120] Improve documentation and add more uses of MBEDTLS_PUT minor changes, such as improving the documentation for the byte reading macros, and using MBEDTLS_PUT_UINT16_xy in place of byte reading macro combinations Signed-off-by: Joe Subbiani --- library/ccm.c | 3 +-- library/common.h | 5 ++--- library/ecp.c | 3 +-- library/ssl_cli.c | 2 +- 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/library/ccm.c b/library/ccm.c index 0188075f5ea3..a21a37f55f3e 100644 --- a/library/ccm.c +++ b/library/ccm.c @@ -221,8 +221,7 @@ static int ccm_auth_crypt( mbedtls_ccm_context *ctx, int mode, size_t length, src = add; memset( b, 0, 16 ); - b[0] = MBEDTLS_BYTE_1( add_len ); - b[1] = MBEDTLS_BYTE_0( add_len ); + MBEDTLS_PUT_UINT16_BE( add_len, b, 0 ); use_len = len_left < 16 - 2 ? len_left : 16 - 2; memcpy( b + 2, src, use_len ); diff --git a/library/common.h b/library/common.h index ba0396c58cb6..d740c7338ccd 100644 --- a/library/common.h +++ b/library/common.h @@ -70,9 +70,8 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c /** Byte Reading Macros * - * Obtain the most significant byte of x using 0xff - * Using MBEDTLS_BYTE_a will shift a*8 bits - * to retrieve the next byte of information + * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th + * byte from x, where byte 0 is the least significant byte. */ #define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) ) #define MBEDTLS_BYTE_1( x ) ( (uint8_t) ( ( ( x ) >> 8 ) & 0xff ) ) diff --git a/library/ecp.c b/library/ecp.c index fdfc960069da..0212069c8389 100644 --- a/library/ecp.c +++ b/library/ecp.c @@ -1012,8 +1012,7 @@ int mbedtls_ecp_tls_write_group( const mbedtls_ecp_group *grp, size_t *olen, /* * Next two bytes are the namedcurve value */ - buf[0] = MBEDTLS_BYTE_1( curve_info->tls_id ); - buf[1] = MBEDTLS_BYTE_0( curve_info->tls_id ); + MBEDTLS_PUT_UINT16_BE( curve_info->tls_id, buf, 0 ); return( 0 ); } diff --git a/library/ssl_cli.c b/library/ssl_cli.c index 3a0e6df6f4c2..0e25b6c60abb 100644 --- a/library/ssl_cli.c +++ b/library/ssl_cli.c @@ -762,7 +762,7 @@ static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_USE_SRTP ); - *p++ = MBEDTLS_BYTE_1( ext_len & 0xFF00 ); + *p++ = MBEDTLS_BYTE_1( ext_len ); *p++ = MBEDTLS_BYTE_0( ext_len ); /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */ From 9ab1866b0d0adace1c6c7ac65e0d8c586ad5a320 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Wed, 21 Jul 2021 16:35:48 +0100 Subject: [PATCH 062/120] Remove trailing whitespace Signed-off-by: Joe Subbiani --- library/common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/common.h b/library/common.h index d740c7338ccd..780ce378dece 100644 --- a/library/common.h +++ b/library/common.h @@ -70,7 +70,7 @@ extern void (*mbedtls_test_hook_test_fail)( const char * test, int line, const c /** Byte Reading Macros * - * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th + * Given a multi-byte integer \p x, MBEDTLS_BYTE_n retrieves the n-th * byte from x, where byte 0 is the least significant byte. */ #define MBEDTLS_BYTE_0( x ) ( (uint8_t) ( ( x ) & 0xff ) ) From ce0e6a9dea61fa7153c189b2c3b3a07a82e322db Mon Sep 17 00:00:00 2001 From: Mateusz Starzyk Date: Tue, 17 Aug 2021 15:24:32 +0200 Subject: [PATCH 063/120] Require MESSAGE flag in PSA MAC setup. Signed-off-by: Mateusz Starzyk --- library/psa_crypto.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/library/psa_crypto.c b/library/psa_crypto.c index 3574b9842acd..f87323c49723 100644 --- a/library/psa_crypto.c +++ b/library/psa_crypto.c @@ -2329,7 +2329,7 @@ static psa_status_t psa_mac_setup( psa_mac_operation_t *operation, status = psa_get_and_lock_key_slot_with_policy( key, &slot, - is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH, + is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, alg ); if( status != PSA_SUCCESS ) goto exit; @@ -2514,8 +2514,9 @@ static psa_status_t psa_mac_compute_internal( mbedtls_svc_key_id_t key, uint8_t operation_mac_size = 0; status = psa_get_and_lock_key_slot_with_policy( - key, &slot, - is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH, + key, + &slot, + is_sign ? PSA_KEY_USAGE_SIGN_MESSAGE : PSA_KEY_USAGE_VERIFY_MESSAGE, alg ); if( status != PSA_SUCCESS ) goto exit; From cb0a7cd142257658c116a10c9d2926ba1b46bab6 Mon Sep 17 00:00:00 2001 From: Mateusz Starzyk Date: Thu, 19 Aug 2021 15:11:50 +0200 Subject: [PATCH 064/120] Fix mac_key_policy test function Signed-off-by: Mateusz Starzyk --- tests/suites/test_suite_psa_crypto.function | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 150a3f43e393..3fe512e8e707 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -818,7 +818,7 @@ void mac_key_policy( int policy_usage_arg, mbedtls_test_update_key_usage_flags( policy_usage ) ); status = psa_mac_sign_setup( &operation, key, exercise_alg ); - if( ( policy_usage & PSA_KEY_USAGE_SIGN_HASH ) == 0 ) + if( ( mbedtls_test_update_key_usage_flags(policy_usage) & PSA_KEY_USAGE_SIGN_MESSAGE ) == 0 ) TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); else TEST_EQUAL( status, expected_status ); @@ -827,7 +827,7 @@ void mac_key_policy( int policy_usage_arg, memset( mac, 0, sizeof( mac ) ); status = psa_mac_verify_setup( &operation, key, exercise_alg ); - if( ( policy_usage & PSA_KEY_USAGE_VERIFY_HASH ) == 0 ) + if( ( mbedtls_test_update_key_usage_flags(policy_usage) & PSA_KEY_USAGE_VERIFY_MESSAGE ) == 0 ) TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); else TEST_EQUAL( status, expected_status ); From 1f6c3aeb63a40c22af17311ea3a8cf6e3f02e554 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Fri, 20 Aug 2021 11:44:44 +0100 Subject: [PATCH 065/120] Tidy up ssl_*.c grouped MBEDTLS_BYTE_x macros exchange groups of the byte reading macros with MBEDTLS_PUT_UINTxyz and then shift the pointer afterwards. Easier to read as you can see how big the data is that you are putting in, and in the case of UINT32 AND UINT64 it saves some vertical space. Signed-off-by: Joe Subbiani --- library/ssl_cli.c | 121 +++++++++++++++++++++++----------------------- library/ssl_srv.c | 59 +++++++++++----------- library/ssl_tls.c | 64 ++++++++---------------- 3 files changed, 107 insertions(+), 137 deletions(-) diff --git a/library/ssl_cli.c b/library/ssl_cli.c index 0e25b6c60abb..df32cfdb6747 100644 --- a/library/ssl_cli.c +++ b/library/ssl_cli.c @@ -136,18 +136,19 @@ static int ssl_write_hostname_ext( mbedtls_ssl_context *ssl, * } ServerNameList; * */ - *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_SERVERNAME ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SERVERNAME, p, 0 ); + p += 2; - *p++ = MBEDTLS_BYTE_1( hostname_len + 5); - *p++ = MBEDTLS_BYTE_0( hostname_len + 5); + MBEDTLS_PUT_UINT16_BE( hostname_len + 5, p, 0 ); + p += 2; - *p++ = MBEDTLS_BYTE_1( hostname_len + 3 ); - *p++ = MBEDTLS_BYTE_0( hostname_len + 3 ); + MBEDTLS_PUT_UINT16_BE( hostname_len + 3, p, 0 ); + p += 2; *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME ); - *p++ = MBEDTLS_BYTE_1( hostname_len ); - *p++ = MBEDTLS_BYTE_0( hostname_len ); + + MBEDTLS_PUT_UINT16_BE( hostname_len, p, 0 ); + p += 2; memcpy( p, ssl->hostname, hostname_len ); @@ -181,8 +182,8 @@ static int ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl, /* * Secure renegotiation */ - *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 ); + p += 2; *p++ = 0x00; *p++ = MBEDTLS_BYTE_0( ssl->verify_data_len + 1 ); @@ -281,14 +282,14 @@ static int ssl_write_signature_algorithms_ext( mbedtls_ssl_context *ssl, * SignatureAndHashAlgorithm * supported_signature_algorithms<2..2^16-2>; */ - *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_SIG_ALG ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SIG_ALG ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, p, 0 ); + p += 2; - *p++ = MBEDTLS_BYTE_1( sig_alg_len + 2 ); - *p++ = MBEDTLS_BYTE_0( sig_alg_len + 2 ); + MBEDTLS_PUT_UINT16_BE( sig_alg_len + 2, p, 0 ); + p += 2; - *p++ = MBEDTLS_BYTE_1( sig_alg_len ); - *p++ = MBEDTLS_BYTE_0( sig_alg_len ); + MBEDTLS_PUT_UINT16_BE( sig_alg_len, p, 0 ); + p += 2; *olen = 6 + sig_alg_len; @@ -356,14 +357,14 @@ static int ssl_write_supported_elliptic_curves_ext( mbedtls_ssl_context *ssl, elliptic_curve_list[elliptic_curve_len++] = MBEDTLS_BYTE_0( info->tls_id ); } - *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES, p, 0 ); + p += 2; - *p++ = MBEDTLS_BYTE_1( elliptic_curve_len + 2 ); - *p++ = MBEDTLS_BYTE_0( elliptic_curve_len + 2 ); + MBEDTLS_PUT_UINT16_BE( elliptic_curve_len + 2, p, 0 ); + p += 2; - *p++ = MBEDTLS_BYTE_1( elliptic_curve_len ); - *p++ = MBEDTLS_BYTE_0( elliptic_curve_len ); + MBEDTLS_PUT_UINT16_BE( elliptic_curve_len, p, 0 ); + p += 2; *olen = 6 + elliptic_curve_len; @@ -384,8 +385,8 @@ static int ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl, ( "client hello, adding supported_point_formats extension" ) ); MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 ); - *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 ); + p += 2; *p++ = 0x00; *p++ = 2; @@ -421,8 +422,8 @@ static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 ); - *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 ); + p += 2; /* * We may need to send ClientHello multiple times for Hello verification. @@ -464,8 +465,8 @@ static int ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, memcpy( p + 2, ssl->handshake->ecjpake_cache, kkpp_len ); } - *p++ = MBEDTLS_BYTE_1( kkpp_len ); - *p++ = MBEDTLS_BYTE_0( kkpp_len ); + MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 ); + p += 2; *olen = kkpp_len + 4; @@ -504,11 +505,11 @@ static int ssl_write_cid_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_CHK_BUF_PTR( p, end, (unsigned)( ssl->own_cid_len + 5 ) ); /* Add extension ID + size */ - *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_CID ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_CID ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 ); + p += 2; ext_len = (size_t) ssl->own_cid_len + 1; - *p++ = MBEDTLS_BYTE_1( ext_len ); - *p++ = MBEDTLS_BYTE_0( ext_len ); + MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 ); + p += 2; *p++ = (uint8_t) ssl->own_cid_len; memcpy( p, ssl->own_cid, ssl->own_cid_len ); @@ -537,8 +538,8 @@ static int ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_CHK_BUF_PTR( p, end, 5 ); - *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 ); + p += 2; *p++ = 0x00; *p++ = 1; @@ -569,8 +570,8 @@ static int ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 ); - *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 ); + p += 2; *p++ = 0x00; *p++ = 0x00; @@ -599,8 +600,8 @@ static int ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 ); - *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 ); + p += 2; *p++ = 0x00; *p++ = 0x00; @@ -631,11 +632,11 @@ static int ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl, /* The addition is safe here since the ticket length is 16 bit. */ MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 + tlen ); - *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_SESSION_TICKET ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SESSION_TICKET ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 ); + p += 2; - *p++ = MBEDTLS_BYTE_1( tlen ); - *p++ = MBEDTLS_BYTE_0( tlen ); + MBEDTLS_PUT_UINT16_BE( tlen, p, 0 ); + p += 2; *olen = 4; @@ -675,8 +676,8 @@ static int ssl_write_alpn_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 + alpnlen ); - *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_ALPN ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_ALPN ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ALPN, p, 0 ); + p += 2; /* * opaque ProtocolName<1..2^8-1>; @@ -758,12 +759,11 @@ static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, */ MBEDTLS_SSL_CHK_BUF_PTR( p, end, ext_len + 4 ); - *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_USE_SRTP ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_USE_SRTP ); - + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_USE_SRTP, p, 0 ); + p += 2; - *p++ = MBEDTLS_BYTE_1( ext_len ); - *p++ = MBEDTLS_BYTE_0( ext_len ); + MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 ); + p += 2; /* protection profile length: 2*(ssl->conf->dtls_srtp_profile_list_len) */ /* micro-optimization: @@ -786,8 +786,9 @@ static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, { MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_write_use_srtp_ext, add profile: %04x", profile_value ) ); - *p++ = MBEDTLS_BYTE_1( profile_value ); - *p++ = MBEDTLS_BYTE_0( profile_value ); + MBEDTLS_PUT_UINT16_BE( profile_value, p, 0 ); + p += 2; + } else { @@ -855,10 +856,8 @@ static int ssl_generate_random( mbedtls_ssl_context *ssl ) #if defined(MBEDTLS_HAVE_TIME) t = mbedtls_time( NULL ); - *p++ = MBEDTLS_BYTE_3( t ); - *p++ = MBEDTLS_BYTE_2( t ); - *p++ = MBEDTLS_BYTE_1( t ); - *p++ = MBEDTLS_BYTE_0( t ); + MBEDTLS_PUT_UINT32_BE( t, p, 0 ); + p += 4; MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, current time: %" MBEDTLS_PRINTF_LONGLONG, (long long) t ) ); @@ -1141,8 +1140,8 @@ static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); n++; - *p++ = MBEDTLS_BYTE_1( ciphersuites[i] ); - *p++ = MBEDTLS_BYTE_0( ciphersuites[i] ); + MBEDTLS_PUT_UINT16_BE( ciphersuites[i], p, 0 ); + p += 2; } MBEDTLS_SSL_DEBUG_MSG( 3, @@ -1157,8 +1156,8 @@ static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) { MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding EMPTY_RENEGOTIATION_INFO_SCSV" ) ); MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); - *p++ = MBEDTLS_BYTE_1( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0 ); + p += 2; n++; } @@ -1321,8 +1320,8 @@ static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) { /* No need to check for space here, because the extension * writing functions already took care of that. */ - *p++ = MBEDTLS_BYTE_1( ext_len ); - *p++ = MBEDTLS_BYTE_0( ext_len ); + MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 ); + p += 2; p += ext_len; } diff --git a/library/ssl_srv.c b/library/ssl_srv.c index a791b807642f..c63af7a32a51 100644 --- a/library/ssl_srv.c +++ b/library/ssl_srv.c @@ -1970,12 +1970,11 @@ static void ssl_write_cid_ext( mbedtls_ssl_context *ssl, * opaque cid<0..2^8-1>; * } ConnectionId; */ - - *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_CID ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_CID ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_CID, p, 0 ); + p += 2; ext_len = (size_t) ssl->own_cid_len + 1; - *p++ = MBEDTLS_BYTE_1( ext_len ); - *p++ = MBEDTLS_BYTE_0( ext_len ); + MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 ); + p += 2; *p++ = (uint8_t) ssl->own_cid_len; memcpy( p, ssl->own_cid, ssl->own_cid_len ); @@ -2016,8 +2015,8 @@ static void ssl_write_encrypt_then_mac_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding encrypt then mac extension" ) ); - *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC, p, 0 ); + p += 2; *p++ = 0x00; *p++ = 0x00; @@ -2042,8 +2041,8 @@ static void ssl_write_extended_ms_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding extended master secret " "extension" ) ); - *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET, p, 0 ); + p += 2; *p++ = 0x00; *p++ = 0x00; @@ -2067,8 +2066,8 @@ static void ssl_write_session_ticket_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, adding session ticket extension" ) ); - *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_SESSION_TICKET ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SESSION_TICKET ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SESSION_TICKET, p, 0 ); + p += 2; *p++ = 0x00; *p++ = 0x00; @@ -2091,8 +2090,8 @@ static void ssl_write_renegotiation_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, secure renegotiation extension" ) ); - *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_RENEGOTIATION_INFO, p, 0 ); + p += 2; #if defined(MBEDTLS_SSL_RENEGOTIATION) if( ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ) @@ -2132,8 +2131,8 @@ static void ssl_write_max_fragment_length_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, max_fragment_length extension" ) ); - *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH, p, 0 ); + p += 2; *p++ = 0x00; *p++ = 1; @@ -2162,8 +2161,8 @@ static void ssl_write_supported_point_formats_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, supported_point_formats extension" ) ); - *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS, p, 0 ); + p += 2; *p++ = 0x00; *p++ = 2; @@ -2200,8 +2199,8 @@ static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, return; } - *p++ = MBEDTLS_BYTE_1( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ); - *p++ = MBEDTLS_BYTE_0( MBEDTLS_TLS_EXT_ECJPAKE_KKPP ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0 ); + p += 2; ret = mbedtls_ecjpake_write_round_one( &ssl->handshake->ecjpake_ctx, p + 2, end - p - 2, &kkpp_len, @@ -2212,8 +2211,8 @@ static void ssl_write_ecjpake_kkpp_ext( mbedtls_ssl_context *ssl, return; } - *p++ = MBEDTLS_BYTE_1( kkpp_len ); - *p++ = MBEDTLS_BYTE_0( kkpp_len ); + MBEDTLS_PUT_UINT16_BE( kkpp_len, p, 0 ); + p += 2; *olen = kkpp_len + 4; } @@ -2488,10 +2487,8 @@ static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) #if defined(MBEDTLS_HAVE_TIME) t = mbedtls_time( NULL ); - *p++ = MBEDTLS_BYTE_3( t ); - *p++ = MBEDTLS_BYTE_2( t ); - *p++ = MBEDTLS_BYTE_1( t ); - *p++ = MBEDTLS_BYTE_0( t ); + MBEDTLS_PUT_UINT32_BE( t, p, 0 ); + p += 4; MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, current time: %" MBEDTLS_PRINTF_LONGLONG, (long long) t ) ); @@ -2572,8 +2569,8 @@ static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) MBEDTLS_SSL_DEBUG_MSG( 3, ( "%s session has been resumed", ssl->handshake->resume ? "a" : "no" ) ); - *p++ = MBEDTLS_BYTE_1( ssl->session_negotiate->ciphersuite ); - *p++ = MBEDTLS_BYTE_0( ssl->session_negotiate->ciphersuite ); + MBEDTLS_PUT_UINT16_BE( ssl->session_negotiate->ciphersuite, p, 0 ); + p += 2; *p++ = MBEDTLS_BYTE_0( ssl->session_negotiate->compression ); MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: %s", @@ -2642,8 +2639,8 @@ static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) if( ext_len > 0 ) { - *p++ = MBEDTLS_BYTE_1( ext_len ); - *p++ = MBEDTLS_BYTE_0( ext_len ); + MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 ); + p += 2; p += ext_len; } @@ -2819,8 +2816,8 @@ static int ssl_write_certificate_request( mbedtls_ssl_context *ssl ) break; } - *p++ = MBEDTLS_BYTE_1( dn_size ); - *p++ = MBEDTLS_BYTE_0( dn_size ); + MBEDTLS_PUT_UINT16_BE( dn_size, p, 0 ); + p += 2; memcpy( p, crt->subject_raw.p, dn_size ); p += dn_size; diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 33f026f4d1d8..abf4c0f5f29a 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -4664,14 +4664,8 @@ static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, { start = (uint64_t) session->start; - *p++ = MBEDTLS_BYTE_7( start ); - *p++ = MBEDTLS_BYTE_6( start ); - *p++ = MBEDTLS_BYTE_5( start ); - *p++ = MBEDTLS_BYTE_4( start ); - *p++ = MBEDTLS_BYTE_3( start ); - *p++ = MBEDTLS_BYTE_2( start ); - *p++ = MBEDTLS_BYTE_1( start ); - *p++ = MBEDTLS_BYTE_0( start ); + MBEDTLS_PUT_UINT64_BE( start, p, 0 ); + p += 8; } #endif /* MBEDTLS_HAVE_TIME */ @@ -4687,8 +4681,8 @@ static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, if( used <= buf_len ) { - *p++ = MBEDTLS_BYTE_1( session->ciphersuite ); - *p++ = MBEDTLS_BYTE_0( session->ciphersuite ); + MBEDTLS_PUT_UINT16_BE( session->ciphersuite, p, 0 ); + p += 2; *p++ = MBEDTLS_BYTE_0( session->compression ); @@ -4699,10 +4693,8 @@ static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, memcpy( p, session->master, 48 ); p += 48; - *p++ = MBEDTLS_BYTE_3( session->verify_result ); - *p++ = MBEDTLS_BYTE_2( session->verify_result ); - *p++ = MBEDTLS_BYTE_1( session->verify_result ); - *p++ = MBEDTLS_BYTE_0( session->verify_result ); + MBEDTLS_PUT_UINT32_BE( session->verify_result, p, 0 ); + p += 4; } /* @@ -4772,10 +4764,8 @@ static size_t ssl_session_save_tls12( const mbedtls_ssl_session *session, p += session->ticket_len; } - *p++ = MBEDTLS_BYTE_3( session->ticket_lifetime ); - *p++ = MBEDTLS_BYTE_2( session->ticket_lifetime ); - *p++ = MBEDTLS_BYTE_1( session->ticket_lifetime ); - *p++ = MBEDTLS_BYTE_0( session->ticket_lifetime ); + MBEDTLS_PUT_UINT32_BE( session->ticket_lifetime, p, 0 ); + p += 4; } #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */ @@ -5713,10 +5703,8 @@ int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, used += 4 + session_len; if( used <= buf_len ) { - *p++ = MBEDTLS_BYTE_3( session_len ); - *p++ = MBEDTLS_BYTE_2( session_len ); - *p++ = MBEDTLS_BYTE_1( session_len ); - *p++ = MBEDTLS_BYTE_0( session_len ); + MBEDTLS_PUT_UINT32_BE( session_len, p, 0 ); + p += 4; ret = ssl_session_save( ssl->session, 1, p, session_len, &session_len ); @@ -5757,33 +5745,19 @@ int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, used += 4; if( used <= buf_len ) { - *p++ = MBEDTLS_BYTE_3( ssl->badmac_seen ); - *p++ = MBEDTLS_BYTE_2( ssl->badmac_seen ); - *p++ = MBEDTLS_BYTE_1( ssl->badmac_seen ); - *p++ = MBEDTLS_BYTE_0( ssl->badmac_seen ); + MBEDTLS_PUT_UINT32_BE( ssl->badmac_seen, p, 0 ); + p += 4; } #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY) used += 16; if( used <= buf_len ) { - *p++ = MBEDTLS_BYTE_7( ssl->in_window_top ); - *p++ = MBEDTLS_BYTE_6( ssl->in_window_top ); - *p++ = MBEDTLS_BYTE_5( ssl->in_window_top ); - *p++ = MBEDTLS_BYTE_4( ssl->in_window_top ); - *p++ = MBEDTLS_BYTE_3( ssl->in_window_top ); - *p++ = MBEDTLS_BYTE_2( ssl->in_window_top ); - *p++ = MBEDTLS_BYTE_1( ssl->in_window_top ); - *p++ = MBEDTLS_BYTE_0( ssl->in_window_top ); - - *p++ = MBEDTLS_BYTE_7( ssl->in_window ); - *p++ = MBEDTLS_BYTE_6( ssl->in_window ); - *p++ = MBEDTLS_BYTE_5( ssl->in_window ); - *p++ = MBEDTLS_BYTE_4( ssl->in_window ); - *p++ = MBEDTLS_BYTE_3( ssl->in_window ); - *p++ = MBEDTLS_BYTE_2( ssl->in_window ); - *p++ = MBEDTLS_BYTE_1( ssl->in_window ); - *p++ = MBEDTLS_BYTE_0( ssl->in_window ); + MBEDTLS_PUT_UINT64_BE( ssl->in_window_top, p, 0 ); + p += 8; + + MBEDTLS_PUT_UINT64_BE( ssl->in_window, p, 0 ); + p += 8; } #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */ @@ -5806,8 +5780,8 @@ int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl, used += 2; if( used <= buf_len ) { - *p++ = MBEDTLS_BYTE_1( ssl->mtu ); - *p++ = MBEDTLS_BYTE_0( ssl->mtu ); + MBEDTLS_PUT_UINT16_BE( ssl->mtu, p, 0 ); + p += 2; } #endif /* MBEDTLS_SSL_PROTO_DTLS */ From e4603eece9aa216190edb5edd0dd3f51cf47df48 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Fri, 20 Aug 2021 13:05:30 +0100 Subject: [PATCH 066/120] Compress byte reading macros in if statements exchange MBEDTLS_BYTE_x in if statements with MBEDTLS_GET_UINT16_BE Signed-off-by: Joe Subbiani --- library/ssl_srv.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/library/ssl_srv.c b/library/ssl_srv.c index c63af7a32a51..1841b557900d 100644 --- a/library/ssl_srv.c +++ b/library/ssl_srv.c @@ -1848,8 +1848,7 @@ static int ssl_parse_client_hello( mbedtls_ssl_context *ssl ) for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 ) for( i = 0; ciphersuites[i] != 0; i++ ) { - if( p[0] != MBEDTLS_BYTE_1( ciphersuites[i] ) || - p[1] != MBEDTLS_BYTE_0( ciphersuites[i] )) + if( MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i] ) continue; got_common_suite = 1; @@ -1865,8 +1864,7 @@ static int ssl_parse_client_hello( mbedtls_ssl_context *ssl ) for( i = 0; ciphersuites[i] != 0; i++ ) for( j = 0, p = buf + ciph_offset + 2; j < ciph_len; j += 2, p += 2 ) { - if( p[0] != MBEDTLS_BYTE_1( ciphersuites[i] ) || - p[1] != MBEDTLS_BYTE_0( ciphersuites[i] )) + if( MBEDTLS_GET_UINT16_BE(p, 0) != ciphersuites[i] ) continue; got_common_suite = 1; From 94180e708ff9496f7b3bc6bfe50bdaab7226ba34 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Fri, 20 Aug 2021 16:20:44 +0100 Subject: [PATCH 067/120] Minor coding style improvement Signed-off-by: Joe Subbiani --- library/ssl_cli.c | 4 +--- library/ssl_srv.c | 3 +-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/library/ssl_cli.c b/library/ssl_cli.c index df32cfdb6747..9120aa2f758e 100644 --- a/library/ssl_cli.c +++ b/library/ssl_cli.c @@ -788,7 +788,6 @@ static int ssl_write_use_srtp_ext( mbedtls_ssl_context *ssl, profile_value ) ); MBEDTLS_PUT_UINT16_BE( profile_value, p, 0 ); p += 2; - } else { @@ -1321,8 +1320,7 @@ static int ssl_write_client_hello( mbedtls_ssl_context *ssl ) /* No need to check for space here, because the extension * writing functions already took care of that. */ MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 ); - p += 2; - p += ext_len; + p += 2 + ext_len; } ssl->out_msglen = p - buf; diff --git a/library/ssl_srv.c b/library/ssl_srv.c index 1841b557900d..b8c431484659 100644 --- a/library/ssl_srv.c +++ b/library/ssl_srv.c @@ -2638,8 +2638,7 @@ static int ssl_write_server_hello( mbedtls_ssl_context *ssl ) if( ext_len > 0 ) { MBEDTLS_PUT_UINT16_BE( ext_len, p, 0 ); - p += 2; - p += ext_len; + p += 2 + ext_len; } ssl->out_msglen = p - buf; From 90cc33aad60b14a7425f751c0d0f2494ac2dd782 Mon Sep 17 00:00:00 2001 From: Andrey Starodubtsev Date: Mon, 23 Aug 2021 12:14:56 +0300 Subject: [PATCH 068/120] Misprint was fixed Signed-off-by: Andrey Starodubtsev --- include/mbedtls/ssl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/mbedtls/ssl.h b/include/mbedtls/ssl.h index f08fc89a5b5d..3cb896cb4c93 100644 --- a/include/mbedtls/ssl.h +++ b/include/mbedtls/ssl.h @@ -602,7 +602,7 @@ typedef int mbedtls_ssl_recv_t( void *ctx, * \param ctx Context for the receive callback (typically a file descriptor) * \param buf Buffer to write the received data to * \param len Length of the receive buffer - * \param timeout Maximum nomber of millisecondes to wait for data + * \param timeout Maximum number of milliseconds to wait for data * 0 means no timeout (potentially waiting forever) * * \return The callback must return the number of bytes received, From a5cb0d24d4737fa02ff9c0df315a582558c0ecc8 Mon Sep 17 00:00:00 2001 From: Joe Subbiani Date: Mon, 23 Aug 2021 11:35:25 +0100 Subject: [PATCH 069/120] Tidy up grouped MBEDTLS_BYTE_x macros exchange groups of the byte reading macros with MBEDTLS_PUT_UINTxyz and then shift the pointer afterwards. Easier to read as you can see how big the data is that you are putting in, and in the case of UINT32 AND UINT64 it saves some vertical space. Signed-off-by: Joe Subbiani --- library/ctr_drbg.c | 7 ++----- library/ecjpake.c | 11 +++-------- library/ssl_tls.c | 18 ++++++++---------- 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/library/ctr_drbg.c b/library/ctr_drbg.c index 68b32a366b69..93a7cdcd1ff2 100644 --- a/library/ctr_drbg.c +++ b/library/ctr_drbg.c @@ -152,11 +152,8 @@ static int block_cipher_df( unsigned char *output, * (Total is padded to a multiple of 16-bytes with zeroes) */ p = buf + MBEDTLS_CTR_DRBG_BLOCKSIZE; - *p++ = MBEDTLS_BYTE_3( data_len ); - *p++ = MBEDTLS_BYTE_2( data_len ); - *p++ = MBEDTLS_BYTE_1( data_len ); - *p++ = MBEDTLS_BYTE_0( data_len ); - p += 3; + MBEDTLS_PUT_UINT32_BE( data_len, p, 0); + p += 4 + 3; *p++ = MBEDTLS_CTR_DRBG_SEEDLEN; memcpy( p, data, data_len ); p[data_len] = 0x80; diff --git a/library/ecjpake.c b/library/ecjpake.c index a599b1ba485f..738a97719cba 100644 --- a/library/ecjpake.c +++ b/library/ecjpake.c @@ -180,10 +180,7 @@ static int ecjpake_write_len_point( unsigned char **p, if( ret != 0 ) return( ret ); - (*p)[0] = MBEDTLS_BYTE_3( len ); - (*p)[1] = MBEDTLS_BYTE_2( len ); - (*p)[2] = MBEDTLS_BYTE_1( len ); - (*p)[3] = MBEDTLS_BYTE_0( len ); + MBEDTLS_PUT_UINT32_BE( len, *p, 0 ); *p += 4 + len; @@ -223,10 +220,8 @@ static int ecjpake_hash( const mbedtls_md_info_t *md_info, if( end - p < 4 ) return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); - *p++ = MBEDTLS_BYTE_3( id_len ); - *p++ = MBEDTLS_BYTE_2( id_len ); - *p++ = MBEDTLS_BYTE_1( id_len ); - *p++ = MBEDTLS_BYTE_0( id_len ); + MBEDTLS_PUT_UINT32_BE( id_len, p, 0 ); + p += 4; if( end < p || (size_t)( end - p ) < id_len ) return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL ); diff --git a/library/ssl_tls.c b/library/ssl_tls.c index abf4c0f5f29a..5cd47e6ce513 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -1487,8 +1487,8 @@ int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exch if( end - p < 2 ) return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); - *(p++) = MBEDTLS_BYTE_1( psk_len ); - *(p++) = MBEDTLS_BYTE_0( psk_len ); + MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); + p += 2; if( end < p || (size_t)( end - p ) < psk_len ) return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); @@ -1528,9 +1528,8 @@ int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exch MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret ); return( ret ); } - *(p++) = MBEDTLS_BYTE_1( len ); - *(p++) = MBEDTLS_BYTE_0( len ); - p += len; + MBEDTLS_PUT_UINT16_BE( len, p, 0 ); + p += 2 + len; MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K ); } @@ -1550,9 +1549,8 @@ int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exch return( ret ); } - *(p++) = MBEDTLS_BYTE_1( zlen ); - *(p++) = MBEDTLS_BYTE_0( zlen ); - p += zlen; + MBEDTLS_PUT_UINT16_BE( zlen, p, 0 ); + p += 2 + zlen; MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx, MBEDTLS_DEBUG_ECDH_Z ); @@ -1568,8 +1566,8 @@ int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exch if( end - p < 2 ) return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); - *(p++) = MBEDTLS_BYTE_1( psk_len ); - *(p++) = MBEDTLS_BYTE_0( psk_len ); + MBEDTLS_PUT_UINT16_BE( psk_len, p, 0 ); + p += 2; if( end < p || (size_t)( end - p ) < psk_len ) return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); From d07f4fc30fec6dcb6d79313b13ebd928559eef24 Mon Sep 17 00:00:00 2001 From: Mateusz Starzyk Date: Tue, 24 Aug 2021 11:01:23 +0200 Subject: [PATCH 070/120] Use separate expected results for MAC sign and verify key policy. Signed-off-by: Mateusz Starzyk --- tests/suites/test_suite_psa_crypto.data | 54 ++++++++++----------- tests/suites/test_suite_psa_crypto.function | 16 +++--- 2 files changed, 33 insertions(+), 37 deletions(-) diff --git a/tests/suites/test_suite_psa_crypto.data b/tests/suites/test_suite_psa_crypto.data index e3e9a5225a43..bc4edb2b7bdd 100644 --- a/tests/suites/test_suite_psa_crypto.data +++ b/tests/suites/test_suite_psa_crypto.data @@ -418,111 +418,111 @@ key_attributes_init: PSA key policy: MAC, SIGN_HASH -> SIGN_HASH+MESSAGE depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH:PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_SUCCESS +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH:PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_SUCCESS:PSA_ERROR_NOT_PERMITTED PSA key policy: MAC, VERIFY_HASH -> VERIFY_HASH+MESSAGE depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_VERIFY_HASH:PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_SUCCESS +mac_key_policy:PSA_KEY_USAGE_VERIFY_HASH:PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_ERROR_NOT_PERMITTED:PSA_SUCCESS PSA key policy: MAC, SIGN+VERIFY_HASH -> {SIGN,VERIFY}_{HASH,MESSAGE} depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH:PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_SUCCESS +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH:PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_SUCCESS:PSA_SUCCESS PSA key policy: MAC, {SIGN,VERIFY}_{HASH,MESSAGE} depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_SUCCESS +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_SUCCESS:PSA_SUCCESS PSA key policy: MAC, SIGN_MESSAGE depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_SIGN_MESSAGE:PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_SUCCESS +mac_key_policy:PSA_KEY_USAGE_SIGN_MESSAGE:PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_SUCCESS:PSA_ERROR_NOT_PERMITTED PSA key policy: MAC, VERIFY_MESSAGE depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_SUCCESS +mac_key_policy:PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_ERROR_NOT_PERMITTED:PSA_SUCCESS PSA key policy: MAC, SIGN+VERIFY_MESSAGE depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_SUCCESS +mac_key_policy:PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_SUCCESS:PSA_SUCCESS PSA key policy: MAC, neither sign nor verify depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:0:PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_ERROR_NOT_PERMITTED +mac_key_policy:0:PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_ERROR_NOT_PERMITTED:PSA_ERROR_NOT_PERMITTED PSA key policy: MAC, wrong algorithm depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_224:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_224):PSA_ERROR_NOT_PERMITTED +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_224):PSA_ERROR_NOT_PERMITTED:PSA_ERROR_NOT_PERMITTED PSA key policy: MAC, alg=0 in policy depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:0:PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_ERROR_NOT_PERMITTED +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:0:PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_ERROR_NOT_PERMITTED:PSA_ERROR_NOT_PERMITTED PSA key policy: MAC, ANY_HASH in policy is not meaningful depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_HMAC(PSA_ALG_ANY_HASH):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_ERROR_NOT_PERMITTED +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_HMAC(PSA_ALG_ANY_HASH):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_ERROR_NOT_PERMITTED:PSA_ERROR_NOT_PERMITTED PSA key policy: HMAC, sign-verify, tag length > min-length policy depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 30):PSA_SUCCESS +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 30):PSA_SUCCESS:PSA_SUCCESS PSA key policy: HMAC, sign-verify, tag length = min-length policy depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_SUCCESS +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_SUCCESS:PSA_SUCCESS PSA key policy: HMAC, sign-verify, tag length < min-length policy depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 10):PSA_ERROR_NOT_PERMITTED +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 10):PSA_ERROR_NOT_PERMITTED:PSA_ERROR_NOT_PERMITTED PSA key policy: CMAC, sign-verify, tag length > min-length policy depends_on:PSA_WANT_ALG_CMAC:PSA_WANT_KEY_TYPE_AES -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_CMAC, 10):PSA_KEY_TYPE_AES:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_CMAC, 16):PSA_SUCCESS +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_CMAC, 10):PSA_KEY_TYPE_AES:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_CMAC, 16):PSA_SUCCESS:PSA_SUCCESS PSA key policy: CMAC, sign-verify, tag length = min-length policy depends_on:PSA_WANT_ALG_CMAC:PSA_WANT_KEY_TYPE_AES -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_CMAC, 10):PSA_KEY_TYPE_AES:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_CMAC, 10):PSA_SUCCESS +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_CMAC, 10):PSA_KEY_TYPE_AES:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_CMAC, 10):PSA_SUCCESS:PSA_SUCCESS PSA key policy: CMAC, sign-verify, tag length < min-length policy depends_on:PSA_WANT_ALG_CMAC:PSA_WANT_KEY_TYPE_AES -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_CMAC, 10):PSA_KEY_TYPE_AES:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_CMAC, 8):PSA_ERROR_NOT_PERMITTED +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_CMAC, 10):PSA_KEY_TYPE_AES:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_CMAC, 8):PSA_ERROR_NOT_PERMITTED:PSA_ERROR_NOT_PERMITTED PSA key policy: HMAC, sign-verify, default tag length > min-length policy depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 31):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_SUCCESS +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 31):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_SUCCESS:PSA_SUCCESS PSA key policy: HMAC, sign-verify, default tag length = min-length policy depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 32):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_SUCCESS +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 32):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_SUCCESS:PSA_SUCCESS PSA key policy: HMAC, sign-verify, default tag length < min-length policy depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 33):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_ERROR_NOT_PERMITTED +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 33):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_HMAC(PSA_ALG_SHA_256):PSA_ERROR_NOT_PERMITTED:PSA_ERROR_NOT_PERMITTED PSA key policy: HMAC, sign-verify, min-length policy, unmatched base alg depends_on:PSA_WANT_ALG_CMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_CMAC, 20):PSA_ERROR_NOT_PERMITTED +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_CMAC, 20):PSA_ERROR_NOT_PERMITTED:PSA_ERROR_NOT_PERMITTED PSA key policy: HMAC, sign-verify, min-length policy, unmatched base alg (different hash base) depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_224:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_224), 20):PSA_ERROR_NOT_PERMITTED +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_224), 20):PSA_ERROR_NOT_PERMITTED:PSA_ERROR_NOT_PERMITTED PSA key policy: HMAC, sign-verify, min-length policy, unmatched base alg (different algorithm) depends_on:PSA_WANT_ALG_CMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 10):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_CMAC:PSA_ERROR_NOT_PERMITTED +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 10):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_CMAC:PSA_ERROR_NOT_PERMITTED:PSA_ERROR_NOT_PERMITTED PSA key policy: HMAC, sign-verify, min-length policy used as algorithm depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_ERROR_INVALID_ARGUMENT +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_AT_LEAST_THIS_LENGTH_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_ERROR_INVALID_ARGUMENT:PSA_ERROR_INVALID_ARGUMENT PSA key policy: HMAC, sign-verify, tag length > exact-length policy depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 10):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_ERROR_NOT_PERMITTED +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 10):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_ERROR_NOT_PERMITTED:PSA_ERROR_NOT_PERMITTED PSA key policy: HMAC, sign-verify, tag length = exact-length policy depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_SUCCESS +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_SUCCESS:PSA_SUCCESS PSA key policy: HMAC, sign-verify, tag length < exact-length policy depends_on:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256:PSA_WANT_KEY_TYPE_HMAC -mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 10):PSA_ERROR_NOT_PERMITTED +mac_key_policy:PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE:PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 20):PSA_KEY_TYPE_HMAC:"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa":PSA_ALG_TRUNCATED_MAC(PSA_ALG_HMAC(PSA_ALG_SHA_256), 10):PSA_ERROR_NOT_PERMITTED:PSA_ERROR_NOT_PERMITTED PSA key policy: cipher, encrypt | decrypt depends_on:PSA_WANT_ALG_CTR:PSA_WANT_KEY_TYPE_AES diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index 3fe512e8e707..cde28a8e755c 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -792,7 +792,8 @@ void mac_key_policy( int policy_usage_arg, int key_type_arg, data_t *key_data, int exercise_alg_arg, - int expected_status_arg ) + int expected_status_sign_arg, + int expected_status_verify_arg ) { mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; @@ -802,7 +803,8 @@ void mac_key_policy( int policy_usage_arg, psa_algorithm_t exercise_alg = exercise_alg_arg; psa_key_usage_t policy_usage = policy_usage_arg; psa_status_t status; - psa_status_t expected_status = expected_status_arg; + psa_status_t expected_status_sign = expected_status_sign_arg; + psa_status_t expected_status_verify = expected_status_verify_arg; unsigned char mac[PSA_MAC_MAX_SIZE]; PSA_ASSERT( psa_crypto_init( ) ); @@ -818,19 +820,13 @@ void mac_key_policy( int policy_usage_arg, mbedtls_test_update_key_usage_flags( policy_usage ) ); status = psa_mac_sign_setup( &operation, key, exercise_alg ); - if( ( mbedtls_test_update_key_usage_flags(policy_usage) & PSA_KEY_USAGE_SIGN_MESSAGE ) == 0 ) - TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); - else - TEST_EQUAL( status, expected_status ); + TEST_EQUAL( status, expected_status_sign ); psa_mac_abort( &operation ); memset( mac, 0, sizeof( mac ) ); status = psa_mac_verify_setup( &operation, key, exercise_alg ); - if( ( mbedtls_test_update_key_usage_flags(policy_usage) & PSA_KEY_USAGE_VERIFY_MESSAGE ) == 0 ) - TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); - else - TEST_EQUAL( status, expected_status ); + TEST_EQUAL( status, expected_status_verify ); exit: psa_mac_abort( &operation ); From 1ebcd55aface9312859044d43f3b3ae6decc59fa Mon Sep 17 00:00:00 2001 From: Mateusz Starzyk Date: Mon, 30 Aug 2021 17:09:03 +0200 Subject: [PATCH 071/120] Extend mac_key_policy test. Add checks for psa_mac_compute and psa_mac_verify. Signed-off-by: Mateusz Starzyk --- tests/suites/test_suite_psa_crypto.function | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/suites/test_suite_psa_crypto.function b/tests/suites/test_suite_psa_crypto.function index cde28a8e755c..8df2ceafef3a 100644 --- a/tests/suites/test_suite_psa_crypto.function +++ b/tests/suites/test_suite_psa_crypto.function @@ -822,6 +822,23 @@ void mac_key_policy( int policy_usage_arg, status = psa_mac_sign_setup( &operation, key, exercise_alg ); TEST_EQUAL( status, expected_status_sign ); + /* Calculate the MAC, one-shot case. */ + uint8_t input[128] = {0}; + size_t mac_len; + TEST_EQUAL( psa_mac_compute( key, exercise_alg, + input, 128, + mac, PSA_MAC_MAX_SIZE, &mac_len ), + expected_status_sign ); + + /* Verify correct MAC, one-shot case. */ + status = psa_mac_verify( key, exercise_alg, input, 128, + mac, mac_len ); + + if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) + TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); + else + TEST_EQUAL( status, expected_status_verify ); + psa_mac_abort( &operation ); memset( mac, 0, sizeof( mac ) ); From e28d49b3b666d10677fd555090eaf75081267943 Mon Sep 17 00:00:00 2001 From: Kenneth Soerensen Date: Thu, 3 Jan 2019 12:39:29 +0100 Subject: [PATCH 072/120] Remove compiler warning if only MBEDTLS_PK_PARSE_C is defined Warning reported with IAR compiler: "mbedtls\library\pkparse.c",1167 Warning[Pe550]: variable "ret" was set but never used Signed-off-by: Kenneth Soerensen --- library/pkparse.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/pkparse.c b/library/pkparse.c index fe6aaca338c0..b2d3bb074761 100644 --- a/library/pkparse.c +++ b/library/pkparse.c @@ -1370,8 +1370,8 @@ int mbedtls_pk_parse_key( mbedtls_pk_context *pk, } #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */ - if( ( ret = pk_parse_key_pkcs8_unencrypted_der( - pk, key, keylen, f_rng, p_rng ) ) == 0 ) + ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen, f_rng, p_rng ); + if( ret == 0 ) { return( 0 ); } From ab46aa0436eb60b4ff15af69758d41fcfe8f7254 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 17 Aug 2021 10:48:26 +0800 Subject: [PATCH 073/120] Upgrade gnutls-next to 3.7.2 v3.7.2 introduces DISABLE_TLS13_COMPAT_MODE. That can be used to verify if TLS13 COMPATIBLE is not available. Change-Id: Id68748e92504835b5a63b2565a618f728e7222f6 Signed-off-by: Jerry Yu --- tests/compat-in-docker.sh | 4 ++-- tests/docker/bionic/Dockerfile | 18 +++++++++--------- tests/ssl-opt-in-docker.sh | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/compat-in-docker.sh b/tests/compat-in-docker.sh index aef0a072898f..3a1cd2144c2c 100755 --- a/tests/compat-in-docker.sh +++ b/tests/compat-in-docker.sh @@ -42,13 +42,13 @@ esac case "${GNUTLS_CLI:-default}" in "legacy") export GNUTLS_CLI="/usr/local/gnutls-3.3.8/bin/gnutls-cli";; - "next") export GNUTLS_CLI="/usr/local/gnutls-3.6.5/bin/gnutls-cli";; + "next") export GNUTLS_CLI="/usr/local/gnutls-3.7.2/bin/gnutls-cli";; *) ;; esac case "${GNUTLS_SERV:-default}" in "legacy") export GNUTLS_SERV="/usr/local/gnutls-3.3.8/bin/gnutls-serv";; - "next") export GNUTLS_SERV="/usr/local/gnutls-3.6.5/bin/gnutls-serv";; + "next") export GNUTLS_SERV="/usr/local/gnutls-3.7.2/bin/gnutls-serv";; *) ;; esac diff --git a/tests/docker/bionic/Dockerfile b/tests/docker/bionic/Dockerfile index 1d24aa326830..41789c677cbd 100644 --- a/tests/docker/bionic/Dockerfile +++ b/tests/docker/bionic/Dockerfile @@ -137,29 +137,29 @@ RUN cd /tmp \ ENV GNUTLS_CLI=/usr/local/gnutls-3.4.10/bin/gnutls-cli ENV GNUTLS_SERV=/usr/local/gnutls-3.4.10/bin/gnutls-serv -# Build libnettle 3.4 (needed by gnutls next) +# Build libnettle 3.7.3 (needed by gnutls next) RUN cd /tmp \ - && wget https://ftp.gnu.org/gnu/nettle/nettle-3.4.1.tar.gz -qO- | tar xz \ - && cd nettle-3.4.1 \ + && wget https://ftp.gnu.org/gnu/nettle/nettle-3.7.3.tar.gz -qO- | tar xz \ + && cd nettle-3.7.3 \ && ./configure --disable-documentation \ && make ${MAKEFLAGS_PARALLEL} \ && make install \ && /sbin/ldconfig \ && rm -rf /tmp/nettle* -# Build gnutls next (3.6.5) +# Build gnutls next (3.7.2) RUN cd /tmp \ - && wget https://www.gnupg.org/ftp/gcrypt/gnutls/v3.6/gnutls-3.6.5.tar.xz -qO- | tar xJ \ - && cd gnutls-3.6.5 \ - && ./configure --prefix=/usr/local/gnutls-3.6.5 --exec_prefix=/usr/local/gnutls-3.6.5 \ + && wget https://www.gnupg.org/ftp/gcrypt/gnutls/v3.7/gnutls-3.7.2.tar.xz -qO- | tar xJ \ + && cd gnutls-3.7.2 \ + && ./configure --prefix=/usr/local/gnutls-3.7.2 --exec_prefix=/usr/local/gnutls-3.7.2 \ --with-included-libtasn1 --with-included-unistring --without-p11-kit \ --disable-shared --disable-guile --disable-doc \ && make ${MAKEFLAGS_PARALLEL} \ && make install \ && rm -rf /tmp/gnutls* -ENV GNUTLS_NEXT_CLI=/usr/local/gnutls-3.6.5/bin/gnutls-cli -ENV GNUTLS_NEXT_SERV=/usr/local/gnutls-3.6.5/bin/gnutls-serv +ENV GNUTLS_NEXT_CLI=/usr/local/gnutls-3.7.2/bin/gnutls-cli +ENV GNUTLS_NEXT_SERV=/usr/local/gnutls-3.7.2/bin/gnutls-serv RUN pip3 install --no-cache-dir \ mbed-host-tests \ diff --git a/tests/ssl-opt-in-docker.sh b/tests/ssl-opt-in-docker.sh index 401a69c569bd..e7bb01d8cab9 100755 --- a/tests/ssl-opt-in-docker.sh +++ b/tests/ssl-opt-in-docker.sh @@ -42,13 +42,13 @@ esac case "${GNUTLS_CLI:-default}" in "legacy") export GNUTLS_CLI="/usr/local/gnutls-3.3.8/bin/gnutls-cli";; - "next") export GNUTLS_CLI="/usr/local/gnutls-3.6.5/bin/gnutls-cli";; + "next") export GNUTLS_CLI="/usr/local/gnutls-3.7.2/bin/gnutls-cli";; *) ;; esac case "${GNUTLS_SERV:-default}" in "legacy") export GNUTLS_SERV="/usr/local/gnutls-3.3.8/bin/gnutls-serv";; - "next") export GNUTLS_SERV="/usr/local/gnutls-3.6.5/bin/gnutls-serv";; + "next") export GNUTLS_SERV="/usr/local/gnutls-3.7.2/bin/gnutls-serv";; *) ;; esac From b12d81d1a30656305e3dd8dc39b9489bc3988002 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 17 Aug 2021 10:56:08 +0800 Subject: [PATCH 074/120] Add feature tests for gnutls-next Test NO_TICKETS and DISABLE_TLS13_COMPAT_MODE Change-Id: Idf21b36bd64c7eefe4e0e6fb875b2e06ebb0aa07 Signed-off-by: Jerry Yu --- tests/ssl-opt.sh | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/tests/ssl-opt.sh b/tests/ssl-opt.sh index ad925f0f0cd5..bf5d9dbd976b 100755 --- a/tests/ssl-opt.sh +++ b/tests/ssl-opt.sh @@ -405,6 +405,44 @@ requires_gnutls_tls1_3() { fi } +# check %NO_TICKETS option +requires_gnutls_next_no_ticket() { + requires_gnutls_next + if [ "$GNUTLS_NEXT_AVAILABLE" = "NO" ]; then + GNUTLS_NO_TICKETS_AVAILABLE="NO" + fi + if [ -z "${GNUTLS_NO_TICKETS_AVAILABLE:-}" ]; then + if $GNUTLS_NEXT_CLI --priority-list 2>&1 | grep NO_TICKETS >/dev/null + then + GNUTLS_NO_TICKETS_AVAILABLE="YES" + else + GNUTLS_NO_TICKETS_AVAILABLE="NO" + fi + fi + if [ "$GNUTLS_NO_TICKETS_AVAILABLE" = "NO" ]; then + SKIP_NEXT="YES" + fi +} + +# check %%DISABLE_TLS13_COMPAT_MODE option +requires_gnutls_next_disable_tls13_compat() { + requires_gnutls_next + if [ "$GNUTLS_NEXT_AVAILABLE" = "NO" ]; then + GNUTLS_DISABLE_TLS13_COMPAT_MODE_AVAILABLE="NO" + fi + if [ -z "${GNUTLS_DISABLE_TLS13_COMPAT_MODE_AVAILABLE:-}" ]; then + if $GNUTLS_NEXT_CLI --priority-list 2>&1 | grep DISABLE_TLS13_COMPAT_MODE >/dev/null + then + GNUTLS_DISABLE_TLS13_COMPAT_MODE_AVAILABLE="YES" + else + GNUTLS_DISABLE_TLS13_COMPAT_MODE_AVAILABLE="NO" + fi + fi + if [ "$GNUTLS_DISABLE_TLS13_COMPAT_MODE_AVAILABLE" = "NO" ]; then + SKIP_NEXT="YES" + fi +} + # skip next test if IPv6 isn't available on this host requires_ipv6() { if [ -z "${HAS_IPV6:-}" ]; then @@ -8589,11 +8627,13 @@ run_test "TLS1.3: Test openssl tls1_3 feature" \ -c "TLS 1.3" \ -s "TLS 1.3" -# gnutls feature tests: check if tls1.3 exists. +# gnutls feature tests: check if tls1.3,NO_TICKETS and DISABLE_TLS13_COMPAT_MODE exist. requires_gnutls_tls1_3 +requires_gnutls_next_no_ticket +requires_gnutls_next_disable_tls13_compat run_test "TLS1.3: Test gnutls tls1_3 feature" \ - "$G_NEXT_SRV --priority=NORMAL:-VERS-ALL:+VERS-TLS1.3" \ - "$G_NEXT_CLI localhost --priority=NORMAL:-VERS-ALL:+VERS-TLS1.3 -V" \ + "$G_NEXT_SRV --priority=NORMAL:-VERS-ALL:+VERS-TLS1.3:%NO_TICKETS:%DISABLE_TLS13_COMPAT_MODE" \ + "$G_NEXT_CLI localhost --priority=NORMAL:-VERS-ALL:+VERS-TLS1.3:%NO_TICKETS:%DISABLE_TLS13_COMPAT_MODE -V" \ 0 \ -s "Version: TLS1.3" \ -c "Version: TLS1.3" From 75261df2e3c9d9e892291e23b7af375a91386e71 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Thu, 2 Sep 2021 17:40:08 +0800 Subject: [PATCH 075/120] fix comment issues Signed-off-by: Jerry Yu --- tests/ssl-opt.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/ssl-opt.sh b/tests/ssl-opt.sh index bf5d9dbd976b..3e199e288100 100755 --- a/tests/ssl-opt.sh +++ b/tests/ssl-opt.sh @@ -405,7 +405,7 @@ requires_gnutls_tls1_3() { fi } -# check %NO_TICKETS option +# Check %NO_TICKETS option requires_gnutls_next_no_ticket() { requires_gnutls_next if [ "$GNUTLS_NEXT_AVAILABLE" = "NO" ]; then @@ -424,7 +424,7 @@ requires_gnutls_next_no_ticket() { fi } -# check %%DISABLE_TLS13_COMPAT_MODE option +# Check %DISABLE_TLS13_COMPAT_MODE option requires_gnutls_next_disable_tls13_compat() { requires_gnutls_next if [ "$GNUTLS_NEXT_AVAILABLE" = "NO" ]; then @@ -8627,7 +8627,7 @@ run_test "TLS1.3: Test openssl tls1_3 feature" \ -c "TLS 1.3" \ -s "TLS 1.3" -# gnutls feature tests: check if tls1.3,NO_TICKETS and DISABLE_TLS13_COMPAT_MODE exist. +# gnutls feature tests: check if TLS 1.3 is supported as well as the NO_TICKETS and DISABLE_TLS13_COMPAT_MODE options. requires_gnutls_tls1_3 requires_gnutls_next_no_ticket requires_gnutls_next_disable_tls13_compat From beb3f41f2f528277d86840ec7bdbefce8d7194cd Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Mon, 16 Aug 2021 15:00:55 +0800 Subject: [PATCH 076/120] Add handshake_set_state helper function Signed-off-by: Jerry Yu --- library/ssl_misc.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/library/ssl_misc.h b/library/ssl_misc.h index b4f841a373c7..36754a323783 100644 --- a/library/ssl_misc.h +++ b/library/ssl_misc.h @@ -1323,4 +1323,14 @@ static inline int mbedtls_ssl_conf_is_hybrid_tls12_tls13( const mbedtls_ssl_conf } #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL*/ +#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) + +static inline void mbedtls_ssl_handshake_set_state( mbedtls_ssl_context* ssl, + int state ) +{ + ssl->state = state; +} + +#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ + #endif /* ssl_misc.h */ From a13c7e739cf3200d4784e1fbbf1b0412d9aee467 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 17 Aug 2021 10:44:40 +0800 Subject: [PATCH 077/120] add dummy client hello process Signed-off-by: Jerry Yu --- include/mbedtls/ssl.h | 3 +++ library/ssl_tls13_client.c | 51 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/include/mbedtls/ssl.h b/include/mbedtls/ssl.h index 2909dc8e5b66..54be3a5b9f41 100644 --- a/include/mbedtls/ssl.h +++ b/include/mbedtls/ssl.h @@ -573,6 +573,9 @@ typedef enum MBEDTLS_SSL_HANDSHAKE_OVER, MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET, MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT, +#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) + MBEDTLS_SSL_ENCRYPTED_EXTENSIONS, +#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ } mbedtls_ssl_states; diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index 368b5572db07..d619d80ed9d2 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -26,11 +26,58 @@ #if defined(MBEDTLS_SSL_CLI_C) #include "ssl_misc.h" +#include + +static int ssl_client_hello_process( mbedtls_ssl_context* ssl ); int mbedtls_ssl_handshake_client_step_tls1_3( mbedtls_ssl_context *ssl ) { - ((void) ssl); - return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); + int ret = 0; + + if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL ) + { + MBEDTLS_SSL_DEBUG_MSG( 2, ( "Handshake completed but ssl->handshake is NULL.\n" ) ); + return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); + } + + MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) ); + + switch( ssl->state ) + { + case MBEDTLS_SSL_HELLO_REQUEST: + mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO ); + break; + + case MBEDTLS_SSL_CLIENT_HELLO: + ret = ssl_client_hello_process( ssl ); + break; + + case MBEDTLS_SSL_SERVER_HELLO: + // Stop here : we haven't finished whole flow + ret=MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; + mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS ); + break; + + default: + MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); + return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); + } + + return( ret ); +} + +static int ssl_client_hello_process( mbedtls_ssl_context* ssl ) +{ + int ret = 0; + + MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) ); + + mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO ); + + MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) ); + /* client_hello_process haven't finished */ + ret=MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; + return ret; } #endif /* MBEDTLS_SSL_CLI_C */ From 65dd2ccfe696d6cfaecfc376038db1d71dc1c28e Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Wed, 18 Aug 2021 16:38:40 +0800 Subject: [PATCH 078/120] Add dummy stages for `client_hello_process` Signed-off-by: Jerry Yu --- library/CMakeLists.txt | 1 + library/Makefile | 1 + library/ssl_misc.h | 37 ++++++++++++++++++++ library/ssl_tls13_client.c | 54 ++++++++++++++++++++++++++++++ library/ssl_tls13_generic.c | 67 +++++++++++++++++++++++++++++++++++++ 5 files changed, 160 insertions(+) create mode 100644 library/ssl_tls13_generic.c diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 5adc128c96aa..a5d692cbe4a4 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -106,6 +106,7 @@ set(src_tls ssl_tls13_keys.c ssl_tls13_server.c ssl_tls13_client.c + ssl_tls13_generic.c ) if(CMAKE_COMPILER_IS_GNUCC) diff --git a/library/Makefile b/library/Makefile index 8c58fb850148..13cd7db0c6c5 100644 --- a/library/Makefile +++ b/library/Makefile @@ -169,6 +169,7 @@ OBJS_TLS= \ ssl_tls13_keys.o \ ssl_tls13_client.o \ ssl_tls13_server.o \ + ssl_tls13_generic.o \ # This line is intentionally left blank .SILENT: diff --git a/library/ssl_misc.h b/library/ssl_misc.h index 36754a323783..4c3f6c0d5e22 100644 --- a/library/ssl_misc.h +++ b/library/ssl_misc.h @@ -26,6 +26,7 @@ #include "mbedtls/ssl.h" #include "mbedtls/cipher.h" +#include "mbedtls/debug.h" #if defined(MBEDTLS_USE_PSA_CRYPTO) #include "psa/crypto.h" @@ -102,6 +103,30 @@ #define MBEDTLS_SSL_RENEGOTIATION_DONE 2 /* Done or aborted */ #define MBEDTLS_SSL_RENEGOTIATION_PENDING 3 /* Requested (server only) */ +#define MBEDTLS_SSL_PROC_STRIP_PARENS( ... ) __VA_ARGS__ + +#define MBEDTLS_SSL_PROC_CHK( fn, args ) \ + do { \ + ret = fn(MBEDTLS_SSL_PROC_STRIP_PARENS args); \ + if( ret != 0 ) \ + { \ + if( ret > 0 ) \ + ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; \ + MBEDTLS_SSL_DEBUG_RET( 1, #fn, ret ); \ + goto cleanup; \ + } \ + } while( 0 ) + +#define MBEDTLS_SSL_PROC_CHK_NEG( fn, args ) \ + do { \ + ret = fn(MBEDTLS_SSL_PROC_STRIP_PARENS args); \ + if( ret < 0 ) \ + { \ + MBEDTLS_SSL_DEBUG_RET( 1, #fn, ret ); \ + goto cleanup; \ + } \ + } while( 0 ) + /* * DTLS retransmission states, see RFC 6347 4.2.4 * @@ -1331,6 +1356,18 @@ static inline void mbedtls_ssl_handshake_set_state( mbedtls_ssl_context* ssl, ssl->state = state; } +int mbedtls_ssl_start_handshake_msg( mbedtls_ssl_context *ssl, + unsigned hs_type, + unsigned char **buf, + size_t *buflen ); +int mbedtls_ssl_finish_handshake_msg( mbedtls_ssl_context *ssl, + size_t buf_len, + size_t msg_len ); +void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, + unsigned hs_type, + size_t total_hs_len ); + + #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ #endif /* ssl_misc.h */ diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index d619d80ed9d2..46c071b6bb76 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -28,6 +28,7 @@ #include "ssl_misc.h" #include +/* Main entry point; orchestrates the other functions */ static int ssl_client_hello_process( mbedtls_ssl_context* ssl ); int mbedtls_ssl_handshake_client_step_tls1_3( mbedtls_ssl_context *ssl ) @@ -66,20 +67,73 @@ int mbedtls_ssl_handshake_client_step_tls1_3( mbedtls_ssl_context *ssl ) return( ret ); } + +static int ssl_client_hello_prepare( mbedtls_ssl_context* ssl ); +static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl, + unsigned char* buf, size_t buflen, + size_t* len_without_binders, + size_t* len_with_binders ); +static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl ); + static int ssl_client_hello_process( mbedtls_ssl_context* ssl ) { int ret = 0; + unsigned char *buf; + size_t buf_len, msg_len; + size_t len_without_binders = 0; MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) ); + MBEDTLS_SSL_PROC_CHK( ssl_client_hello_prepare, ( ssl ) ); + + MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg, ( ssl, + MBEDTLS_SSL_HS_CLIENT_HELLO, &buf, &buf_len ) ); + + MBEDTLS_SSL_PROC_CHK( ssl_client_hello_write_partial, ( ssl, buf, buf_len, + &len_without_binders, + &msg_len ) ); + + mbedtls_ssl_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, + msg_len ); + ssl->handshake->update_checksum( ssl, buf, len_without_binders ); + + MBEDTLS_SSL_PROC_CHK( ssl_client_hello_postprocess, ( ssl ) ); + MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg, ( ssl, buf_len, msg_len ) ); mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO ); +cleanup: + MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) ); /* client_hello_process haven't finished */ ret=MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; return ret; } +static int ssl_client_hello_prepare( mbedtls_ssl_context* ssl ) +{ + ((void) ssl); + return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); +} + +static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl, + unsigned char* buf, size_t buflen, + size_t* len_without_binders, + size_t* len_with_binders ) +{ + ((void) ssl); + ((void) buf); + ((void) buflen); + ((void) len_without_binders); + ((void) len_with_binders); + return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); +} + +static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl ) +{ + ((void) ssl); + return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); +} + #endif /* MBEDTLS_SSL_CLI_C */ #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ diff --git a/library/ssl_tls13_generic.c b/library/ssl_tls13_generic.c new file mode 100644 index 000000000000..51c8fe3bd547 --- /dev/null +++ b/library/ssl_tls13_generic.c @@ -0,0 +1,67 @@ +/* + * TLS 1.3 functionality shared between client and server + * + * Copyright The Mbed TLS Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "common.h" + +#if defined(MBEDTLS_SSL_TLS_C) + +#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) + +#include "ssl_misc.h" + +int mbedtls_ssl_start_handshake_msg( mbedtls_ssl_context *ssl, + unsigned hs_type, + unsigned char **buf, + size_t *buflen ) +{ + ((void) ssl); + ((void) hs_type); + ((void) buf); + ((void) buflen); + return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); +} + +int mbedtls_ssl_finish_handshake_msg( mbedtls_ssl_context *ssl, + size_t buf_len, + size_t msg_len ) +{ + ((void) ssl); + ((void) buf_len); + ((void) msg_len); + return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); +} + +void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, + unsigned hs_type, + size_t total_hs_len ) +{ + unsigned char hs_hdr[4]; + + /* Build HS header for checksum update. */ + hs_hdr[0] = hs_type; + hs_hdr[1] = (unsigned char)( total_hs_len >> 16 ); + hs_hdr[2] = (unsigned char)( total_hs_len >> 8 ); + hs_hdr[3] = (unsigned char)( total_hs_len >> 0 ); + + ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) ); +} + +#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ + +#endif /* MBEDTLS_SSL_TLS_C */ From c8a392c47e1c71b67b90c47f509f47fc57cefdc9 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Wed, 18 Aug 2021 16:46:28 +0800 Subject: [PATCH 079/120] Implement stages except write_partial Signed-off-by: Jerry Yu --- library/ssl_tls13_client.c | 34 ++++++++++++++++++++++++++-------- library/ssl_tls13_generic.c | 24 ++++++++++++++++-------- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index 46c071b6bb76..b06147c0c991 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -99,7 +99,6 @@ static int ssl_client_hello_process( mbedtls_ssl_context* ssl ) MBEDTLS_SSL_PROC_CHK( ssl_client_hello_postprocess, ( ssl ) ); MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg, ( ssl, buf_len, msg_len ) ); - mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO ); cleanup: @@ -111,8 +110,31 @@ static int ssl_client_hello_process( mbedtls_ssl_context* ssl ) static int ssl_client_hello_prepare( mbedtls_ssl_context* ssl ) { - ((void) ssl); - return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); + int ret; + size_t rand_bytes_len; + + if( ssl->conf->f_rng == NULL ) + { + MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) ); + return( MBEDTLS_ERR_SSL_NO_RNG ); + } + + rand_bytes_len = 32; + + if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->handshake->randbytes, rand_bytes_len ) ) != 0 ) + { + MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret ); + return( ret ); + } + + return( 0 ); +} + +static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl ) +{ + mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO ); + + return( 0 ); } static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl, @@ -128,11 +150,7 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl, return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); } -static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl ) -{ - ((void) ssl); - return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); -} + #endif /* MBEDTLS_SSL_CLI_C */ diff --git a/library/ssl_tls13_generic.c b/library/ssl_tls13_generic.c index 51c8fe3bd547..5aa5d8a014c9 100644 --- a/library/ssl_tls13_generic.c +++ b/library/ssl_tls13_generic.c @@ -23,6 +23,8 @@ #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) +#include "mbedtls/error.h" + #include "ssl_misc.h" int mbedtls_ssl_start_handshake_msg( mbedtls_ssl_context *ssl, @@ -30,21 +32,27 @@ int mbedtls_ssl_start_handshake_msg( mbedtls_ssl_context *ssl, unsigned char **buf, size_t *buflen ) { - ((void) ssl); - ((void) hs_type); - ((void) buf); - ((void) buflen); - return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); + *buf = ssl->out_msg + 4; + *buflen = MBEDTLS_SSL_OUT_CONTENT_LEN - 4; + + ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; + ssl->out_msg[0] = hs_type; + + return( 0 ); } int mbedtls_ssl_finish_handshake_msg( mbedtls_ssl_context *ssl, size_t buf_len, size_t msg_len ) { - ((void) ssl); + int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; ((void) buf_len); - ((void) msg_len); - return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); + + ssl->out_msglen = msg_len + 4; + MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_handshake_msg_ext, ( ssl, 0 ) ); + +cleanup: + return( ret ); } void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, From 93bcd61a414c9e320b149a9d1e66009cd94dfd87 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Wed, 18 Aug 2021 12:47:24 +0800 Subject: [PATCH 080/120] Add field into handshake params Add `extensions_present` field. It represents which are present. Signed-off-by: Jerry Yu --- library/ssl_misc.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/library/ssl_misc.h b/library/ssl_misc.h index 4c3f6c0d5e22..5f15b8da77c7 100644 --- a/library/ssl_misc.h +++ b/library/ssl_misc.h @@ -103,6 +103,21 @@ #define MBEDTLS_SSL_RENEGOTIATION_DONE 2 /* Done or aborted */ #define MBEDTLS_SSL_RENEGOTIATION_PENDING 3 /* Requested (server only) */ +/* List of extensions used in extensions_present of mbedtls_ssl_handshake_params */ +#define MBEDTLS_SSL_EXT_NONE 0 +#define MBEDTLS_SSL_EXT_PRE_SHARED_KEY ( 1 << 0 ) +#define MBEDTLS_SSL_EXT_KEY_SHARE ( 1 << 1 ) +#define MBEDTLS_SSL_EXT_SIGNATURE_ALGORITHM ( 1 << 2 ) +#define MBEDTLS_SSL_EXT_SUPPORTED_GROUPS ( 1 << 3 ) +#define MBEDTLS_SSL_EXT_MAX_FRAGMENT_LENGTH ( 1 << 4 ) +#define MBEDTLS_SSL_EXT_ALPN ( 1 << 5 ) +#define MBEDTLS_SSL_EXT_SUPPORTED_VERSION ( 1 << 6 ) +#define MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ( 1 << 7 ) +#define MBEDTLS_SSL_EXT_EARLY_DATA ( 1 << 8 ) +#define MBEDTLS_SSL_EXT_SERVERNAME ( 1 << 9 ) +#define MBEDTLS_SSL_EXT_COOKIE ( 1 << 10 ) +#define MBEDTLS_SSL_EXT_CID ( 1 << 11 ) + #define MBEDTLS_SSL_PROC_STRIP_PARENS( ... ) __VA_ARGS__ #define MBEDTLS_SSL_PROC_CHK( fn, args ) \ @@ -631,6 +646,9 @@ struct mbedtls_ssl_handshake_params int max_major_ver; /*!< max. major version client*/ int max_minor_ver; /*!< max. minor version client*/ int cli_exts; /*!< client extension presence*/ +#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) + int extensions_present; /*!< extension presence; Each bitfield represents an extension and defined as \c MBEDTLS_SSL_EXT_XXX */ +#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ #if defined(MBEDTLS_SSL_SESSION_TICKETS) int new_session_ticket; /*!< use NewSessionTicket? */ From 7984d9931e549d040a75b964dd8a24dfd96f0b3d Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Wed, 18 Aug 2021 10:31:29 +0800 Subject: [PATCH 081/120] Add tls1.3 extension IANA values Signed-off-by: Jerry Yu --- include/mbedtls/ssl.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/mbedtls/ssl.h b/include/mbedtls/ssl.h index 54be3a5b9f41..f988c0d8f1f1 100644 --- a/include/mbedtls/ssl.h +++ b/include/mbedtls/ssl.h @@ -474,6 +474,8 @@ #define MBEDTLS_TLS_EXT_TRUNCATED_HMAC 4 #define MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES 10 +#define MBEDTLS_TLS_EXT_SUPPORTED_GROUPS 10 /* Renamed in TLS 1.3 */ + #define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS 11 #define MBEDTLS_TLS_EXT_SIG_ALG 13 @@ -487,6 +489,15 @@ #define MBEDTLS_TLS_EXT_SESSION_TICKET 35 +/* TLS 1.3 */ +#define MBEDTLS_TLS_EXT_PRE_SHARED_KEY 41 +#define MBEDTLS_TLS_EXT_EARLY_DATA 42 +#define MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS 43 +#define MBEDTLS_TLS_EXT_COOKIE 44 +#define MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES 45 + +#define MBEDTLS_TLS_EXT_KEY_SHARES 51 + /* The value of the CID extension is still TBD as of * draft-ietf-tls-dtls-connection-id-05 * (https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05). From bc20bdd3a9f271ac0605716714111090436d18d9 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 24 Aug 2021 15:59:48 +0800 Subject: [PATCH 082/120] Implement write_partial with dummy exts Signed-off-by: Jerry Yu --- library/ssl_misc.h | 3 + library/ssl_tls13_client.c | 293 +++++++++++++++++++++++++++++++++++- library/ssl_tls13_generic.c | 34 +++++ 3 files changed, 327 insertions(+), 3 deletions(-) diff --git a/library/ssl_misc.h b/library/ssl_misc.h index 5f15b8da77c7..dfb5634974b5 100644 --- a/library/ssl_misc.h +++ b/library/ssl_misc.h @@ -1385,6 +1385,9 @@ void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, unsigned hs_type, size_t total_hs_len ); +#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) +int mbedtls_ssl_write_signature_algorithms_ext(mbedtls_ssl_context* ssl, unsigned char* buf, unsigned char* end, size_t* olen); +#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index b06147c0c991..fbc8fd5fb4d4 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -25,6 +25,8 @@ #if defined(MBEDTLS_SSL_CLI_C) +#include + #include "ssl_misc.h" #include @@ -137,20 +139,305 @@ static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl ) return( 0 ); } +/* Write extensions */ + +static void ssl_write_supported_versions_ext( mbedtls_ssl_context *ssl, + unsigned char* buf, + unsigned char* end, + size_t* olen ); + +#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) + +static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl, + unsigned char* buf, + unsigned char* end, + size_t* olen ); + +static int ssl_write_key_shares_ext( mbedtls_ssl_context *ssl, + unsigned char* buf, + unsigned char* end, + size_t* olen ); + +#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ + static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl, unsigned char* buf, size_t buflen, size_t* len_without_binders, size_t* len_with_binders ) +{ + /* Extensions */ + + /* extension_start + * Used during extension writing where the + * buffer pointer to the beginning of the + * extension list must be kept to write + * the total extension list size in the end. + */ + + int ret; + unsigned char* extension_start; + size_t cur_ext_len; /* Size of the current extension */ + size_t total_ext_len; /* Size of list of extensions */ + + /* Length information */ + size_t rand_bytes_len; + size_t version_len; + + /* Buffer management */ + unsigned char* start = buf; + unsigned char* end = buf + buflen; + + /* Ciphersuite-related variables */ + const int* ciphersuites; + const mbedtls_ssl_ciphersuite_t* ciphersuite_info; + size_t i; /* used to iterate through ciphersuite list */ + /* ciphersuite_start points to the start of the ciphersuite list, i.e. to the length field*/ + unsigned char* ciphersuite_start; + size_t ciphersuite_count; + + /* Keeping track of the included extensions */ + ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE; + + rand_bytes_len = 32; + + /* NOTE: + * Even for DTLS 1.3, we are writing a TLS handshake header here. + * The actual DTLS 1.3 handshake header is inserted in + * the record writing routine mbedtls_ssl_write_record(). + * + * For cTLS the length, and the version field + * are elided. The random bytes are shorter. + */ + version_len = 2; + + if( ssl->conf->max_major_ver == 0 ) + { + MBEDTLS_SSL_DEBUG_MSG( 1, ( "configured max major version is invalid, " + "consider using mbedtls_ssl_config_defaults()" ) ); + return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); + } + + ssl->major_ver = ssl->conf->min_major_ver; + ssl->minor_ver = ssl->conf->min_minor_ver; + + /* For TLS 1.3 we use the legacy version number {0x03, 0x03} + * instead of the true version number. + * + * For DTLS 1.3 we use the legacy version number + * {254,253}. + * + * In cTLS the version number is elided. + */ + *buf++ = 0x03; + *buf++ = 0x03; + buflen -= version_len; + + /* Write random bytes */ + memcpy( buf, ssl->handshake->randbytes, rand_bytes_len ); + MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf, rand_bytes_len ); + + buf += rand_bytes_len; + buflen -= rand_bytes_len; + + /* Versions of TLS before TLS 1.3 supported a + * "session resumption" feature which has been merged with pre-shared + * keys in this version. A client which has a + * cached session ID set by a pre-TLS 1.3 server SHOULD set this + * field to that value. In compatibility mode, + * this field MUST be non-empty, so a client not offering a + * pre-TLS 1.3 session MUST generate a new 32-byte value. This value + * need not be random but SHOULD be unpredictable to avoid + * implementations fixating on a specific value ( also known as + * ossification ). Otherwise, it MUST be set as a zero-length vector + * ( i.e., a zero-valued single byte length field ). + */ + if( buflen < 1 ) + { + MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) ); + return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); + } + + *buf++ = 0; /* session id length set to zero */ + buflen -= 1; + + /* + * Ciphersuite list + * + * This is a list of the symmetric cipher options supported by + * the client, specifically the record protection algorithm + * ( including secret key length ) and a hash to be used with + * HKDF, in descending order of client preference. + */ + ciphersuites = ssl->conf->ciphersuite_list; + + if( buflen < 2 /* for ciphersuite list length */ ) + { + MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) ); + return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); + } + + /* Skip writing ciphersuite length for now */ + ciphersuite_count = 0; + ciphersuite_start = buf; + buf += 2; + buflen -= 2; + + for ( i = 0; ciphersuites[i] != 0; i++ ) + { + ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] ); + + if( ciphersuite_info == NULL ) + continue; + + if( ciphersuite_info->min_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 || + ciphersuite_info->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 ) + continue; + + MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s", + (unsigned int) ciphersuites[i], ciphersuite_info->name ) ); + + ciphersuite_count++; + + if( buflen < 2 /* for ciphersuite list length */ ) + { + MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) ); + return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); + } + + *buf++ = (unsigned char)( ciphersuites[i] >> 8 ); + *buf++ = (unsigned char)( ciphersuites[i] ); + + buflen -= 2; + + } + + /* write ciphersuite length now */ + *ciphersuite_start++ = (unsigned char)( ciphersuite_count*2 >> 8 ); + *ciphersuite_start++ = (unsigned char)( ciphersuite_count*2 ); + + MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites", ciphersuite_count ) ); + + /* For every TLS 1.3 ClientHello, this vector MUST contain exactly + * one byte set to zero, which corresponds to the 'null' compression + * method in prior versions of TLS. + * + * For cTLS this field is elided. + */ + if( buflen < 2 /* for ciphersuite list length */ ) + { + MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) ); + return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); + } + + *buf++ = 1; + *buf++ = MBEDTLS_SSL_COMPRESS_NULL; + + buflen -= 2; + + /* First write extensions, then the total length */ + extension_start = buf; + total_ext_len = 0; + buf += 2; + + /* Supported Versions Extension is mandatory with TLS 1.3. + * + * For cTLS we only need to provide it if there is more than one version + * and currently there is only one. + */ + ssl_write_supported_versions_ext( ssl, buf, end, &cur_ext_len ); + total_ext_len += cur_ext_len; + buf += cur_ext_len; + +#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) + /* The supported_groups and the key_share extensions are + * REQUIRED for ECDHE ciphersuites. + */ + ret = ssl_write_supported_groups_ext( ssl, buf, end, &cur_ext_len ); + if( ret != 0 ) + return( ret ); + + total_ext_len += cur_ext_len; + buf += cur_ext_len; + + /* The supported_signature_algorithms extension is REQUIRED for + * certificate authenticated ciphersuites. */ + ret = mbedtls_ssl_write_signature_algorithms_ext( ssl, buf, end, &cur_ext_len ); + if( ret != 0 ) + return( ret ); + + total_ext_len += cur_ext_len; + buf += cur_ext_len; + + /* We need to send the key shares under three conditions: + * 1 ) A certificate-based ciphersuite is being offered. In this case + * supported_groups and supported_signature extensions have been successfully added. + * 2 ) A PSK-based ciphersuite with ECDHE is offered. In this case the + * psk_key_exchange_modes has been added as the last extension. + * 3 ) Or, in case all ciphers are supported ( which includes #1 and #2 from above ) + */ + + ret = ssl_write_key_shares_ext( ssl, buf, end, &cur_ext_len ); + if( ret != 0 ) + return( ret ); + + total_ext_len += cur_ext_len; + buf += cur_ext_len; +#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ + + /* Add more extensions here */ + + MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET , + total_ext_len ) ); + + MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extension_start, total_ext_len ); + + /* Write extension length */ + *extension_start++ = (unsigned char)( ( total_ext_len >> 8 ) & 0xFF ); + *extension_start++ = (unsigned char)( ( total_ext_len ) & 0xFF ); + + *len_without_binders = buf - start; + *len_with_binders = ( extension_start + total_ext_len ) - start; + return( 0 ); +} + +static void ssl_write_supported_versions_ext( mbedtls_ssl_context *ssl, + unsigned char* buf, + unsigned char* end, + size_t* olen ) { ((void) ssl); ((void) buf); - ((void) buflen); - ((void) len_without_binders); - ((void) len_with_binders); + ((void) end); + ((void) olen); +} + +#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) + +static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl, + unsigned char* buf, + unsigned char* end, + size_t* olen ) +{ + ((void) ssl); + ((void) buf); + ((void) end); + ((void) olen); return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); } +static int ssl_write_key_shares_ext( mbedtls_ssl_context *ssl, + unsigned char* buf, + unsigned char* end, + size_t* olen ) +{ + ((void) ssl); + ((void) buf); + ((void) end); + ((void) olen); + return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); +} +#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ #endif /* MBEDTLS_SSL_CLI_C */ diff --git a/library/ssl_tls13_generic.c b/library/ssl_tls13_generic.c index 5aa5d8a014c9..7ec7423ea067 100644 --- a/library/ssl_tls13_generic.c +++ b/library/ssl_tls13_generic.c @@ -70,6 +70,40 @@ void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) ); } +#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) + +/* + * mbedtls_ssl_write_signature_algorithms_ext( ) + * + * enum { + * .... + * ecdsa_secp256r1_sha256( 0x0403 ), + * ecdsa_secp384r1_sha384( 0x0503 ), + * ecdsa_secp521r1_sha512( 0x0603 ), + * .... + * } SignatureScheme; + * + * struct { + * SignatureScheme supported_signature_algorithms<2..2^16-2>; + * } SignatureSchemeList; + * + * Only if we handle at least one key exchange that needs signatures. + */ + +int mbedtls_ssl_write_signature_algorithms_ext( mbedtls_ssl_context *ssl, + unsigned char* buf, + unsigned char* end, + size_t* olen ) +{ + ((void) ssl); + ((void) buf); + ((void) end); + ((void) olen); + return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); +} + +#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ + #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ #endif /* MBEDTLS_SSL_TLS_C */ From ef6b36b484ea00bae37c5833d14b15127d76ea98 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 24 Aug 2021 16:29:02 +0800 Subject: [PATCH 083/120] add supported versions extension Signed-off-by: Jerry Yu --- library/ssl_tls13_client.c | 42 ++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index fbc8fd5fb4d4..0b10b12f412a 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -400,15 +400,49 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl, return( 0 ); } +/* + * ssl_write_supported_versions_ext(): + * + * struct { + * ProtocolVersion versions<2..254>; + * } SupportedVersions; + */ static void ssl_write_supported_versions_ext( mbedtls_ssl_context *ssl, unsigned char* buf, unsigned char* end, size_t* olen ) { - ((void) ssl); - ((void) buf); - ((void) end); - ((void) olen); + unsigned char *p = buf; + + *olen = 0; + + MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported version extension" ) ); + + if( end < p || (size_t)( end - p ) < 7 ) + { + MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); + return; + } + + *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS >> 8 ) & 0xFF ); + *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS ) & 0xFF ); + + /* total length */ + *p++ = 0x00; + *p++ = 3; + + /* length of next field */ + *p++ = 0x2; + + /* This implementation only supports a single TLS version, and only + * advertises a single value. + */ + mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver, + ssl->conf->transport, p ); + + MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]", ssl->conf->max_major_ver, ssl->conf->max_minor_ver ) ); + + *olen = 7; } #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) From 32cd5b19dc15ece9669e6e40a1730301082297ff Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 24 Aug 2021 18:07:13 +0800 Subject: [PATCH 084/120] fix unused variable warning Signed-off-by: Jerry Yu --- library/ssl_tls13_client.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index 0b10b12f412a..f6e145b294c7 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -173,8 +173,9 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl, * extension list must be kept to write * the total extension list size in the end. */ - +#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) int ret; +#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ unsigned char* extension_start; size_t cur_ext_len; /* Size of the current extension */ size_t total_ext_len; /* Size of list of extensions */ From b3317e1a01d2754e7ca1a9fbfb1566a8b5798d83 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Thu, 26 Aug 2021 14:30:22 +0800 Subject: [PATCH 085/120] Add extension types in rfc8446 Signed-off-by: Jerry Yu --- include/mbedtls/ssl.h | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/include/mbedtls/ssl.h b/include/mbedtls/ssl.h index f988c0d8f1f1..b7b22c20d4ed 100644 --- a/include/mbedtls/ssl.h +++ b/include/mbedtls/ssl.h @@ -472,31 +472,37 @@ #define MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH 1 #define MBEDTLS_TLS_EXT_TRUNCATED_HMAC 4 +#define MBEDTLS_TLS_EXT_STATUS_REQUEST 5 /* RFC 8446 TLS 1.3 */ #define MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES 10 -#define MBEDTLS_TLS_EXT_SUPPORTED_GROUPS 10 /* Renamed in TLS 1.3 */ - +#define MBEDTLS_TLS_EXT_SUPPORTED_GROUPS 10 /* RFC 8446 TLS 1.3 */ #define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS 11 -#define MBEDTLS_TLS_EXT_SIG_ALG 13 - +#define MBEDTLS_TLS_EXT_SIG_ALG 13 /* RFC 8446 TLS 1.3 */ #define MBEDTLS_TLS_EXT_USE_SRTP 14 - +#define MBEDTLS_TLS_EXT_HEARTBEAT 15 /* RFC 8446 TLS 1.3 */ #define MBEDTLS_TLS_EXT_ALPN 16 +#define MBEDTLS_TLS_EXT_SCT 18 /* RFC 8446 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_SERV_CERT_TYPE 19 /* RFC 8446 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_CLI_CERT_TYPE 20 /* RFC 8446 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_PADDING 21 /* RFC 8446 TLS 1.3 */ #define MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC 22 /* 0x16 */ #define MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET 0x0017 /* 23 */ #define MBEDTLS_TLS_EXT_SESSION_TICKET 35 -/* TLS 1.3 */ -#define MBEDTLS_TLS_EXT_PRE_SHARED_KEY 41 -#define MBEDTLS_TLS_EXT_EARLY_DATA 42 -#define MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS 43 -#define MBEDTLS_TLS_EXT_COOKIE 44 -#define MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES 45 - -#define MBEDTLS_TLS_EXT_KEY_SHARES 51 +#define MBEDTLS_TLS_EXT_PRE_SHARED_KEY 41 /* RFC 8446 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_EARLY_DATA 42 /* RFC 8446 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS 43 /* RFC 8446 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_COOKIE 44 /* RFC 8446 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES 45 /* RFC 8446 TLS 1.3 */ + +#define MBEDTLS_TLS_EXT_CERT_AUTH 47 /* RFC 8446 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_OID_FILTERS 48 /* RFC 8446 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH 49 /* RFC 8446 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_SIG_ALG_CERT 50 /* RFC 8446 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_KEY_SHARE 51 /* RFC 8446 TLS 1.3 */ /* The value of the CID extension is still TBD as of * draft-ietf-tls-dtls-connection-id-05 From 8e7ca0432ef291e1e0502e352cf830cfbabdd875 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Thu, 26 Aug 2021 15:31:37 +0800 Subject: [PATCH 086/120] fix extensions_present issues fix comments for the mask values. follow same order as IANA values. Signed-off-by: Jerry Yu --- library/ssl_misc.h | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/library/ssl_misc.h b/library/ssl_misc.h index dfb5634974b5..bda2a7a1a69e 100644 --- a/library/ssl_misc.h +++ b/library/ssl_misc.h @@ -103,20 +103,34 @@ #define MBEDTLS_SSL_RENEGOTIATION_DONE 2 /* Done or aborted */ #define MBEDTLS_SSL_RENEGOTIATION_PENDING 3 /* Requested (server only) */ -/* List of extensions used in extensions_present of mbedtls_ssl_handshake_params */ -#define MBEDTLS_SSL_EXT_NONE 0 -#define MBEDTLS_SSL_EXT_PRE_SHARED_KEY ( 1 << 0 ) -#define MBEDTLS_SSL_EXT_KEY_SHARE ( 1 << 1 ) -#define MBEDTLS_SSL_EXT_SIGNATURE_ALGORITHM ( 1 << 2 ) -#define MBEDTLS_SSL_EXT_SUPPORTED_GROUPS ( 1 << 3 ) -#define MBEDTLS_SSL_EXT_MAX_FRAGMENT_LENGTH ( 1 << 4 ) -#define MBEDTLS_SSL_EXT_ALPN ( 1 << 5 ) -#define MBEDTLS_SSL_EXT_SUPPORTED_VERSION ( 1 << 6 ) -#define MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ( 1 << 7 ) -#define MBEDTLS_SSL_EXT_EARLY_DATA ( 1 << 8 ) -#define MBEDTLS_SSL_EXT_SERVERNAME ( 1 << 9 ) -#define MBEDTLS_SSL_EXT_COOKIE ( 1 << 10 ) -#define MBEDTLS_SSL_EXT_CID ( 1 << 11 ) +/* + * Mask of TLS 1.3 handshake extensions used in extensions_present + * of mbedtls_ssl_handshake_params. + */ +#define MBEDTLS_SSL_EXT_NONE 0 + +#define MBEDTLS_SSL_EXT_SERVERNAME ( 1 << 0 ) +#define MBEDTLS_SSL_EXT_MAX_FRAGMENT_LENGTH ( 1 << 1 ) +#define MBEDTLS_SSL_EXT_STATUS_REQUEST ( 1 << 2 ) +#define MBEDTLS_SSL_EXT_SUPPORTED_GROUPS ( 1 << 3 ) +#define MBEDTLS_SSL_EXT_SIG_ALG ( 1 << 4 ) +#define MBEDTLS_SSL_EXT_USE_SRTP ( 1 << 5 ) +#define MBEDTLS_SSL_EXT_HEARTBEAT ( 1 << 6 ) +#define MBEDTLS_SSL_EXT_ALPN ( 1 << 7 ) +#define MBEDTLS_SSL_EXT_SCT ( 1 << 8 ) +#define MBEDTLS_SSL_EXT_SERV_CERT_TYPE ( 1 << 9 ) +#define MBEDTLS_SSL_EXT_CLI_CERT_TYPE ( 1 << 10 ) +#define MBEDTLS_SSL_EXT_PADDING ( 1 << 11 ) +#define MBEDTLS_SSL_EXT_PRE_SHARED_KEY ( 1 << 12 ) +#define MBEDTLS_SSL_EXT_EARLY_DATA ( 1 << 13 ) +#define MBEDTLS_SSL_EXT_SUPPORTED_VERSIONS ( 1 << 14 ) +#define MBEDTLS_SSL_EXT_COOKIE ( 1 << 15 ) +#define MBEDTLS_SSL_EXT_PSK_KEY_EXCHANGE_MODES ( 1 << 16 ) +#define MBEDTLS_SSL_EXT_CERT_AUTH ( 1 << 17 ) +#define MBEDTLS_SSL_EXT_OID_FILTERS ( 1 << 18 ) +#define MBEDTLS_SSL_EXT_POST_HANDSHAKE_AUTH ( 1 << 19 ) +#define MBEDTLS_SSL_EXT_SIG_ALG_CERT ( 1 << 20 ) +#define MBEDTLS_SSL_EXT_KEY_SHARE ( 1 << 21 ) #define MBEDTLS_SSL_PROC_STRIP_PARENS( ... ) __VA_ARGS__ @@ -647,7 +661,9 @@ struct mbedtls_ssl_handshake_params int max_minor_ver; /*!< max. minor version client*/ int cli_exts; /*!< client extension presence*/ #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) - int extensions_present; /*!< extension presence; Each bitfield represents an extension and defined as \c MBEDTLS_SSL_EXT_XXX */ + int extensions_present; /*!< extension presence; Each bitfield + represents an extension and defined + as \c MBEDTLS_SSL_EXT_XXX */ #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ #if defined(MBEDTLS_SSL_SESSION_TICKETS) From c7ddeec22988235608aa7a4f3647b14d74546a53 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Thu, 26 Aug 2021 16:23:47 +0800 Subject: [PATCH 087/120] Remove `len_without_binders` Signed-off-by: Jerry Yu --- library/ssl_tls13_client.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index f6e145b294c7..2cada0decaaf 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -73,8 +73,7 @@ int mbedtls_ssl_handshake_client_step_tls1_3( mbedtls_ssl_context *ssl ) static int ssl_client_hello_prepare( mbedtls_ssl_context* ssl ); static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl, unsigned char* buf, size_t buflen, - size_t* len_without_binders, - size_t* len_with_binders ); + size_t *len_with_binders ); static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl ); static int ssl_client_hello_process( mbedtls_ssl_context* ssl ) @@ -82,7 +81,6 @@ static int ssl_client_hello_process( mbedtls_ssl_context* ssl ) int ret = 0; unsigned char *buf; size_t buf_len, msg_len; - size_t len_without_binders = 0; MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) ); @@ -91,13 +89,11 @@ static int ssl_client_hello_process( mbedtls_ssl_context* ssl ) MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg, ( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, &buf, &buf_len ) ); - MBEDTLS_SSL_PROC_CHK( ssl_client_hello_write_partial, ( ssl, buf, buf_len, - &len_without_binders, - &msg_len ) ); + MBEDTLS_SSL_PROC_CHK( ssl_client_hello_write_partial, ( ssl, buf, buf_len, &msg_len ) ); mbedtls_ssl_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, msg_len ); - ssl->handshake->update_checksum( ssl, buf, len_without_binders ); + ssl->handshake->update_checksum( ssl, buf, 0 ); MBEDTLS_SSL_PROC_CHK( ssl_client_hello_postprocess, ( ssl ) ); MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg, ( ssl, buf_len, msg_len ) ); @@ -162,8 +158,7 @@ static int ssl_write_key_shares_ext( mbedtls_ssl_context *ssl, static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl, unsigned char* buf, size_t buflen, - size_t* len_without_binders, - size_t* len_with_binders ) + size_t *len_with_binders ) { /* Extensions */ @@ -396,7 +391,6 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl, *extension_start++ = (unsigned char)( ( total_ext_len >> 8 ) & 0xFF ); *extension_start++ = (unsigned char)( ( total_ext_len ) & 0xFF ); - *len_without_binders = buf - start; *len_with_binders = ( extension_start + total_ext_len ) - start; return( 0 ); } From 708202b7d08812f3859b854cd8e534e4154655b0 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Thu, 26 Aug 2021 16:28:36 +0800 Subject: [PATCH 088/120] Move random function check move to `ssl_conf_check` Signed-off-by: Jerry Yu --- library/ssl_tls.c | 6 ++++++ library/ssl_tls13_client.c | 6 ------ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 07b51003ab17..1c8c7bd7340b 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -3182,6 +3182,12 @@ static int ssl_conf_check(const mbedtls_ssl_context *ssl) if( ret != 0 ) return( ret ); + if( ssl->conf->f_rng == NULL ) + { + MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) ); + return( MBEDTLS_ERR_SSL_NO_RNG ); + } + /* Space for further checks */ return( 0 ); diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index 2cada0decaaf..036c5e5537bb 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -111,12 +111,6 @@ static int ssl_client_hello_prepare( mbedtls_ssl_context* ssl ) int ret; size_t rand_bytes_len; - if( ssl->conf->f_rng == NULL ) - { - MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) ); - return( MBEDTLS_ERR_SSL_NO_RNG ); - } - rand_bytes_len = 32; if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->handshake->randbytes, rand_bytes_len ) ) != 0 ) From 6f13f64aa668155a91e39e99f361687927a4b6f6 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Thu, 26 Aug 2021 17:18:15 +0800 Subject: [PATCH 089/120] fix various format issues Signed-off-by: Jerry Yu --- library/ssl_misc.h | 4 +- library/ssl_tls13_client.c | 94 ++++++++++++++++++-------------------- 2 files changed, 47 insertions(+), 51 deletions(-) diff --git a/library/ssl_misc.h b/library/ssl_misc.h index bda2a7a1a69e..9cf1e4ff39e2 100644 --- a/library/ssl_misc.h +++ b/library/ssl_misc.h @@ -1402,7 +1402,9 @@ void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, size_t total_hs_len ); #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) -int mbedtls_ssl_write_signature_algorithms_ext(mbedtls_ssl_context* ssl, unsigned char* buf, unsigned char* end, size_t* olen); +int mbedtls_ssl_write_signature_algorithms_ext( mbedtls_ssl_context *ssl, + unsigned char *buf, unsigned char *end, + size_t *olen); #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index 036c5e5537bb..62b6ce18f8cc 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -30,8 +30,10 @@ #include "ssl_misc.h" #include +#define CLIENT_HELLO_RAND_BYTES_LEN 32 +#define CLIENT_HELLO_VERSION_LEN 2 /* Main entry point; orchestrates the other functions */ -static int ssl_client_hello_process( mbedtls_ssl_context* ssl ); +static int ssl_client_hello_process( mbedtls_ssl_context *ssl ); int mbedtls_ssl_handshake_client_step_tls1_3( mbedtls_ssl_context *ssl ) { @@ -70,13 +72,13 @@ int mbedtls_ssl_handshake_client_step_tls1_3( mbedtls_ssl_context *ssl ) } -static int ssl_client_hello_prepare( mbedtls_ssl_context* ssl ); -static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl, - unsigned char* buf, size_t buflen, +static int ssl_client_hello_prepare( mbedtls_ssl_context *ssl ); +static int ssl_client_hello_write_partial( mbedtls_ssl_context *ssl, + unsigned char *buf, size_t buflen, size_t *len_with_binders ); -static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl ); +static int ssl_client_hello_postprocess( mbedtls_ssl_context *ssl ); -static int ssl_client_hello_process( mbedtls_ssl_context* ssl ) +static int ssl_client_hello_process( mbedtls_ssl_context *ssl ) { int ret = 0; unsigned char *buf; @@ -106,14 +108,13 @@ static int ssl_client_hello_process( mbedtls_ssl_context* ssl ) return ret; } -static int ssl_client_hello_prepare( mbedtls_ssl_context* ssl ) +static int ssl_client_hello_prepare( mbedtls_ssl_context *ssl ) { int ret; - size_t rand_bytes_len; - rand_bytes_len = 32; - - if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->handshake->randbytes, rand_bytes_len ) ) != 0 ) + if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, + ssl->handshake->randbytes, + CLIENT_HELLO_RAND_BYTES_LEN ) ) != 0 ) { MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret ); return( ret ); @@ -131,27 +132,27 @@ static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl ) /* Write extensions */ -static void ssl_write_supported_versions_ext( mbedtls_ssl_context *ssl, - unsigned char* buf, - unsigned char* end, - size_t* olen ); +static int ssl_write_supported_versions_ext( mbedtls_ssl_context *ssl, + unsigned char *buf, + unsigned char *end, + size_t *olen ); #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl, - unsigned char* buf, - unsigned char* end, - size_t* olen ); + unsigned char *buf, + unsigned char *end, + size_t *olen ); static int ssl_write_key_shares_ext( mbedtls_ssl_context *ssl, - unsigned char* buf, - unsigned char* end, - size_t* olen ); + unsigned char *buf, + unsigned char *end, + size_t *olen ); #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ -static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl, - unsigned char* buf, size_t buflen, +static int ssl_client_hello_write_partial( mbedtls_ssl_context *ssl, + unsigned char *buf, size_t buflen, size_t *len_with_binders ) { /* Extensions */ @@ -169,10 +170,6 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl, size_t cur_ext_len; /* Size of the current extension */ size_t total_ext_len; /* Size of list of extensions */ - /* Length information */ - size_t rand_bytes_len; - size_t version_len; - /* Buffer management */ unsigned char* start = buf; unsigned char* end = buf + buflen; @@ -188,8 +185,6 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl, /* Keeping track of the included extensions */ ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE; - rand_bytes_len = 32; - /* NOTE: * Even for DTLS 1.3, we are writing a TLS handshake header here. * The actual DTLS 1.3 handshake header is inserted in @@ -198,7 +193,6 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl, * For cTLS the length, and the version field * are elided. The random bytes are shorter. */ - version_len = 2; if( ssl->conf->max_major_ver == 0 ) { @@ -218,16 +212,18 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl, * * In cTLS the version number is elided. */ + MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_VERSION_LEN); *buf++ = 0x03; *buf++ = 0x03; - buflen -= version_len; + buflen -= CLIENT_HELLO_VERSION_LEN; /* Write random bytes */ - memcpy( buf, ssl->handshake->randbytes, rand_bytes_len ); - MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf, rand_bytes_len ); + MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_RAND_BYTES_LEN); + memcpy( buf, ssl->handshake->randbytes, CLIENT_HELLO_RAND_BYTES_LEN ); + MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf, CLIENT_HELLO_RAND_BYTES_LEN ); - buf += rand_bytes_len; - buflen -= rand_bytes_len; + buf += CLIENT_HELLO_RAND_BYTES_LEN; + buflen -= CLIENT_HELLO_RAND_BYTES_LEN; /* Versions of TLS before TLS 1.3 supported a * "session resumption" feature which has been merged with pre-shared @@ -396,10 +392,10 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context* ssl, * ProtocolVersion versions<2..254>; * } SupportedVersions; */ -static void ssl_write_supported_versions_ext( mbedtls_ssl_context *ssl, - unsigned char* buf, - unsigned char* end, - size_t* olen ) +static int ssl_write_supported_versions_ext( mbedtls_ssl_context *ssl, + unsigned char *buf, + unsigned char *end, + size_t *olen ) { unsigned char *p = buf; @@ -407,11 +403,7 @@ static void ssl_write_supported_versions_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported version extension" ) ); - if( end < p || (size_t)( end - p ) < 7 ) - { - MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small" ) ); - return; - } + MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 ); *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS >> 8 ) & 0xFF ); *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS ) & 0xFF ); @@ -432,14 +424,16 @@ static void ssl_write_supported_versions_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]", ssl->conf->max_major_ver, ssl->conf->max_minor_ver ) ); *olen = 7; + + return( 0 ); } #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl, - unsigned char* buf, - unsigned char* end, - size_t* olen ) + unsigned char *buf, + unsigned char *end, + size_t *olen ) { ((void) ssl); ((void) buf); @@ -449,9 +443,9 @@ static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl, } static int ssl_write_key_shares_ext( mbedtls_ssl_context *ssl, - unsigned char* buf, - unsigned char* end, - size_t* olen ) + unsigned char *buf, + unsigned char *end, + size_t *olen ) { ((void) ssl); ((void) buf); From e885b7698062920884a4e6b85b08129d2ab8c334 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Thu, 26 Aug 2021 17:32:34 +0800 Subject: [PATCH 090/120] fix too long lines Signed-off-by: Jerry Yu --- library/ssl_tls13_client.c | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index 62b6ce18f8cc..d293629857ab 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -59,7 +59,7 @@ int mbedtls_ssl_handshake_client_step_tls1_3( mbedtls_ssl_context *ssl ) case MBEDTLS_SSL_SERVER_HELLO: // Stop here : we haven't finished whole flow - ret=MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; + ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS ); break; @@ -88,17 +88,20 @@ static int ssl_client_hello_process( mbedtls_ssl_context *ssl ) MBEDTLS_SSL_PROC_CHK( ssl_client_hello_prepare, ( ssl ) ); - MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg, ( ssl, - MBEDTLS_SSL_HS_CLIENT_HELLO, &buf, &buf_len ) ); + MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg, + ( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, + &buf, &buf_len ) ); - MBEDTLS_SSL_PROC_CHK( ssl_client_hello_write_partial, ( ssl, buf, buf_len, &msg_len ) ); + MBEDTLS_SSL_PROC_CHK( ssl_client_hello_write_partial, + ( ssl, buf, buf_len, &msg_len ) ); mbedtls_ssl_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, msg_len ); ssl->handshake->update_checksum( ssl, buf, 0 ); MBEDTLS_SSL_PROC_CHK( ssl_client_hello_postprocess, ( ssl ) ); - MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg, ( ssl, buf_len, msg_len ) ); + MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg, + ( ssl, buf_len, msg_len ) ); cleanup: @@ -112,8 +115,8 @@ static int ssl_client_hello_prepare( mbedtls_ssl_context *ssl ) { int ret; - if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, - ssl->handshake->randbytes, + if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, + ssl->handshake->randbytes, CLIENT_HELLO_RAND_BYTES_LEN ) ) != 0 ) { MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret ); @@ -177,8 +180,8 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context *ssl, /* Ciphersuite-related variables */ const int* ciphersuites; const mbedtls_ssl_ciphersuite_t* ciphersuite_info; - size_t i; /* used to iterate through ciphersuite list */ - /* ciphersuite_start points to the start of the ciphersuite list, i.e. to the length field*/ + /* ciphersuite_start points to the start of + the ciphersuite list, i.e. to the length field*/ unsigned char* ciphersuite_start; size_t ciphersuite_count; @@ -220,7 +223,8 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context *ssl, /* Write random bytes */ MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_RAND_BYTES_LEN); memcpy( buf, ssl->handshake->randbytes, CLIENT_HELLO_RAND_BYTES_LEN ); - MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf, CLIENT_HELLO_RAND_BYTES_LEN ); + MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", + buf, CLIENT_HELLO_RAND_BYTES_LEN ); buf += CLIENT_HELLO_RAND_BYTES_LEN; buflen -= CLIENT_HELLO_RAND_BYTES_LEN; @@ -268,7 +272,7 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context *ssl, buf += 2; buflen -= 2; - for ( i = 0; ciphersuites[i] != 0; i++ ) + for ( size_t i = 0; ciphersuites[i] != 0; i++ ) { ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] ); @@ -280,7 +284,8 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context *ssl, continue; MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s", - (unsigned int) ciphersuites[i], ciphersuite_info->name ) ); + (unsigned int) ciphersuites[i], + ciphersuite_info->name ) ); ciphersuite_count++; @@ -301,7 +306,9 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context *ssl, *ciphersuite_start++ = (unsigned char)( ciphersuite_count*2 >> 8 ); *ciphersuite_start++ = (unsigned char)( ciphersuite_count*2 ); - MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites", ciphersuite_count ) ); + MBEDTLS_SSL_DEBUG_MSG( 3, + ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites", + ciphersuite_count ) ); /* For every TLS 1.3 ClientHello, this vector MUST contain exactly * one byte set to zero, which corresponds to the 'null' compression @@ -421,7 +428,8 @@ static int ssl_write_supported_versions_ext( mbedtls_ssl_context *ssl, mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver, ssl->conf->transport, p ); - MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]", ssl->conf->max_major_ver, ssl->conf->max_minor_ver ) ); + MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]", + ssl->conf->max_major_ver, ssl->conf->max_minor_ver ) ); *olen = 7; From 2ac64193ad5cafa401e69d40b1ff7bc93a584cda Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Thu, 26 Aug 2021 18:38:58 +0800 Subject: [PATCH 091/120] Apply MBEDTLS_PUT_xyz Signed-off-by: Jerry Yu --- library/ssl_tls13_client.c | 24 ++++++++++++------------ library/ssl_tls13_generic.c | 8 ++++---- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index d293629857ab..cd929ce7eddb 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -216,8 +216,8 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context *ssl, * In cTLS the version number is elided. */ MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_VERSION_LEN); - *buf++ = 0x03; - *buf++ = 0x03; + MBEDTLS_PUT_UINT16_BE( 0x0303, buf, 0); + buf += 2; buflen -= CLIENT_HELLO_VERSION_LEN; /* Write random bytes */ @@ -295,16 +295,16 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context *ssl, return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); } - *buf++ = (unsigned char)( ciphersuites[i] >> 8 ); - *buf++ = (unsigned char)( ciphersuites[i] ); + MBEDTLS_PUT_UINT16_BE( ciphersuites[i], buf, 0); + buf += 2; buflen -= 2; } /* write ciphersuite length now */ - *ciphersuite_start++ = (unsigned char)( ciphersuite_count*2 >> 8 ); - *ciphersuite_start++ = (unsigned char)( ciphersuite_count*2 ); + MBEDTLS_PUT_UINT16_BE( ciphersuite_count*2, ciphersuite_start, 0); + ciphersuite_start += 2; MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites", @@ -385,8 +385,8 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extension_start, total_ext_len ); /* Write extension length */ - *extension_start++ = (unsigned char)( ( total_ext_len >> 8 ) & 0xFF ); - *extension_start++ = (unsigned char)( ( total_ext_len ) & 0xFF ); + MBEDTLS_PUT_UINT16_BE( total_ext_len, extension_start, 0); + extension_start += 2; *len_with_binders = ( extension_start + total_ext_len ) - start; return( 0 ); @@ -412,12 +412,12 @@ static int ssl_write_supported_versions_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS >> 8 ) & 0xFF ); - *p++ = (unsigned char)( ( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS ) & 0xFF ); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0); /* total length */ - *p++ = 0x00; - *p++ = 3; + MBEDTLS_PUT_UINT16_BE( 3, p, 2); + + p+=4; /* length of next field */ *p++ = 0x2; diff --git a/library/ssl_tls13_generic.c b/library/ssl_tls13_generic.c index 7ec7423ea067..4a0493a94089 100644 --- a/library/ssl_tls13_generic.c +++ b/library/ssl_tls13_generic.c @@ -62,10 +62,10 @@ void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, unsigned char hs_hdr[4]; /* Build HS header for checksum update. */ - hs_hdr[0] = hs_type; - hs_hdr[1] = (unsigned char)( total_hs_len >> 16 ); - hs_hdr[2] = (unsigned char)( total_hs_len >> 8 ); - hs_hdr[3] = (unsigned char)( total_hs_len >> 0 ); + hs_hdr[0] = MBEDTLS_BYTE_0( hs_type ); + hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len ); + hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len ); + hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len ); ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) ); } From 55b90386004e882922bbb045144e047865ba2fd2 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Thu, 26 Aug 2021 18:42:05 +0800 Subject: [PATCH 092/120] fix coding style issue Signed-off-by: Jerry Yu --- library/ssl_tls13_client.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index cd929ce7eddb..8996e8adcb61 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -107,7 +107,7 @@ static int ssl_client_hello_process( mbedtls_ssl_context *ssl ) MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) ); /* client_hello_process haven't finished */ - ret=MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; + ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; return ret; } From f443681f561e604740f6626f8fb411c52e2bf696 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Thu, 26 Aug 2021 22:59:56 +0800 Subject: [PATCH 093/120] fix function name conversion issues Signed-off-by: Jerry Yu --- library/ssl_misc.h | 12 ++++---- library/ssl_tls.c | 2 +- library/ssl_tls13_client.c | 55 +++++++++++++++++++------------------ library/ssl_tls13_generic.c | 10 +++---- 4 files changed, 40 insertions(+), 39 deletions(-) diff --git a/library/ssl_misc.h b/library/ssl_misc.h index 9cf1e4ff39e2..016dfe162e3a 100644 --- a/library/ssl_misc.h +++ b/library/ssl_misc.h @@ -949,7 +949,7 @@ int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl ); int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl ); void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ); #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) -int mbedtls_ssl_handshake_client_step_tls1_3( mbedtls_ssl_context *ssl ); +int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl ); int mbedtls_ssl_handshake_server_step_tls1_3( mbedtls_ssl_context *ssl ); #endif @@ -1385,24 +1385,24 @@ static inline int mbedtls_ssl_conf_is_hybrid_tls12_tls13( const mbedtls_ssl_conf #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) static inline void mbedtls_ssl_handshake_set_state( mbedtls_ssl_context* ssl, - int state ) + mbedtls_ssl_states state ) { ssl->state = state; } -int mbedtls_ssl_start_handshake_msg( mbedtls_ssl_context *ssl, +int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl, unsigned hs_type, unsigned char **buf, size_t *buflen ); -int mbedtls_ssl_finish_handshake_msg( mbedtls_ssl_context *ssl, +int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl, size_t buf_len, size_t msg_len ); -void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, +void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, unsigned hs_type, size_t total_hs_len ); #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) -int mbedtls_ssl_write_signature_algorithms_ext( mbedtls_ssl_context *ssl, +int mbedtls_ssl_tls13_write_signature_algorithms_ext( mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end, size_t *olen); #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 1c8c7bd7340b..831cc52ac086 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -5181,7 +5181,7 @@ int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) { #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) - ret = mbedtls_ssl_handshake_client_step_tls1_3( ssl ); + ret = mbedtls_ssl_tls13_handshake_client_step( ssl ); #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ #if defined(MBEDTLS_SSL_PROTO_TLS1_2) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index 8996e8adcb61..ceb692ea5612 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -33,9 +33,9 @@ #define CLIENT_HELLO_RAND_BYTES_LEN 32 #define CLIENT_HELLO_VERSION_LEN 2 /* Main entry point; orchestrates the other functions */ -static int ssl_client_hello_process( mbedtls_ssl_context *ssl ); +static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl ); -int mbedtls_ssl_handshake_client_step_tls1_3( mbedtls_ssl_context *ssl ) +int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl ) { int ret = 0; @@ -54,7 +54,7 @@ int mbedtls_ssl_handshake_client_step_tls1_3( mbedtls_ssl_context *ssl ) break; case MBEDTLS_SSL_CLIENT_HELLO: - ret = ssl_client_hello_process( ssl ); + ret = ssl_tls13_write_client_hello( ssl ); break; case MBEDTLS_SSL_SERVER_HELLO: @@ -72,13 +72,13 @@ int mbedtls_ssl_handshake_client_step_tls1_3( mbedtls_ssl_context *ssl ) } -static int ssl_client_hello_prepare( mbedtls_ssl_context *ssl ); -static int ssl_client_hello_write_partial( mbedtls_ssl_context *ssl, +static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl ); +static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl, unsigned char *buf, size_t buflen, size_t *len_with_binders ); -static int ssl_client_hello_postprocess( mbedtls_ssl_context *ssl ); +static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl ); -static int ssl_client_hello_process( mbedtls_ssl_context *ssl ) +static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl ) { int ret = 0; unsigned char *buf; @@ -86,21 +86,21 @@ static int ssl_client_hello_process( mbedtls_ssl_context *ssl ) MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) ); - MBEDTLS_SSL_PROC_CHK( ssl_client_hello_prepare, ( ssl ) ); + MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello, ( ssl ) ); - MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_start_handshake_msg, + MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg, ( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, &buf, &buf_len ) ); - MBEDTLS_SSL_PROC_CHK( ssl_client_hello_write_partial, + MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_exts_client_hello, ( ssl, buf, buf_len, &msg_len ) ); - mbedtls_ssl_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, + mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, msg_len ); ssl->handshake->update_checksum( ssl, buf, 0 ); - MBEDTLS_SSL_PROC_CHK( ssl_client_hello_postprocess, ( ssl ) ); - MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_finish_handshake_msg, + MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello, ( ssl ) ); + MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg, ( ssl, buf_len, msg_len ) ); cleanup: @@ -111,7 +111,7 @@ static int ssl_client_hello_process( mbedtls_ssl_context *ssl ) return ret; } -static int ssl_client_hello_prepare( mbedtls_ssl_context *ssl ) +static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl ) { int ret; @@ -126,7 +126,7 @@ static int ssl_client_hello_prepare( mbedtls_ssl_context *ssl ) return( 0 ); } -static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl ) +static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context* ssl ) { mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO ); @@ -135,26 +135,26 @@ static int ssl_client_hello_postprocess( mbedtls_ssl_context* ssl ) /* Write extensions */ -static int ssl_write_supported_versions_ext( mbedtls_ssl_context *ssl, +static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end, size_t *olen ); #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) -static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl, +static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end, size_t *olen ); -static int ssl_write_key_shares_ext( mbedtls_ssl_context *ssl, +static int ssl_tls13_write_key_shares_ext( mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end, size_t *olen ); #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ -static int ssl_client_hello_write_partial( mbedtls_ssl_context *ssl, +static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl, unsigned char *buf, size_t buflen, size_t *len_with_binders ) { @@ -337,7 +337,7 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context *ssl, * For cTLS we only need to provide it if there is more than one version * and currently there is only one. */ - ssl_write_supported_versions_ext( ssl, buf, end, &cur_ext_len ); + ssl_tls13_write_supported_versions_ext( ssl, buf, end, &cur_ext_len ); total_ext_len += cur_ext_len; buf += cur_ext_len; @@ -345,7 +345,7 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context *ssl, /* The supported_groups and the key_share extensions are * REQUIRED for ECDHE ciphersuites. */ - ret = ssl_write_supported_groups_ext( ssl, buf, end, &cur_ext_len ); + ret = ssl_tls13_write_supported_groups_ext( ssl, buf, end, &cur_ext_len ); if( ret != 0 ) return( ret ); @@ -354,7 +354,8 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context *ssl, /* The supported_signature_algorithms extension is REQUIRED for * certificate authenticated ciphersuites. */ - ret = mbedtls_ssl_write_signature_algorithms_ext( ssl, buf, end, &cur_ext_len ); + ret = mbedtls_ssl_tls13_write_signature_algorithms_ext( ssl, buf, + end, &cur_ext_len ); if( ret != 0 ) return( ret ); @@ -369,7 +370,7 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context *ssl, * 3 ) Or, in case all ciphers are supported ( which includes #1 and #2 from above ) */ - ret = ssl_write_key_shares_ext( ssl, buf, end, &cur_ext_len ); + ret = ssl_tls13_write_key_shares_ext( ssl, buf, end, &cur_ext_len ); if( ret != 0 ) return( ret ); @@ -393,13 +394,13 @@ static int ssl_client_hello_write_partial( mbedtls_ssl_context *ssl, } /* - * ssl_write_supported_versions_ext(): + * ssl_tls13_write_supported_versions_ext(): * * struct { * ProtocolVersion versions<2..254>; * } SupportedVersions; */ -static int ssl_write_supported_versions_ext( mbedtls_ssl_context *ssl, +static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end, size_t *olen ) @@ -438,7 +439,7 @@ static int ssl_write_supported_versions_ext( mbedtls_ssl_context *ssl, #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) -static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl, +static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end, size_t *olen ) @@ -450,7 +451,7 @@ static int ssl_write_supported_groups_ext( mbedtls_ssl_context *ssl, return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); } -static int ssl_write_key_shares_ext( mbedtls_ssl_context *ssl, +static int ssl_tls13_write_key_shares_ext( mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end, size_t *olen ) diff --git a/library/ssl_tls13_generic.c b/library/ssl_tls13_generic.c index 4a0493a94089..be441415186a 100644 --- a/library/ssl_tls13_generic.c +++ b/library/ssl_tls13_generic.c @@ -27,7 +27,7 @@ #include "ssl_misc.h" -int mbedtls_ssl_start_handshake_msg( mbedtls_ssl_context *ssl, +int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl, unsigned hs_type, unsigned char **buf, size_t *buflen ) @@ -41,7 +41,7 @@ int mbedtls_ssl_start_handshake_msg( mbedtls_ssl_context *ssl, return( 0 ); } -int mbedtls_ssl_finish_handshake_msg( mbedtls_ssl_context *ssl, +int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl, size_t buf_len, size_t msg_len ) { @@ -55,7 +55,7 @@ int mbedtls_ssl_finish_handshake_msg( mbedtls_ssl_context *ssl, return( ret ); } -void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, +void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, unsigned hs_type, size_t total_hs_len ) { @@ -73,7 +73,7 @@ void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) /* - * mbedtls_ssl_write_signature_algorithms_ext( ) + * mbedtls_ssl_tls13_write_signature_algorithms_ext( ) * * enum { * .... @@ -90,7 +90,7 @@ void mbedtls_ssl_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, * Only if we handle at least one key exchange that needs signatures. */ -int mbedtls_ssl_write_signature_algorithms_ext( mbedtls_ssl_context *ssl, +int mbedtls_ssl_tls13_write_signature_algorithms_ext( mbedtls_ssl_context *ssl, unsigned char* buf, unsigned char* end, size_t* olen ) From d532fe77206296f428aba403416e394bb0a1dc7c Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Thu, 26 Aug 2021 23:11:55 +0800 Subject: [PATCH 094/120] write client hello also in hello reqeust Signed-off-by: Jerry Yu --- library/ssl_tls13_client.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index ceb692ea5612..0a07e06c4040 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -49,10 +49,11 @@ int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl ) switch( ssl->state ) { + /* + * ssl->state is initialized as HELLO_REQUEST. It is same + * with CLIENT_HELLO status + */ case MBEDTLS_SSL_HELLO_REQUEST: - mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_HELLO ); - break; - case MBEDTLS_SSL_CLIENT_HELLO: ret = ssl_tls13_write_client_hello( ssl ); break; From 9e42f6efd36a661f177da276487a5242fd1006fc Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Fri, 27 Aug 2021 15:14:01 +0800 Subject: [PATCH 095/120] Revert "Move random function check" This reverts commit cc88b34f7942f57ea0fd27ee4b3e29f49c91f10e. It causes many test fail. It should be re-considered. Signed-off-by: Jerry Yu --- library/ssl_tls.c | 6 ------ library/ssl_tls13_client.c | 6 ++++++ 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 831cc52ac086..c43f95ee1b54 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -3182,12 +3182,6 @@ static int ssl_conf_check(const mbedtls_ssl_context *ssl) if( ret != 0 ) return( ret ); - if( ssl->conf->f_rng == NULL ) - { - MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) ); - return( MBEDTLS_ERR_SSL_NO_RNG ); - } - /* Space for further checks */ return( 0 ); diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index 0a07e06c4040..f5a6e20cab9c 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -116,6 +116,12 @@ static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl ) { int ret; + if( ssl->conf->f_rng == NULL ) + { + MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) ); + return( MBEDTLS_ERR_SSL_NO_RNG ); + } + if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->handshake->randbytes, CLIENT_HELLO_RAND_BYTES_LEN ) ) != 0 ) From 9176c3ad8c58b68d1770dcdf46fd90bbd5072968 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Fri, 27 Aug 2021 14:58:49 +0800 Subject: [PATCH 096/120] trim spaces Signed-off-by: Jerry Yu --- library/ssl_tls13_client.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index f5a6e20cab9c..3611f70b0dd9 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -49,7 +49,7 @@ int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl ) switch( ssl->state ) { - /* + /* * ssl->state is initialized as HELLO_REQUEST. It is same * with CLIENT_HELLO status */ @@ -361,7 +361,7 @@ static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl, /* The supported_signature_algorithms extension is REQUIRED for * certificate authenticated ciphersuites. */ - ret = mbedtls_ssl_tls13_write_signature_algorithms_ext( ssl, buf, + ret = mbedtls_ssl_tls13_write_signature_algorithms_ext( ssl, buf, end, &cur_ext_len ); if( ret != 0 ) return( ret ); From 92c6b402d7ccf127d4700b49af5a9977c908ee3f Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Fri, 27 Aug 2021 16:59:09 +0800 Subject: [PATCH 097/120] Remove prototype of static functions Signed-off-by: Jerry Yu --- library/ssl_tls13_client.c | 290 +++++++++++++++++-------------------- 1 file changed, 133 insertions(+), 157 deletions(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index 3611f70b0dd9..41b133437de3 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -32,135 +32,82 @@ #define CLIENT_HELLO_RAND_BYTES_LEN 32 #define CLIENT_HELLO_VERSION_LEN 2 -/* Main entry point; orchestrates the other functions */ -static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl ); -int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl ) -{ - int ret = 0; - - if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL ) - { - MBEDTLS_SSL_DEBUG_MSG( 2, ( "Handshake completed but ssl->handshake is NULL.\n" ) ); - return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); - } - - MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) ); - - switch( ssl->state ) - { - /* - * ssl->state is initialized as HELLO_REQUEST. It is same - * with CLIENT_HELLO status - */ - case MBEDTLS_SSL_HELLO_REQUEST: - case MBEDTLS_SSL_CLIENT_HELLO: - ret = ssl_tls13_write_client_hello( ssl ); - break; - - case MBEDTLS_SSL_SERVER_HELLO: - // Stop here : we haven't finished whole flow - ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; - mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS ); - break; - - default: - MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); - return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); - } - - return( ret ); -} - - -static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl ); -static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl, - unsigned char *buf, size_t buflen, - size_t *len_with_binders ); -static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl ); +/* Write extensions */ -static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl ) +/* + * ssl_tls13_write_supported_versions_ext(): + * + * struct { + * ProtocolVersion versions<2..254>; + * } SupportedVersions; + */ +static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl, + unsigned char *buf, + unsigned char *end, + size_t *olen ) { - int ret = 0; - unsigned char *buf; - size_t buf_len, msg_len; - - MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) ); - - MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello, ( ssl ) ); - - MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg, - ( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, - &buf, &buf_len ) ); - - MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_exts_client_hello, - ( ssl, buf, buf_len, &msg_len ) ); + unsigned char *p = buf; - mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, - msg_len ); - ssl->handshake->update_checksum( ssl, buf, 0 ); + *olen = 0; - MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello, ( ssl ) ); - MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg, - ( ssl, buf_len, msg_len ) ); + MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported version extension" ) ); -cleanup: + MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 ); - MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) ); - /* client_hello_process haven't finished */ - ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; - return ret; -} + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0); -static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl ) -{ - int ret; + /* total length */ + MBEDTLS_PUT_UINT16_BE( 3, p, 2); + p+=4; - if( ssl->conf->f_rng == NULL ) - { - MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) ); - return( MBEDTLS_ERR_SSL_NO_RNG ); - } + /* length of next field */ + *p++ = 0x2; - if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, - ssl->handshake->randbytes, - CLIENT_HELLO_RAND_BYTES_LEN ) ) != 0 ) - { - MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret ); - return( ret ); - } + /* This implementation only supports a single TLS version, and only + * advertises a single value. + */ + mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver, + ssl->conf->transport, p ); - return( 0 ); -} + MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]", + ssl->conf->max_major_ver, ssl->conf->max_minor_ver ) ); -static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context* ssl ) -{ - mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO ); + *olen = 7; return( 0 ); } -/* Write extensions */ - -static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl, - unsigned char *buf, - unsigned char *end, - size_t *olen ); - #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl, - unsigned char *buf, - unsigned char *end, - size_t *olen ); + unsigned char *buf, + unsigned char *end, + size_t *olen ) +{ + ((void) ssl); + ((void) buf); + ((void) end); + ((void) olen); + return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); +} static int ssl_tls13_write_key_shares_ext( mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end, - size_t *olen ); + size_t *olen ) +{ + ((void) ssl); + ((void) buf); + ((void) end); + ((void) olen); + return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); +} #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ +/* Functions for ClientHello */ + static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl, unsigned char *buf, size_t buflen, size_t *len_with_binders ) @@ -344,7 +291,9 @@ static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl, * For cTLS we only need to provide it if there is more than one version * and currently there is only one. */ - ssl_tls13_write_supported_versions_ext( ssl, buf, end, &cur_ext_len ); + ret = ssl_tls13_write_supported_versions_ext( ssl, buf, end, &cur_ext_len ); + if( ret != 0 ) + return( ret ); total_ext_len += cur_ext_len; buf += cur_ext_len; @@ -400,77 +349,104 @@ static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl, return( 0 ); } -/* - * ssl_tls13_write_supported_versions_ext(): - * - * struct { - * ProtocolVersion versions<2..254>; - * } SupportedVersions; - */ -static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl, - unsigned char *buf, - unsigned char *end, - size_t *olen ) +static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context* ssl ) { - unsigned char *p = buf; + mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO ); + return( 0 ); +} - *olen = 0; +static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl ) +{ + int ret; - MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported version extension" ) ); + if( ssl->conf->f_rng == NULL ) + { + MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) ); + return( MBEDTLS_ERR_SSL_NO_RNG ); + } - MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 ); + if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, + ssl->handshake->randbytes, + CLIENT_HELLO_RAND_BYTES_LEN ) ) != 0 ) + { + MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret ); + return( ret ); + } - MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0); + return( 0 ); +} - /* total length */ - MBEDTLS_PUT_UINT16_BE( 3, p, 2); +/* + * ClientHello Main entry point. + * orchestrates the other functions. + */ +static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl ) +{ + int ret = 0; + unsigned char *buf; + size_t buf_len, msg_len; - p+=4; + MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) ); - /* length of next field */ - *p++ = 0x2; + MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello, ( ssl ) ); - /* This implementation only supports a single TLS version, and only - * advertises a single value. - */ - mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver, - ssl->conf->transport, p ); + MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg, + ( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, + &buf, &buf_len ) ); - MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]", - ssl->conf->max_major_ver, ssl->conf->max_minor_ver ) ); + MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_exts_client_hello, + ( ssl, buf, buf_len, &msg_len ) ); - *olen = 7; + mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, + msg_len ); + ssl->handshake->update_checksum( ssl, buf, 0 ); - return( 0 ); -} + MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello, ( ssl ) ); + MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg, + ( ssl, buf_len, msg_len ) ); -#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) +cleanup: -static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl, - unsigned char *buf, - unsigned char *end, - size_t *olen ) -{ - ((void) ssl); - ((void) buf); - ((void) end); - ((void) olen); - return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); + MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) ); + return ret; } -static int ssl_tls13_write_key_shares_ext( mbedtls_ssl_context *ssl, - unsigned char *buf, - unsigned char *end, - size_t *olen ) +int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl ) { - ((void) ssl); - ((void) buf); - ((void) end); - ((void) olen); - return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); -} + int ret = 0; -#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ + if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL ) + { + MBEDTLS_SSL_DEBUG_MSG( 2, ( "Handshake completed but ssl->handshake is NULL.\n" ) ); + return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); + } + + MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) ); + + switch( ssl->state ) + { + /* + * ssl->state is initialized as HELLO_REQUEST. It is same + * with CLIENT_HELLO status + */ + case MBEDTLS_SSL_HELLO_REQUEST: + case MBEDTLS_SSL_CLIENT_HELLO: + ret = ssl_tls13_write_client_hello( ssl ); + break; + + case MBEDTLS_SSL_SERVER_HELLO: + // Stop here : we haven't finished whole flow + ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; + mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS ); + break; + + default: + MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) ); + return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); + } + + return( ret ); +} #endif /* MBEDTLS_SSL_CLI_C */ From 275619336a78c6879f82168e00fa6b510ee63ccc Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Fri, 27 Aug 2021 17:07:38 +0800 Subject: [PATCH 098/120] fix name conversion issue for tls13 server entry Signed-off-by: Jerry Yu --- library/ssl_misc.h | 2 +- library/ssl_tls.c | 2 +- library/ssl_tls13_server.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/library/ssl_misc.h b/library/ssl_misc.h index 016dfe162e3a..10b07b751a79 100644 --- a/library/ssl_misc.h +++ b/library/ssl_misc.h @@ -950,7 +950,7 @@ int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl ); void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ); #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl ); -int mbedtls_ssl_handshake_server_step_tls1_3( mbedtls_ssl_context *ssl ); +int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl ); #endif int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl ); diff --git a/library/ssl_tls.c b/library/ssl_tls.c index c43f95ee1b54..360419240f56 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -5189,7 +5189,7 @@ int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl ) { #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) if( mbedtls_ssl_conf_is_tls13_only( ssl->conf ) ) - ret = mbedtls_ssl_handshake_server_step_tls1_3( ssl ); + ret = mbedtls_ssl_tls13_handshake_server_step( ssl ); #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ #if defined(MBEDTLS_SSL_PROTO_TLS1_2) diff --git a/library/ssl_tls13_server.c b/library/ssl_tls13_server.c index a56727741bc2..0dcd7ed60273 100644 --- a/library/ssl_tls13_server.c +++ b/library/ssl_tls13_server.c @@ -25,7 +25,7 @@ #include "ssl_misc.h" -int mbedtls_ssl_handshake_server_step_tls1_3( mbedtls_ssl_context *ssl ) +int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl ) { ((void) ssl); return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); From 5cc8f0a0d849407adac6e033a80a0b8cf34eb58a Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Fri, 27 Aug 2021 17:21:44 +0800 Subject: [PATCH 099/120] Add simple document for tls13 functions Signed-off-by: Jerry Yu --- library/ssl_misc.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/library/ssl_misc.h b/library/ssl_misc.h index 10b07b751a79..8c38cd06e182 100644 --- a/library/ssl_misc.h +++ b/library/ssl_misc.h @@ -132,8 +132,15 @@ #define MBEDTLS_SSL_EXT_SIG_ALG_CERT ( 1 << 20 ) #define MBEDTLS_SSL_EXT_KEY_SHARE ( 1 << 21 ) +/* + * Helper macros for function call with returen check. + */ +/* utils for strip parens in marcro */ #define MBEDTLS_SSL_PROC_STRIP_PARENS( ... ) __VA_ARGS__ +/* + * Exit and print debug message when return none zero value + */ #define MBEDTLS_SSL_PROC_CHK( fn, args ) \ do { \ ret = fn(MBEDTLS_SSL_PROC_STRIP_PARENS args); \ @@ -146,6 +153,9 @@ } \ } while( 0 ) +/* + * Exit and print debug message when return negative value + */ #define MBEDTLS_SSL_PROC_CHK_NEG( fn, args ) \ do { \ ret = fn(MBEDTLS_SSL_PROC_STRIP_PARENS args); \ @@ -949,7 +959,18 @@ int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl ); int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl ); void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ); #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) +/** + * \brief TLS1.3 client side state machine entry + * + * \param ssl SSL context + */ int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl ); + +/** + * \brief TLS1.3 server side state machine entry + * + * \param ssl SSL context + */ int mbedtls_ssl_tls13_handshake_server_step( mbedtls_ssl_context *ssl ); #endif @@ -1390,18 +1411,30 @@ static inline void mbedtls_ssl_handshake_set_state( mbedtls_ssl_context* ssl, ssl->state = state; } +/* + * Write tls13 handshake message header + */ int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl, unsigned hs_type, unsigned char **buf, size_t *buflen ); +/* + * Write tls13 handshake message tail + */ int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl, size_t buf_len, size_t msg_len ); +/* + * Update checksum with handshake header + */ void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, unsigned hs_type, size_t total_hs_len ); #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) +/* + * Write TLS1.3 Signature Algorithm extesion + */ int mbedtls_ssl_tls13_write_signature_algorithms_ext( mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end, size_t *olen); From c4d22444d65c6483e349f3019ed38454c41176e8 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Fri, 27 Aug 2021 20:04:33 +0800 Subject: [PATCH 100/120] fix undeclared variable error Signed-off-by: Jerry Yu # Conflicts: # library/ssl_tls13_client.c --- library/ssl_tls13_client.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index 41b133437de3..a03aa8e461e3 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -112,7 +112,7 @@ static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl, unsigned char *buf, size_t buflen, size_t *len_with_binders ) { - /* Extensions */ + /* Extensions */ /* extension_start * Used during extension writing where the @@ -120,9 +120,7 @@ static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl, * extension list must be kept to write * the total extension list size in the end. */ -#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) int ret; -#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ unsigned char* extension_start; size_t cur_ext_len; /* Size of the current extension */ size_t total_ext_len; /* Size of list of extensions */ From 995ecd396ffbba84dcf84d3399c6fd270ec1b820 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Mon, 30 Aug 2021 17:53:49 +0800 Subject: [PATCH 101/120] fix wrong iana values and comments Signed-off-by: Jerry Yu --- include/mbedtls/ssl.h | 14 +++++++------- library/ssl_misc.h | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/mbedtls/ssl.h b/include/mbedtls/ssl.h index b7b22c20d4ed..540ff1fc52f1 100644 --- a/include/mbedtls/ssl.h +++ b/include/mbedtls/ssl.h @@ -472,21 +472,21 @@ #define MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH 1 #define MBEDTLS_TLS_EXT_TRUNCATED_HMAC 4 -#define MBEDTLS_TLS_EXT_STATUS_REQUEST 5 /* RFC 8446 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_STATUS_REQUEST 5 /* RFC 6066 TLS 1.3 */ #define MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES 10 -#define MBEDTLS_TLS_EXT_SUPPORTED_GROUPS 10 /* RFC 8446 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_SUPPORTED_GROUPS 10 /* RFC 8422,7919 TLS 1.3 */ #define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS 11 #define MBEDTLS_TLS_EXT_SIG_ALG 13 /* RFC 8446 TLS 1.3 */ #define MBEDTLS_TLS_EXT_USE_SRTP 14 -#define MBEDTLS_TLS_EXT_HEARTBEAT 15 /* RFC 8446 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_HEARTBEAT 15 /* RFC 6520 TLS 1.3 */ #define MBEDTLS_TLS_EXT_ALPN 16 -#define MBEDTLS_TLS_EXT_SCT 18 /* RFC 8446 TLS 1.3 */ -#define MBEDTLS_TLS_EXT_SERV_CERT_TYPE 19 /* RFC 8446 TLS 1.3 */ -#define MBEDTLS_TLS_EXT_CLI_CERT_TYPE 20 /* RFC 8446 TLS 1.3 */ -#define MBEDTLS_TLS_EXT_PADDING 21 /* RFC 8446 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_SCT 18 /* RFC 6962 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_CLI_CERT_TYPE 19 /* RFC 7259 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_SERV_CERT_TYPE 20 /* RFC 7250 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_PADDING 21 /* RFC 7685 TLS 1.3 */ #define MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC 22 /* 0x16 */ #define MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET 0x0017 /* 23 */ diff --git a/library/ssl_misc.h b/library/ssl_misc.h index 8c38cd06e182..32017f3458e0 100644 --- a/library/ssl_misc.h +++ b/library/ssl_misc.h @@ -118,8 +118,8 @@ #define MBEDTLS_SSL_EXT_HEARTBEAT ( 1 << 6 ) #define MBEDTLS_SSL_EXT_ALPN ( 1 << 7 ) #define MBEDTLS_SSL_EXT_SCT ( 1 << 8 ) -#define MBEDTLS_SSL_EXT_SERV_CERT_TYPE ( 1 << 9 ) -#define MBEDTLS_SSL_EXT_CLI_CERT_TYPE ( 1 << 10 ) +#define MBEDTLS_SSL_EXT_CLI_CERT_TYPE ( 1 << 9 ) +#define MBEDTLS_SSL_EXT_SERV_CERT_TYPE ( 1 << 10 ) #define MBEDTLS_SSL_EXT_PADDING ( 1 << 11 ) #define MBEDTLS_SSL_EXT_PRE_SHARED_KEY ( 1 << 12 ) #define MBEDTLS_SSL_EXT_EARLY_DATA ( 1 << 13 ) From eecfbf001cb6dff76bb6756e259818ce40fc38d1 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Mon, 30 Aug 2021 18:32:07 +0800 Subject: [PATCH 102/120] fix format issues Signed-off-by: Jerry Yu --- library/ssl_misc.h | 23 ++++++++-------- library/ssl_tls13_client.c | 55 +++++++++++++++++++------------------ library/ssl_tls13_generic.c | 23 ++++++++-------- 3 files changed, 53 insertions(+), 48 deletions(-) diff --git a/library/ssl_misc.h b/library/ssl_misc.h index 32017f3458e0..50aee6ffd23a 100644 --- a/library/ssl_misc.h +++ b/library/ssl_misc.h @@ -1405,8 +1405,8 @@ static inline int mbedtls_ssl_conf_is_hybrid_tls12_tls13( const mbedtls_ssl_conf #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) -static inline void mbedtls_ssl_handshake_set_state( mbedtls_ssl_context* ssl, - mbedtls_ssl_states state ) +static inline void mbedtls_ssl_handshake_set_state( mbedtls_ssl_context *ssl, + mbedtls_ssl_states state ) { ssl->state = state; } @@ -1415,29 +1415,30 @@ static inline void mbedtls_ssl_handshake_set_state( mbedtls_ssl_context* ssl, * Write tls13 handshake message header */ int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl, - unsigned hs_type, - unsigned char **buf, - size_t *buflen ); + unsigned hs_type, + unsigned char **buf, + size_t *buflen ); /* * Write tls13 handshake message tail */ int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl, - size_t buf_len, - size_t msg_len ); + size_t buf_len, + size_t msg_len ); /* * Update checksum with handshake header */ void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, - unsigned hs_type, - size_t total_hs_len ); + unsigned hs_type, + size_t total_hs_len ); #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) /* * Write TLS1.3 Signature Algorithm extesion */ int mbedtls_ssl_tls13_write_signature_algorithms_ext( mbedtls_ssl_context *ssl, - unsigned char *buf, unsigned char *end, - size_t *olen); + unsigned char *buf, + unsigned char *end, + size_t *olen); #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index a03aa8e461e3..ef9836d1950e 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -43,9 +43,9 @@ * } SupportedVersions; */ static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl, - unsigned char *buf, - unsigned char *end, - size_t *olen ) + unsigned char *buf, + unsigned char *end, + size_t *olen ) { unsigned char *p = buf; @@ -55,11 +55,11 @@ static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 ); - MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0); + MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 ); /* total length */ MBEDTLS_PUT_UINT16_BE( 3, p, 2); - p+=4; + p += 4; /* length of next field */ *p++ = 0x2; @@ -67,11 +67,13 @@ static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl, /* This implementation only supports a single TLS version, and only * advertises a single value. */ - mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver, - ssl->conf->transport, p ); + mbedtls_ssl_write_version( ssl->conf->max_major_ver, + ssl->conf->max_minor_ver, + ssl->conf->transport, p ); MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]", - ssl->conf->max_major_ver, ssl->conf->max_minor_ver ) ); + ssl->conf->max_major_ver, + ssl->conf->max_minor_ver ) ); *olen = 7; @@ -81,9 +83,9 @@ static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl, #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl, - unsigned char *buf, - unsigned char *end, - size_t *olen ) + unsigned char *buf, + unsigned char *end, + size_t *olen ) { ((void) ssl); ((void) buf); @@ -93,9 +95,9 @@ static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl, } static int ssl_tls13_write_key_shares_ext( mbedtls_ssl_context *ssl, - unsigned char *buf, - unsigned char *end, - size_t *olen ) + unsigned char *buf, + unsigned char *end, + size_t *olen ) { ((void) ssl); ((void) buf); @@ -109,8 +111,9 @@ static int ssl_tls13_write_key_shares_ext( mbedtls_ssl_context *ssl, /* Functions for ClientHello */ static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl, - unsigned char *buf, size_t buflen, - size_t *len_with_binders ) + unsigned char *buf, + size_t buflen, + size_t *len_with_binders ) { /* Extensions */ @@ -121,20 +124,20 @@ static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl, * the total extension list size in the end. */ int ret; - unsigned char* extension_start; + unsigned char *extension_start; size_t cur_ext_len; /* Size of the current extension */ size_t total_ext_len; /* Size of list of extensions */ /* Buffer management */ - unsigned char* start = buf; - unsigned char* end = buf + buflen; + unsigned char *start = buf; + unsigned char *end = buf + buflen; /* Ciphersuite-related variables */ - const int* ciphersuites; - const mbedtls_ssl_ciphersuite_t* ciphersuite_info; + const int *ciphersuites; + const mbedtls_ssl_ciphersuite_t *ciphersuite_info; /* ciphersuite_start points to the start of the ciphersuite list, i.e. to the length field*/ - unsigned char* ciphersuite_start; + unsigned char *ciphersuite_start; size_t ciphersuite_count; /* Keeping track of the included extensions */ @@ -167,13 +170,13 @@ static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl, * * In cTLS the version number is elided. */ - MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_VERSION_LEN); + MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_VERSION_LEN ); MBEDTLS_PUT_UINT16_BE( 0x0303, buf, 0); buf += 2; buflen -= CLIENT_HELLO_VERSION_LEN; /* Write random bytes */ - MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_RAND_BYTES_LEN); + MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_RAND_BYTES_LEN ); memcpy( buf, ssl->handshake->randbytes, CLIENT_HELLO_RAND_BYTES_LEN ); MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf, CLIENT_HELLO_RAND_BYTES_LEN ); @@ -255,7 +258,7 @@ static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl, } /* write ciphersuite length now */ - MBEDTLS_PUT_UINT16_BE( ciphersuite_count*2, ciphersuite_start, 0); + MBEDTLS_PUT_UINT16_BE( ciphersuite_count*2, ciphersuite_start, 0 ); ciphersuite_start += 2; MBEDTLS_SSL_DEBUG_MSG( 3, @@ -340,7 +343,7 @@ static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extension_start, total_ext_len ); /* Write extension length */ - MBEDTLS_PUT_UINT16_BE( total_ext_len, extension_start, 0); + MBEDTLS_PUT_UINT16_BE( total_ext_len, extension_start, 0 ); extension_start += 2; *len_with_binders = ( extension_start + total_ext_len ) - start; diff --git a/library/ssl_tls13_generic.c b/library/ssl_tls13_generic.c index be441415186a..4b087baa2631 100644 --- a/library/ssl_tls13_generic.c +++ b/library/ssl_tls13_generic.c @@ -28,9 +28,9 @@ #include "ssl_misc.h" int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl, - unsigned hs_type, - unsigned char **buf, - size_t *buflen ) + unsigned hs_type, + unsigned char **buf, + size_t *buflen ) { *buf = ssl->out_msg + 4; *buflen = MBEDTLS_SSL_OUT_CONTENT_LEN - 4; @@ -42,8 +42,8 @@ int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl, } int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl, - size_t buf_len, - size_t msg_len ) + size_t buf_len, + size_t msg_len ) { int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; ((void) buf_len); @@ -56,8 +56,8 @@ int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl, } void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, - unsigned hs_type, - size_t total_hs_len ) + unsigned hs_type, + size_t total_hs_len ) { unsigned char hs_hdr[4]; @@ -90,10 +90,11 @@ void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, * Only if we handle at least one key exchange that needs signatures. */ -int mbedtls_ssl_tls13_write_signature_algorithms_ext( mbedtls_ssl_context *ssl, - unsigned char* buf, - unsigned char* end, - size_t* olen ) +int mbedtls_ssl_tls13_write_signature_algorithms_ext( + mbedtls_ssl_context *ssl, + unsigned char *buf, + unsigned char *end, + size_t *olen ) { ((void) ssl); ((void) buf); From e41dec015846b3e0d461654f6ef06814d10bde61 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 31 Aug 2021 10:57:07 +0800 Subject: [PATCH 103/120] Rename write signature algorithms function To keep similar name with other place. Signed-off-by: Jerry Yu --- library/ssl_misc.h | 8 ++++---- library/ssl_tls13_client.c | 3 +-- library/ssl_tls13_generic.c | 11 +++++------ 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/library/ssl_misc.h b/library/ssl_misc.h index 50aee6ffd23a..6b0bf574b695 100644 --- a/library/ssl_misc.h +++ b/library/ssl_misc.h @@ -1435,10 +1435,10 @@ void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, /* * Write TLS1.3 Signature Algorithm extesion */ -int mbedtls_ssl_tls13_write_signature_algorithms_ext( mbedtls_ssl_context *ssl, - unsigned char *buf, - unsigned char *end, - size_t *olen); +int mbedtls_ssl_tls13_write_sig_alg_ext( mbedtls_ssl_context *ssl, + unsigned char *buf, + unsigned char *end, + size_t *olen); #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index ef9836d1950e..dce83f427bf4 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -311,8 +311,7 @@ static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl, /* The supported_signature_algorithms extension is REQUIRED for * certificate authenticated ciphersuites. */ - ret = mbedtls_ssl_tls13_write_signature_algorithms_ext( ssl, buf, - end, &cur_ext_len ); + ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, buf, end, &cur_ext_len ); if( ret != 0 ) return( ret ); diff --git a/library/ssl_tls13_generic.c b/library/ssl_tls13_generic.c index 4b087baa2631..1713d4c813a5 100644 --- a/library/ssl_tls13_generic.c +++ b/library/ssl_tls13_generic.c @@ -73,7 +73,7 @@ void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) /* - * mbedtls_ssl_tls13_write_signature_algorithms_ext( ) + * mbedtls_ssl_tls13_write_sig_alg_ext( ) * * enum { * .... @@ -90,11 +90,10 @@ void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, * Only if we handle at least one key exchange that needs signatures. */ -int mbedtls_ssl_tls13_write_signature_algorithms_ext( - mbedtls_ssl_context *ssl, - unsigned char *buf, - unsigned char *end, - size_t *olen ) +int mbedtls_ssl_tls13_write_sig_alg_ext( mbedtls_ssl_context *ssl, + unsigned char *buf, + unsigned char *end, + size_t *olen ) { ((void) ssl); ((void) buf); From 08906d006b01566791587a2a9e6e9623bbea7a93 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 31 Aug 2021 11:05:27 +0800 Subject: [PATCH 104/120] fix name conversion issues Signed-off-by: Jerry Yu --- library/ssl_tls13_client.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index dce83f427bf4..80386c80d35b 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -30,8 +30,8 @@ #include "ssl_misc.h" #include -#define CLIENT_HELLO_RAND_BYTES_LEN 32 -#define CLIENT_HELLO_VERSION_LEN 2 +#define CLIENT_HELLO_RANDOM_LEN 32 +#define CLIENT_HELLO_LEGACY_VERSION_LEN 2 /* Write extensions */ @@ -110,7 +110,7 @@ static int ssl_tls13_write_key_shares_ext( mbedtls_ssl_context *ssl, /* Functions for ClientHello */ -static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl, +static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, unsigned char *buf, size_t buflen, size_t *len_with_binders ) @@ -170,19 +170,19 @@ static int ssl_tls13_write_exts_client_hello( mbedtls_ssl_context *ssl, * * In cTLS the version number is elided. */ - MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_VERSION_LEN ); + MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_LEGACY_VERSION_LEN ); MBEDTLS_PUT_UINT16_BE( 0x0303, buf, 0); - buf += 2; - buflen -= CLIENT_HELLO_VERSION_LEN; + buf += CLIENT_HELLO_LEGACY_VERSION_LEN; + buflen -= CLIENT_HELLO_LEGACY_VERSION_LEN; /* Write random bytes */ - MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_RAND_BYTES_LEN ); - memcpy( buf, ssl->handshake->randbytes, CLIENT_HELLO_RAND_BYTES_LEN ); + MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_RANDOM_LEN ); + memcpy( buf, ssl->handshake->randbytes, CLIENT_HELLO_RANDOM_LEN ); MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", - buf, CLIENT_HELLO_RAND_BYTES_LEN ); + buf, CLIENT_HELLO_RANDOM_LEN ); - buf += CLIENT_HELLO_RAND_BYTES_LEN; - buflen -= CLIENT_HELLO_RAND_BYTES_LEN; + buf += CLIENT_HELLO_RANDOM_LEN; + buflen -= CLIENT_HELLO_RANDOM_LEN; /* Versions of TLS before TLS 1.3 supported a * "session resumption" feature which has been merged with pre-shared @@ -367,7 +367,7 @@ static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl ) if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng, ssl->handshake->randbytes, - CLIENT_HELLO_RAND_BYTES_LEN ) ) != 0 ) + CLIENT_HELLO_RANDOM_LEN ) ) != 0 ) { MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret ); return( ret ); @@ -394,7 +394,7 @@ static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl ) ( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, &buf, &buf_len ) ); - MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_exts_client_hello, + MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body, ( ssl, buf, buf_len, &msg_len ) ); mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, From 159c5a0e12b3769b14ae7c7e7aab2fe310eafd05 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 31 Aug 2021 12:51:25 +0800 Subject: [PATCH 105/120] fix comments issues Signed-off-by: Jerry Yu --- library/ssl_misc.h | 6 +++--- library/ssl_tls13_client.c | 42 +++++++++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/library/ssl_misc.h b/library/ssl_misc.h index 6b0bf574b695..b1f5f36f557d 100644 --- a/library/ssl_misc.h +++ b/library/ssl_misc.h @@ -1412,14 +1412,14 @@ static inline void mbedtls_ssl_handshake_set_state( mbedtls_ssl_context *ssl, } /* - * Write tls13 handshake message header + * Write TLS 1.3 handshake message header */ int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl, unsigned hs_type, unsigned char **buf, size_t *buflen ); /* - * Write tls13 handshake message tail + * Write TLS 1.3 handshake message tail */ int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl, size_t buf_len, @@ -1433,7 +1433,7 @@ void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl, #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) /* - * Write TLS1.3 Signature Algorithm extesion + * Write TLS 1.3 Signature Algorithm extension */ int mbedtls_ssl_tls13_write_sig_alg_ext( mbedtls_ssl_context *ssl, unsigned char *buf, diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index 80386c80d35b..f30d4082305d 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -51,17 +51,24 @@ static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl, *olen = 0; - MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported version extension" ) ); + MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) ); + /* + * ExtensionType 2 + * ExtensionLength 2 + * VersionSLength 1 + * Version 2 + */ MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 ); + /* Write Extension Type */ MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 ); - /* total length */ + /* Write Extension Length */ MBEDTLS_PUT_UINT16_BE( 3, p, 2); p += 4; - /* length of next field */ + /* Length of the SupportedVersions field data */ *p++ = 0x2; /* This implementation only supports a single TLS version, and only @@ -108,7 +115,7 @@ static int ssl_tls13_write_key_shares_ext( mbedtls_ssl_context *ssl, #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ -/* Functions for ClientHello */ +/* Functions for writing ClientHello message */ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, unsigned char *buf, @@ -319,11 +326,13 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, buf += cur_ext_len; /* We need to send the key shares under three conditions: - * 1 ) A certificate-based ciphersuite is being offered. In this case - * supported_groups and supported_signature extensions have been successfully added. - * 2 ) A PSK-based ciphersuite with ECDHE is offered. In this case the + * 1) A certificate-based ciphersuite is being offered. In this case + * supported_groups and supported_signature extensions have been + * successfully added. + * 2) A PSK-based ciphersuite with ECDHE is offered. In this case the * psk_key_exchange_modes has been added as the last extension. - * 3 ) Or, in case all ciphers are supported ( which includes #1 and #2 from above ) + * 3) Or, in case all ciphers are supported ( which includes #1 and #2 + * from above ) */ ret = ssl_tls13_write_key_shares_ext( ssl, buf, end, &cur_ext_len ); @@ -377,8 +386,21 @@ static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl ) } /* - * ClientHello Main entry point. - * orchestrates the other functions. + * Write ClientHello handshake message. + * + * Structure of this message: + * + * uint16 ProtocolVersion; + * opaque Random[32]; + * uint8 CipherSuite[2]; // Cryptographic suite selector + * struct { + * ProtocolVersion legacy_version = 0x0303; // TLS v1.2 + * Random random; + * opaque legacy_session_id<0..32>; + * CipherSuite cipher_suites<2..2^16-2>; + * opaque legacy_compression_methods<1..2^8-1>; + * Extension extensions<8..2^16-1>; + * } ClientHello; */ static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl ) { From 67d4ed5b22d846d05144989ad2ba33cb0656cb17 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 31 Aug 2021 13:12:43 +0800 Subject: [PATCH 106/120] force change state type Signed-off-by: Jerry Yu --- library/ssl_misc.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/ssl_misc.h b/library/ssl_misc.h index b1f5f36f557d..e93e55b633a5 100644 --- a/library/ssl_misc.h +++ b/library/ssl_misc.h @@ -1408,7 +1408,7 @@ static inline int mbedtls_ssl_conf_is_hybrid_tls12_tls13( const mbedtls_ssl_conf static inline void mbedtls_ssl_handshake_set_state( mbedtls_ssl_context *ssl, mbedtls_ssl_states state ) { - ssl->state = state; + ssl->state = ( int ) state; } /* From 6a643100029d4e739f0f45d5290b81fd3d9e836d Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 31 Aug 2021 14:40:36 +0800 Subject: [PATCH 107/120] Cleanup client_hello body. cleanup `ssl_tls13_write_client_hello_body`, fix comments issues. And move ciphersuites to separate function Signed-off-by: Jerry Yu --- library/ssl_tls13_client.c | 186 ++++++++++++++++++------------------- 1 file changed, 89 insertions(+), 97 deletions(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index f30d4082305d..f9cfff521774 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -115,8 +115,79 @@ static int ssl_tls13_write_key_shares_ext( mbedtls_ssl_context *ssl, #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ -/* Functions for writing ClientHello message */ +/* Write ciphersuites + * CipherSuite cipher_suites<2..2^16-2>; + */ +static int ssl_tls13_write_client_hello_ciphersuites( + mbedtls_ssl_context *ssl, + unsigned char *buf, + unsigned char *end, + size_t *olen ) +{ + /* Ciphersuite-related variables */ + const int *ciphersuites; + const mbedtls_ssl_ciphersuite_t *ciphersuite_info; + /* ciphersuite_start points to the start of + the ciphersuite list, i.e. to the length field*/ + unsigned char *ciphersuite_start, *ciphersuite_iter; + size_t buf_len; + + *olen = 0 ; + /* + * Ciphersuite list + * + * This is a list of the symmetric cipher options supported by + * the client, specifically the record protection algorithm + * ( including secret key length ) and a hash to be used with + * HKDF, in descending order of client preference. + */ + ciphersuites = ssl->conf->ciphersuite_list; + + /* Check available spaces for ciphersuite */ + MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 ); + + /* Write ciphersuites */ + ciphersuite_start = buf + 2; + ciphersuite_iter = ciphersuite_start; + + for ( size_t i = 0; ciphersuites[i] != 0; i++ ) + { + ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] ); + + if( ciphersuite_info == NULL ) + continue; + + if( ciphersuite_info->min_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 || + ciphersuite_info->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 ) + continue; + + MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s", + (unsigned int) ciphersuites[i], + ciphersuite_info->name ) ); + + /* Check for available spaces */ + MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 ); + + MBEDTLS_PUT_UINT16_BE( ciphersuites[i], ciphersuite_iter, 0); + ciphersuite_iter += 2; + + } + + buf_len = ciphersuite_iter - ciphersuite_start; + + /* write ciphersuite buf length */ + MBEDTLS_PUT_UINT16_BE( buf_len, buf, 0 ); + + + MBEDTLS_SSL_DEBUG_MSG( 3, + ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites", + buf_len/2 ) ); + + return( 0 ); +} + +/* Functions for writing ClientHello message */ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, unsigned char *buf, size_t buflen, @@ -139,13 +210,7 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, unsigned char *start = buf; unsigned char *end = buf + buflen; - /* Ciphersuite-related variables */ - const int *ciphersuites; - const mbedtls_ssl_ciphersuite_t *ciphersuite_info; - /* ciphersuite_start points to the start of - the ciphersuite list, i.e. to the length field*/ - unsigned char *ciphersuite_start; - size_t ciphersuite_count; + *len_with_binders = 0; /* Keeping track of the included extensions */ ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE; @@ -169,7 +234,9 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, ssl->major_ver = ssl->conf->min_major_ver; ssl->minor_ver = ssl->conf->min_minor_ver; - /* For TLS 1.3 we use the legacy version number {0x03, 0x03} + /* Write legacy_version + * ProtocolVersion legacy_version = 0x0303; // TLS v1.2 + * For TLS 1.3 we use the legacy version number {0x03, 0x03} * instead of the true version number. * * For DTLS 1.3 we use the legacy version number @@ -180,16 +247,16 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_LEGACY_VERSION_LEN ); MBEDTLS_PUT_UINT16_BE( 0x0303, buf, 0); buf += CLIENT_HELLO_LEGACY_VERSION_LEN; - buflen -= CLIENT_HELLO_LEGACY_VERSION_LEN; - /* Write random bytes */ + /* Write random bytes + Random random + */ MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_RANDOM_LEN ); memcpy( buf, ssl->handshake->randbytes, CLIENT_HELLO_RANDOM_LEN ); MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf, CLIENT_HELLO_RANDOM_LEN ); buf += CLIENT_HELLO_RANDOM_LEN; - buflen -= CLIENT_HELLO_RANDOM_LEN; /* Versions of TLS before TLS 1.3 supported a * "session resumption" feature which has been merged with pre-shared @@ -203,74 +270,14 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, * ossification ). Otherwise, it MUST be set as a zero-length vector * ( i.e., a zero-valued single byte length field ). */ - if( buflen < 1 ) - { - MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) ); - return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); - } - + MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 ); *buf++ = 0; /* session id length set to zero */ - buflen -= 1; - - /* - * Ciphersuite list - * - * This is a list of the symmetric cipher options supported by - * the client, specifically the record protection algorithm - * ( including secret key length ) and a hash to be used with - * HKDF, in descending order of client preference. - */ - ciphersuites = ssl->conf->ciphersuite_list; - - if( buflen < 2 /* for ciphersuite list length */ ) - { - MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) ); - return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); - } - - /* Skip writing ciphersuite length for now */ - ciphersuite_count = 0; - ciphersuite_start = buf; - buf += 2; - buflen -= 2; - - for ( size_t i = 0; ciphersuites[i] != 0; i++ ) - { - ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] ); - - if( ciphersuite_info == NULL ) - continue; - if( ciphersuite_info->min_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 || - ciphersuite_info->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 ) - continue; - - MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s", - (unsigned int) ciphersuites[i], - ciphersuite_info->name ) ); - - ciphersuite_count++; - - if( buflen < 2 /* for ciphersuite list length */ ) - { - MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) ); - return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); - } - - MBEDTLS_PUT_UINT16_BE( ciphersuites[i], buf, 0); - - buf += 2; - buflen -= 2; - - } - - /* write ciphersuite length now */ - MBEDTLS_PUT_UINT16_BE( ciphersuite_count*2, ciphersuite_start, 0 ); - ciphersuite_start += 2; - - MBEDTLS_SSL_DEBUG_MSG( 3, - ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites", - ciphersuite_count ) ); + /* Write ciphersuites */ + ret = ssl_tls13_write_client_hello_ciphersuites( ssl, buf, end, &cur_ext_len ); + if( ret != 0) + return( ret ); + buf += cur_ext_len; /* For every TLS 1.3 ClientHello, this vector MUST contain exactly * one byte set to zero, which corresponds to the 'null' compression @@ -278,20 +285,13 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, * * For cTLS this field is elided. */ - if( buflen < 2 /* for ciphersuite list length */ ) - { - MBEDTLS_SSL_DEBUG_MSG( 1, ( "buffer too small to hold ClientHello" ) ); - return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); - } - + MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 ); *buf++ = 1; *buf++ = MBEDTLS_SSL_COMPRESS_NULL; - buflen -= 2; /* First write extensions, then the total length */ extension_start = buf; - total_ext_len = 0; buf += 2; /* Supported Versions Extension is mandatory with TLS 1.3. @@ -302,7 +302,6 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, ret = ssl_tls13_write_supported_versions_ext( ssl, buf, end, &cur_ext_len ); if( ret != 0 ) return( ret ); - total_ext_len += cur_ext_len; buf += cur_ext_len; #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) @@ -312,8 +311,6 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, ret = ssl_tls13_write_supported_groups_ext( ssl, buf, end, &cur_ext_len ); if( ret != 0 ) return( ret ); - - total_ext_len += cur_ext_len; buf += cur_ext_len; /* The supported_signature_algorithms extension is REQUIRED for @@ -321,8 +318,6 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, buf, end, &cur_ext_len ); if( ret != 0 ) return( ret ); - - total_ext_len += cur_ext_len; buf += cur_ext_len; /* We need to send the key shares under three conditions: @@ -338,13 +333,13 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, ret = ssl_tls13_write_key_shares_ext( ssl, buf, end, &cur_ext_len ); if( ret != 0 ) return( ret ); - - total_ext_len += cur_ext_len; buf += cur_ext_len; + #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ /* Add more extensions here */ + total_ext_len = buf - extension_start - 2; MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET , total_ext_len ) ); @@ -354,7 +349,7 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, MBEDTLS_PUT_UINT16_BE( total_ext_len, extension_start, 0 ); extension_start += 2; - *len_with_binders = ( extension_start + total_ext_len ) - start; + *len_with_binders = buf - start; return( 0 ); } @@ -390,9 +385,6 @@ static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl ) * * Structure of this message: * - * uint16 ProtocolVersion; - * opaque Random[32]; - * uint8 CipherSuite[2]; // Cryptographic suite selector * struct { * ProtocolVersion legacy_version = 0x0303; // TLS v1.2 * Random random; From a2cf7bd2436f3ad506272547e3674bc40a827527 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 31 Aug 2021 16:12:00 +0800 Subject: [PATCH 108/120] fix comment issues Signed-off-by: Jerry Yu --- include/mbedtls/ssl.h | 10 +++++----- library/ssl_misc.h | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/include/mbedtls/ssl.h b/include/mbedtls/ssl.h index 540ff1fc52f1..b0491bfeadb0 100644 --- a/include/mbedtls/ssl.h +++ b/include/mbedtls/ssl.h @@ -472,19 +472,19 @@ #define MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH 1 #define MBEDTLS_TLS_EXT_TRUNCATED_HMAC 4 -#define MBEDTLS_TLS_EXT_STATUS_REQUEST 5 /* RFC 6066 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_STATUS_REQUEST 5 /* RFC 6066 TLS 1.2 and 1.3 */ #define MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES 10 -#define MBEDTLS_TLS_EXT_SUPPORTED_GROUPS 10 /* RFC 8422,7919 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_SUPPORTED_GROUPS 10 /* RFC 8422,7919 TLS 1.2 and 1.3 */ #define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS 11 #define MBEDTLS_TLS_EXT_SIG_ALG 13 /* RFC 8446 TLS 1.3 */ #define MBEDTLS_TLS_EXT_USE_SRTP 14 -#define MBEDTLS_TLS_EXT_HEARTBEAT 15 /* RFC 6520 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_HEARTBEAT 15 /* RFC 6520 TLS 1.2 and 1.3 */ #define MBEDTLS_TLS_EXT_ALPN 16 -#define MBEDTLS_TLS_EXT_SCT 18 /* RFC 6962 TLS 1.3 */ -#define MBEDTLS_TLS_EXT_CLI_CERT_TYPE 19 /* RFC 7259 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_SCT 18 /* RFC 6962 TLS 1.2 and 1.3 */ +#define MBEDTLS_TLS_EXT_CLI_CERT_TYPE 19 /* RFC 7250 TLS 1.3 */ #define MBEDTLS_TLS_EXT_SERV_CERT_TYPE 20 /* RFC 7250 TLS 1.3 */ #define MBEDTLS_TLS_EXT_PADDING 21 /* RFC 7685 TLS 1.3 */ #define MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC 22 /* 0x16 */ diff --git a/library/ssl_misc.h b/library/ssl_misc.h index e93e55b633a5..6b17667bfec2 100644 --- a/library/ssl_misc.h +++ b/library/ssl_misc.h @@ -960,14 +960,14 @@ int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl ); void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl ); #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) /** - * \brief TLS1.3 client side state machine entry + * \brief TLS 1.3 client side state machine entry * * \param ssl SSL context */ int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl ); /** - * \brief TLS1.3 server side state machine entry + * \brief TLS 1.3 server side state machine entry * * \param ssl SSL context */ From b7ab336b3a98c2d2e8c8ff0bd5d328647e01849a Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 31 Aug 2021 16:16:19 +0800 Subject: [PATCH 109/120] fix format issue Signed-off-by: Jerry Yu --- library/ssl_tls13_client.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index f9cfff521774..736cd208e413 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -65,7 +65,7 @@ static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl, MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 ); /* Write Extension Length */ - MBEDTLS_PUT_UINT16_BE( 3, p, 2); + MBEDTLS_PUT_UINT16_BE( 3, p, 2 ); p += 4; /* Length of the SupportedVersions field data */ @@ -169,7 +169,7 @@ static int ssl_tls13_write_client_hello_ciphersuites( /* Check for available spaces */ MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 ); - MBEDTLS_PUT_UINT16_BE( ciphersuites[i], ciphersuite_iter, 0); + MBEDTLS_PUT_UINT16_BE( ciphersuites[i], ciphersuite_iter, 0 ); ciphersuite_iter += 2; } @@ -245,7 +245,7 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, * In cTLS the version number is elided. */ MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_LEGACY_VERSION_LEN ); - MBEDTLS_PUT_UINT16_BE( 0x0303, buf, 0); + MBEDTLS_PUT_UINT16_BE( 0x0303, buf, 0 ); buf += CLIENT_HELLO_LEGACY_VERSION_LEN; /* Write random bytes From f171e836eb58f79f315f0d12cde0c50847a22647 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 31 Aug 2021 18:31:09 +0800 Subject: [PATCH 110/120] fix lenght mismatch error Signed-off-by: Jerry Yu --- library/ssl_tls13_client.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index 736cd208e413..41d2a321ec79 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -184,6 +184,8 @@ static int ssl_tls13_write_client_hello_ciphersuites( ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites", buf_len/2 ) ); + *olen = ciphersuite_iter - buf; + return( 0 ); } From 1bc2c1f1a3086a0d20c0251580f1ecd7564a0e7c Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Wed, 1 Sep 2021 12:57:29 +0800 Subject: [PATCH 111/120] fix various issues fix comments, format and name conversion issues Signed-off-by: Jerry Yu --- include/mbedtls/ssl.h | 6 +- library/ssl_tls13_client.c | 240 ++++++++++++++++-------------------- library/ssl_tls13_generic.c | 8 ++ 3 files changed, 120 insertions(+), 134 deletions(-) diff --git a/include/mbedtls/ssl.h b/include/mbedtls/ssl.h index b0491bfeadb0..f5338599596b 100644 --- a/include/mbedtls/ssl.h +++ b/include/mbedtls/ssl.h @@ -484,9 +484,9 @@ #define MBEDTLS_TLS_EXT_ALPN 16 #define MBEDTLS_TLS_EXT_SCT 18 /* RFC 6962 TLS 1.2 and 1.3 */ -#define MBEDTLS_TLS_EXT_CLI_CERT_TYPE 19 /* RFC 7250 TLS 1.3 */ -#define MBEDTLS_TLS_EXT_SERV_CERT_TYPE 20 /* RFC 7250 TLS 1.3 */ -#define MBEDTLS_TLS_EXT_PADDING 21 /* RFC 7685 TLS 1.3 */ +#define MBEDTLS_TLS_EXT_CLI_CERT_TYPE 19 /* RFC 7250 TLS 1.2 and 1.3 */ +#define MBEDTLS_TLS_EXT_SERV_CERT_TYPE 20 /* RFC 7250 TLS 1.2 and 1.3 */ +#define MBEDTLS_TLS_EXT_PADDING 21 /* RFC 7685 TLS 1.2 and 1.3 */ #define MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC 22 /* 0x16 */ #define MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET 0x0017 /* 23 */ diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index 41d2a321ec79..944e5b50c3db 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -31,7 +31,7 @@ #include #define CLIENT_HELLO_RANDOM_LEN 32 -#define CLIENT_HELLO_LEGACY_VERSION_LEN 2 +#define CLIENT_HELLO_LEGACY_VERSION_LEN 2 /* Write extensions */ @@ -54,25 +54,31 @@ static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) ); /* - * ExtensionType 2 - * ExtensionLength 2 - * VersionSLength 1 - * Version 2 + * Reserve space for extension header. + * + * extension_type 2 + * extension_data_length 2 + * version_length 1 + * versions 2 */ MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 ); - /* Write Extension Type */ + /* Write extension_type */ MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 ); - /* Write Extension Length */ + /* Write extension_data_length */ MBEDTLS_PUT_UINT16_BE( 3, p, 2 ); p += 4; - /* Length of the SupportedVersions field data */ + /* Length of versions */ *p++ = 0x2; - /* This implementation only supports a single TLS version, and only - * advertises a single value. + /* Write values of supported version. + * + * They are come from configuration values. And + * ssl_conf_check has valided the values. + * + * Currently, only one vesrion is advertised. */ mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver, @@ -115,22 +121,22 @@ static int ssl_tls13_write_key_shares_ext( mbedtls_ssl_context *ssl, #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ -/* Write ciphersuites +/* + * Functions for writing ClientHello message. + */ +/* Write cipher_suites * CipherSuite cipher_suites<2..2^16-2>; */ -static int ssl_tls13_write_client_hello_ciphersuites( +static int ssl_tls13_write_client_hello_cipher_suites( mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end, size_t *olen ) { - /* Ciphersuite-related variables */ - const int *ciphersuites; - const mbedtls_ssl_ciphersuite_t *ciphersuite_info; - /* ciphersuite_start points to the start of - the ciphersuite list, i.e. to the length field*/ - unsigned char *ciphersuite_start, *ciphersuite_iter; - size_t buf_len; + const int *cipher_suite_list; + unsigned char *cipher_suites_start; /* start of the cipher_suite_list */ + unsigned char *cipher_suites_iter; /* iteration of the cipher_suite_list */ + size_t cipher_suites_len; *olen = 0 ; @@ -142,164 +148,146 @@ static int ssl_tls13_write_client_hello_ciphersuites( * ( including secret key length ) and a hash to be used with * HKDF, in descending order of client preference. */ - ciphersuites = ssl->conf->ciphersuite_list; + cipher_suite_list = ssl->conf->ciphersuite_list; - /* Check available spaces for ciphersuite */ + /* Check there is space for the cipher suite list length (2 bytes). */ MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 ); - /* Write ciphersuites */ - ciphersuite_start = buf + 2; - ciphersuite_iter = ciphersuite_start; + /* Write cipher_suite_list */ + cipher_suites_start = buf + 2; + cipher_suites_iter = cipher_suites_start; - for ( size_t i = 0; ciphersuites[i] != 0; i++ ) + for ( size_t i = 0; cipher_suite_list[i] != 0; i++ ) { - ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuites[i] ); + int cipher_suite = cipher_suite_list[i]; + const mbedtls_ssl_ciphersuite_t *ciphersuite_info; + ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite ); if( ciphersuite_info == NULL ) continue; - if( ciphersuite_info->min_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 || ciphersuite_info->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 ) continue; MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s", - (unsigned int) ciphersuites[i], + (unsigned int) cipher_suite, ciphersuite_info->name ) ); - /* Check for available spaces */ + /* Check there is space for the cipher suite identifier (2 bytes). */ MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 ); - - MBEDTLS_PUT_UINT16_BE( ciphersuites[i], ciphersuite_iter, 0 ); - ciphersuite_iter += 2; - + MBEDTLS_PUT_UINT16_BE( cipher_suite, cipher_suites_iter, 0 ); + cipher_suites_iter += 2; } - buf_len = ciphersuite_iter - ciphersuite_start; - - /* write ciphersuite buf length */ - MBEDTLS_PUT_UINT16_BE( buf_len, buf, 0 ); - - + /* Write the cipher_suite_list length in number of bytes */ + cipher_suites_len = cipher_suites_iter - cipher_suites_start; + MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 ); MBEDTLS_SSL_DEBUG_MSG( 3, - ( "client hello, got %" MBEDTLS_PRINTF_SIZET " ciphersuites", - buf_len/2 ) ); + ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites", + cipher_suites_len/2 ) ); - *olen = ciphersuite_iter - buf; + /* Output the total length of cipher_suites field. */ + *olen = cipher_suites_iter - buf; return( 0 ); } -/* Functions for writing ClientHello message */ +/* + * Structure of ClientHello message: + * + * struct { + * ProtocolVersion legacy_version = 0x0303; // TLS v1.2 + * Random random; + * opaque legacy_session_id<0..32>; + * CipherSuite cipher_suites<2..2^16-2>; + * opaque legacy_compression_methods<1..2^8-1>; + * Extension extensions<8..2^16-1>; + * } ClientHello; + */ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, unsigned char *buf, size_t buflen, - size_t *len_with_binders ) + size_t *olen ) { - /* Extensions */ - /* extension_start - * Used during extension writing where the - * buffer pointer to the beginning of the - * extension list must be kept to write - * the total extension list size in the end. - */ int ret; - unsigned char *extension_start; - size_t cur_ext_len; /* Size of the current extension */ - size_t total_ext_len; /* Size of list of extensions */ + unsigned char *extension_start; /* Start of extensions buffer */ + size_t cur_ext_len; /* Size of the current extension */ + size_t total_ext_len; /* Size of list of extensions */ /* Buffer management */ unsigned char *start = buf; unsigned char *end = buf + buflen; - *len_with_binders = 0; - - /* Keeping track of the included extensions */ - ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE; - - /* NOTE: - * Even for DTLS 1.3, we are writing a TLS handshake header here. - * The actual DTLS 1.3 handshake header is inserted in - * the record writing routine mbedtls_ssl_write_record(). - * - * For cTLS the length, and the version field - * are elided. The random bytes are shorter. - */ - - if( ssl->conf->max_major_ver == 0 ) - { - MBEDTLS_SSL_DEBUG_MSG( 1, ( "configured max major version is invalid, " - "consider using mbedtls_ssl_config_defaults()" ) ); - return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); - } + *olen = 0; + /* No validation needed here. It has been done by ssl_conf_check() */ ssl->major_ver = ssl->conf->min_major_ver; ssl->minor_ver = ssl->conf->min_minor_ver; - /* Write legacy_version + /* + * Write legacy_version * ProtocolVersion legacy_version = 0x0303; // TLS v1.2 - * For TLS 1.3 we use the legacy version number {0x03, 0x03} - * instead of the true version number. - * - * For DTLS 1.3 we use the legacy version number - * {254,253}. * - * In cTLS the version number is elided. + * For TLS 1.3 we use the legacy version number {0x03, 0x03} + * instead of the true version number. */ MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_LEGACY_VERSION_LEN ); MBEDTLS_PUT_UINT16_BE( 0x0303, buf, 0 ); buf += CLIENT_HELLO_LEGACY_VERSION_LEN; - /* Write random bytes - Random random - */ + /* Write the random bytes ( random ).*/ MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_RANDOM_LEN ); memcpy( buf, ssl->handshake->randbytes, CLIENT_HELLO_RANDOM_LEN ); MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", buf, CLIENT_HELLO_RANDOM_LEN ); - buf += CLIENT_HELLO_RANDOM_LEN; - /* Versions of TLS before TLS 1.3 supported a - * "session resumption" feature which has been merged with pre-shared - * keys in this version. A client which has a - * cached session ID set by a pre-TLS 1.3 server SHOULD set this - * field to that value. In compatibility mode, - * this field MUST be non-empty, so a client not offering a - * pre-TLS 1.3 session MUST generate a new 32-byte value. This value - * need not be random but SHOULD be unpredictable to avoid - * implementations fixating on a specific value ( also known as - * ossification ). Otherwise, it MUST be set as a zero-length vector - * ( i.e., a zero-valued single byte length field ). + /* + * Write legacy_session_id + * + * Versions of TLS before TLS 1.3 supported a "session resumption" feature + * which has been merged with pre-shared keys in this version. A client + * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set + * this field to that value. In compatibility mode, this field MUST be + * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate + * a new 32-byte value. This value need not be random but SHOULD be + * unpredictable to avoid implementations fixating on a specific value + * ( also known as ossification ). Otherwise, it MUST be set as a zero-length + * vector ( i.e., a zero-valued single byte length field ). */ MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 ); *buf++ = 0; /* session id length set to zero */ - /* Write ciphersuites */ - ret = ssl_tls13_write_client_hello_ciphersuites( ssl, buf, end, &cur_ext_len ); + /* Write cipher_suites */ + ret = ssl_tls13_write_client_hello_cipher_suites( ssl, buf, end, &cur_ext_len ); if( ret != 0) return( ret ); buf += cur_ext_len; - /* For every TLS 1.3 ClientHello, this vector MUST contain exactly + /* Write legacy_compression_methods + * + * For every TLS 1.3 ClientHello, this vector MUST contain exactly * one byte set to zero, which corresponds to the 'null' compression * method in prior versions of TLS. - * - * For cTLS this field is elided. */ MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 ); *buf++ = 1; *buf++ = MBEDTLS_SSL_COMPRESS_NULL; + /* Write extensions */ + + /* Keeping track of the included extensions */ + ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE; /* First write extensions, then the total length */ + MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 ); extension_start = buf; buf += 2; - /* Supported Versions Extension is mandatory with TLS 1.3. + /* Write supported_versions extension * - * For cTLS we only need to provide it if there is more than one version - * and currently there is only one. + * Supported Versions Extension is mandatory with TLS 1.3. */ ret = ssl_tls13_write_supported_versions_ext( ssl, buf, end, &cur_ext_len ); if( ret != 0 ) @@ -307,22 +295,18 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, buf += cur_ext_len; #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) - /* The supported_groups and the key_share extensions are - * REQUIRED for ECDHE ciphersuites. + /* Write supported_groups extension + * + * It is REQUIRED for ECDHE cipher_suites. */ ret = ssl_tls13_write_supported_groups_ext( ssl, buf, end, &cur_ext_len ); if( ret != 0 ) return( ret ); buf += cur_ext_len; - /* The supported_signature_algorithms extension is REQUIRED for - * certificate authenticated ciphersuites. */ - ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, buf, end, &cur_ext_len ); - if( ret != 0 ) - return( ret ); - buf += cur_ext_len; - - /* We need to send the key shares under three conditions: + /* Write key_share extension + * + * We need to send the key shares under three conditions: * 1) A certificate-based ciphersuite is being offered. In this case * supported_groups and supported_signature extensions have been * successfully added. @@ -331,27 +315,32 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, * 3) Or, in case all ciphers are supported ( which includes #1 and #2 * from above ) */ - ret = ssl_tls13_write_key_shares_ext( ssl, buf, end, &cur_ext_len ); if( ret != 0 ) return( ret ); buf += cur_ext_len; + /* Write signature_algorithms extension + * + * It is REQUIRED for certificate authenticated cipher_suites. + */ + ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, buf, end, &cur_ext_len ); + if( ret != 0 ) + return( ret ); + buf += cur_ext_len; + #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ /* Add more extensions here */ + /* Write the length of the list of extensions. */ total_ext_len = buf - extension_start - 2; MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET , total_ext_len ) ); - MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extension_start, total_ext_len ); - - /* Write extension length */ MBEDTLS_PUT_UINT16_BE( total_ext_len, extension_start, 0 ); - extension_start += 2; - *len_with_binders = buf - start; + *olen = buf - start; return( 0 ); } @@ -384,17 +373,6 @@ static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl ) /* * Write ClientHello handshake message. - * - * Structure of this message: - * - * struct { - * ProtocolVersion legacy_version = 0x0303; // TLS v1.2 - * Random random; - * opaque legacy_session_id<0..32>; - * CipherSuite cipher_suites<2..2^16-2>; - * opaque legacy_compression_methods<1..2^8-1>; - * Extension extensions<8..2^16-1>; - * } ClientHello; */ static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl ) { diff --git a/library/ssl_tls13_generic.c b/library/ssl_tls13_generic.c index 1713d4c813a5..f33c2f636b5f 100644 --- a/library/ssl_tls13_generic.c +++ b/library/ssl_tls13_generic.c @@ -32,6 +32,13 @@ int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl, unsigned char **buf, size_t *buflen ) { + /* + * Reserve 4 bytes for hanshake header. ( Section 4,RFC 8446 ) + * ... + * HandshakeType msg_type; + * uint24 length; + * ... + */ *buf = ssl->out_msg + 4; *buflen = MBEDTLS_SSL_OUT_CONTENT_LEN - 4; @@ -48,6 +55,7 @@ int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl, int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; ((void) buf_len); + /* Add reserved 4 bytes for handshake header */ ssl->out_msglen = msg_len + 4; MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_handshake_msg_ext, ( ssl, 0 ) ); From 790656a0a61961296552b75b446bc3f4c50d1b32 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Wed, 1 Sep 2021 15:51:48 +0800 Subject: [PATCH 112/120] fix name conversion issues fix name conversion issues in `ssl_tls13_write_client_hello_body` Signed-off-by: Jerry Yu --- library/ssl_tls13_client.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index 944e5b50c3db..d35408741300 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -211,9 +211,9 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, { int ret; - unsigned char *extension_start; /* Start of extensions buffer */ - size_t cur_ext_len; /* Size of the current extension */ - size_t total_ext_len; /* Size of list of extensions */ + unsigned char *extensions_len_ptr; /* pointer of extensions length */ + size_t output_len; /* Length of buffer used by function */ + size_t extensions_len; /* Length of the list of extensions*/ /* Buffer management */ unsigned char *start = buf; @@ -260,10 +260,10 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, *buf++ = 0; /* session id length set to zero */ /* Write cipher_suites */ - ret = ssl_tls13_write_client_hello_cipher_suites( ssl, buf, end, &cur_ext_len ); + ret = ssl_tls13_write_client_hello_cipher_suites( ssl, buf, end, &output_len ); if( ret != 0) return( ret ); - buf += cur_ext_len; + buf += output_len; /* Write legacy_compression_methods * @@ -282,27 +282,27 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, /* First write extensions, then the total length */ MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 ); - extension_start = buf; + extensions_len_ptr = buf; buf += 2; /* Write supported_versions extension * * Supported Versions Extension is mandatory with TLS 1.3. */ - ret = ssl_tls13_write_supported_versions_ext( ssl, buf, end, &cur_ext_len ); + ret = ssl_tls13_write_supported_versions_ext( ssl, buf, end, &output_len ); if( ret != 0 ) return( ret ); - buf += cur_ext_len; + buf += output_len; #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) /* Write supported_groups extension * * It is REQUIRED for ECDHE cipher_suites. */ - ret = ssl_tls13_write_supported_groups_ext( ssl, buf, end, &cur_ext_len ); + ret = ssl_tls13_write_supported_groups_ext( ssl, buf, end, &output_len ); if( ret != 0 ) return( ret ); - buf += cur_ext_len; + buf += output_len; /* Write key_share extension * @@ -315,30 +315,30 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, * 3) Or, in case all ciphers are supported ( which includes #1 and #2 * from above ) */ - ret = ssl_tls13_write_key_shares_ext( ssl, buf, end, &cur_ext_len ); + ret = ssl_tls13_write_key_shares_ext( ssl, buf, end, &output_len ); if( ret != 0 ) return( ret ); - buf += cur_ext_len; + buf += output_len; /* Write signature_algorithms extension * * It is REQUIRED for certificate authenticated cipher_suites. */ - ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, buf, end, &cur_ext_len ); + ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, buf, end, &output_len ); if( ret != 0 ) return( ret ); - buf += cur_ext_len; + buf += output_len; #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ /* Add more extensions here */ /* Write the length of the list of extensions. */ - total_ext_len = buf - extension_start - 2; + extensions_len = buf - extensions_len_ptr - 2; + MBEDTLS_PUT_UINT16_BE( extensions_len, extensions_len_ptr, 0 ); MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET , - total_ext_len ) ); - MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extension_start, total_ext_len ); - MBEDTLS_PUT_UINT16_BE( total_ext_len, extension_start, 0 ); + extensions_len ) ); + MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extensions_len_ptr, extensions_len ); *olen = buf - start; return( 0 ); From 0c63af6ed6576aebdb8dcc3ec9852ae42b53c75e Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Thu, 2 Sep 2021 12:59:12 +0800 Subject: [PATCH 113/120] fix comment issues Signed-off-by: Jerry Yu --- library/ssl_tls13_client.c | 35 +++++++++++++++++------------------ library/ssl_tls13_generic.c | 4 ++-- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index d35408741300..373efff10f71 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -54,7 +54,7 @@ static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl, MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) ); /* - * Reserve space for extension header. + * Check space for extension header. * * extension_type 2 * extension_data_length 2 @@ -73,12 +73,11 @@ static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl, /* Length of versions */ *p++ = 0x2; - /* Write values of supported version. + /* Write values of supported versions. * - * They are come from configuration values. And - * ssl_conf_check has valided the values. + * They are defined by the configuration. * - * Currently, only one vesrion is advertised. + * Currently, only one version is advertised. */ mbedtls_ssl_write_version( ssl->conf->max_major_ver, ssl->conf->max_minor_ver, @@ -133,9 +132,9 @@ static int ssl_tls13_write_client_hello_cipher_suites( unsigned char *end, size_t *olen ) { - const int *cipher_suite_list; - unsigned char *cipher_suites_start; /* start of the cipher_suite_list */ - unsigned char *cipher_suites_iter; /* iteration of the cipher_suite_list */ + const int *ciphersuite_list; + unsigned char *cipher_suites_start; /* Start of the cipher_suites list */ + unsigned char *cipher_suites_iter; /* Iteration over the cipher_suites list */ size_t cipher_suites_len; *olen = 0 ; @@ -148,18 +147,18 @@ static int ssl_tls13_write_client_hello_cipher_suites( * ( including secret key length ) and a hash to be used with * HKDF, in descending order of client preference. */ - cipher_suite_list = ssl->conf->ciphersuite_list; + ciphersuite_list = ssl->conf->ciphersuite_list; /* Check there is space for the cipher suite list length (2 bytes). */ MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 ); - /* Write cipher_suite_list */ + /* Write cipher_suites */ cipher_suites_start = buf + 2; cipher_suites_iter = cipher_suites_start; - for ( size_t i = 0; cipher_suite_list[i] != 0; i++ ) + for ( size_t i = 0; ciphersuite_list[i] != 0; i++ ) { - int cipher_suite = cipher_suite_list[i]; + int cipher_suite = ciphersuite_list[i]; const mbedtls_ssl_ciphersuite_t *ciphersuite_info; ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite ); @@ -179,7 +178,7 @@ static int ssl_tls13_write_client_hello_cipher_suites( cipher_suites_iter += 2; } - /* Write the cipher_suite_list length in number of bytes */ + /* Write the cipher_suites length in number of bytes */ cipher_suites_len = cipher_suites_iter - cipher_suites_start; MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 ); MBEDTLS_SSL_DEBUG_MSG( 3, @@ -211,7 +210,7 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, { int ret; - unsigned char *extensions_len_ptr; /* pointer of extensions length */ + unsigned char *extensions_len_ptr; /* Pointer of extensions length */ size_t output_len; /* Length of buffer used by function */ size_t extensions_len; /* Length of the list of extensions*/ @@ -392,8 +391,8 @@ static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl ) ( ssl, buf, buf_len, &msg_len ) ); mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, - msg_len ); - ssl->handshake->update_checksum( ssl, buf, 0 ); + msg_len ); + ssl->handshake->update_checksum( ssl, buf, msg_len ); MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello, ( ssl ) ); MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg, @@ -420,8 +419,8 @@ int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl ) switch( ssl->state ) { /* - * ssl->state is initialized as HELLO_REQUEST. It is same - * with CLIENT_HELLO status + * ssl->state is initialized as HELLO_REQUEST. It is the same + * as CLIENT_HELLO state. */ case MBEDTLS_SSL_HELLO_REQUEST: case MBEDTLS_SSL_CLIENT_HELLO: diff --git a/library/ssl_tls13_generic.c b/library/ssl_tls13_generic.c index f33c2f636b5f..fb6da346fc87 100644 --- a/library/ssl_tls13_generic.c +++ b/library/ssl_tls13_generic.c @@ -30,7 +30,7 @@ int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl, unsigned hs_type, unsigned char **buf, - size_t *buflen ) + size_t *buf_len ) { /* * Reserve 4 bytes for hanshake header. ( Section 4,RFC 8446 ) @@ -40,7 +40,7 @@ int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl, * ... */ *buf = ssl->out_msg + 4; - *buflen = MBEDTLS_SSL_OUT_CONTENT_LEN - 4; + *buf_len = MBEDTLS_SSL_OUT_CONTENT_LEN - 4; ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE; ssl->out_msg[0] = hs_type; From 2c0fbf3405aa1d71f8f2d966a06399454a53afce Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Thu, 2 Sep 2021 13:53:46 +0800 Subject: [PATCH 114/120] modify proc_chk macros - change the parameter - remove debug output - remove return value modify Signed-off-by: Jerry Yu --- library/ssl_misc.h | 17 ++++------------- library/ssl_tls13_client.c | 23 +++++++++++++---------- library/ssl_tls13_generic.c | 2 +- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/library/ssl_misc.h b/library/ssl_misc.h index 6b17667bfec2..e16c674cb0f3 100644 --- a/library/ssl_misc.h +++ b/library/ssl_misc.h @@ -26,7 +26,6 @@ #include "mbedtls/ssl.h" #include "mbedtls/cipher.h" -#include "mbedtls/debug.h" #if defined(MBEDTLS_USE_PSA_CRYPTO) #include "psa/crypto.h" @@ -135,33 +134,25 @@ /* * Helper macros for function call with returen check. */ -/* utils for strip parens in marcro */ -#define MBEDTLS_SSL_PROC_STRIP_PARENS( ... ) __VA_ARGS__ - /* * Exit and print debug message when return none zero value */ -#define MBEDTLS_SSL_PROC_CHK( fn, args ) \ +#define MBEDTLS_SSL_PROC_CHK( f ) \ do { \ - ret = fn(MBEDTLS_SSL_PROC_STRIP_PARENS args); \ + ret = ( f ); \ if( ret != 0 ) \ { \ - if( ret > 0 ) \ - ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; \ - MBEDTLS_SSL_DEBUG_RET( 1, #fn, ret ); \ goto cleanup; \ } \ } while( 0 ) - /* * Exit and print debug message when return negative value */ -#define MBEDTLS_SSL_PROC_CHK_NEG( fn, args ) \ +#define MBEDTLS_SSL_PROC_CHK_NEG( f ) \ do { \ - ret = fn(MBEDTLS_SSL_PROC_STRIP_PARENS args); \ + ret = ( f ); \ if( ret < 0 ) \ { \ - MBEDTLS_SSL_DEBUG_RET( 1, #fn, ret ); \ goto cleanup; \ } \ } while( 0 ) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index 373efff10f71..6b89273353fd 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -381,22 +381,25 @@ static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl ) MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) ); - MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello, ( ssl ) ); + MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) ); - MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg, - ( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, - &buf, &buf_len ) ); + MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( + ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, + &buf, &buf_len ) ); - MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body, - ( ssl, buf, buf_len, &msg_len ) ); + MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf, + buf_len, + &msg_len ) ); - mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, + mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, + MBEDTLS_SSL_HS_CLIENT_HELLO, msg_len ); ssl->handshake->update_checksum( ssl, buf, msg_len ); - MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello, ( ssl ) ); - MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg, - ( ssl, buf_len, msg_len ) ); + MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) ); + MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl, + buf_len, + msg_len ) ); cleanup: diff --git a/library/ssl_tls13_generic.c b/library/ssl_tls13_generic.c index fb6da346fc87..3c49a379bd5f 100644 --- a/library/ssl_tls13_generic.c +++ b/library/ssl_tls13_generic.c @@ -57,7 +57,7 @@ int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl, /* Add reserved 4 bytes for handshake header */ ssl->out_msglen = msg_len + 4; - MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_handshake_msg_ext, ( ssl, 0 ) ); + MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_handshake_msg_ext( ssl, 0 ) ); cleanup: return( ret ); From ef387d79a467c77b68605bcfd91a2981518e9b62 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Thu, 2 Sep 2021 13:59:41 +0800 Subject: [PATCH 115/120] change prototype of write body To keep consistence with others Signed-off-by: Jerry Yu --- library/ssl_tls13_client.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index 6b89273353fd..262481c6a7f9 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -205,7 +205,7 @@ static int ssl_tls13_write_client_hello_cipher_suites( */ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, unsigned char *buf, - size_t buflen, + unsigned char *end, size_t *olen ) { @@ -216,7 +216,6 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, /* Buffer management */ unsigned char *start = buf; - unsigned char *end = buf + buflen; *olen = 0; @@ -388,7 +387,7 @@ static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl ) &buf, &buf_len ) ); MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf, - buf_len, + buf + buf_len, &msg_len ) ); mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, From 8c02bb4b7194631f1a901745400ac76e1180f1a3 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Fri, 3 Sep 2021 21:09:22 +0800 Subject: [PATCH 116/120] fix various comment issues Signed-off-by: Jerry Yu --- library/ssl_misc.h | 6 +++--- library/ssl_tls13_client.c | 16 +++++----------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/library/ssl_misc.h b/library/ssl_misc.h index e16c674cb0f3..7035c278cf4d 100644 --- a/library/ssl_misc.h +++ b/library/ssl_misc.h @@ -132,10 +132,10 @@ #define MBEDTLS_SSL_EXT_KEY_SHARE ( 1 << 21 ) /* - * Helper macros for function call with returen check. + * Helper macros for function call with return check. */ /* - * Exit and print debug message when return none zero value + * Exit when return non-zero value */ #define MBEDTLS_SSL_PROC_CHK( f ) \ do { \ @@ -146,7 +146,7 @@ } \ } while( 0 ) /* - * Exit and print debug message when return negative value + * Exit when return negative value */ #define MBEDTLS_SSL_PROC_CHK_NEG( f ) \ do { \ diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index 262481c6a7f9..f8779a09fd56 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -164,8 +164,8 @@ static int ssl_tls13_write_client_hello_cipher_suites( ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite ); if( ciphersuite_info == NULL ) continue; - if( ciphersuite_info->min_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 || - ciphersuite_info->max_minor_ver != MBEDTLS_SSL_MINOR_VERSION_4 ) + if( !( MBEDTLS_SSL_MINOR_VERSION_4 > ciphersuite_info->min_minor_ver && + MBEDTLS_SSL_MINOR_VERSION_4 < ciphersuite_info->max_minor_ver ) ) continue; MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s", @@ -173,7 +173,7 @@ static int ssl_tls13_write_client_hello_cipher_suites( ciphersuite_info->name ) ); /* Check there is space for the cipher suite identifier (2 bytes). */ - MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 ); + MBEDTLS_SSL_CHK_BUF_PTR( cipher_suites_iter, end, 2 ); MBEDTLS_PUT_UINT16_BE( cipher_suite, cipher_suites_iter, 0 ); cipher_suites_iter += 2; } @@ -210,7 +210,7 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, { int ret; - unsigned char *extensions_len_ptr; /* Pointer of extensions length */ + unsigned char *extensions_len_ptr; /* Pointer to extensions length */ size_t output_len; /* Length of buffer used by function */ size_t extensions_len; /* Length of the list of extensions*/ @@ -362,7 +362,7 @@ static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl ) ssl->handshake->randbytes, CLIENT_HELLO_RANDOM_LEN ) ) != 0 ) { - MBEDTLS_SSL_DEBUG_RET( 1, "ssl_generate_random", ret ); + MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret ); return( ret ); } @@ -410,12 +410,6 @@ int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl ) { int ret = 0; - if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL ) - { - MBEDTLS_SSL_DEBUG_MSG( 2, ( "Handshake completed but ssl->handshake is NULL.\n" ) ); - return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); - } - MBEDTLS_SSL_DEBUG_MSG( 2, ( "client state: %d", ssl->state ) ); switch( ssl->state ) From dbfb7bd873dfa8e5bf9afb5aae3aa2680e9e2fd4 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Sat, 4 Sep 2021 09:58:58 +0800 Subject: [PATCH 117/120] fix various issues - wrong cipher suite filter condition - name conversion - format issue Signed-off-by: Jerry Yu --- library/ssl_tls13_client.c | 6 +++--- library/ssl_tls13_generic.c | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index f8779a09fd56..2a62dc6fdd72 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -164,8 +164,8 @@ static int ssl_tls13_write_client_hello_cipher_suites( ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite ); if( ciphersuite_info == NULL ) continue; - if( !( MBEDTLS_SSL_MINOR_VERSION_4 > ciphersuite_info->min_minor_ver && - MBEDTLS_SSL_MINOR_VERSION_4 < ciphersuite_info->max_minor_ver ) ) + if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver && + MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) ) continue; MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s", @@ -259,7 +259,7 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, /* Write cipher_suites */ ret = ssl_tls13_write_client_hello_cipher_suites( ssl, buf, end, &output_len ); - if( ret != 0) + if( ret != 0 ) return( ret ); buf += output_len; diff --git a/library/ssl_tls13_generic.c b/library/ssl_tls13_generic.c index 3c49a379bd5f..ca4c16713267 100644 --- a/library/ssl_tls13_generic.c +++ b/library/ssl_tls13_generic.c @@ -53,10 +53,12 @@ int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl, size_t msg_len ) { int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; + size_t msg_len_with_header; ((void) buf_len); /* Add reserved 4 bytes for handshake header */ - ssl->out_msglen = msg_len + 4; + msg_len_with_header = msg_len + 4; + ssl->out_msglen = msg_len_with_header; MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_handshake_msg_ext( ssl, 0 ) ); cleanup: From bbe09526b78746e37ebdfcb4d62a0f66581412a4 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Mon, 6 Sep 2021 21:17:54 +0800 Subject: [PATCH 118/120] fix name conversion issues Signed-off-by: Jerry Yu --- library/ssl_tls13_client.c | 74 +++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index 2a62dc6fdd72..fe2e6f850b8e 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -133,8 +133,8 @@ static int ssl_tls13_write_client_hello_cipher_suites( size_t *olen ) { const int *ciphersuite_list; - unsigned char *cipher_suites_start; /* Start of the cipher_suites list */ - unsigned char *cipher_suites_iter; /* Iteration over the cipher_suites list */ + unsigned char *cipher_suites_ptr; /* Start of the cipher_suites list */ + unsigned char *p; /* Iteration over the cipher_suites list */ size_t cipher_suites_len; *olen = 0 ; @@ -153,8 +153,8 @@ static int ssl_tls13_write_client_hello_cipher_suites( MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 ); /* Write cipher_suites */ - cipher_suites_start = buf + 2; - cipher_suites_iter = cipher_suites_start; + cipher_suites_ptr = buf + 2; + p = cipher_suites_ptr; for ( size_t i = 0; ciphersuite_list[i] != 0; i++ ) { @@ -173,20 +173,20 @@ static int ssl_tls13_write_client_hello_cipher_suites( ciphersuite_info->name ) ); /* Check there is space for the cipher suite identifier (2 bytes). */ - MBEDTLS_SSL_CHK_BUF_PTR( cipher_suites_iter, end, 2 ); - MBEDTLS_PUT_UINT16_BE( cipher_suite, cipher_suites_iter, 0 ); - cipher_suites_iter += 2; + MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); + MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 ); + p += 2; } /* Write the cipher_suites length in number of bytes */ - cipher_suites_len = cipher_suites_iter - cipher_suites_start; + cipher_suites_len = p - cipher_suites_ptr; MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 ); MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites", cipher_suites_len/2 ) ); /* Output the total length of cipher_suites field. */ - *olen = cipher_suites_iter - buf; + *olen = p - buf; return( 0 ); } @@ -215,7 +215,7 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, size_t extensions_len; /* Length of the list of extensions*/ /* Buffer management */ - unsigned char *start = buf; + unsigned char *p = buf; *olen = 0; @@ -230,16 +230,16 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, * For TLS 1.3 we use the legacy version number {0x03, 0x03} * instead of the true version number. */ - MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_LEGACY_VERSION_LEN ); - MBEDTLS_PUT_UINT16_BE( 0x0303, buf, 0 ); - buf += CLIENT_HELLO_LEGACY_VERSION_LEN; + MBEDTLS_SSL_CHK_BUF_PTR( p, end, CLIENT_HELLO_LEGACY_VERSION_LEN ); + MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 ); + p += CLIENT_HELLO_LEGACY_VERSION_LEN; /* Write the random bytes ( random ).*/ - MBEDTLS_SSL_CHK_BUF_PTR( buf, end, CLIENT_HELLO_RANDOM_LEN ); - memcpy( buf, ssl->handshake->randbytes, CLIENT_HELLO_RANDOM_LEN ); + MBEDTLS_SSL_CHK_BUF_PTR( p, end, CLIENT_HELLO_RANDOM_LEN ); + memcpy( p, ssl->handshake->randbytes, CLIENT_HELLO_RANDOM_LEN ); MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes", - buf, CLIENT_HELLO_RANDOM_LEN ); - buf += CLIENT_HELLO_RANDOM_LEN; + p, CLIENT_HELLO_RANDOM_LEN ); + p += CLIENT_HELLO_RANDOM_LEN; /* * Write legacy_session_id @@ -254,14 +254,14 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, * ( also known as ossification ). Otherwise, it MUST be set as a zero-length * vector ( i.e., a zero-valued single byte length field ). */ - MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 1 ); - *buf++ = 0; /* session id length set to zero */ + MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 ); + *p++ = 0; /* session id length set to zero */ /* Write cipher_suites */ - ret = ssl_tls13_write_client_hello_cipher_suites( ssl, buf, end, &output_len ); + ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len ); if( ret != 0 ) return( ret ); - buf += output_len; + p += output_len; /* Write legacy_compression_methods * @@ -269,9 +269,9 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, * one byte set to zero, which corresponds to the 'null' compression * method in prior versions of TLS. */ - MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 ); - *buf++ = 1; - *buf++ = MBEDTLS_SSL_COMPRESS_NULL; + MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); + *p++ = 1; + *p++ = MBEDTLS_SSL_COMPRESS_NULL; /* Write extensions */ @@ -279,28 +279,28 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE; /* First write extensions, then the total length */ - MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 ); - extensions_len_ptr = buf; - buf += 2; + MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); + extensions_len_ptr = p; + p += 2; /* Write supported_versions extension * * Supported Versions Extension is mandatory with TLS 1.3. */ - ret = ssl_tls13_write_supported_versions_ext( ssl, buf, end, &output_len ); + ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len ); if( ret != 0 ) return( ret ); - buf += output_len; + p += output_len; #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) /* Write supported_groups extension * * It is REQUIRED for ECDHE cipher_suites. */ - ret = ssl_tls13_write_supported_groups_ext( ssl, buf, end, &output_len ); + ret = ssl_tls13_write_supported_groups_ext( ssl, p, end, &output_len ); if( ret != 0 ) return( ret ); - buf += output_len; + p += output_len; /* Write key_share extension * @@ -313,32 +313,32 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, * 3) Or, in case all ciphers are supported ( which includes #1 and #2 * from above ) */ - ret = ssl_tls13_write_key_shares_ext( ssl, buf, end, &output_len ); + ret = ssl_tls13_write_key_shares_ext( ssl, p, end, &output_len ); if( ret != 0 ) return( ret ); - buf += output_len; + p += output_len; /* Write signature_algorithms extension * * It is REQUIRED for certificate authenticated cipher_suites. */ - ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, buf, end, &output_len ); + ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, p, end, &output_len ); if( ret != 0 ) return( ret ); - buf += output_len; + p += output_len; #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */ /* Add more extensions here */ /* Write the length of the list of extensions. */ - extensions_len = buf - extensions_len_ptr - 2; + extensions_len = p - extensions_len_ptr - 2; MBEDTLS_PUT_UINT16_BE( extensions_len, extensions_len_ptr, 0 ); MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET , extensions_len ) ); MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extensions_len_ptr, extensions_len ); - *olen = buf - start; + *olen = p - buf; return( 0 ); } From 4e388286af740564055f004e99ae133f65b9aece Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Mon, 6 Sep 2021 21:28:08 +0800 Subject: [PATCH 119/120] fix usage of iteration Signed-off-by: Jerry Yu --- library/ssl_tls13_client.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index fe2e6f850b8e..b10e33bfbfb2 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -132,9 +132,9 @@ static int ssl_tls13_write_client_hello_cipher_suites( unsigned char *end, size_t *olen ) { + unsigned char *p = buf; /* Iteration over the cipher_suites list */ const int *ciphersuite_list; unsigned char *cipher_suites_ptr; /* Start of the cipher_suites list */ - unsigned char *p; /* Iteration over the cipher_suites list */ size_t cipher_suites_len; *olen = 0 ; @@ -150,12 +150,11 @@ static int ssl_tls13_write_client_hello_cipher_suites( ciphersuite_list = ssl->conf->ciphersuite_list; /* Check there is space for the cipher suite list length (2 bytes). */ - MBEDTLS_SSL_CHK_BUF_PTR( buf, end, 2 ); + MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); + p += 2; /* Write cipher_suites */ - cipher_suites_ptr = buf + 2; - p = cipher_suites_ptr; - + cipher_suites_ptr = p; for ( size_t i = 0; ciphersuite_list[i] != 0; i++ ) { int cipher_suite = ciphersuite_list[i]; From fec982eacc184b08d00eaeb118c06918147bcfe7 Mon Sep 17 00:00:00 2001 From: Jerry Yu Date: Tue, 7 Sep 2021 17:26:06 +0800 Subject: [PATCH 120/120] fix coding style issues Signed-off-by: Jerry Yu --- library/ssl_tls13_client.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/library/ssl_tls13_client.c b/library/ssl_tls13_client.c index b10e33bfbfb2..41c7a4d14439 100644 --- a/library/ssl_tls13_client.c +++ b/library/ssl_tls13_client.c @@ -31,7 +31,6 @@ #include #define CLIENT_HELLO_RANDOM_LEN 32 -#define CLIENT_HELLO_LEGACY_VERSION_LEN 2 /* Write extensions */ @@ -132,7 +131,7 @@ static int ssl_tls13_write_client_hello_cipher_suites( unsigned char *end, size_t *olen ) { - unsigned char *p = buf; /* Iteration over the cipher_suites list */ + unsigned char *p = buf; const int *ciphersuite_list; unsigned char *cipher_suites_ptr; /* Start of the cipher_suites list */ size_t cipher_suites_len; @@ -229,9 +228,9 @@ static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl, * For TLS 1.3 we use the legacy version number {0x03, 0x03} * instead of the true version number. */ - MBEDTLS_SSL_CHK_BUF_PTR( p, end, CLIENT_HELLO_LEGACY_VERSION_LEN ); + MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 ); MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 ); - p += CLIENT_HELLO_LEGACY_VERSION_LEN; + p += 2; /* Write the random bytes ( random ).*/ MBEDTLS_SSL_CHK_BUF_PTR( p, end, CLIENT_HELLO_RANDOM_LEN );