Commit Graph

7424 Commits

Author SHA1 Message Date
Gilles Peskine
fa85cc2da5 mbedtls_mpi_sub_abs: check the range of the result when it happens
The function mbedtls_mpi_sub_abs first checked that A >= B and then
performed the subtraction, relying on the fact that A >= B to
guarantee that the carry propagation would stop, and not taking
advantage of the fact that the carry when subtracting two numbers can
only be 0 or 1. This made the carry propagation code a little hard to
follow.

Write an ad hoc loop for the carry propagation, checking the size of
the result. This makes termination obvious.

The initial check that A >= B is no longer needed, since the function
now checks that the carry propagation terminates, which is equivalent.
This is a slight performance gain.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-06-09 11:54:20 +02:00
Gilles Peskine
cc6a6bfda7 Simplify the final reduction in mpi_montmul
There was some confusion during review about when A->p[n] could be
nonzero. In fact, there is no need to set A->p[n]: only the
intermediate result d might need to extend to n+1 limbs, not the final
result A. So never access A->p[n]. Rework the explanation of the
calculation in a way that should be easier to follow.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-06-09 11:53:46 +02:00
Gilles Peskine
6f3b68db74 Move carry propagation out of mpi_sub_hlp
The function mpi_sub_hlp had confusing semantics: although it took a
size parameter, it accessed the limb array d beyond this size, to
propagate the carry. This made the function difficult to understand
and analyze, with a potential buffer overflow if misused (not enough
room to propagate the carry).

Change the function so that it only performs the subtraction within
the specified number of limbs, and returns the carry.

Move the carry propagation out of mpi_sub_hlp and into its caller
mbedtls_mpi_sub_abs. This makes the code of subtraction very slightly
less neat, but not significantly different.

In the one other place where mpi_sub_hlp is used, namely mpi_montmul,
this is a net win because the carry is potentially sensitive data and
the function carefully arranges to not have to propagate it.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-06-09 11:53:46 +02:00
Gilles Peskine
dc24cece12 More logical parameter order for mpi_sub_hlp
mpi_sub_hlp performs a subtraction A - B, but took parameters in the
order (B, A). Swap the parameters so that they match the usual
mathematical syntax.

This has the additional benefit of putting the output parameter (A)
first, which is the normal convention in this module.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-06-09 11:50:44 +02:00
Gilles Peskine
ea9ba77e55 Explicitly cast down from mbedtls_mpi_uint to unsigned char
Let code analyzers know that this is deliberate. For example MSVC
warns about the conversion if it's implicit.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-06-09 11:50:44 +02:00
Gilles Peskine
70529abbac Add changelog entry: fix #3394
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-06-09 11:50:44 +02:00
Gilles Peskine
7ff812e0d7 Remove a secret-dependent branch in Montgomery multiplication
In mpi_montmul, an auxiliary function for modular
exponentiation (mbedtls_mpi_mod_exp) that performs Montgomery
multiplication, the last step is a conditional subtraction to force
the result into the correct range. The current implementation uses a
branch and therefore may leak information about secret data to an
adversary who can observe what branch is taken through a side channel.

Avoid this potential leak by always doing the same subtraction and
doing a contant-trace conditional assignment to set the result.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-06-09 11:50:44 +02:00
Gilles Peskine
3c44c65fc1 Separate out low-level mpi_safe_cond_assign
Separate out a version of mpi_safe_cond_assign that works on
equal-sized limb arrays, without worrying about allocation sizes or
signs.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-06-09 11:50:44 +02:00
Gilles Peskine
d108d07050 Document some internal bignum functions
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-06-09 11:50:44 +02:00
Gilles Peskine
8ff7cc9911 Revert "Shut up a clang-analyzer warning"
This reverts commit 2cc69fffcf.

A check was added in mpi_montmul because clang-analyzer warned about a
possibly null pointer. However this was a false positive. Recent
versions of clang-analyzer no longer emit a warning (3.6 does, 6
doesn't).

Incidentally, the size check was wrong: mpi_montmul needs
T->n >= 2 * (N->n + 1), not just T->n >= N->n + 1.

Given that this is an internal function which is only used from one
public function and in a tightly controlled way, remove both the null
check (which is of low value to begin with) and the size check (which
would be slightly more valuable, but was wrong anyway). This allows
the function not to need to return an error, which makes the source
code a little easier to read and makes the object code a little
smaller.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-06-09 11:50:44 +02:00
Gilles Peskine
d6496afa0b Add a const annotation to the non-changing argument of mpi_sub_mul
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-06-09 11:48:25 +02:00
Bence Szépkúti
09b4f19f94 Add Apache-2.0 headers to all scripts
This commit was generated using the following script:

# ========================
#!/bin/sh

# Find scripts
find -path './.git' -prune -o '(' -name '*.gdb' -o -name '*.pl' -o -name '*.py' -o -name '*.sh' ')' -print | xargs sed -i '

# Remove Mbed TLS declaration if it occurs before the copyright line
1,/Copyright.*Arm/I {
  /This file is part of/,$ {
    /Copyright.*Arm/I! d
  }
}

# Convert non-standard header in scripts/abi_check.py to the format used in the other scripts
/"""/,/"""/ {

  # Cut copyright declaration
  /Copyright.*Arm/I {
    h
    N
    d
  }

  # Paste copyright declaration
  /"""/ {
    x
    /./ {
      s/^/# /    # Add #
      x          # Replace orignal buffer with Copyright declaration
      p          # Print original buffer, insert newline
      i\

      s/.*//     # Clear original buffer
    }
    x
  }
}

/Copyright.*Arm/I {

  # Print copyright declaration
  p

  # Read the two lines immediately following the copyright declaration
  N
  N

  # Insert Apache header if it is missing
  /SPDX/! {
    i\
# 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.

    # Insert Mbed TLS declaration if it is missing
    /This file is part of/! i\
#\
# This file is part of Mbed TLS (https://tls.mbed.org)
  }

  # Clear copyright declaration from buffer
  D
}
'
# ========================

Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2020-06-08 17:46:40 +02:00
Bence Szépkúti
b7246ad938 Add copyright dates to all scripts
To find any files with a missing copyright declaration, use the following script:

# ========================
#!/bin/sh

# Find files with copyright declarations, and list their file extensions
exts=$(grep -Ril --exclude-dir .git 'Copyright.*Arm' | sed '
  s/.*\./-name "*./
  s/$/"/
' | sort -u | sed -n '
  :l
    N
    $!bl
  s/\n/ -o /gp
')

# Find files with file extensions that ususally include copyright extensions,
# but don't include a copyright declaration themselves.
eval "find -path './.git' -prune -o\
  ! -path './tests/data_files/format_pkcs12.fmt'\
  ! -path './programs/psa/psa_constant_names_generated.c'\
  '(' $exts ')' -print" | xargs grep -Li 'Copyright.*Arm'
# ========================

Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2020-06-08 17:26:24 +02:00
Manuel Pégourié-Gonnard
9967bfe35b
Merge pull request #3397 from danh-arm/dh/branch-cov-2.7
Backport 2.7: Enable branch coverage in basic_build_test.sh
2020-06-08 10:15:26 +02:00
Manuel Pégourié-Gonnard
74908a0465
Merge pull request #3402 from mpg/fix-hmac-drbg-deps-2.7
[Backport 2.7] Fix undeclared dependencies on HMAC_DRBG
2020-06-05 11:50:16 +02:00
Manuel Pégourié-Gonnard
1539d15dd5
Merge pull request #3353 from gilles-peskine-arm/fix-ecp-mul-memory-leak-2.7
Backport 2.7: Fix potential memory leak in EC multiplication
2020-06-05 11:44:14 +02:00
Manuel Pégourié-Gonnard
cdfa2f983b Add test for dependencies on HMAC_DRBG in all.sh
Similarly to the recently-added tests for dependencies on CTR_DRBG:
constrained environments will probably want only one DRBG module, and we
should make sure that tests pass in such a configuration.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2020-06-05 09:52:39 +02:00
Dan Handley
aba9e22d25 Enable branch coverage in basic_build_test.sh
Enable branch coverage output in basic_build_test.sh. This
includes enabling branch coverage output to the lcov make target,
which is disabled by default.

Signed-off-by: Dan Handley <dan.handley@arm.com>
2020-06-04 16:41:02 +01:00
Jonas
701063be99 Add Changelog entry for #3318
Signed-off-by: Jonas <jonas.lejeune4420@gmail.com>
2020-06-04 13:39:29 +02:00
Manuel Pégourié-Gonnard
7e384f11f5
Merge pull request #3385 from mpg/fix-ctr-drbg-deps-2.7
[Backport 2.7] Fix undeclared dependencies on CTR_DRBG (and add test)
2020-06-03 10:56:13 +02:00
Manuel Pégourié-Gonnard
c98fde5ca8 Add test for building without CTR_DRBG
People who prefer to rely on HMAC_DRBG (for example because they use it for
deterministic ECDSA and don't want a second DRBG for code size reasons) should
be able to build and run the tests suites without CTR_DRBG.

Ideally we should make sure the level of testing (SSL) is the same regardless
of which DRBG modules is enabled, but that's a more significant piece of work.
For now, just ensure everything builds and `make test` passes.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2020-06-03 09:57:08 +02:00
Manuel Pégourié-Gonnard
801318d47e
Merge pull request #3376 from gilles-peskine-arm/basic-build-test-status-2.7
Backport 2.7: Fix failure detection in basic-build-test.sh
2020-06-03 09:41:34 +02:00
Manuel Pégourié-Gonnard
8eea3ae860 Fix undeclared deps on MBEDTLS_CTR_DRBG in tests
While at it, declare deps on ENTROPY as well.

A non-regression test will be added in a follow-up commit.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2020-06-02 12:33:21 +02:00
Manuel Pégourié-Gonnard
003813f800
Merge pull request #3373 from gilles-peskine-arm/check-files-changelog-2.7
Backport 2.7: Check changelog entries on CI
2020-06-02 09:38:49 +02:00
Gilles Peskine
39b610227b MBEDTLS_MEMORY_BACKTRACE is no longer included in the full config
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-05-28 20:47:34 +02:00
Gilles Peskine
9cc203a7ea Create a seedfile explicitly
Running the entropy unit test creates a suitable seedfile, but this
only works due to the happy accident that no prior unit test needs one
(specifically, test_suite_entropy runs before test_suite_rsa). So
create one explicitly, both for robustness and to keep the script
closer to the version in development where the explicit seedfile
creation is required.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-05-28 20:47:34 +02:00
Gilles Peskine
b07541d9f0 If 'make lcov' failed, exit immediately
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-05-28 20:47:34 +02:00
Gilles Peskine
27c36f050f Note that we keep going even if some tests fail
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-05-28 20:47:34 +02:00
Gilles Peskine
dd7ea389ff Exit with a failure status if some tests failed
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-05-28 20:47:34 +02:00
Gilles Peskine
0506f62569 Fix an LTS version number in a changelog entry
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-05-28 18:35:01 +02:00
Gilles Peskine
14b559a4c3 Finish the documentation of normalize_path
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-05-28 18:34:20 +02:00
Gilles Peskine
c3189259e7 Run assemble_changelog.py in all.sh
Avoid nasty surprises where it would fail when we want to make a release.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-05-28 18:34:20 +02:00
Gilles Peskine
8fa5be5224 Some .pem files are openssl output and have tabs and that's ok
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-05-28 18:34:20 +02:00
Gilles Peskine
e7e149f469 .dsw files are Visual Studio stuff
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-05-28 18:34:20 +02:00
Gilles Peskine
be76c19801 Permit empty files
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-05-28 18:34:20 +02:00
Gilles Peskine
4be18cf1f1 Normalize line endings
Convert all text files to Unix line endings unless they're Windows
stuff.

Make sure that all text files have a trailing newline.

Remove whitespace at the end of lines.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-05-28 18:34:20 +02:00
Gilles Peskine
4bda3254cf Check only files checked into Git
We're only interested in files that are committed and pushed to be
included in Mbed TLS, not in any other files that may be lying around.
So ask git for the list of file names.

This script is primarily intended to run on the CI, and there it runs
on a fresh Git checkout plus potentially some other checkouts or
leftovers from a previous part of the CI job. It should also run
reasonably well on developer machines, where there may be various
additional files. In both cases, git is available.

Ad hoc directory exclusions are no longer needed.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-05-28 18:33:56 +02:00
Gilles Peskine
986a06de94 Exclude binary files from text checks
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-05-28 18:33:18 +02:00
Gilles Peskine
b4805ece78 Regex mechanism for check-specific exemptions
Suffixes are convenient but not always sufficient.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-05-28 18:33:18 +02:00
Gilles Peskine
ab55bc71fe Check all files by default
Have an explicit list of exemptions for specific checks rather than
whitelisting files to check. Some checks, such as permissions, should
apply to all files.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-05-28 18:33:18 +02:00
Gilles Peskine
45137617db More accurate variable name
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-05-28 18:33:18 +02:00
Gilles Peskine
c65aa672dd
Merge pull request #3369 from gilles-peskine-arm/error-include-asn1-2.7
Backport 2.7: Include asn1.h in error.c
2020-05-28 15:09:33 +02:00
Gilles Peskine
a6193908ee Fix #3328
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-05-28 12:06:10 +02:00
Gilles Peskine
ac631bb52a Re-generate error.c
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-05-28 12:06:10 +02:00
Gilles Peskine
c33e2a6a56 Do include asn1.h in error.c
When generate_errors.pl was first written, there was no asn1.h. But
now there is one and it does not need any special treatment.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-05-28 12:04:13 +02:00
Gilles Peskine
43838492af Check that all necessary headers are included in error.c
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2020-05-28 12:04:13 +02:00
danh-arm
64cad2fa07
Merge pull request #3359 from ronald-cron-arm/tool_versions-2.7
Backport 2.7: Add output of make, cmake and python3 versions
2020-05-27 17:44:15 +01:00
Janos Follath
ebc603051a
Merge pull request #3361 from paul-elliott-arm/fix-contributing-2.7
Backport 2.7: Fix contributing link to changelog howto
2020-05-27 07:44:47 +01:00
Paul Elliott
97bd3fa827 Change Changelog link to point at Changelog readme
Make the contributing document link to how to create a changelog rather
than just linking to the Changelog itself. Backported to 2.7

Signed-off-by: Paul Elliott <paul.elliott@arm.com>
2020-05-26 16:16:44 +01:00
Ronald Cron
6872e17b0e Add output of python3 version
Add output of python3 version to output_env.sh.
Added in addition to the version of `python` as some
project's scripts try both executable names.

Signed-off-by: Ronald Cron <ronald.cron@arm.com>
2020-05-26 14:27:54 +02:00