|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/ztest.h> |
| 8 | +#include <pm_config.h> |
| 9 | +#include <zephyr/kernel.h> |
| 10 | +#include <zephyr/sys/printk.h> |
| 11 | +#include <zephyr/logging/log.h> |
| 12 | +#include <stdio.h> |
| 13 | +#include <stdlib.h> |
| 14 | +#include <psa/crypto.h> |
| 15 | +#include <psa/crypto_values.h> |
| 16 | +#include <psa/crypto_extra.h> |
| 17 | +#include <zephyr/sys/printk.h> |
| 18 | +#include "psa_tests_common.h" |
| 19 | + |
| 20 | +#ifdef CONFIG_BUILD_WITH_TFM |
| 21 | +#include <tfm_ns_interface.h> |
| 22 | +#include <tfm_builtin_key_ids.h> |
| 23 | +#include "cracen_psa_kmu.h" |
| 24 | +static psa_key_id_t identity_key_id = TFM_BUILTIN_KEY_ID_IAK; |
| 25 | +#else |
| 26 | +#include <cracen_psa.h> |
| 27 | +static psa_key_id_t identity_key_id = CRACEN_BUILTIN_IDENTITY_KEY_ID; |
| 28 | +#endif |
| 29 | + |
| 30 | +/* ====================================================================== */ |
| 31 | +/* Global variables/defines for the IKG signing tests */ |
| 32 | + |
| 33 | +#define NRF_CRYPTO_TEST_IKG_TEXT_SIZE (68) |
| 34 | +#define NRF_CRYPTO_TEST_IKG_SIGNATURE_SIZE (64) |
| 35 | +#define NRF_CRYPTO_EXAMPLE_ECDSA_PUBLIC_KEY_SIZE (65) |
| 36 | + |
| 37 | + |
| 38 | +/* Below text is used as plaintext for signing/verification */ |
| 39 | +static uint8_t m_plain_text[NRF_CRYPTO_TEST_IKG_TEXT_SIZE] = { |
| 40 | + "Example string to demonstrate basic usage of the IKG identity key." |
| 41 | +}; |
| 42 | + |
| 43 | +static uint8_t m_pub_key[NRF_CRYPTO_EXAMPLE_ECDSA_PUBLIC_KEY_SIZE]; |
| 44 | +static uint8_t m_signature[NRF_CRYPTO_TEST_IKG_SIGNATURE_SIZE]; |
| 45 | +static psa_key_handle_t key_handle; |
| 46 | +static psa_key_id_t key_id; |
| 47 | +/* ====================================================================== */ |
| 48 | + |
| 49 | +LOG_MODULE_DECLARE(app, LOG_LEVEL_DBG); |
| 50 | + |
| 51 | +int get_identity_key(void) |
| 52 | +{ |
| 53 | + psa_status_t status; |
| 54 | + psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 55 | + size_t data_length; |
| 56 | + |
| 57 | + key_handle = mbedtls_svc_key_id_make(0, identity_key_id); |
| 58 | + psa_key_attributes_t attr = key_attributes; |
| 59 | + |
| 60 | + status = psa_get_key_attributes(key_handle, &attr); |
| 61 | + if (status != APP_SUCCESS) { |
| 62 | + return status; |
| 63 | + } |
| 64 | + |
| 65 | + status = psa_export_public_key(key_handle, |
| 66 | + m_pub_key, |
| 67 | + sizeof(m_pub_key), |
| 68 | + &data_length); |
| 69 | + |
| 70 | + if (status != PSA_SUCCESS) { |
| 71 | + return status; |
| 72 | + } |
| 73 | + return APP_SUCCESS; |
| 74 | +} |
| 75 | + |
| 76 | +int import_ecdsa_pub_key(void) |
| 77 | +{ |
| 78 | + psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 79 | + psa_status_t status; |
| 80 | + |
| 81 | + psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_VERIFY_MESSAGE); |
| 82 | + psa_set_key_lifetime(&key_attributes, PSA_KEY_LIFETIME_VOLATILE); |
| 83 | + psa_set_key_algorithm(&key_attributes, PSA_ALG_ECDSA(PSA_ALG_SHA_256)); |
| 84 | + psa_set_key_type(&key_attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_SECP_R1)); |
| 85 | + psa_set_key_bits(&key_attributes, 256); |
| 86 | + |
| 87 | + status = psa_import_key(&key_attributes, m_pub_key, sizeof(m_pub_key), &key_id); |
| 88 | + if (status != PSA_SUCCESS) { |
| 89 | + return status; |
| 90 | + } |
| 91 | + |
| 92 | + psa_reset_key_attributes(&key_attributes); |
| 93 | + |
| 94 | + return APP_SUCCESS; |
| 95 | +} |
| 96 | + |
| 97 | +int sign_message(void) |
| 98 | +{ |
| 99 | + uint32_t output_len; |
| 100 | + psa_status_t status; |
| 101 | + |
| 102 | + status = psa_sign_message(identity_key_id, |
| 103 | + PSA_ALG_ECDSA(PSA_ALG_SHA_256), |
| 104 | + m_plain_text, |
| 105 | + sizeof(m_plain_text), |
| 106 | + m_signature, |
| 107 | + sizeof(m_signature), |
| 108 | + &output_len); |
| 109 | + |
| 110 | + if (status != PSA_SUCCESS) { |
| 111 | + return status; |
| 112 | + } |
| 113 | + |
| 114 | + return APP_SUCCESS; |
| 115 | +} |
| 116 | + |
| 117 | +int verify_message(void) |
| 118 | +{ |
| 119 | + psa_status_t status; |
| 120 | + |
| 121 | + status = psa_verify_message(key_id, |
| 122 | + PSA_ALG_ECDSA(PSA_ALG_SHA_256), |
| 123 | + m_plain_text, |
| 124 | + sizeof(m_plain_text), |
| 125 | + m_signature, |
| 126 | + NRF_CRYPTO_TEST_IKG_SIGNATURE_SIZE); |
| 127 | + |
| 128 | + if (status != PSA_SUCCESS) { |
| 129 | + return status; |
| 130 | + } |
| 131 | + |
| 132 | + return APP_SUCCESS; |
| 133 | +} |
| 134 | + |
| 135 | +int ikg_identity_key_test(void) |
| 136 | +{ |
| 137 | + int status; |
| 138 | + |
| 139 | + status = crypto_init(); |
| 140 | + TEST_VECTOR_ASSERT_EQUAL(APP_SUCCESS, status); |
| 141 | + |
| 142 | + status = get_identity_key(); |
| 143 | + TEST_VECTOR_ASSERT_EQUAL(APP_SUCCESS, status); |
| 144 | + |
| 145 | + status = import_ecdsa_pub_key(); |
| 146 | + TEST_VECTOR_ASSERT_EQUAL(APP_SUCCESS, status); |
| 147 | + |
| 148 | + status = sign_message(); |
| 149 | + TEST_VECTOR_ASSERT_EQUAL(APP_SUCCESS, status); |
| 150 | + |
| 151 | + status = verify_message(); |
| 152 | + TEST_VECTOR_ASSERT_EQUAL(APP_SUCCESS, status); |
| 153 | + |
| 154 | + status = crypto_finish(); |
| 155 | + TEST_VECTOR_ASSERT_EQUAL(APP_SUCCESS, status); |
| 156 | + |
| 157 | + return status; |
| 158 | +} |
| 159 | + |
| 160 | +ZTEST(test_suite_ikg, ikg_identity_key_test) |
| 161 | +{ |
| 162 | + ikg_identity_key_test(); |
| 163 | +} |
0 commit comments