Commit Graph

9649 Commits

Author SHA1 Message Date
Janos Follath
921415b0c2 Add ecp_check_pub tests for Curve 448
Signed-off-by: Janos Follath <janos.follath@arm.com>
2021-06-28 10:24:20 +01:00
Janos Follath
5f9b667396 Reject low-order points on Curve448 early
We were already rejecting them at the end, due to the fact that with the
usual (x, z) formulas they lead to the result (0, 0) so when we want to
normalize at the end, trying to compute the modular inverse of z will
give an error.

If we wanted to support those points, we'd a special case in
ecp_normalize_mxz(). But it's actually permitted by all sources (RFC
7748 say we MAY reject 0 as a result) and recommended by some to reject
those points (either to ensure contributory behaviour, or to protect
against timing attack when the underlying field arithmetic is not
constant-time).

Since our field arithmetic is indeed not constant-time, let's reject
those points before they get mixed with sensitive data (in
ecp_mul_mxz()), in order to avoid exploitable leaks caused by the
special cases they would trigger. (See the "May the Fourth" paper
https://eprint.iacr.org/2017/806.pdf)

Signed-off-by: Janos Follath <janos.follath@arm.com>
2021-06-28 10:24:20 +01:00
Janos Follath
15fc884f04 Add DoS test case for ecp_check_pub
A test case for which the loop would take practically forever if it was
reached. The point would be to validate that the loop is not reached.
The test case should cause the CI to time out if starting with the
current code, ecp_check_pubkey_mx() was changed to call
ecp_check_pubkey_x25519() first and run the mbedtls_mpi_size(() test
afterwards, which would make no semantic difference in terms of memory
contents when the function returns, but would open the way for a DoS.

Signed-off-by: Janos Follath <janos.follath@arm.com>
2021-06-28 10:24:20 +01:00
Janos Follath
b741e8d263 Use mbedtls_mpi_lset() more
Signed-off-by: Janos Follath <janos.follath@arm.com>
2021-06-28 10:24:20 +01:00
Janos Follath
7d34e2e655 Move mpi constant macros to bn_mul.h
Signed-off-by: Janos Follath <janos.follath@arm.com>
2021-06-28 10:23:57 +01:00
Janos Follath
bf1afdc2bb Remove redundant ecp_check_pub() tests
Signed-off-by: Janos Follath <janos.follath@arm.com>
2021-06-28 10:05:32 +01:00
Janos Follath
c16ec6be85 Prevent memory leak in ecp_check_pubkey_x25519()
Signed-off-by: Janos Follath <janos.follath@arm.com>
2021-06-28 10:05:31 +01:00
Manuel Pégourié-Gonnard
9f12b11be0 Avoid complaints about undeclared non-static symbols
Clang was complaining and check-names.sh too

This only duplicates macros, so no impact on code size. In 3.0 we can
probably avoid the duplication by using an internal header under
library/ but this won't work for 2.16.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-28 10:05:31 +01:00
Manuel Pégourié-Gonnard
89ce7d2445 Use more compact encoding of Montgomery curve constants
Base 256 beats base 16.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-28 10:05:31 +01:00
Manuel Pégourié-Gonnard
6ec1535148 Use a more compact encoding of bad points
Base 10 is horrible, base 256 is much better.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-28 10:05:31 +01:00
Manuel Pégourié-Gonnard
98967959df Add test for check_pubkey for x25519
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-28 10:05:31 +01:00
Manuel Pégourié-Gonnard
4d0b9da37d Reject low-order points on Curve25519 early
We were already rejecting them at the end, due to the fact that with the
usual (x, z) formulas they lead to the result (0, 0) so when we want to
normalize at the end, trying to compute the modular inverse of z will
give an error.

If we wanted to support those points, we'd a special case in
ecp_normalize_mxz(). But it's actually permitted by all sources
(RFC 7748 say we MAY reject 0 as a result) and recommended by some to
reject those points (either to ensure contributory behaviour, or to
protect against timing attack when the underlying field arithmetic is
not constant-time).

Since our field arithmetic is indeed not constant-time, let's reject
those points before they get mixed with sensitive data (in
ecp_mul_mxz()), in order to avoid exploitable leaks caused by the
special cases they would trigger. (See the "May the Fourth" paper
https://eprint.iacr.org/2017/806.pdf)

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-28 10:05:29 +01:00
Manuel Pégourié-Gonnard
fa719f7415 Merge branch 'mbedtls-2.16' into mbedtls-2.16-restricted
* mbedtls-2.16: (21 commits)
  Reword changelog - Test Resource Leak
  Update changelog formatting - Missing Free Context
  Fix fd range for select on Windows
  Refactor file descriptor checks into a common function
  Update changelog formatting - Missing Free Context
  Update changelog formatting - Missing Free Context
  Changelog entry for Free Context in test_suite_aes fix
  Free context at the end of aes_crypt_xts_size()
  Add changelog entry for non-uniform MPI random generation
  ecp: Fix bias in the generation of blinding values
  DHM: add test case with x_size < 0
  DHM tests: add some explanations
  DHM: add notes about leading zeros
  dhm: Fix bias in private key generation and blinding
  dhm_check_range: microoptimization
  DHM refactoring: use dhm_random_below in dhm_make_common
  DHM blinding: don't accept P-1 as a blinding value
  DHM refactoring: unify mbedtls_dhm_make_{params,public}
  Test mbedtls_dhm_make_params with different x_size
  Repeat a few DH tests
  ...
2021-06-22 10:57:13 +02:00
Dave Rodgman
b3b1d4d9b4 Merge pull request #4684 from JoeSubbiani/2.16FixedMissingContextFree
Backport 2.16: Add Free context at the end of aes_crypt_xts_size()
2021-06-22 09:24:19 +01:00
Manuel Pégourié-Gonnard
34666bb361 Merge pull request #836 from mpg/rsa-lookup-2.16-restricted
[Backport 2.16] Use constant-time look-up in modular exponentiation
2021-06-22 09:33:28 +02:00
Manuel Pégourié-Gonnard
58344efc91 Merge pull request #4689 from gilles-peskine-arm/winsock-fd-range-2.16
Backport 2.16: Fix net_sockets regression on Windows
2021-06-22 09:29:41 +02:00
Joe Subbiani
b047f99441 Reword changelog - Test Resource Leak
- “Fix an issue where X happens” → ”Fix X“
  the extra words are just a distraction.
- “resource” → “a resource”
- “where resource is never freed” has a name: it's a resource leak
- “when running one particular test suite” → “in a test suite”

Signed-off-by: Joe Subbiani <joe.subbiani@arm.com>
2021-06-21 16:59:25 +01:00
Joe Subbiani
c8031855d0 Update changelog formatting - Missing Free Context
Missing trailing full stop added to the end of the fixed issue number

Signed-off-by: Joe Subbiani <joe.subbiani@arm.com>
2021-06-21 09:30:50 +01:00
Gilles Peskine
8297657759 Fix fd range for select on Windows
Fix mbedtls_net_poll() and mbedtls_net_recv_timeout() often failing with
MBEDTLS_ERR_NET_POLL_FAILED on Windows: they were testing that the file
descriptor is in range for fd_set, but on Windows socket descriptors are not
limited to a small range. Fixes #4465.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-20 23:19:05 +02:00
Gilles Peskine
9065d786fd Refactor file descriptor checks into a common function
This will make it easier to change the behavior uniformly.

No behavior change.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-20 23:19:04 +02:00
Joe Subbiani
dcdb277f34 Update changelog formatting - Missing Free Context
Trailing white space causing check_files.py to fail
issue4176.txt was also in dos format - this has been
changed to unix

Signed-off-by: Joe Subbiani <joe.subbiani@arm.com>
2021-06-18 18:59:01 +01:00
Joe Subbiani
cbe60337e3 Update changelog formatting - Missing Free Context
The original formatting was in dos and the changelog
assembler would fail. The length of the description was
too long horizontally. This has been updated.

Signed-off-by: Joe Subbiani <joe.subbiani@arm.com>
2021-06-18 15:23:34 +01:00
JoeSubbiani
402b1451c0 Changelog entry for Free Context in test_suite_aes fix
Signed-off-by: Joe Subbiani <joe.subbiani@arm.com>
2021-06-18 11:47:08 +01:00
JoeSubbiani
2f28c6b677 Free context at the end of aes_crypt_xts_size()
in file tests/suite/test_suite_aes.function, aes_crypt_xts_size()
did not free the context upon the function exit.
The function now frees the context on exit.

Already resolved for 2.x and development - this is a backport for
2.16

Fixes #4176

Signed-off-by: JoeSubbiani <Joe.Subbiani@arm.com>
2021-06-18 11:05:47 +01:00
Manuel Pégourié-Gonnard
e9eca7fe8d Homogenize coding patterns
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-17 16:40:22 +02:00
Manuel Pégourié-Gonnard
56efc52d6b Merge pull request #4628 from ronald-cron-arm/dhm-key-generation-bias
dhm: Fix bias in private key generation
2021-06-16 13:13:34 +02:00
Manuel Pégourié-Gonnard
6aba8fc230 No C99 loops in this branch
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-15 13:28:50 +02:00
Manuel Pégourié-Gonnard
de2ab2a4bd Fix GCC warning
We know that we'll never call this function with T_size == 0, but the
compiler doesn't.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-15 12:37:23 +02:00
Ronald Cron
6f4fbb4640 Merge pull request #838 from gilles-peskine-arm/ecp_max_bits-check-2.16
Backport 2.16: check MBEDTLS_ECP_MAX_BITS
2021-06-15 11:12:52 +02:00
Gilles Peskine
5a6549752f Check MBEDTLS_ECP_MAX_xxx constants in unit tests
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-14 13:04:48 +02:00
Gilles Peskine
eaf7442f62 Fail the build if MBEDTLS_ECP_MAX_BITS is not large enough
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-14 13:04:45 +02:00
Manuel Pégourié-Gonnard
3907bb9a7b Add ChangeLog entry about RSA side channel.
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-11 10:22:56 +02:00
Manuel Pégourié-Gonnard
4fc96dff3d Silence MSVC type conversion warnings
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-11 10:22:56 +02:00
Manuel Pégourié-Gonnard
12f0238c7f Simplify sign selection
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-11 10:22:56 +02:00
Manuel Pégourié-Gonnard
dc6a5f2f68 Avoid UB caused by conversion to int
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-11 10:22:56 +02:00
Manuel Pégourié-Gonnard
a1283cc638 Use bit operations for mpi_safe_cond_swap()
Unrelated to RSA (only used in ECP), but while improving one
mbedtls_safe_cond_xxx function, let's improve the other as well.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-11 10:22:56 +02:00
Manuel Pégourié-Gonnard
245a806086 Use bit operations for mpi_safe_cond_assign()
- copied limbs
- sign
- cleared limbs

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-11 10:22:55 +02:00
Manuel Pégourié-Gonnard
432ebbaf71 Avoid using == for sensitive comparisons
mbedtls_mpi_cf_bool_eq() is a verbatim copy of mbedtls_ssl_cf_bool_eq()

Deduplication will be part of a future task.

Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-11 10:22:55 +02:00
Manuel Pégourié-Gonnard
87bd44405e Use constant-time look-up for modular exponentiation
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2021-06-11 10:22:55 +02:00
Manuel Pégourié-Gonnard
1283ed9e3b Merge branch 'mbedtls-2.16' into mbedtls-2.16-restricted
* mbedtls-2.16:
  Disable OS X builds on Travis
  Fix null pointer arithmetic in error case
  Fix ecp_muladd test cases never getting executed
  New macros TEST_EQUAL, ASSERT_ALLOC, ASSERT_ALLOC_WEAK
  Remove spurious dependencies on PEM
  Fix misuse of MD API in SSL constant-flow HMAC
2021-06-11 10:18:55 +02:00
Gilles Peskine
ebe9ec448b Add changelog entry for non-uniform MPI random generation
Fix #4245.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-11 08:52:21 +02:00
Ronald Cron
2e0969abce ecp: Fix bias in the generation of blinding values
Signed-off-by: Ronald Cron <ronald.cron@arm.com>
2021-06-11 08:52:14 +02:00
Dave Rodgman
7ef266045b Merge pull request #4645 from daverodgman/travis-disable-osx-2.16
Backport 2.16: Disable OS X Travis builds
2021-06-10 17:48:42 +01:00
Dave Rodgman
58d238a3a4 Disable OS X builds on Travis
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
2021-06-10 15:51:28 +01:00
Gilles Peskine
424d13ce7d DHM: add test case with x_size < 0
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-10 10:40:48 +02:00
Gilles Peskine
60c4fec07f DHM tests: add some explanations
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-10 10:40:42 +02:00
Gilles Peskine
39b1a51cc3 DHM: add notes about leading zeros
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-10 10:40:37 +02:00
Ronald Cron
b4367a358f dhm: Fix bias in private key generation and blinding
Signed-off-by: Ronald Cron <ronald.cron@arm.com>
2021-06-10 10:40:34 +02:00
Gilles Peskine
260be63e7d dhm_check_range: microoptimization
No need to build a bignum for the value 2.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-10 10:40:30 +02:00
Gilles Peskine
c53560ea00 DHM refactoring: use dhm_random_below in dhm_make_common
dhm_make_common includes a piece of code that is identical to
dhm_random_below except for returning a different error code in one
case. Call dhm_random_below instead of repeating the code.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-10 10:40:26 +02:00