|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <psa/crypto.h> |
| 8 | +#include <stdint.h> |
| 9 | +#include <zephyr/kernel.h> |
| 10 | +#include <zephyr/sys/printk.h> |
| 11 | +#include <zephyr/logging/log.h> |
| 12 | +#include <zephyr/sys/util.h> |
| 13 | + |
| 14 | +LOG_MODULE_REGISTER(spake2p, LOG_LEVEL_DBG); |
| 15 | + |
| 16 | +#define PRINT_HEX(p_label, p_text, len) \ |
| 17 | + ({ \ |
| 18 | + LOG_INF("---- %s (len: %u): ----", p_label, len); \ |
| 19 | + LOG_HEXDUMP_INF(p_text, len, "Content:"); \ |
| 20 | + LOG_INF("---- %s end ----", p_label); \ |
| 21 | + }) |
| 22 | + |
| 23 | +#define APP_SUCCESS (0) |
| 24 | +#define APP_ERROR (-1) |
| 25 | +#define APP_SUCCESS_MESSAGE "Example finished successfully!" |
| 26 | +#define APP_ERROR_MESSAGE "Example exited with error!" |
| 27 | + |
| 28 | +/* w0 || w1 */ |
| 29 | +static const uint8_t key_pair[] = {0x54, 0x8E, 0xC1, 0x42, 0xE2, 0x27, 0x90, 0x23, 0x7C, 0x67, 0xA8, |
| 30 | + 0x88, 0x49, 0xE8, 0x61, 0xD3, 0x77, 0x00, 0x5F, 0x0A, 0x5C, 0x33, |
| 31 | + 0x88, 0xF9, 0xAF, 0xA1, 0xC2, 0xFA, 0x58, 0xC7, 0xDA, 0x51, 0x35, |
| 32 | + 0x99, 0xF8, 0x67, 0x1D, 0xBB, 0x67, 0x04, 0xA2, 0xC6, 0x3A, 0x78, |
| 33 | + 0x4F, 0xC9, 0x5C, 0xD2, 0x8E, 0xBC, 0x55, 0x2E, 0xA4, 0x79, 0x98, |
| 34 | + 0xB9, 0x18, 0x69, 0x9A, 0xB9, 0x3F, 0x4F, 0x7A, 0xD7}; |
| 35 | + |
| 36 | +/* w0 || L */ |
| 37 | +static const uint8_t public_key[] = { |
| 38 | + 0x54, 0x8E, 0xC1, 0x42, 0xE2, 0x27, 0x90, 0x23, 0x7C, 0x67, 0xA8, 0x88, 0x49, 0xE8, |
| 39 | + 0x61, 0xD3, 0x77, 0x00, 0x5F, 0x0A, 0x5C, 0x33, 0x88, 0xF9, 0xAF, 0xA1, 0xC2, 0xFA, |
| 40 | + 0x58, 0xC7, 0xDA, 0x51, 0x04, 0x81, 0x43, 0x3D, 0xC5, 0x93, 0xC9, 0x46, 0xC9, 0x37, |
| 41 | + 0xD9, 0x90, 0x26, 0xDD, 0x42, 0x14, 0x40, 0xE1, 0xC8, 0x7D, 0x0E, 0xC4, 0x94, 0x8B, |
| 42 | + 0xFF, 0x59, 0xEA, 0xF4, 0x77, 0xE3, 0x35, 0xE5, 0x52, 0x49, 0x66, 0xB2, 0x03, 0x31, |
| 43 | + 0x37, 0xD8, 0x4C, 0x65, 0x56, 0xDE, 0x07, 0x31, 0x57, 0x5C, 0xD2, 0x95, 0xC9, 0x75, |
| 44 | + 0x12, 0x4F, 0x52, 0x13, 0x25, 0xF7, 0x80, 0x01, 0xEC, 0xBE, 0x67, 0xE8, 0xB7}; |
| 45 | + |
| 46 | +static psa_status_t send_message(psa_pake_operation_t *from, psa_pake_operation_t *to, |
| 47 | + psa_pake_step_t step) |
| 48 | +{ |
| 49 | + uint8_t data[1024]; |
| 50 | + size_t length; |
| 51 | + |
| 52 | + psa_status_t status = psa_pake_output(from, step, data, sizeof(data), &length); |
| 53 | + |
| 54 | + if (status) { |
| 55 | + return status; |
| 56 | + } |
| 57 | + PRINT_HEX("send_message", data, length); |
| 58 | + status = psa_pake_input(to, step, data, length); |
| 59 | + return status; |
| 60 | +} |
| 61 | + |
| 62 | +static psa_status_t pake_setup(psa_pake_operation_t *op, psa_pake_role_t role, psa_key_id_t key, |
| 63 | + psa_pake_cipher_suite_t *cipher_suite) |
| 64 | +{ |
| 65 | + psa_status_t status; |
| 66 | + |
| 67 | + status = psa_pake_setup(op, key, cipher_suite); |
| 68 | + if (status != PSA_SUCCESS) { |
| 69 | + LOG_INF("psa_pake_setup failed. (Error: %d)", status); |
| 70 | + return status; |
| 71 | + } |
| 72 | + |
| 73 | + status = psa_pake_set_role(op, role); |
| 74 | + if (status != PSA_SUCCESS) { |
| 75 | + LOG_INF("psa_pake_set_role failed. (Error: %d)", status); |
| 76 | + return status; |
| 77 | + } |
| 78 | + |
| 79 | + if (role == PSA_PAKE_ROLE_SERVER) { |
| 80 | + status = psa_pake_set_user(op, "client", strlen("client")); |
| 81 | + if (status != PSA_SUCCESS) { |
| 82 | + LOG_INF("psa_pake_set_user failed. (Error: %d)", status); |
| 83 | + return status; |
| 84 | + } |
| 85 | + |
| 86 | + status = psa_pake_set_peer(op, "server", strlen("server")); |
| 87 | + if (status != PSA_SUCCESS) { |
| 88 | + LOG_INF("psa_pake_set_peer failed. (Error: %d)", status); |
| 89 | + return status; |
| 90 | + } |
| 91 | + } else { |
| 92 | + status = psa_pake_set_user(op, "server", strlen("server")); |
| 93 | + if (status != PSA_SUCCESS) { |
| 94 | + LOG_INF("psa_pake_set_user failed. (Error: %d)", status); |
| 95 | + return status; |
| 96 | + } |
| 97 | + |
| 98 | + status = psa_pake_set_peer(op, "client", strlen("client")); |
| 99 | + if (status != PSA_SUCCESS) { |
| 100 | + LOG_INF("psa_pake_set_peer failed. (Error: %d)", status); |
| 101 | + return status; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return status; |
| 106 | +} |
| 107 | + |
| 108 | +int main(void) |
| 109 | +{ |
| 110 | + /* For Matter-compatible Spake2+, this will be PSA_ALG_SPAKE2P_MATTER */ |
| 111 | + psa_algorithm_t psa_spake_alg = PSA_ALG_SPAKE2P_HMAC(PSA_ALG_SHA_256); |
| 112 | + psa_status_t status = psa_crypto_init(); |
| 113 | + |
| 114 | + if (status != PSA_SUCCESS) { |
| 115 | + LOG_INF("psa_crypto_init failed. (Error: %d)", status); |
| 116 | + } |
| 117 | + |
| 118 | + psa_pake_cipher_suite_t cipher_suite = PSA_PAKE_CIPHER_SUITE_INIT; |
| 119 | + |
| 120 | + psa_pake_cs_set_algorithm(&cipher_suite, psa_spake_alg); |
| 121 | + psa_pake_cs_set_primitive(&cipher_suite, PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC, |
| 122 | + PSA_ECC_FAMILY_SECP_R1, 256)); |
| 123 | + psa_pake_cs_set_key_confirmation(&cipher_suite, PSA_PAKE_CONFIRMED_KEY); |
| 124 | + |
| 125 | + psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 126 | + |
| 127 | + psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); |
| 128 | + psa_set_key_algorithm(&key_attributes, psa_spake_alg); |
| 129 | + psa_set_key_type(&key_attributes, PSA_KEY_TYPE_SPAKE2P_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1)); |
| 130 | + psa_set_key_bits(&key_attributes, 256); |
| 131 | + |
| 132 | + psa_key_id_t key; |
| 133 | + |
| 134 | + status = psa_import_key(&key_attributes, key_pair, sizeof(key_pair), &key); |
| 135 | + if (status != PSA_SUCCESS) { |
| 136 | + LOG_INF("psa_import_key failed. (Error: %d)", status); |
| 137 | + goto error; |
| 138 | + } |
| 139 | + |
| 140 | + psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE); |
| 141 | + psa_set_key_algorithm(&key_attributes, psa_spake_alg); |
| 142 | + psa_set_key_type(&key_attributes, PSA_KEY_TYPE_SPAKE2P_PUBLIC_KEY(PSA_ECC_FAMILY_SECP_R1)); |
| 143 | + psa_set_key_bits(&key_attributes, 256); |
| 144 | + |
| 145 | + psa_key_id_t key_server; |
| 146 | + |
| 147 | + status = psa_import_key(&key_attributes, public_key, sizeof(public_key), &key_server); |
| 148 | + if (status != PSA_SUCCESS) { |
| 149 | + LOG_INF("psa_import_key failed. (Error: %d)", status); |
| 150 | + goto error; |
| 151 | + } |
| 152 | + |
| 153 | + psa_reset_key_attributes(&key_attributes); |
| 154 | + |
| 155 | + psa_pake_operation_t client_op = PSA_PAKE_OPERATION_INIT; |
| 156 | + |
| 157 | + status = pake_setup(&client_op, PSA_PAKE_ROLE_CLIENT, key, &cipher_suite); |
| 158 | + if (status != PSA_SUCCESS) { |
| 159 | + goto error; |
| 160 | + } |
| 161 | + |
| 162 | + psa_pake_operation_t server_op = PSA_PAKE_OPERATION_INIT; |
| 163 | + |
| 164 | + status = pake_setup(&server_op, PSA_PAKE_ROLE_SERVER, key, &cipher_suite); |
| 165 | + if (status != PSA_SUCCESS) { |
| 166 | + goto error; |
| 167 | + } |
| 168 | + |
| 169 | + /* Share P */ |
| 170 | + status = send_message(&client_op, &server_op, PSA_PAKE_STEP_KEY_SHARE); |
| 171 | + if (status != PSA_SUCCESS) { |
| 172 | + LOG_INF("send_message failed. (Error: %d, step: %d)", status, |
| 173 | + PSA_PAKE_STEP_KEY_SHARE); |
| 174 | + goto error; |
| 175 | + } |
| 176 | + |
| 177 | + /* Share V */ |
| 178 | + status = send_message(&server_op, &client_op, PSA_PAKE_STEP_KEY_SHARE); |
| 179 | + if (status != PSA_SUCCESS) { |
| 180 | + LOG_INF("send_message failed. (Error: %d, step: %d)", status, |
| 181 | + PSA_PAKE_STEP_KEY_SHARE); |
| 182 | + goto error; |
| 183 | + } |
| 184 | + |
| 185 | + /* Confirm P */ |
| 186 | + status = send_message(&server_op, &client_op, PSA_PAKE_STEP_CONFIRM); |
| 187 | + if (status != PSA_SUCCESS) { |
| 188 | + LOG_INF("send_message failed. (Error: %d, step: %d)", status, |
| 189 | + PSA_PAKE_STEP_CONFIRM); |
| 190 | + goto error; |
| 191 | + } |
| 192 | + |
| 193 | + /* Confirm V */ |
| 194 | + status = send_message(&client_op, &server_op, PSA_PAKE_STEP_CONFIRM); |
| 195 | + if (status != PSA_SUCCESS) { |
| 196 | + LOG_INF("send_message failed. (Error: %d, step: %d)", status, |
| 197 | + PSA_PAKE_STEP_CONFIRM); |
| 198 | + goto error; |
| 199 | + } |
| 200 | + |
| 201 | + psa_key_derivation_operation_t kdf = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 202 | + uint8_t client_secret[32] = {0}; |
| 203 | + uint8_t server_secret[32] = {0}; |
| 204 | + |
| 205 | + psa_key_attributes_t shared_key_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 206 | + |
| 207 | + psa_set_key_type(&shared_key_attributes, PSA_KEY_TYPE_DERIVE); |
| 208 | + psa_set_key_usage_flags(&shared_key_attributes, PSA_KEY_USAGE_DERIVE); |
| 209 | + psa_set_key_algorithm(&shared_key_attributes, PSA_ALG_HKDF(PSA_ALG_SHA_256)); |
| 210 | + |
| 211 | + struct { |
| 212 | + psa_pake_operation_t *op; |
| 213 | + uint8_t *secret; |
| 214 | + } client_server[] = {{&client_op, client_secret}, {&server_op, server_secret}}; |
| 215 | + |
| 216 | + for (size_t i = 0; i < ARRAY_SIZE(client_server); i++) { |
| 217 | + psa_key_id_t key; |
| 218 | + |
| 219 | + status = psa_pake_get_shared_key(client_server[i].op, &shared_key_attributes, &key); |
| 220 | + if (status != PSA_SUCCESS) { |
| 221 | + LOG_INF("psa_pake_get_shared_key failed. (Error: %d)", status); |
| 222 | + goto error; |
| 223 | + } |
| 224 | + |
| 225 | + status = psa_key_derivation_setup(&kdf, PSA_ALG_HKDF(PSA_ALG_SHA_256)); |
| 226 | + if (status != PSA_SUCCESS) { |
| 227 | + LOG_INF("psa_key_derivation_setup failed. (Error: %d)", status); |
| 228 | + goto error; |
| 229 | + } |
| 230 | + |
| 231 | + status = psa_key_derivation_input_key(&kdf, PSA_KEY_DERIVATION_INPUT_SECRET, key); |
| 232 | + if (status != PSA_SUCCESS) { |
| 233 | + LOG_INF("psa_key_derivation_input_key failed. (Error: %d)", status); |
| 234 | + goto error; |
| 235 | + } |
| 236 | + |
| 237 | + status = psa_key_derivation_input_bytes(&kdf, PSA_KEY_DERIVATION_INPUT_INFO, "Info", |
| 238 | + 4); |
| 239 | + if (status != PSA_SUCCESS) { |
| 240 | + LOG_INF("psa_key_derivation_input_bytes failed. (Error: %d)", status); |
| 241 | + goto error; |
| 242 | + } |
| 243 | + |
| 244 | + status = psa_key_derivation_output_bytes(&kdf, client_server[i].secret, |
| 245 | + sizeof(client_secret)); |
| 246 | + if (status != PSA_SUCCESS) { |
| 247 | + LOG_INF("psa_key_derivation_output_bytes failed. (Error: %d)", status); |
| 248 | + goto error; |
| 249 | + } |
| 250 | + |
| 251 | + status = psa_key_derivation_abort(&kdf); |
| 252 | + if (status != PSA_SUCCESS) { |
| 253 | + LOG_INF("psa_key_derivation_abort failed. (Error: %d)", status); |
| 254 | + goto error; |
| 255 | + } |
| 256 | + |
| 257 | + status = psa_destroy_key(key); |
| 258 | + if (status != PSA_SUCCESS) { |
| 259 | + LOG_INF("psa_destroy_key failed. (Error: %d)", status); |
| 260 | + goto error; |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + psa_reset_key_attributes(&shared_key_attributes); |
| 265 | + PRINT_HEX("server_secret", client_secret, sizeof(client_secret)); |
| 266 | + PRINT_HEX("client_secret", server_secret, sizeof(server_secret)); |
| 267 | + |
| 268 | + bool compare_eq = true; |
| 269 | + |
| 270 | + for (size_t i = 0; i < sizeof(server_secret); i++) { |
| 271 | + if (server_secret[i] != client_secret[i]) { |
| 272 | + compare_eq = false; |
| 273 | + } |
| 274 | + } |
| 275 | + |
| 276 | + if (!compare_eq) { |
| 277 | + LOG_ERR("Derived keys for server and client are not equal."); |
| 278 | + goto error; |
| 279 | + } |
| 280 | + |
| 281 | + LOG_INF(APP_SUCCESS_MESSAGE); |
| 282 | + return APP_SUCCESS; |
| 283 | + |
| 284 | +error: |
| 285 | + LOG_INF(APP_ERROR_MESSAGE); |
| 286 | + return APP_ERROR; |
| 287 | +} |
0 commit comments