Skip to content

Merge development into API spec branch #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

gilles-peskine-arm
Copy link
Collaborator

@gilles-peskine-arm gilles-peskine-arm commented Apr 11, 2019

  1. Make a branch containing both the latest specification improvements and the latest implementation. This is a merge of the development branch at c70a3c7 into psa-api-1.0-beta.
  2. Update the key agreement tests so that the CI passes apart from X509/TLS code.

This makes some updates to the spec:

Out of scope:

  • Backward compatibility with the old one-shot key derivation API from before beta 1.
  • Good test coverage of key derivation and key agreement.
  • Making MBEDTLS_USE_PSA_CRYPTO work.

Reviewers: please assume that the current tip of psa-api-1.0-beta and the commit used from development are ok, and review only “Merge remote-tracking branch 'upstream-crypto/development' into psa-api-beta2-merge-development” and the subsequent commits.

gilles-peskine-arm and others added 30 commits February 19, 2019 19:45
In multipart cipher tests, test that each step of psa_cipher_update
produces output of the expected length. The length is hard-coded in
the test data since it depends on the mode.

The length of the output of psa_cipher_finish is effectively tested
because it's the total output length minus the length produced by the
update steps.
…itial

Specification of how keys and the random seed are stored
Prepare support for 64 bit key ids under SPM
It is now required to initialize PSA Crypto operation contexts before
calling psa_*_setup(). Otherwise, one gets a PSA_ERROR_BAD_STATE error.
Add missing initializers to PSA Crypto example. Operation contexts must
be initialized before calling psa_*_setup().
It is now required to initialize PSA Crypto operation contexts before
calling psa_*_setup(). Otherwise, one gets a PSA_ERROR_BAD_STATE error.
…er_update_test

Improve multipart cipher tests: 2 blocks, CTR
PSA_MAX_PERSISTENT_KEY_IDENTIFIER was actually one plus the maximum
key identifier. Change it to be the maximum value, and change the code
that uses it accordingly.

There is no semantic change here (the maximum value hasn't changed).
This commit only makes the implementation clearer.
Differentiate between _key identifiers_, which are always `uint32_t`,
and _key file identifiers_, which are platform-dependent. Normally,
the two are the same.

In `psa/crypto_platform.h`, define `psa_app_key_id_t` (which is always
32 bits, the standard key identifier type) and
`psa_key_file_id_t` (which will be different in some service builds).
A subsequent commit will introduce a platform where the two are different.

It would make sense for the function declarations in `psa/crypto.h` to
use `psa_key_file_id_t`. However this file is currently part of the
PSA Crypto API specification, so it must stick to the standard type
`psa_key_id_t`. Hence, as long as the specification and Mbed Crypto
are not separate, use the implementation-specific file
`psa/crypto_platform.h` to define `psa_key_id_t` as `psa_key_file_id_t`.

In the library, systematically use `psa_key_file_id_t`.

    perl -i -pe 's/psa_key_id_t/psa_key_file_id_t/g' library/*.[hc]
Declare the owner as psa_key_owner_id_t, of which an implementation
must be provided separately.

Make this a configuration option
MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER, to make the conditional
compilation flow easier to follow. Declare it in config.h to
pacify check_names.sh.

Support for a specific implementation of psa_key_owner_id_t in storage
backends will come in a subsequent commit.
When building for the PSA crypto service (defined(PSA_CRYPTO_SECURE)),
define psa_key_owner_id_t as int32_t, which is how a PSA platform
encodes partition identity. Note that this only takes effect when the
build option MBEDTLS_PSA_CRYPTO_KEY_FILE_ID_ENCODES_OWNER is active.

Support this configuration in the ITS backend.
…internal_key_id

Support key file IDs encoding the key owner
In case the new constants aren't available yet in Mbed TLS, continue to
use the deprecated constants if they are available.
Switch to the terminology "key file identifier", as has been done in
the code.

The owner uid is now in the upper 32 bits of the key file identifier,
which facilitates namespacing.
Ensure that when doing MAC operations out of order, PSA_ERROR_BAD_STATE
is returned as documented in crypto.h and the PSA Crypto specification.
Ensure that when doing cipher operations out of order,
PSA_ERROR_BAD_STATE is returned as documented in crypto.h and the PSA
Crypto specification.
If a hash context has not been set up, fail with PSA_ERROR_BAD_STATE as
documented in crypto.h and the PSA Crypto specification.
Extend hash bad order test in line with the new bad order tests for MAC
and cipher, covering more cases and making comments and test layout
consistent.

Ensure that when doing hash operations out of order, PSA_ERROR_BAD_STATE
is returned as documented in crypto.h and the PSA Crypto specification.
Calling psa_*_setup() twice on a MAC, cipher, or hash context should
result in a PSA_ERROR_BAD_STATE error because the operation has already
been set up.

Fixes ARMmbed#10
psa: Be compatible with deprecated constants
…y_file_id

Update the architecture document regarding key file identifiers
In places where we detect a context is in a bad state and there is no
sensitive data to clear, simply return PSA_ERROR_BAD_STATE and don't
abort on behalf of the application. The application will choose what to
do when it gets a bad state error.

The motivation for this change is that an application should decide what
to do when it misuses the API and encounters a PSA_ERROR_BAD_STATE
error. The library should not attempt to abort on behalf of the
application, as that may not be the correct thing to do in all
circumstances.
Add deprecated error codes to help transition between the previous
version of the PSA Crypto specification and the current one.
psa: Add backwards compatible error codes
Add a test case for doing an ECDH calculation by calling
mbedtls_ecdh_get_params on both keys, then mbedtls_ecdh_calc_secret.
Add a test case for doing an ECDH calculation by calling
mbedtls_ecdh_get_params on both keys, with keys belonging to
different groups. This should fail, but currently passes.
@Patater
Copy link
Contributor

Patater commented Apr 15, 2019

I've completed my review.

yanesca
yanesca previously approved these changes Apr 17, 2019
Copy link
Collaborator

@yanesca yanesca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a minor style issue, but other than that it looks good to me.

@@ -4665,6 +4684,38 @@ psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
return( status );
}

psa_status_t psa_key_agreement_raw_shared_secret(psa_algorithm_t alg,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: missing space after/before parentheses.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in rebase

@gilles-peskine-arm
Copy link
Collaborator Author

Thanks for the reviews. I rebased some commits to address minor issues.

For the driver model, there hadn't been any changes in the API branch, so the development branch had the latest version.

/* If an error happens and is not handled properly, the output
* may be used as a key to protect sensitive data. Arrange for such
* a key to be random, which is likely to resist in decryption or
* verification errors. This is better than filling the buffer with
Copy link
Contributor

@Patater Patater Apr 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In commit "Implement psa_key_agreement_raw_shared_secret":

s/resist/result

-         * a key to be random, which is likely to resist in decryption or
-         * verification errors. This is better than filling the buffer with
+         * a key to be random, which is likely to result in decryption or
+         * verification errors. This is better than filling the buffer with

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in rebase

@Patater
Copy link
Contributor

Patater commented Apr 17, 2019

I've completed another round of review. Just one typo found.

@Patater
Copy link
Contributor

Patater commented Apr 17, 2019

Note that Windows tests are failing until the latest development is again merged into the API branch.

Refactor: split psa_key_agreement_raw_internal out of
psa_key_agreement_internal, and call it from
psa_key_agreement_raw_shared_secret as well.
Separate test functions for raw key agreement and key agreement with
KDF.
Allow either the key derivation step or the key agreement step to
fail.

These tests should be split into three groups: key derivation setup
tests with an algorithm that includes a key agreement step, and
multipart key agreement failure tests, and raw key agreement failure
tests.
Since the format change for EC public key import from
SubjectPublicKeyInfo to the ECPoint content, it is no longer possible
to import a key with metadata marking it as ECDH-only. This test was
converted systematically but now no longer has any purpose since the
public key is now like any other public key.
Change test cases with test data for raw agreement to this new test
function.
Basic coverage with one algorithm only and a restricted choice of
output lengths.
This gives a little more room to encode key agreement algorithms,
while keeping enough space for key derivation algorithms.

This doesn't affect any of the already-defined algorithms.
One was obsolete. Reword the other two to avoid the magic word that
our CI rejects.
Fix logic error that clang helpfully points out
uintN_t is not a standard type for a bitfield, as armcc points out.
Simplify the logic inside a few case statements. This removes
unreachable break statements.
@gilles-peskine-arm gilles-peskine-arm force-pushed the psa-api-beta2-merge-development branch from 91824b5 to 2b522db Compare April 18, 2019 07:42
@gilles-peskine-arm
Copy link
Collaborator Author

I added a new merge commit to update to current development, so the CI should pass now.

Copy link
Contributor

@Patater Patater left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Patater
Copy link
Contributor

Patater commented Apr 18, 2019

Looks like there are some build failures in CI, but these are expected as we haven't fixed MBEDTLS_USE_PSA_CRYPTO yet.

In file included from /var/lib/build/include/psa/crypto.h:62:0,
                 from /var/lib/build/include/mbedtls/pk.h:49,
                 from /var/lib/build/include/mbedtls/ssl_ciphersuites.h:33,
                 from /var/lib/build/include/mbedtls/ssl.h:36,
                 from /var/lib/build/include/mbedtls/debug.h:33,
                 from /var/lib/build/library/ssl_cli.c:38:
/var/lib/build/library/ssl_cli.c: In function 'ssl_write_client_key_exchange':
/var/lib/build/include/psa/crypto_values.h:1381:49: error: called object is not a function or function pointer
 #define PSA_ALG_ECDH                            ((psa_algorithm_t)0x30200000)
                                                 ^
/var/lib/build/library/ssl_cli.c:3145:35: note: in expansion of macro 'PSA_ALG_ECDH'
                                   PSA_ALG_ECDH( PSA_ALG_SELECT_RAW ) );
                                   ^
/var/lib/build/include/psa/crypto_values.h:1381:49: error: called object is not a function or function pointer
 #define PSA_ALG_ECDH                            ((psa_algorithm_t)0x30200000)
                                                 ^
/var/lib/build/library/ssl_cli.c:3185:37: note: in expansion of macro 'PSA_ALG_ECDH'
                                     PSA_ALG_ECDH( PSA_ALG_SELECT_RAW ) );
                                     ^
/var/lib/build/library/ssl_cli.c:3183:37: error: passing argument 3 of 'psa_key_agreement' makes integer from pointer without a cast [-Werror=int-conversion]
                                     handshake->ecdh_psa_peerkey,
                                     ^
In file included from /var/lib/build/include/mbedtls/pk.h:49:0,
                 from /var/lib/build/include/mbedtls/ssl_ciphersuites.h:33,
                 from /var/lib/build/include/mbedtls/ssl.h:36,
                 from /var/lib/build/include/mbedtls/debug.h:33,
                 from /var/lib/build/library/ssl_cli.c:38:
/var/lib/build/include/psa/crypto.h:3233:14: note: expected 'psa_key_handle_t {aka short unsigned int}' but argument is of type 'unsigned char *'
 psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
              ^
/var/lib/build/library/ssl_cli.c:3184:37: error: passing argument 4 of 'psa_key_agreement' makes pointer from integer without a cast [-Werror=int-conversion]
                                     handshake->ecdh_psa_peerkey_len,
                                     ^
In file included from /var/lib/build/include/mbedtls/pk.h:49:0,
                 from /var/lib/build/include/mbedtls/ssl_ciphersuites.h:33,
                 from /var/lib/build/include/mbedtls/ssl.h:36,
                 from /var/lib/build/include/mbedtls/debug.h:33,
                 from /var/lib/build/library/ssl_cli.c:38:
/var/lib/build/include/psa/crypto.h:3233:14: note: expected 'const uint8_t * {aka const unsigned char *}' but argument is of type 'size_t {aka long unsigned int}'
 psa_status_t psa_key_agreement(psa_crypto_generator_t *generator,
              ^
cc1: all warnings being treated as errors
library/CMakeFiles/mbedtls.dir/build.make:158: recipe for target 'library/CMakeFiles/mbedtls.dir/ssl_cli.c.o' failed
make[2]: *** [library/CMakeFiles/mbedtls.dir/ssl_cli.c.o] Error 1
make[2]: *** Waiting for unfinished jobs....
CMakeFiles/Makefile2:253: recipe for target 'library/CMakeFiles/mbedtls.dir/all' failed
make[1]: *** [library/CMakeFiles/mbedtls.dir/all] Error 2
make: *** [all] Error 2
Makefile:138: recipe for target 'all' failed
^^^^test_use_psa_crypto_full_cmake_asan: build: cmake, full config + MBEDTLS_USE_PSA_CRYPTO, ASan: command make -> 2^^^^

@gilles-peskine-arm
Copy link
Collaborator Author

Oh right, yes, MBEDTLS_USE_PSA_CRYPTO is not expected to build. Everything else should pass.

@Patater Patater removed the needs: review The pull request is ready for review. This generally means that it has no known issues. label Apr 18, 2019
@Patater Patater merged commit c69af20 into ARMmbed:psa-api-1.0-beta Apr 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants