Skip to content

Commit 88523f2

Browse files
vlilleboenordicjm
authored andcommitted
samples: crypto: Add Spake2+
Adds sample demonstrating Spake2+ usage through PSA Crypto APIs. Signed-off-by: Vidar Lillebø <[email protected]>
1 parent eb0caf5 commit 88523f2

File tree

6 files changed

+376
-1
lines changed

6 files changed

+376
-1
lines changed

doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ Cellular samples
203203
Cryptography samples
204204
--------------------
205205

206-
|no_changes_yet_note|
206+
* Added :ref:`crypto_spake2p` sample.
207207

208208
Debug samples
209209
-------------
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.20.0)
2+
3+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
4+
project(spake2p)
5+
6+
target_sources(app PRIVATE src/main.c)

samples/crypto/spake2p/README.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
.. _crypto_spake2p:
2+
3+
Crypto: Spake2+
4+
###############
5+
6+
.. contents::
7+
:local:
8+
:depth: 2
9+
10+
The Spake2+ sample demonstrates how to do a password-authenticated key exchange using the Spake2+ protocol.
11+
12+
Requirements
13+
************
14+
15+
The sample supports the following development kits:
16+
17+
.. table-from-sample-yaml::
18+
19+
Overview
20+
********
21+
22+
The sample performs the following operations:
23+
24+
1. Initializes the Platform Security Architecture (PSA) API.
25+
#. Goes through the steps for Spake2+ on server and client sides.
26+
#. Verifies that the derived keys are the same.
27+
28+
Building and running
29+
********************
30+
31+
.. |sample path| replace:: :file:`samples/crypto/spake2p`
32+
33+
34+
Testing
35+
=======
36+
37+
After programming the sample to your development kit, complete the following steps to test it:
38+
39+
1. |connect_terminal|
40+
#. Compile and program the application.
41+
#. Observe the logs from the application using a terminal emulator.

samples/crypto/spake2p/prj.conf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Enable logging
2+
CONFIG_LOG=y
3+
CONFIG_CONSOLE=y
4+
5+
CONFIG_MAIN_STACK_SIZE=16000
6+
7+
# Enable nordic security backend and PSA APIs
8+
CONFIG_PSA_WANT_GENERATE_RANDOM=y
9+
CONFIG_NRF_SECURITY=y
10+
CONFIG_MBEDTLS_ENABLE_HEAP=y
11+
CONFIG_MBEDTLS_HEAP_SIZE=8192
12+
CONFIG_PSA_WANT_ALG_SPAKE2P_HMAC=y
13+
CONFIG_PSA_WANT_ALG_HKDF=y
14+
CONFIG_PSA_WANT_ALG_HMAC=y
15+
CONFIG_PSA_WANT_ALG_SHA_256=y
16+
CONFIG_PSA_WANT_ECC_SECP_R1_256=y
17+
CONFIG_PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_IMPORT=y

samples/crypto/spake2p/sample.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
sample:
2+
description: This app provides an example of Spake2+
3+
name: Spake2+ example
4+
tests:
5+
sample.spake2p:
6+
tags: introduction psa oberon
7+
platform_allow: >
8+
nrf5340dk_nrf5340_cpuapp
9+
nrf9160dk_nrf9160 nrf52840dk_nrf52840
10+
nrf9161dk_nrf9161
11+
nrf54l15pdk_nrf54l15_cpuapp
12+
nrf54h20pdk_nrf54h20_cpuapp
13+
harness: console
14+
harness_config:
15+
type: multi_line
16+
regex:
17+
- ".*Example finished successfully!.*"
18+
integration_platforms:
19+
- nrf5340dk_nrf5340_cpuapp
20+
- nrf9160dk_nrf9160
21+
- nrf9161dk_nrf9161
22+
- nrf52840dk_nrf52840
23+
- nrf54l15pdk_nrf54l15_cpuapp
24+
- nrf54h20pdk_nrf54h20_cpuapp

samples/crypto/spake2p/src/main.c

Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
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

Comments
 (0)