Commit Graph

2908 Commits

Author SHA1 Message Date
Bence Szépkúti
124a87ea6f Bump library version numbers
Signed-off-by: Bence Szépkúti <bence.szepkuti@arm.com>
2021-07-05 18:47:36 +02:00
Bence Szépkúti
726a8ccb6d Merge branch 'mbedtls-2.16-restricted' into mbedtls-2.16.11rc0-pr 2021-07-02 14:00:19 +01:00
Dave Rodgman
cee9e92255 Correct required config flag in ECJPAKE tests
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
2021-06-29 21:15:28 +01:00
Janos Follath
84ef97a450 Fix ecp_check_pub() test cases
Negative x coordinate was tested with the value -1. It happens to be one
of the low order points both for Curve25519 and Curve448 and might be
rejected because of that and not because it is negative. Make sure that
x < 0 is the only plausible reason for the point to be rejected.

Signed-off-by: Janos Follath <janos.follath@arm.com>
2021-06-28 10:24:20 +01:00
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
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
bf1afdc2bb Remove redundant ecp_check_pub() tests
Signed-off-by: Janos Follath <janos.follath@arm.com>
2021-06-28 10:05:32 +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
Gilles Peskine
55e6abc99f Fix copypasta in test data
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
ead6660d8c Fix copypasta in test cases
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
3cfb7be72f Annotate the choice of representation of 0 in more places
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
8c68c97db7 Improve coverage of mbedtls_mpi_cmp_mpi
Test with and without leading zeros on each side.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
5ce7cb3d3c Fix copypasta in test function argument name
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
14357a35f7 Unify G=1 and G=-1 test cases
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
05a1af6d0f In test cases where the result is 0, express it as "0", not ""
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
44e6bb6b38 Fix multiplication with negative result and a low-order 0 limb
Fix a bug introduced in "Fix multiplication producing a negative zero" that
caused the sign to be forced to +1 when A > 0, B < 0 and B's low-order limb
is 0.

Add a non-regression test. More generally, systematically test combinations
of leading zeros, trailing zeros and signs.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
266275e924 mpi_shrink test: just set the top bit
No need to bypass the API to fill limbs. It's a better test to just
set the top bit that we want to have set, and it's one less bypass of
the API.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
81a6743cfb Tweak grouping of GCD test cases
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
d57f403f0c Make GCD test descriptions more uniform
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
0d3bc852b7 DHM: test some edge cases for the generator
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
4d106c1306 Add RSA tests with message=0
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
ab6ab6aaf0 Fix multiplication producing a negative zero
Fix mbedtls_mpi_mul_mpi() when one of the operands is zero and the
other is negative. The sign of the result must be 1, since some
library functions do not treat {-1, 0, NULL} or {-1, n, {0}} as
representing the value 0.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
c559eac574 Fix null pointer dereference in mbedtls_mpi_exp_mod
Fix a null pointer dereference in mbedtls_mpi_exp_mod(X, A, N, E, _RR) when
A is the value 0 represented with 0 limbs.

Make the code a little more robust against similar bugs.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
bc781eab47 Add many test cases involving 0
Test both 0 represented with 0 limbs ("0 (null)") and 0 represented
with 1 limb ("0 (1 limb)"), because occasionally there are bugs with
0-limb bignums and occasionally there are bugs with removing leading
zero limbs.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
efc3fd4c03 Test mbedtls_mpi_exp_mod both with and without _RR
mbedtls_mpi_exp_mod can be called in three ways regarding the speed-up
parameter _RR: null (unused), zero (will be updated), nonzero (will be
used). Systematically test all three.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
cca6bb909d mbedtls_mpi_exp_mod test: don't read RR from test data
Remove the RR parameter to the mbedtls_mpi_exp_mod test function.
It was never used in the test data, so there is no loss of functionality.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
9e8316e6eb Add some GCD tests
Add GCD tests with negative arguments and with large non-co-prime arguments.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
4cbb1c9cc9 Test mbedtls_mpi_safe_cond_{assign,swap} with the basic functions
Test mbedtls_mpi_safe_cond_assign() and mbedtls_mpi_safe_cond_swap()
with their "unsafe" counterparts mbedtls_mpi_copy() and
mbedtls_mpi_swap(). This way we don't need to repeat the coverage of
test cases.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
1e914269b0 Overhaul testing of mbedtls_mpi_swap
Similarly to "Overhaul testing of mbedtls_mpi_copy", simplify the code
to test mbedtls_mpi_swap to have just one function for distinct MPIs
and one function for swapping an MPI with itself, covering all cases
of size (0, 1, >1) and sign (>0, <0).

The test cases are exactly the same as for mbedtls_mpi_copy with the
following replacements:
* `Copy` -> `Swap`
* ` to ` -> ` with `
* `_copy` -> `_swap`

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
8e1aa66479 Overhaul testing of mbedtls_mpi_copy
Replace the two test functions mbedtls_mpi_copy_sint (supporting signed
inputs but always with exactly one limb) and mbedtls_mpi_copy_binary
(supporting arbitrary-sized inputs but not negative inputs) by a single
function that supports both arbitrary-sized inputs and arbitrary-signed
inputs. This will allows testing combinations like negative source and
zero-sized destination.

Also generalize mpi_copy_self to support arbitrary inputs.

Generate a new list of test cases systematically enumerating all
possibilities among various categories: zero with 0 or 1 limb, negative or
positive with 1 limb, negative or positive with >1 limb. I used the
following Perl script:

```
sub rhs { $_ = $_[0]; s/bead/beef/; s/ca5cadedb01dfaceacc01ade/face1e55ca11ab1ecab005e5/; $_ }
%v = (
    "zero (null)" => "",
    "zero (1 limb)" => "0",
    "small positive" => "bead",
    "large positive" => "ca5cadedb01dfaceacc01ade",
    "small negative" => "-bead",
    "large negative" => "-ca5cadedb01dfaceacc01ade",
);
foreach $s (sort keys %v) {
    foreach $d (sort keys %v) {
        printf "Copy %s to %s\nmbedtls_mpi_copy:\"%s\":\"%s\"\n\n",
               $s, $d, $v{$s}, rhs($v{$d});
    }
}
foreach $s (sort keys %v) {
    printf "Copy self: %s\nmpi_copy_self:\"%s\"\n\n", $s, $v{$s};
}
```

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
8854c5d450 Test the validity of the sign bit after constructing an MPI object
This is mostly to look for cases where the sign bit may have been left at 0
after zerozing memory, or a value of 0 with the sign bit set to -11. Both of
these mostly work fine, so they can go otherwise undetected by unit tests,
but they can break when certain combinations of functions are used.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
b8e1534a0d Use mbedtls_test_read_mpi in test suites
Replace calls to mbedtls_mpi_read_string() with a wrapper
mbedtls_test_read_mpi() when reading test data except for the purpose
of testing mbedtls_mpi_read_string() itself. The wrapper lets the test
data control precisely how many limbs the constructed MPI has.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:48:37 +02:00
Gilles Peskine
bbc4b8d2be New test helper mbedtls_test_read_mpi
This test helper reads an MPI from a string and guarantees control over the
number of limbs of the MPI, allowing test cases to construct values with or
without leading zeros, including 0 with 0 limbs.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:47:44 +02:00
Gilles Peskine
aa9d9ac598 Clarify a few test descriptions (mostly involving 0)
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-22 18:47:44 +02:00
Manuel Pégourié-Gonnard
07941f45e6 Merge pull request #4690 from gilles-peskine-arm/debug-print-mpi-null-2.16
Backport 2.16: Fix mbedtls_debug_print_mpi crash on 0
2021-06-22 12:09:05 +02: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
Gilles Peskine
6345e12161 Add mbedtls_debug_print_mpi test case for 0
There was already a test case for 0 but with a non-empty representation
(X->n == 1). Add a test case with X->n == 0 (freshly initialized mpi).

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-21 10:13:43 +02:00
Gilles Peskine
d8aa3dbb04 Clarify test case descriptions
Reorder test cases and make their descriptions more explicit. No
change in test data.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-21 10:13:43 +02: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
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
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
Gilles Peskine
d1eb14ae8a Test mbedtls_dhm_make_params with different x_size
mbedtls_dhm_make_params() with x_size != size of P is not likely to be
useful, but it's supported, so test it.

Cherry-picked 33ec863570
Changed mbedtls_test_rnd_pseudo_info type to rnd_pseudo_info

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Ronald Cron <ronald.cron@arm.com>
2021-06-10 10:35:36 +02:00
Gilles Peskine
863b83b666 Repeat a few DH tests
Repeat a few tests that use random data. This way the code is
exercised with a few different random values.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-06-10 10:35:18 +02:00
Gilles Peskine
92f4714e7e Test range and format of dhm_make_params output
Improve the validation of the output from mbedtls_dhm_make_params:
* Test that the output in the byte buffer matches the value in the
  context structure.
* Test that the calculated values are in the desired range.

Cherry-picked dc0b6e44b0.
Changed mbedtls_test_rnd_pseudo_rand to rnd_pseudo_rand.
Removed test step code.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
Signed-off-by: Ronald Cron <ronald.cron@arm.com>
2021-06-10 10:34:37 +02:00
Gilles Peskine
8e7d7ee1ae Fix ecp_muladd test cases never getting executed
These test cases had been backported from Mbed TLS 2.x with a dependency
symbol that didn't exist in 2.16. Declare that symbol.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-05-31 20:25:26 +02:00
Gilles Peskine
5f45bd2bab New macros TEST_EQUAL, ASSERT_ALLOC, ASSERT_ALLOC_WEAK
Backports some test helper macros added after 2.16. This will facilitate
backporting new test code.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-05-31 15:40:31 +02:00
Gilles Peskine
e0b455fb51 Remove spurious dependencies on PEM
Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
2021-05-31 15:23:00 +02:00
Ronald Cron
1bc2c9cb8b Merge pull request #4534 from gilles-peskine-arm/host_test-int32-2.16
Backport 2.16: Fix build error in host_test.function when int32_t is not int
2021-05-21 16:01:50 +02:00