Skip to content

Commit d2b9e5f

Browse files
Vge0rgerlubos
authored andcommitted
[nrf fromtree] net: websocket: Allow using PSA APIs to calculate SHA1
The websocket used mbedtls functions to calculate the SHA1 needed. Update the code to use PSA crypto calls instead when the configuration CONFIG_MBEDTLS_PSA_CRYPTO_CLIENT is enabled. This can be useful for applications which use TF-M since it only provides PSA crypto APIs. Also check the error code from the mbedtls_sha1 call since it can fail and it was not checked before. Signed-off-by: Georgios Vasilakis <[email protected]> (cherry picked from commit 53b2802)
1 parent c5d7fd8 commit d2b9e5f

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

subsys/net/lib/websocket/websocket.c

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ LOG_MODULE_REGISTER(net_websocket, CONFIG_NET_WEBSOCKET_LOG_LEVEL);
3434
#include <zephyr/random/random.h>
3535
#include <zephyr/sys/byteorder.h>
3636
#include <zephyr/sys/base64.h>
37+
38+
#ifdef CONFIG_MBEDTLS_PSA_CRYPTO_CLIENT
39+
#include <psa/crypto.h>
40+
#else
3741
#include <mbedtls/sha1.h>
42+
#endif /* CONFIG_MBEDTLS_PSA_CRYPTO_CLIENT */
3843

3944
#include "net_private.h"
4045
#include "sockets_internal.h"
@@ -253,6 +258,10 @@ int websocket_connect(int sock, struct websocket_request *wreq,
253258
"Sec-WebSocket-Version: 13\r\n",
254259
NULL
255260
};
261+
#ifdef CONFIG_MBEDTLS_PSA_CRYPTO_CLIENT
262+
psa_status_t psa_status;
263+
size_t hash_length;
264+
#endif /* CONFIG_MBEDTLS_PSA_CRYPTO_CLIENT */
256265

257266
fd = -1;
258267

@@ -280,8 +289,23 @@ int websocket_connect(int sock, struct websocket_request *wreq,
280289
ctx->http_cb = wreq->http_cb;
281290
ctx->is_client = 1;
282291

283-
mbedtls_sha1((const unsigned char *)&rnd_value, sizeof(rnd_value),
284-
sec_accept_key);
292+
#ifdef CONFIG_MBEDTLS_PSA_CRYPTO_CLIENT
293+
psa_status = psa_hash_compute(PSA_ALG_SHA_1, (const uint8_t *)&rnd_value, sizeof(rnd_value),
294+
sec_accept_key, sizeof(sec_accept_key), &hash_length);
295+
if (psa_status != PSA_SUCCESS) {
296+
NET_DBG("[%p] Cannot calculate sha1 (%d)", ctx, psa_status);
297+
ret = -EPROTO;
298+
goto out;
299+
}
300+
#else
301+
ret = mbedtls_sha1((const unsigned char *)&rnd_value, sizeof(rnd_value), sec_accept_key);
302+
if (ret != 0) {
303+
NET_DBG("[%p] Cannot calculate sha1 (%d)", ctx, ret);
304+
ret = -EPROTO;
305+
goto out;
306+
}
307+
#endif /* CONFIG_MBEDTLS_PSA_CRYPTO_CLIENT */
308+
285309

286310
ret = base64_encode(sec_ws_key + sizeof("Sec-Websocket-Key: ") - 1,
287311
sizeof(sec_ws_key) -
@@ -344,7 +368,22 @@ int websocket_connect(int sock, struct websocket_request *wreq,
344368
strncpy(key_accept + key_len, WS_MAGIC, olen);
345369

346370
/* This SHA-1 value is then checked when we receive the response */
347-
mbedtls_sha1(key_accept, olen + key_len, sec_accept_key);
371+
#ifdef CONFIG_MBEDTLS_PSA_CRYPTO_CLIENT
372+
psa_status = psa_hash_compute(PSA_ALG_SHA_1, (const uint8_t *)key_accept, olen + key_len,
373+
sec_accept_key, sizeof(sec_accept_key), &hash_length);
374+
if (psa_status != PSA_SUCCESS) {
375+
NET_DBG("[%p] Cannot calculate sha1 (%d)", ctx, psa_status);
376+
ret = -EPROTO;
377+
goto out;
378+
}
379+
#else
380+
ret = mbedtls_sha1(key_accept, olen + key_len, sec_accept_key);
381+
if (ret != 0) {
382+
NET_DBG("[%p] Cannot calculate sha1 (%d)", ctx, ret);
383+
ret = -EPROTO;
384+
goto out;
385+
}
386+
#endif /* CONFIG_MBEDTLS_PSA_CRYPTO_CLIENT */
348387

349388
ret = http_client_req(sock, &req, timeout, ctx);
350389
if (ret < 0) {

0 commit comments

Comments
 (0)