mirror of
https://github.com/cuberite/polarssl.git
synced 2025-09-29 00:18:06 -04:00
Merge pull request #8899 from gilles-peskine-arm/pk_copy_public_from_psa
New function mbedtls_pk_copy_public_from_psa
This commit is contained in:
commit
3b20bda352
@ -1,3 +1,4 @@
|
||||
Features
|
||||
* The new function mbedtls_pk_copy_from_psa() provides a way to set up a PK
|
||||
context with the same content as a PSA key.
|
||||
* The new functions mbedtls_pk_copy_from_psa() and
|
||||
mbedtls_pk_copy_public_from_psa() provide ways to set up a PK context
|
||||
with the same content as a PSA key.
|
||||
|
@ -426,6 +426,39 @@ int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
|
||||
* parameters are not correct.
|
||||
*/
|
||||
int mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk);
|
||||
|
||||
/**
|
||||
* \brief Create a PK context for the public key of a PSA key.
|
||||
*
|
||||
* The key must be an RSA or ECC key. It can be either a
|
||||
* public key or a key pair, and only the public key is copied.
|
||||
* The resulting PK object will be a transparent type:
|
||||
* - #MBEDTLS_PK_RSA for RSA keys or
|
||||
* - #MBEDTLS_PK_ECKEY for EC keys.
|
||||
*
|
||||
* Once this functions returns the PK object will be completely
|
||||
* independent from the original PSA key that it was generated
|
||||
* from.
|
||||
* Calling mbedtls_pk_verify() or
|
||||
* mbedtls_pk_encrypt() on the resulting
|
||||
* PK context will perform the corresponding algorithm for that
|
||||
* PK context type.
|
||||
*
|
||||
* For an RSA key, the output PK context will allow both
|
||||
* encrypt and verify regardless of the original key's policy.
|
||||
* The original key's policy determines the output key's padding
|
||||
* mode: PCKS1 v2.1 is set if the PSA key policy is OAEP or PSS,
|
||||
* otherwise PKCS1 v1.5 is set.
|
||||
*
|
||||
* \param key_id The key identifier of the key stored in PSA.
|
||||
* \param pk The PK context that will be filled. It must be initialized,
|
||||
* but not set up.
|
||||
*
|
||||
* \return 0 on success.
|
||||
* \return MBEDTLS_ERR_PK_BAD_INPUT_DATA in case the provided input
|
||||
* parameters are not correct.
|
||||
*/
|
||||
int mbedtls_pk_copy_public_from_psa(mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk);
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
|
||||
#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
|
||||
|
29
library/pk.c
29
library/pk.c
@ -1379,7 +1379,9 @@ mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
||||
int mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk)
|
||||
static int copy_from_psa(mbedtls_svc_key_id_t key_id,
|
||||
mbedtls_pk_context *pk,
|
||||
int public_only)
|
||||
{
|
||||
psa_status_t status;
|
||||
psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
|
||||
@ -1400,13 +1402,20 @@ int mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk
|
||||
return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
status = psa_export_key(key_id, exp_key, sizeof(exp_key), &exp_key_len);
|
||||
if (public_only) {
|
||||
status = psa_export_public_key(key_id, exp_key, sizeof(exp_key), &exp_key_len);
|
||||
} else {
|
||||
status = psa_export_key(key_id, exp_key, sizeof(exp_key), &exp_key_len);
|
||||
}
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = PSA_PK_TO_MBEDTLS_ERR(status);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
key_type = psa_get_key_type(&key_attr);
|
||||
if (public_only) {
|
||||
key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(key_type);
|
||||
}
|
||||
key_bits = psa_get_key_bits(&key_attr);
|
||||
alg_type = psa_get_key_algorithm(&key_attr);
|
||||
|
||||
@ -1435,7 +1444,8 @@ int mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk
|
||||
|
||||
if (PSA_ALG_IS_RSA_OAEP(alg_type) || PSA_ALG_IS_RSA_PSS(alg_type)) {
|
||||
ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(*pk), MBEDTLS_RSA_PKCS_V21, md_type);
|
||||
} else {
|
||||
} else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg_type) ||
|
||||
alg_type == PSA_ALG_RSA_PKCS1V15_CRYPT) {
|
||||
ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(*pk), MBEDTLS_RSA_PKCS_V15, md_type);
|
||||
}
|
||||
if (ret != 0) {
|
||||
@ -1485,6 +1495,19 @@ exit:
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_t key_id,
|
||||
mbedtls_pk_context *pk)
|
||||
{
|
||||
return copy_from_psa(key_id, pk, 0);
|
||||
}
|
||||
|
||||
int mbedtls_pk_copy_public_from_psa(mbedtls_svc_key_id_t key_id,
|
||||
mbedtls_pk_context *pk)
|
||||
{
|
||||
return copy_from_psa(key_id, pk, 1);
|
||||
}
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
|
||||
#endif /* MBEDTLS_PK_C */
|
||||
|
@ -1567,8 +1567,24 @@ Copy from PSA: valid RSA (PSS + SHA_512)
|
||||
depends_on:MBEDTLS_RSA_C:MBEDTLS_PKCS1_V21:MBEDTLS_MD_CAN_SHA512
|
||||
pk_copy_from_psa_success:"308204a40201000282010100ef24d80f6b7a0f62ab2f750a8370c1c39781abe2f7ae5cbc698ebbc51067af68c8b02e5bfafe0b296a2fdca8ee5327bf3370bd26c529d173c4356d8ad51f606ac730e3be509d8535c9c51927222d6c4e770efec4d9b0bd11410e5e2e01e093700d358aab8292297483c65870ea6d4ca9299f4347790f6223480732726a97b34bb4d53cb3f188e3c97115b029fa9a2cce4c6d935977a90737ac8b2a2c5691ad928b22681ca05ee38ddba2278c854f51281c5e4856090aca59bf719a167e63bb932580ae2b599e1a297194696e637a919bc9d2caf214e59d46ed1a12e591b608f2031744111551430d9ac39082957ae1ce03a88068896701e6ce19a83890ff5761020301000102820100706fb53a02c13fcc9749d7d09a9e002c12e6bfc715c6a00961e3defab74cd896fe8c7f2f75e1cda3aa2e58a400718e65822d0671dd0f5d4ffdb7550a8a4b974c7cdccaa72745f864a2ba0daa6d9247b2d89d6f41644c89883c3b2222a5754e3cc7a91dcaa7b84acf6249763998aeccf558016e638352ad44835006f2ee94e691d0070ce561677f2a22a12f357bd762c57f80f1f4921f0f26b3ed758478d11086c182874355ef5039e8d854291b9ce7f8b284ec81f141b7255313507f5ea159d6b1c0ee176e7743d3c65d536e1e4aaf24089c1e00c8021012b8846a4971a0695030504ace362077e8b2fcb4fbdd70bfb734a3fe7d9e1a25bdd0cb0f2fcb56ecc502818100f8fdfbac1c033911b5a184980d081f700f4d450cebf18cbdc68f160a5abd580e6f8f5800fd0b60521dbe2d549e82617afe70d2ad004c2f45405d94e4418e8c2b8da6bcaa407bbfa5477b5a6fceccfcb99f51c6c16bd17202d997bdcaec83b870e3e101acc05e0754020ec207ef5ec9934ac81cd617af72cd94b2bb400eb2078302818100f5dfe74a548c04950178f50130d5aadbe5d1f4b52527c0bfad9aa0d73731fb24219cb5ea5c4b4fa56133d5ea9225fa7d0ccc9bdcc78b77303a2e73c17e9a46b9b09020604496a849f069d0d87713e06a5d374271b2629f5ba220506b606a101828d20da9fcfa3a7e75b135987260be6d37622fc3f4bf4fd2dfd9655da5ff0c4b02818100d4d797c959f0cf59fa1f65ceec64e32ad189c5daf3ddf9e747d28c8eb15e65e5812bd19896b6a0d1d126fe6cf54a92b5a6c71ef04feed001acb1d253044f2c3716d14f396201e6a30c65bfbb0fd65ebaf61bdb80ffff7c2c3f80dcf69813491907531231700770d0392a1066e411ecd201fce9d98149b32355572b85e889faad028181009d898bc165709d52f7b18f91e6bf508d3ab08ed12df04da0c2d40b7039ce4d72b61299c082c8424cdd7dfff71f13346ec12fac42069cc68e6108f86427012485bfaa6904258e3e5fb9a9a305bf2e3e21087eea94bcce51fabd63650397affd85ed49c1358480b3cfe90ad5234b4dcf555d220d26c9ff765ecfcc94152fd1be070281804bf77b4bae8386772de830cc75f2d1d4b8221b3f817208e08c002ac0549902677e4f0e7bce5ba1b3da74fbbe138758e6853b4a5b7bf0672bc1170c64fa502a5e24e3472db433b4e30761eab6ebb9e207235fd88b97b1b30e14f364b628219d6e17056543a4e29a4de1e41ad37927ce23d0442623744bc35a1874296960029044":PSA_KEY_TYPE_RSA_KEY_PAIR:PSA_ALG_RSA_PSS(PSA_ALG_SHA_512)
|
||||
|
||||
# Key's algorithm is wrong for an RSA key, but pk_copy_from_psa() ignores
|
||||
# this information when building the PK context.
|
||||
Copy from PSA: valid RSA, PSA_ALG_NONE
|
||||
depends_on:MBEDTLS_RSA_C:MBEDTLS_MD_ALG_FOR_TEST
|
||||
pk_copy_from_psa_success:"308204a40201000282010100ef24d80f6b7a0f62ab2f750a8370c1c39781abe2f7ae5cbc698ebbc51067af68c8b02e5bfafe0b296a2fdca8ee5327bf3370bd26c529d173c4356d8ad51f606ac730e3be509d8535c9c51927222d6c4e770efec4d9b0bd11410e5e2e01e093700d358aab8292297483c65870ea6d4ca9299f4347790f6223480732726a97b34bb4d53cb3f188e3c97115b029fa9a2cce4c6d935977a90737ac8b2a2c5691ad928b22681ca05ee38ddba2278c854f51281c5e4856090aca59bf719a167e63bb932580ae2b599e1a297194696e637a919bc9d2caf214e59d46ed1a12e591b608f2031744111551430d9ac39082957ae1ce03a88068896701e6ce19a83890ff5761020301000102820100706fb53a02c13fcc9749d7d09a9e002c12e6bfc715c6a00961e3defab74cd896fe8c7f2f75e1cda3aa2e58a400718e65822d0671dd0f5d4ffdb7550a8a4b974c7cdccaa72745f864a2ba0daa6d9247b2d89d6f41644c89883c3b2222a5754e3cc7a91dcaa7b84acf6249763998aeccf558016e638352ad44835006f2ee94e691d0070ce561677f2a22a12f357bd762c57f80f1f4921f0f26b3ed758478d11086c182874355ef5039e8d854291b9ce7f8b284ec81f141b7255313507f5ea159d6b1c0ee176e7743d3c65d536e1e4aaf24089c1e00c8021012b8846a4971a0695030504ace362077e8b2fcb4fbdd70bfb734a3fe7d9e1a25bdd0cb0f2fcb56ecc502818100f8fdfbac1c033911b5a184980d081f700f4d450cebf18cbdc68f160a5abd580e6f8f5800fd0b60521dbe2d549e82617afe70d2ad004c2f45405d94e4418e8c2b8da6bcaa407bbfa5477b5a6fceccfcb99f51c6c16bd17202d997bdcaec83b870e3e101acc05e0754020ec207ef5ec9934ac81cd617af72cd94b2bb400eb2078302818100f5dfe74a548c04950178f50130d5aadbe5d1f4b52527c0bfad9aa0d73731fb24219cb5ea5c4b4fa56133d5ea9225fa7d0ccc9bdcc78b77303a2e73c17e9a46b9b09020604496a849f069d0d87713e06a5d374271b2629f5ba220506b606a101828d20da9fcfa3a7e75b135987260be6d37622fc3f4bf4fd2dfd9655da5ff0c4b02818100d4d797c959f0cf59fa1f65ceec64e32ad189c5daf3ddf9e747d28c8eb15e65e5812bd19896b6a0d1d126fe6cf54a92b5a6c71ef04feed001acb1d253044f2c3716d14f396201e6a30c65bfbb0fd65ebaf61bdb80ffff7c2c3f80dcf69813491907531231700770d0392a1066e411ecd201fce9d98149b32355572b85e889faad028181009d898bc165709d52f7b18f91e6bf508d3ab08ed12df04da0c2d40b7039ce4d72b61299c082c8424cdd7dfff71f13346ec12fac42069cc68e6108f86427012485bfaa6904258e3e5fb9a9a305bf2e3e21087eea94bcce51fabd63650397affd85ed49c1358480b3cfe90ad5234b4dcf555d220d26c9ff765ecfcc94152fd1be070281804bf77b4bae8386772de830cc75f2d1d4b8221b3f817208e08c002ac0549902677e4f0e7bce5ba1b3da74fbbe138758e6853b4a5b7bf0672bc1170c64fa502a5e24e3472db433b4e30761eab6ebb9e207235fd88b97b1b30e14f364b628219d6e17056543a4e29a4de1e41ad37927ce23d0442623744bc35a1874296960029044":PSA_KEY_TYPE_RSA_KEY_PAIR:PSA_ALG_NONE
|
||||
|
||||
# Key's algorithm is wrong for an RSA key, but pk_copy_from_psa() accepts
|
||||
# it anyway.
|
||||
Copy from PSA: valid RSA, wrong alg (CMAC)
|
||||
depends_on:MBEDTLS_RSA_C:MBEDTLS_PKCS1_V15:MBEDTLS_MD_ALG_FOR_TEST
|
||||
depends_on:MBEDTLS_RSA_C:MBEDTLS_MD_ALG_FOR_TEST
|
||||
pk_copy_from_psa_success:"308204a40201000282010100ef24d80f6b7a0f62ab2f750a8370c1c39781abe2f7ae5cbc698ebbc51067af68c8b02e5bfafe0b296a2fdca8ee5327bf3370bd26c529d173c4356d8ad51f606ac730e3be509d8535c9c51927222d6c4e770efec4d9b0bd11410e5e2e01e093700d358aab8292297483c65870ea6d4ca9299f4347790f6223480732726a97b34bb4d53cb3f188e3c97115b029fa9a2cce4c6d935977a90737ac8b2a2c5691ad928b22681ca05ee38ddba2278c854f51281c5e4856090aca59bf719a167e63bb932580ae2b599e1a297194696e637a919bc9d2caf214e59d46ed1a12e591b608f2031744111551430d9ac39082957ae1ce03a88068896701e6ce19a83890ff5761020301000102820100706fb53a02c13fcc9749d7d09a9e002c12e6bfc715c6a00961e3defab74cd896fe8c7f2f75e1cda3aa2e58a400718e65822d0671dd0f5d4ffdb7550a8a4b974c7cdccaa72745f864a2ba0daa6d9247b2d89d6f41644c89883c3b2222a5754e3cc7a91dcaa7b84acf6249763998aeccf558016e638352ad44835006f2ee94e691d0070ce561677f2a22a12f357bd762c57f80f1f4921f0f26b3ed758478d11086c182874355ef5039e8d854291b9ce7f8b284ec81f141b7255313507f5ea159d6b1c0ee176e7743d3c65d536e1e4aaf24089c1e00c8021012b8846a4971a0695030504ace362077e8b2fcb4fbdd70bfb734a3fe7d9e1a25bdd0cb0f2fcb56ecc502818100f8fdfbac1c033911b5a184980d081f700f4d450cebf18cbdc68f160a5abd580e6f8f5800fd0b60521dbe2d549e82617afe70d2ad004c2f45405d94e4418e8c2b8da6bcaa407bbfa5477b5a6fceccfcb99f51c6c16bd17202d997bdcaec83b870e3e101acc05e0754020ec207ef5ec9934ac81cd617af72cd94b2bb400eb2078302818100f5dfe74a548c04950178f50130d5aadbe5d1f4b52527c0bfad9aa0d73731fb24219cb5ea5c4b4fa56133d5ea9225fa7d0ccc9bdcc78b77303a2e73c17e9a46b9b09020604496a849f069d0d87713e06a5d374271b2629f5ba220506b606a101828d20da9fcfa3a7e75b135987260be6d37622fc3f4bf4fd2dfd9655da5ff0c4b02818100d4d797c959f0cf59fa1f65ceec64e32ad189c5daf3ddf9e747d28c8eb15e65e5812bd19896b6a0d1d126fe6cf54a92b5a6c71ef04feed001acb1d253044f2c3716d14f396201e6a30c65bfbb0fd65ebaf61bdb80ffff7c2c3f80dcf69813491907531231700770d0392a1066e411ecd201fce9d98149b32355572b85e889faad028181009d898bc165709d52f7b18f91e6bf508d3ab08ed12df04da0c2d40b7039ce4d72b61299c082c8424cdd7dfff71f13346ec12fac42069cc68e6108f86427012485bfaa6904258e3e5fb9a9a305bf2e3e21087eea94bcce51fabd63650397affd85ed49c1358480b3cfe90ad5234b4dcf555d220d26c9ff765ecfcc94152fd1be070281804bf77b4bae8386772de830cc75f2d1d4b8221b3f817208e08c002ac0549902677e4f0e7bce5ba1b3da74fbbe138758e6853b4a5b7bf0672bc1170c64fa502a5e24e3472db433b4e30761eab6ebb9e207235fd88b97b1b30e14f364b628219d6e17056543a4e29a4de1e41ad37927ce23d0442623744bc35a1874296960029044":PSA_KEY_TYPE_RSA_KEY_PAIR:PSA_ALG_CMAC
|
||||
|
||||
Copy from PSA: non-exportable -> public, RSA
|
||||
depends_on:MBEDTLS_RSA_C
|
||||
pk_copy_public_from_psa:"308204a40201000282010100ef24d80f6b7a0f62ab2f750a8370c1c39781abe2f7ae5cbc698ebbc51067af68c8b02e5bfafe0b296a2fdca8ee5327bf3370bd26c529d173c4356d8ad51f606ac730e3be509d8535c9c51927222d6c4e770efec4d9b0bd11410e5e2e01e093700d358aab8292297483c65870ea6d4ca9299f4347790f6223480732726a97b34bb4d53cb3f188e3c97115b029fa9a2cce4c6d935977a90737ac8b2a2c5691ad928b22681ca05ee38ddba2278c854f51281c5e4856090aca59bf719a167e63bb932580ae2b599e1a297194696e637a919bc9d2caf214e59d46ed1a12e591b608f2031744111551430d9ac39082957ae1ce03a88068896701e6ce19a83890ff5761020301000102820100706fb53a02c13fcc9749d7d09a9e002c12e6bfc715c6a00961e3defab74cd896fe8c7f2f75e1cda3aa2e58a400718e65822d0671dd0f5d4ffdb7550a8a4b974c7cdccaa72745f864a2ba0daa6d9247b2d89d6f41644c89883c3b2222a5754e3cc7a91dcaa7b84acf6249763998aeccf558016e638352ad44835006f2ee94e691d0070ce561677f2a22a12f357bd762c57f80f1f4921f0f26b3ed758478d11086c182874355ef5039e8d854291b9ce7f8b284ec81f141b7255313507f5ea159d6b1c0ee176e7743d3c65d536e1e4aaf24089c1e00c8021012b8846a4971a0695030504ace362077e8b2fcb4fbdd70bfb734a3fe7d9e1a25bdd0cb0f2fcb56ecc502818100f8fdfbac1c033911b5a184980d081f700f4d450cebf18cbdc68f160a5abd580e6f8f5800fd0b60521dbe2d549e82617afe70d2ad004c2f45405d94e4418e8c2b8da6bcaa407bbfa5477b5a6fceccfcb99f51c6c16bd17202d997bdcaec83b870e3e101acc05e0754020ec207ef5ec9934ac81cd617af72cd94b2bb400eb2078302818100f5dfe74a548c04950178f50130d5aadbe5d1f4b52527c0bfad9aa0d73731fb24219cb5ea5c4b4fa56133d5ea9225fa7d0ccc9bdcc78b77303a2e73c17e9a46b9b09020604496a849f069d0d87713e06a5d374271b2629f5ba220506b606a101828d20da9fcfa3a7e75b135987260be6d37622fc3f4bf4fd2dfd9655da5ff0c4b02818100d4d797c959f0cf59fa1f65ceec64e32ad189c5daf3ddf9e747d28c8eb15e65e5812bd19896b6a0d1d126fe6cf54a92b5a6c71ef04feed001acb1d253044f2c3716d14f396201e6a30c65bfbb0fd65ebaf61bdb80ffff7c2c3f80dcf69813491907531231700770d0392a1066e411ecd201fce9d98149b32355572b85e889faad028181009d898bc165709d52f7b18f91e6bf508d3ab08ed12df04da0c2d40b7039ce4d72b61299c082c8424cdd7dfff71f13346ec12fac42069cc68e6108f86427012485bfaa6904258e3e5fb9a9a305bf2e3e21087eea94bcce51fabd63650397affd85ed49c1358480b3cfe90ad5234b4dcf555d220d26c9ff765ecfcc94152fd1be070281804bf77b4bae8386772de830cc75f2d1d4b8221b3f817208e08c002ac0549902677e4f0e7bce5ba1b3da74fbbe138758e6853b4a5b7bf0672bc1170c64fa502a5e24e3472db433b4e30761eab6ebb9e207235fd88b97b1b30e14f364b628219d6e17056543a4e29a4de1e41ad37927ce23d0442623744bc35a1874296960029044":PSA_KEY_TYPE_RSA_KEY_PAIR
|
||||
|
||||
Copy from PSA: non-exportable -> public, SECP_R1_256
|
||||
depends_on:MBEDTLS_PK_HAVE_ECC_KEYS:MBEDTLS_ECP_HAVE_SECP256R1
|
||||
pk_copy_public_from_psa:"587CF7C57EB7C6254CBF80CC59846521B4FBCBA8BC4B362A9B043F0DEB49CCA1":PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)
|
||||
|
||||
Copy from PSA: non-exportable -> public, Curve25519
|
||||
depends_on:MBEDTLS_PK_HAVE_ECC_KEYS:MBEDTLS_ECP_HAVE_CURVE25519
|
||||
pk_copy_public_from_psa:"a546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4":PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_MONTGOMERY)
|
||||
|
@ -322,6 +322,83 @@ static psa_key_usage_t pk_get_psa_attributes_implied_usage(
|
||||
expected_usage |= PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY;
|
||||
return expected_usage;
|
||||
}
|
||||
|
||||
#define RSA_WRITE_PUBKEY_MAX_SIZE \
|
||||
PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)
|
||||
#define ECP_WRITE_PUBKEY_MAX_SIZE \
|
||||
PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
|
||||
static int pk_public_same(const mbedtls_pk_context *pk1,
|
||||
const mbedtls_pk_context *pk2)
|
||||
{
|
||||
int ok = 0;
|
||||
|
||||
mbedtls_pk_type_t type = mbedtls_pk_get_type(pk1);
|
||||
TEST_EQUAL(type, mbedtls_pk_get_type(pk2));
|
||||
|
||||
switch (type) {
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
case MBEDTLS_PK_RSA:
|
||||
{
|
||||
const mbedtls_rsa_context *rsa1 = mbedtls_pk_rsa(*pk1);
|
||||
const mbedtls_rsa_context *rsa2 = mbedtls_pk_rsa(*pk2);
|
||||
TEST_EQUAL(mbedtls_rsa_get_padding_mode(rsa1),
|
||||
mbedtls_rsa_get_padding_mode(rsa2));
|
||||
TEST_EQUAL(mbedtls_rsa_get_md_alg(rsa1),
|
||||
mbedtls_rsa_get_md_alg(rsa2));
|
||||
unsigned char buf1[RSA_WRITE_PUBKEY_MAX_SIZE];
|
||||
unsigned char *p1 = buf1 + sizeof(buf1);
|
||||
int len1 = mbedtls_rsa_write_pubkey(rsa1, buf1, &p1);
|
||||
TEST_LE_U(0, len1);
|
||||
unsigned char buf2[RSA_WRITE_PUBKEY_MAX_SIZE];
|
||||
unsigned char *p2 = buf2 + sizeof(buf2);
|
||||
int len2 = mbedtls_rsa_write_pubkey(rsa2, buf2, &p2);
|
||||
TEST_LE_U(0, len2);
|
||||
TEST_MEMORY_COMPARE(p1, len1, p2, len2);
|
||||
break;
|
||||
}
|
||||
#endif /* MBEDTLS_RSA_C */
|
||||
|
||||
#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
|
||||
case MBEDTLS_PK_ECKEY:
|
||||
case MBEDTLS_PK_ECKEY_DH:
|
||||
case MBEDTLS_PK_ECDSA:
|
||||
{
|
||||
#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
|
||||
TEST_MEMORY_COMPARE(pk1->pub_raw, pk1->pub_raw_len,
|
||||
pk2->pub_raw, pk2->pub_raw_len);
|
||||
TEST_EQUAL(pk1->ec_family, pk2->ec_family);
|
||||
TEST_EQUAL(pk1->ec_bits, pk2->ec_bits);
|
||||
|
||||
#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
|
||||
const mbedtls_ecp_keypair *ec1 = mbedtls_pk_ec_ro(*pk1);
|
||||
const mbedtls_ecp_keypair *ec2 = mbedtls_pk_ec_ro(*pk2);
|
||||
TEST_EQUAL(mbedtls_ecp_keypair_get_group_id(ec1),
|
||||
mbedtls_ecp_keypair_get_group_id(ec2));
|
||||
unsigned char buf1[ECP_WRITE_PUBKEY_MAX_SIZE];
|
||||
size_t len1 = 99999991;
|
||||
TEST_EQUAL(mbedtls_ecp_write_public_key(
|
||||
ec1, MBEDTLS_ECP_PF_UNCOMPRESSED,
|
||||
&len1, buf1, sizeof(buf1)), 0);
|
||||
unsigned char buf2[ECP_WRITE_PUBKEY_MAX_SIZE];
|
||||
size_t len2 = 99999992;
|
||||
TEST_EQUAL(mbedtls_ecp_write_public_key(
|
||||
ec2, MBEDTLS_ECP_PF_UNCOMPRESSED,
|
||||
&len2, buf2, sizeof(buf2)), 0);
|
||||
TEST_MEMORY_COMPARE(buf1, len1, buf2, len2);
|
||||
#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
|
||||
}
|
||||
break;
|
||||
#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
|
||||
|
||||
default:
|
||||
TEST_FAIL("Unsupported pk type in pk_public_same");
|
||||
}
|
||||
|
||||
ok = 1;
|
||||
|
||||
exit:
|
||||
return ok;
|
||||
}
|
||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
||||
|
||||
#if defined(MBEDTLS_RSA_C)
|
||||
@ -2322,16 +2399,21 @@ void pk_copy_from_psa_fail(void)
|
||||
/* Null pk pointer. */
|
||||
TEST_EQUAL(mbedtls_pk_copy_from_psa(key_id, NULL),
|
||||
MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
||||
TEST_EQUAL(mbedtls_pk_copy_public_from_psa(key_id, NULL),
|
||||
MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
||||
|
||||
/* Invalid key ID. */
|
||||
TEST_EQUAL(mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_make(0, 0), &pk_ctx),
|
||||
MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
||||
TEST_EQUAL(mbedtls_pk_copy_public_from_psa(mbedtls_svc_key_id_make(0, 0), &pk_ctx),
|
||||
MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
||||
|
||||
#if defined(PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE)
|
||||
/* Generate a key type that is not handled by the PK module. */
|
||||
PSA_ASSERT(pk_psa_genkey_generic(PSA_KEY_TYPE_DH_KEY_PAIR(PSA_DH_FAMILY_RFC7919), 2048,
|
||||
PSA_KEY_USAGE_EXPORT, PSA_ALG_NONE, &key_id));
|
||||
TEST_EQUAL(mbedtls_pk_copy_from_psa(key_id, &pk_ctx), MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
||||
TEST_EQUAL(mbedtls_pk_copy_public_from_psa(key_id, &pk_ctx), MBEDTLS_ERR_PK_BAD_INPUT_DATA);
|
||||
psa_destroy_key(key_id);
|
||||
#endif /* PSA_WANT_KEY_TYPE_DH_KEY_PAIR_GENERATE */
|
||||
|
||||
@ -2382,7 +2464,7 @@ void pk_copy_from_psa_success(data_t *priv_key_data, int key_type_arg,
|
||||
psa_algorithm_t key_alg = key_alg_arg;
|
||||
psa_key_usage_t key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH |
|
||||
PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY;
|
||||
mbedtls_pk_context pk_priv, pk_pub;
|
||||
mbedtls_pk_context pk_priv, pk_priv_copy_public, pk_pub, pk_pub_copy_public;
|
||||
mbedtls_svc_key_id_t priv_key_id = MBEDTLS_SVC_KEY_ID_INIT;
|
||||
mbedtls_svc_key_id_t pub_key_id = MBEDTLS_SVC_KEY_ID_INIT;
|
||||
unsigned char *in_buf = NULL;
|
||||
@ -2392,7 +2474,9 @@ void pk_copy_from_psa_success(data_t *priv_key_data, int key_type_arg,
|
||||
size_t out_buf_len, out_buf2_len;
|
||||
|
||||
mbedtls_pk_init(&pk_priv);
|
||||
mbedtls_pk_init(&pk_priv_copy_public);
|
||||
mbedtls_pk_init(&pk_pub);
|
||||
mbedtls_pk_init(&pk_pub_copy_public);
|
||||
PSA_INIT();
|
||||
|
||||
if (key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
|
||||
@ -2404,9 +2488,11 @@ void pk_copy_from_psa_success(data_t *priv_key_data, int key_type_arg,
|
||||
key_type, key_usage, key_alg, &priv_key_id));
|
||||
pub_key_id = psa_pub_key_from_priv(priv_key_id);
|
||||
|
||||
/* Create 2 PK contexts starting from the PSA keys we just created. */
|
||||
/* Create 4 PK contexts starting from the PSA keys we just created. */
|
||||
TEST_EQUAL(mbedtls_pk_copy_from_psa(priv_key_id, &pk_priv), 0);
|
||||
TEST_EQUAL(mbedtls_pk_copy_public_from_psa(priv_key_id, &pk_priv_copy_public), 0);
|
||||
TEST_EQUAL(mbedtls_pk_copy_from_psa(pub_key_id, &pk_pub), 0);
|
||||
TEST_EQUAL(mbedtls_pk_copy_public_from_psa(pub_key_id, &pk_pub_copy_public), 0);
|
||||
|
||||
/* Destoy both PSA keys to prove that generated PK contexts are independent
|
||||
* from them. */
|
||||
@ -2461,14 +2547,36 @@ void pk_copy_from_psa_success(data_t *priv_key_data, int key_type_arg,
|
||||
* - Verify with the PK context generated using public key.
|
||||
* - Verify using the public PSA key directly.
|
||||
*/
|
||||
TEST_EQUAL(mbedtls_pk_sign(&pk_priv, md_for_test, in_buf, in_buf_len,
|
||||
out_buf, sizeof(out_buf), &out_buf_len,
|
||||
mbedtls_test_rnd_std_rand, NULL), 0);
|
||||
|
||||
TEST_EQUAL(mbedtls_pk_verify(&pk_priv, md_for_test, in_buf, in_buf_len,
|
||||
out_buf, out_buf_len), 0);
|
||||
TEST_EQUAL(mbedtls_pk_verify(&pk_pub, md_for_test, in_buf, in_buf_len,
|
||||
out_buf, out_buf_len), 0);
|
||||
/* Edge cases: in a build with RSA key support but not RSA padding modes,
|
||||
* or with ECDSA verify support but not signature, the signature might be
|
||||
* impossible. */
|
||||
int pk_can_sign = 0;
|
||||
#if defined(MBEDTLS_PKCS1_V15)
|
||||
if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(key_alg) || key_alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
|
||||
pk_can_sign = 1;
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_PKCS1_V21)
|
||||
if (PSA_ALG_IS_RSA_PSS(key_alg) || PSA_ALG_IS_RSA_OAEP(key_alg)) {
|
||||
pk_can_sign = 1;
|
||||
}
|
||||
#endif
|
||||
#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
|
||||
if (PSA_ALG_IS_ECDSA(key_alg) || PSA_ALG_IS_DETERMINISTIC_ECDSA(key_alg)) {
|
||||
pk_can_sign = 1;
|
||||
}
|
||||
#endif
|
||||
if (pk_can_sign) {
|
||||
TEST_EQUAL(mbedtls_pk_sign(&pk_priv, md_for_test, in_buf, in_buf_len,
|
||||
out_buf, sizeof(out_buf), &out_buf_len,
|
||||
mbedtls_test_rnd_std_rand, NULL), 0);
|
||||
|
||||
TEST_EQUAL(mbedtls_pk_verify(&pk_priv, md_for_test, in_buf, in_buf_len,
|
||||
out_buf, out_buf_len), 0);
|
||||
TEST_EQUAL(mbedtls_pk_verify(&pk_pub, md_for_test, in_buf, in_buf_len,
|
||||
out_buf, out_buf_len), 0);
|
||||
}
|
||||
|
||||
if (PSA_ALG_IS_HASH_AND_SIGN(key_alg)) {
|
||||
#if defined(MBEDTLS_PSA_UTIL_HAVE_ECDSA)
|
||||
@ -2534,12 +2642,76 @@ void pk_copy_from_psa_success(data_t *priv_key_data, int key_type_arg,
|
||||
}
|
||||
}
|
||||
|
||||
/* Test that the keys from mbedtls_pk_copy_public_from_psa() are identical
|
||||
* to the public key from mbedtls_pk_copy_from_psa(). */
|
||||
mbedtls_test_set_step(1);
|
||||
TEST_ASSERT(pk_public_same(&pk_pub, &pk_priv_copy_public));
|
||||
mbedtls_test_set_step(2);
|
||||
TEST_ASSERT(pk_public_same(&pk_pub, &pk_pub_copy_public));
|
||||
|
||||
exit:
|
||||
mbedtls_free(in_buf);
|
||||
mbedtls_pk_free(&pk_priv);
|
||||
mbedtls_pk_free(&pk_priv_copy_public);
|
||||
mbedtls_pk_free(&pk_pub);
|
||||
mbedtls_pk_free(&pk_pub_copy_public);
|
||||
psa_destroy_key(priv_key_id);
|
||||
psa_destroy_key(pub_key_id);
|
||||
PSA_DONE();
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C*/
|
||||
void pk_copy_public_from_psa(data_t *priv_key_data, int key_type_arg)
|
||||
{
|
||||
psa_key_type_t key_type = key_type_arg;
|
||||
mbedtls_pk_context pk_from_exportable;
|
||||
mbedtls_pk_init(&pk_from_exportable);
|
||||
mbedtls_pk_context pk_from_non_exportable;
|
||||
mbedtls_pk_init(&pk_from_non_exportable);
|
||||
mbedtls_pk_context pk_private;
|
||||
mbedtls_pk_init(&pk_private);
|
||||
mbedtls_svc_key_id_t non_exportable_key_id = MBEDTLS_SVC_KEY_ID_INIT;
|
||||
mbedtls_svc_key_id_t exportable_key_id = MBEDTLS_SVC_KEY_ID_INIT;
|
||||
|
||||
PSA_INIT();
|
||||
|
||||
PSA_ASSERT(pk_psa_import_key(priv_key_data->x, priv_key_data->len,
|
||||
key_type,
|
||||
PSA_KEY_USAGE_EXPORT,
|
||||
PSA_ALG_NONE,
|
||||
&exportable_key_id));
|
||||
PSA_ASSERT(pk_psa_import_key(priv_key_data->x, priv_key_data->len,
|
||||
key_type,
|
||||
0,
|
||||
PSA_ALG_NONE,
|
||||
&non_exportable_key_id));
|
||||
|
||||
TEST_EQUAL(mbedtls_pk_copy_public_from_psa(exportable_key_id,
|
||||
&pk_from_exportable), 0);
|
||||
TEST_EQUAL(mbedtls_pk_copy_public_from_psa(non_exportable_key_id,
|
||||
&pk_from_non_exportable), 0);
|
||||
|
||||
/* Check that the non-exportable key really is non-exportable */
|
||||
TEST_EQUAL(mbedtls_pk_copy_from_psa(non_exportable_key_id, &pk_private),
|
||||
MBEDTLS_ERR_PK_TYPE_MISMATCH);
|
||||
|
||||
psa_destroy_key(exportable_key_id);
|
||||
psa_destroy_key(non_exportable_key_id);
|
||||
|
||||
/* The goal of this test function is mostly to check that
|
||||
* mbedtls_pk_copy_public_from_psa works with a non-exportable key pair.
|
||||
* We check that the resulting key is the same as for an exportable
|
||||
* key pair. We rely on pk_copy_from_psa_success tests to validate that
|
||||
* the result is correct. */
|
||||
TEST_ASSERT(pk_public_same(&pk_from_non_exportable, &pk_from_exportable));
|
||||
|
||||
exit:
|
||||
mbedtls_pk_free(&pk_from_non_exportable);
|
||||
mbedtls_pk_free(&pk_from_exportable);
|
||||
mbedtls_pk_free(&pk_private);
|
||||
psa_destroy_key(exportable_key_id);
|
||||
psa_destroy_key(non_exportable_key_id);
|
||||
PSA_DONE();
|
||||
}
|
||||
/* END_CASE */
|
||||
|
Loading…
x
Reference in New Issue
Block a user