|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, Arm Limited and affiliates |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#if ((!defined(TARGET_PSA)) || (!defined(MBEDTLS_PSA_CRYPTO_C))) |
| 19 | +#error [NOT_SUPPORTED] Mbed Crypto is OFF - skipping. |
| 20 | +#endif |
| 21 | + |
| 22 | +#include <stdio.h> |
| 23 | +#include "mbed.h" |
| 24 | +#include "greentea-client/test_env.h" |
| 25 | +#include "unity.h" |
| 26 | +#include "utest.h" |
| 27 | +#include "psa/crypto.h" |
| 28 | +#include "entropy.h" |
| 29 | +#include "entropy_poll.h" |
| 30 | + |
| 31 | +using namespace utest::v1; |
| 32 | + |
| 33 | +#ifdef MBEDTLS_ENTROPY_NV_SEED |
| 34 | + |
| 35 | +#if !defined(MAX) |
| 36 | +#define MAX(a,b) (((a)>(b))?(a):(b)) |
| 37 | +#endif |
| 38 | + |
| 39 | +#define MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE \ |
| 40 | + MAX(MBEDTLS_ENTROPY_MIN_PLATFORM, MBEDTLS_ENTROPY_BLOCK_SIZE) |
| 41 | + |
| 42 | +void inject_entropy() |
| 43 | +{ |
| 44 | + uint8_t seed[MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE] = { 0 }; |
| 45 | + for (int i = 0; i < MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE; ++i) { |
| 46 | + seed[i] = i; |
| 47 | + } |
| 48 | + mbedtls_psa_inject_entropy(seed, MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE); |
| 49 | +} |
| 50 | +#endif |
| 51 | + |
| 52 | +void test_crypto_random(void) |
| 53 | +{ |
| 54 | + static const unsigned char trail[] = "don't overwrite me"; |
| 55 | + unsigned char changed[256] = { 0 }; |
| 56 | + unsigned char output[sizeof(changed) + sizeof(trail)]; |
| 57 | + size_t i, bytes = sizeof(changed); |
| 58 | + unsigned int run; |
| 59 | + |
| 60 | + memcpy(output + bytes, trail, sizeof(trail)); |
| 61 | + /* Run several times, to ensure that every output byte will be |
| 62 | + * nonzero at least once with overwhelming probability |
| 63 | + * (2^(-8*number_of_runs)). */ |
| 64 | + for (run = 0; run < 10; run++) { |
| 65 | + memset(output, 0, bytes); |
| 66 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_generate_random(output, bytes)); |
| 67 | + /* Check that no more than 'bytes' have been overwritten */ |
| 68 | + TEST_ASSERT_EQUAL_UINT8_ARRAY(trail, output + bytes, sizeof(trail)); |
| 69 | + |
| 70 | + for (i = 0; i < bytes; i++) { |
| 71 | + if (0 != output[i]) { |
| 72 | + ++changed[i]; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /* Check that every byte was changed to nonzero at least once. This |
| 78 | + * validates that psa_generate_random is overwriting every byte of |
| 79 | + * the output buffer. */ |
| 80 | + for (i = 0; i < bytes; i++) { |
| 81 | + TEST_ASSERT_NOT_EQUAL(0, changed[i]); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +void test_crypto_asymmetric_encrypt_decrypt(void) |
| 86 | +{ |
| 87 | + psa_status_t status = PSA_SUCCESS; |
| 88 | + psa_key_slot_t slot = 1; |
| 89 | + psa_key_type_t key_type = PSA_KEY_TYPE_RSA_KEYPAIR; |
| 90 | + psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_CRYPT; |
| 91 | + size_t key_bits = 512, got_bits = 0, output_length; |
| 92 | + psa_key_policy_t policy; |
| 93 | + static const unsigned char input[] = "encrypt me!"; |
| 94 | + unsigned char encrypted[64]; |
| 95 | + unsigned char decrypted[sizeof(input)]; |
| 96 | + |
| 97 | + psa_key_policy_init(&policy); |
| 98 | + psa_key_policy_set_usage(&policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg); |
| 99 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_set_key_policy(slot, &policy)); |
| 100 | + |
| 101 | + status = psa_generate_key(slot, key_type, key_bits, NULL, 0); |
| 102 | + TEST_SKIP_UNLESS_MESSAGE(status != PSA_ERROR_NOT_SUPPORTED, "RSA key generation is not supported"); |
| 103 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, status); |
| 104 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_get_key_information(slot, NULL, &got_bits)); |
| 105 | + TEST_ASSERT_EQUAL(key_bits, got_bits); |
| 106 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_asymmetric_encrypt(slot, alg, input, sizeof(input), NULL, 0, |
| 107 | + encrypted, sizeof(encrypted), &output_length)); |
| 108 | + TEST_ASSERT_EQUAL(sizeof(encrypted), output_length); |
| 109 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_asymmetric_decrypt(slot, alg, encrypted, sizeof(encrypted), NULL, 0, |
| 110 | + decrypted, sizeof(decrypted), &output_length)); |
| 111 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_destroy_key(slot)); |
| 112 | + TEST_ASSERT_EQUAL(sizeof(input), output_length); |
| 113 | + TEST_ASSERT_EQUAL_UINT8_ARRAY(input, decrypted, output_length); |
| 114 | +} |
| 115 | + |
| 116 | +void test_crypto_hash_verify(void) |
| 117 | +{ |
| 118 | + psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 119 | + psa_hash_operation_t operation; |
| 120 | + /* SHA-256 hash of an empty string */ |
| 121 | + static const unsigned char hash[] = { |
| 122 | + 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 123 | + 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 124 | + 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 125 | + }; |
| 126 | + |
| 127 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_hash_setup(&operation, alg)); |
| 128 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_hash_verify(&operation, hash, sizeof(hash))); |
| 129 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_hash_abort(&operation)); |
| 130 | +} |
| 131 | + |
| 132 | +void test_crypto_symmetric_cipher_encrypt_decrypt(void) |
| 133 | +{ |
| 134 | + psa_key_slot_t slot = 1; |
| 135 | + psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 136 | + psa_algorithm_t alg = PSA_ALG_CBC_NO_PADDING; |
| 137 | + psa_cipher_operation_t operation; |
| 138 | + psa_key_policy_t policy; |
| 139 | + size_t output_len; |
| 140 | + static const unsigned char key[] = { |
| 141 | + 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, |
| 142 | + 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c |
| 143 | + }; |
| 144 | + static const unsigned char input[] = { |
| 145 | + 0xb0, 0x28, 0x9f, 0x04, 0xdc, 0x7f, 0xe2, 0x25, |
| 146 | + 0xa2, 0xce, 0xe9, 0xd3, 0xb9, 0xbc, 0xc7, 0x2f |
| 147 | + }; |
| 148 | + static const unsigned char expected_encryption[] = { |
| 149 | + 0x28, 0x8d, 0x76, 0xc0, 0xa7, 0x09, 0x50, 0x3f, |
| 150 | + 0x87, 0x96, 0x1e, 0x96, 0x05, 0xcb, 0xb9, 0x6d |
| 151 | + }; |
| 152 | + unsigned char encrypted[sizeof(input)], decrypted[sizeof(input)], iv[16]; |
| 153 | + |
| 154 | + memset(iv, 0x2a, sizeof(iv)); |
| 155 | + psa_key_policy_init(&policy); |
| 156 | + psa_key_policy_set_usage(&policy, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT, alg); |
| 157 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_set_key_policy(slot, &policy)); |
| 158 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_import_key(slot, key_type, key, sizeof(key))); |
| 159 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_cipher_encrypt_setup(&operation, slot, alg)); |
| 160 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_cipher_set_iv(&operation, iv, sizeof(iv))); |
| 161 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_cipher_update(&operation, input, sizeof(input), |
| 162 | + encrypted, sizeof(encrypted), &output_len)); |
| 163 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_cipher_finish(&operation, encrypted + output_len, |
| 164 | + sizeof(encrypted) - output_len, &output_len)); |
| 165 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_cipher_abort(&operation)); |
| 166 | + TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_encryption, encrypted, sizeof(expected_encryption)); |
| 167 | + |
| 168 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_cipher_decrypt_setup(&operation, slot, alg)); |
| 169 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_cipher_set_iv(&operation, iv, sizeof(iv))); |
| 170 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_cipher_update(&operation, encrypted, sizeof(encrypted), |
| 171 | + decrypted, sizeof(decrypted), &output_len)); |
| 172 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_cipher_finish(&operation, decrypted + output_len, |
| 173 | + sizeof(decrypted) - output_len, &output_len)); |
| 174 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_cipher_abort(&operation)); |
| 175 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_destroy_key(slot)); |
| 176 | + TEST_ASSERT_EQUAL_HEX8_ARRAY(input, decrypted, sizeof(input)); |
| 177 | +} |
| 178 | + |
| 179 | +void test_crypto_asymmetric_sign_verify(void) |
| 180 | +{ |
| 181 | + psa_key_slot_t slot = 1; |
| 182 | + psa_key_type_t key_type = PSA_KEY_TYPE_RSA_KEYPAIR; |
| 183 | + psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 184 | + psa_key_policy_t policy; |
| 185 | + static const unsigned char key[] = { |
| 186 | + 0x30, 0x82, 0x02, 0x5e, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xaf, |
| 187 | + 0x05, 0x7d, 0x39, 0x6e, 0xe8, 0x4f, 0xb7, 0x5f, 0xdb, 0xb5, 0xc2, 0xb1, |
| 188 | + 0x3c, 0x7f, 0xe5, 0xa6, 0x54, 0xaa, 0x8a, 0xa2, 0x47, 0x0b, 0x54, 0x1e, |
| 189 | + 0xe1, 0xfe, 0xb0, 0xb1, 0x2d, 0x25, 0xc7, 0x97, 0x11, 0x53, 0x12, 0x49, |
| 190 | + 0xe1, 0x12, 0x96, 0x28, 0x04, 0x2d, 0xbb, 0xb6, 0xc1, 0x20, 0xd1, 0x44, |
| 191 | + 0x35, 0x24, 0xef, 0x4c, 0x0e, 0x6e, 0x1d, 0x89, 0x56, 0xee, 0xb2, 0x07, |
| 192 | + 0x7a, 0xf1, 0x23, 0x49, 0xdd, 0xee, 0xe5, 0x44, 0x83, 0xbc, 0x06, 0xc2, |
| 193 | + 0xc6, 0x19, 0x48, 0xcd, 0x02, 0xb2, 0x02, 0xe7, 0x96, 0xae, 0xbd, 0x94, |
| 194 | + 0xd3, 0xa7, 0xcb, 0xf8, 0x59, 0xc2, 0xc1, 0x81, 0x9c, 0x32, 0x4c, 0xb8, |
| 195 | + 0x2b, 0x9c, 0xd3, 0x4e, 0xde, 0x26, 0x3a, 0x2a, 0xbf, 0xfe, 0x47, 0x33, |
| 196 | + 0xf0, 0x77, 0x86, 0x9e, 0x86, 0x60, 0xf7, 0xd6, 0x83, 0x4d, 0xa5, 0x3d, |
| 197 | + 0x69, 0x0e, 0xf7, 0x98, 0x5f, 0x6b, 0xc3, 0x02, 0x03, 0x01, 0x00, 0x01, |
| 198 | + 0x02, 0x81, 0x81, 0x00, 0x87, 0x4b, 0xf0, 0xff, 0xc2, 0xf2, 0xa7, 0x1d, |
| 199 | + 0x14, 0x67, 0x1d, 0xdd, 0x01, 0x71, 0xc9, 0x54, 0xd7, 0xfd, 0xbf, 0x50, |
| 200 | + 0x28, 0x1e, 0x4f, 0x6d, 0x99, 0xea, 0x0e, 0x1e, 0xbc, 0xf8, 0x2f, 0xaa, |
| 201 | + 0x58, 0xe7, 0xb5, 0x95, 0xff, 0xb2, 0x93, 0xd1, 0xab, 0xe1, 0x7f, 0x11, |
| 202 | + 0x0b, 0x37, 0xc4, 0x8c, 0xc0, 0xf3, 0x6c, 0x37, 0xe8, 0x4d, 0x87, 0x66, |
| 203 | + 0x21, 0xd3, 0x27, 0xf6, 0x4b, 0xbe, 0x08, 0x45, 0x7d, 0x3e, 0xc4, 0x09, |
| 204 | + 0x8b, 0xa2, 0xfa, 0x0a, 0x31, 0x9f, 0xba, 0x41, 0x1c, 0x28, 0x41, 0xed, |
| 205 | + 0x7b, 0xe8, 0x31, 0x96, 0xa8, 0xcd, 0xf9, 0xda, 0xa5, 0xd0, 0x06, 0x94, |
| 206 | + 0xbc, 0x33, 0x5f, 0xc4, 0xc3, 0x22, 0x17, 0xfe, 0x04, 0x88, 0xbc, 0xe9, |
| 207 | + 0xcb, 0x72, 0x02, 0xe5, 0x94, 0x68, 0xb1, 0xea, 0xd1, 0x19, 0x00, 0x04, |
| 208 | + 0x77, 0xdb, 0x2c, 0xa7, 0x97, 0xfa, 0xc1, 0x9e, 0xda, 0x3f, 0x58, 0xc1, |
| 209 | + 0x02, 0x41, 0x00, 0xe2, 0xab, 0x76, 0x08, 0x41, 0xbb, 0x9d, 0x30, 0xa8, |
| 210 | + 0x1d, 0x22, 0x2d, 0xe1, 0xeb, 0x73, 0x81, 0xd8, 0x22, 0x14, 0x40, 0x7f, |
| 211 | + 0x1b, 0x97, 0x5c, 0xbb, 0xfe, 0x4e, 0x1a, 0x94, 0x67, 0xfd, 0x98, 0xad, |
| 212 | + 0xbd, 0x78, 0xf6, 0x07, 0x83, 0x6c, 0xa5, 0xbe, 0x19, 0x28, 0xb9, 0xd1, |
| 213 | + 0x60, 0xd9, 0x7f, 0xd4, 0x5c, 0x12, 0xd6, 0xb5, 0x2e, 0x2c, 0x98, 0x71, |
| 214 | + 0xa1, 0x74, 0xc6, 0x6b, 0x48, 0x81, 0x13, 0x02, 0x41, 0x00, 0xc5, 0xab, |
| 215 | + 0x27, 0x60, 0x21, 0x59, 0xae, 0x7d, 0x6f, 0x20, 0xc3, 0xc2, 0xee, 0x85, |
| 216 | + 0x1e, 0x46, 0xdc, 0x11, 0x2e, 0x68, 0x9e, 0x28, 0xd5, 0xfc, 0xbb, 0xf9, |
| 217 | + 0x90, 0xa9, 0x9e, 0xf8, 0xa9, 0x0b, 0x8b, 0xb4, 0x4f, 0xd3, 0x64, 0x67, |
| 218 | + 0xe7, 0xfc, 0x17, 0x89, 0xce, 0xb6, 0x63, 0xab, 0xda, 0x33, 0x86, 0x52, |
| 219 | + 0xc3, 0xc7, 0x3f, 0x11, 0x17, 0x74, 0x90, 0x2e, 0x84, 0x05, 0x65, 0x92, |
| 220 | + 0x70, 0x91, 0x02, 0x41, 0x00, 0xb6, 0xcd, 0xbd, 0x35, 0x4f, 0x7d, 0xf5, |
| 221 | + 0x79, 0xa6, 0x3b, 0x48, 0xb3, 0x64, 0x3e, 0x35, 0x3b, 0x84, 0x89, 0x87, |
| 222 | + 0x77, 0xb4, 0x8b, 0x15, 0xf9, 0x4e, 0x0b, 0xfc, 0x05, 0x67, 0xa6, 0xae, |
| 223 | + 0x59, 0x11, 0xd5, 0x7a, 0xd6, 0x40, 0x9c, 0xf7, 0x64, 0x7b, 0xf9, 0x62, |
| 224 | + 0x64, 0xe9, 0xbd, 0x87, 0xeb, 0x95, 0xe2, 0x63, 0xb7, 0x11, 0x0b, 0x9a, |
| 225 | + 0x1f, 0x9f, 0x94, 0xac, 0xce, 0xd0, 0xfa, 0xfa, 0x4d, 0x02, 0x40, 0x71, |
| 226 | + 0x19, 0x5e, 0xec, 0x37, 0xe8, 0xd2, 0x57, 0xde, 0xcf, 0xc6, 0x72, 0xb0, |
| 227 | + 0x7a, 0xe6, 0x39, 0xf1, 0x0c, 0xbb, 0x9b, 0x0c, 0x73, 0x9d, 0x0c, 0x80, |
| 228 | + 0x99, 0x68, 0xd6, 0x44, 0xa9, 0x4e, 0x3f, 0xd6, 0xed, 0x92, 0x87, 0x07, |
| 229 | + 0x7a, 0x14, 0x58, 0x3f, 0x37, 0x90, 0x58, 0xf7, 0x6a, 0x8a, 0xec, 0xd4, |
| 230 | + 0x3c, 0x62, 0xdc, 0x8c, 0x0f, 0x41, 0x76, 0x66, 0x50, 0xd7, 0x25, 0x27, |
| 231 | + 0x5a, 0xc4, 0xa1, 0x02, 0x41, 0x00, 0xbb, 0x32, 0xd1, 0x33, 0xed, 0xc2, |
| 232 | + 0xe0, 0x48, 0xd4, 0x63, 0x38, 0x8b, 0x7b, 0xe9, 0xcb, 0x4b, 0xe2, 0x9f, |
| 233 | + 0x4b, 0x62, 0x50, 0xbe, 0x60, 0x3e, 0x70, 0xe3, 0x64, 0x75, 0x01, 0xc9, |
| 234 | + 0x7d, 0xdd, 0xe2, 0x0a, 0x4e, 0x71, 0xbe, 0x95, 0xfd, 0x5e, 0x71, 0x78, |
| 235 | + 0x4e, 0x25, 0xac, 0xa4, 0xba, 0xf2, 0x5b, 0xe5, 0x73, 0x8a, 0xae, 0x59, |
| 236 | + 0xbb, 0xfe, 0x1c, 0x99, 0x77, 0x81, 0x44, 0x7a, 0x2b, 0x24 |
| 237 | + }; |
| 238 | + static const unsigned char input[] = { 0x61, 0x62, 0x63 }; |
| 239 | + static const unsigned char expected_signature[] = { |
| 240 | + 0x2c, 0x77, 0x44, 0x98, 0x3f, 0x02, 0x3a, 0xc7, 0xbb, 0x1c, 0x55, 0x52, |
| 241 | + 0x9d, 0x83, 0xed, 0x11, 0xa7, 0x6a, 0x78, 0x98, 0xa1, 0xbb, 0x5c, 0xe1, |
| 242 | + 0x91, 0x37, 0x5a, 0x4a, 0xa7, 0x49, 0x5a, 0x63, 0x3d, 0x27, 0x87, 0x9f, |
| 243 | + 0xf5, 0x8e, 0xba, 0x5a, 0x57, 0x37, 0x1c, 0x34, 0xfe, 0xb1, 0x18, 0x0e, |
| 244 | + 0x8b, 0x85, 0x0d, 0x55, 0x24, 0x76, 0xeb, 0xb5, 0x63, 0x4d, 0xf6, 0x20, |
| 245 | + 0x26, 0x19, 0x92, 0xf1, 0x2e, 0xbe, 0xe9, 0x09, 0x70, 0x41, 0xdb, 0xbe, |
| 246 | + 0xa8, 0x5a, 0x42, 0xd4, 0x5b, 0x34, 0x4b, 0xe5, 0x07, 0x3c, 0xeb, 0x77, |
| 247 | + 0x2f, 0xfc, 0x60, 0x49, 0x54, 0xb9, 0x15, 0x8b, 0xa8, 0x1e, 0xc3, 0xdc, |
| 248 | + 0x4d, 0x9d, 0x65, 0xe3, 0xab, 0x7a, 0xa3, 0x18, 0x16, 0x5f, 0x38, 0xc3, |
| 249 | + 0x6f, 0x84, 0x1f, 0x1c, 0x69, 0xcb, 0x1c, 0xfa, 0x49, 0x4a, 0xa5, 0xcb, |
| 250 | + 0xb4, 0xd6, 0xc0, 0xef, 0xba, 0xfb, 0x04, 0x3a |
| 251 | + }; |
| 252 | + unsigned char signature[sizeof(expected_signature)]; |
| 253 | + size_t signature_len; |
| 254 | + |
| 255 | + psa_key_policy_init(&policy); |
| 256 | + psa_key_policy_set_usage(&policy, PSA_KEY_USAGE_SIGN | PSA_KEY_USAGE_VERIFY, alg); |
| 257 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_set_key_policy(slot, &policy)); |
| 258 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_import_key(slot, key_type, key, sizeof(key))); |
| 259 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_asymmetric_sign(slot, alg, input, sizeof(input), |
| 260 | + signature, sizeof(signature), &signature_len)); |
| 261 | + TEST_ASSERT_EQUAL(sizeof(signature), signature_len); |
| 262 | + TEST_ASSERT_EQUAL_HEX8_ARRAY(expected_signature, signature, signature_len); |
| 263 | + |
| 264 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_asymmetric_verify(slot, alg, input, sizeof(input), |
| 265 | + signature, signature_len)); |
| 266 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_destroy_key(slot)); |
| 267 | +} |
| 268 | + |
| 269 | +void test_crypto_key_derivation(void) |
| 270 | +{ |
| 271 | + psa_key_slot_t slot = 1, derived_slot = 2; |
| 272 | + psa_algorithm_t alg = PSA_ALG_HKDF(PSA_ALG_SHA_256), derived_alg = PSA_ALG_CTR; |
| 273 | + psa_key_type_t derived_key_type = PSA_KEY_TYPE_AES, got_type; |
| 274 | + psa_key_policy_t policy; |
| 275 | + psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT; |
| 276 | + size_t key_bits = 512, derived_key_bits = 256, got_bits; |
| 277 | + |
| 278 | + psa_key_policy_init(&policy); |
| 279 | + psa_key_policy_set_usage(&policy, PSA_KEY_USAGE_DERIVE, alg); |
| 280 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_set_key_policy(slot, &policy)); |
| 281 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_generate_key(slot, PSA_KEY_TYPE_DERIVE, key_bits, NULL, 0)); |
| 282 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_key_derivation(&generator, slot, alg, NULL, 0, NULL, 0, |
| 283 | + PSA_BITS_TO_BYTES(derived_key_bits))); |
| 284 | + psa_key_policy_set_usage(&policy, PSA_KEY_USAGE_ENCRYPT, derived_alg); |
| 285 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_set_key_policy(derived_slot, &policy)); |
| 286 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_generator_import_key(derived_slot, derived_key_type, |
| 287 | + derived_key_bits, &generator)); |
| 288 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_get_key_information(derived_slot, &got_type, &got_bits)); |
| 289 | + TEST_ASSERT_EQUAL(derived_key_type, got_type); |
| 290 | + TEST_ASSERT_EQUAL(derived_key_bits, got_bits); |
| 291 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_generator_abort(&generator)); |
| 292 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_destroy_key(slot)); |
| 293 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_destroy_key(derived_slot)); |
| 294 | +} |
| 295 | + |
| 296 | + |
| 297 | +utest::v1::status_t case_setup_handler(const Case *const source, const size_t index_of_case) |
| 298 | +{ |
| 299 | + psa_status_t status = psa_crypto_init(); |
| 300 | +#if defined(MBEDTLS_ENTROPY_NV_SEED) |
| 301 | + if (status == PSA_ERROR_INSUFFICIENT_ENTROPY) { |
| 302 | + inject_entropy(); |
| 303 | + status = psa_crypto_init(); |
| 304 | + } |
| 305 | +#endif /* MBEDTLS_ENTROPY_NV_SEED */ |
| 306 | + TEST_ASSERT_EQUAL(PSA_SUCCESS, status); |
| 307 | + return greentea_case_setup_handler(source, index_of_case); |
| 308 | +} |
| 309 | + |
| 310 | +utest::v1::status_t case_teardown_handler(const Case *const source, const size_t passed, const size_t failed, const failure_t failure) |
| 311 | +{ |
| 312 | + mbedtls_psa_crypto_free(); |
| 313 | + return greentea_case_teardown_handler(source, passed, failed, failure); |
| 314 | +} |
| 315 | + |
| 316 | +utest::v1::status_t test_setup(const size_t number_of_cases) |
| 317 | +{ |
| 318 | + GREENTEA_SETUP(120, "default_auto"); |
| 319 | + return verbose_test_setup_handler(number_of_cases); |
| 320 | +} |
| 321 | + |
| 322 | +Case cases[] = { |
| 323 | + Case("mbed-crypto random", case_setup_handler, test_crypto_random, case_teardown_handler), |
| 324 | + Case("mbed-crypto asymmetric encrypt/decrypt", case_setup_handler, test_crypto_asymmetric_encrypt_decrypt, case_teardown_handler), |
| 325 | + Case("mbed-crypto hash verify", case_setup_handler, test_crypto_hash_verify, case_teardown_handler), |
| 326 | + Case("mbed-crypto symmetric cipher encrypt/decrypt", case_setup_handler, test_crypto_symmetric_cipher_encrypt_decrypt, case_teardown_handler), |
| 327 | + Case("mbed-crypto asymmetric sign/verify", case_setup_handler, test_crypto_asymmetric_sign_verify, case_teardown_handler), |
| 328 | + Case("mbed-crypto key derivation", case_setup_handler, test_crypto_key_derivation, case_teardown_handler), |
| 329 | +}; |
| 330 | + |
| 331 | +Specification specification(test_setup, cases); |
| 332 | + |
| 333 | +int main(void) |
| 334 | +{ |
| 335 | + return !Harness::run(specification); |
| 336 | +} |
0 commit comments