@@ -44,6 +44,37 @@ void test_crypto_random(void)
44
44
mbedtls_psa_crypto_free ();
45
45
}
46
46
47
+ void test_crypto_asymmetric_encrypt_decrypt (void )
48
+ {
49
+ psa_key_slot_t slot = 1 ;
50
+ psa_key_type_t key_type = PSA_KEY_TYPE_RSA_KEYPAIR;
51
+ psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_CRYPT;
52
+ size_t key_bits = 512 , got_bits = 0 , output_length;
53
+ psa_key_policy_t policy;
54
+ static const unsigned char input[] = " encrypt me!" ;
55
+ unsigned char encrypted[64 ];
56
+ unsigned char decrypted[sizeof (input)];
57
+
58
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_crypto_init ());
59
+
60
+ psa_key_policy_init (&policy);
61
+ psa_key_policy_set_usage (&policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg);
62
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_set_key_policy (slot, &policy));
63
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_generate_key (slot, key_type, key_bits, NULL , 0 ));
64
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_get_key_information (slot, NULL , &got_bits));
65
+ TEST_ASSERT_EQUAL (key_bits, got_bits);
66
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_asymmetric_encrypt (slot, alg, input, sizeof (input), NULL , 0 ,
67
+ encrypted, sizeof (encrypted), &output_length));
68
+ TEST_ASSERT_EQUAL (sizeof (encrypted), output_length);
69
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_asymmetric_decrypt (slot, alg, encrypted, sizeof (encrypted), NULL , 0 ,
70
+ decrypted, sizeof (decrypted), &output_length));
71
+ TEST_ASSERT_EQUAL (PSA_SUCCESS, psa_destroy_key (slot));
72
+ TEST_ASSERT_EQUAL (sizeof (input), output_length);
73
+ TEST_ASSERT_EQUAL_UINT8_ARRAY (input, decrypted, output_length);
74
+
75
+ mbedtls_psa_crypto_free ();
76
+ }
77
+
47
78
utest::v1::status_t case_failure_handler (const Case *const source, const failure_t reason)
48
79
{
49
80
mbedtls_psa_crypto_free ();
@@ -59,6 +90,7 @@ utest::v1::status_t test_setup(const size_t number_of_cases)
59
90
60
91
Case cases[] = {
61
92
Case (" mbed-crypto random" , test_crypto_random, case_failure_handler),
93
+ Case (" mbed-crypto asymmetric encrypt/decrypt" , test_crypto_asymmetric_encrypt_decrypt, case_failure_handler),
62
94
};
63
95
64
96
Specification specification (test_setup, cases);
0 commit comments