Skip to content

Commit 1bc817a

Browse files
authored
Merge pull request #690 from Ashish285/fix_libsodium/fix_nonnull_compare_build_error
fix(libsodium): improve esp port layer
2 parents 8f6f74b + 21517e7 commit 1bc817a

File tree

10 files changed

+319
-300
lines changed

10 files changed

+319
-300
lines changed

.github/workflows/deploy_gh_pages.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ jobs:
5252
args: >-
5353
--no-progress
5454
--include-fragments
55-
'./docs_build_output/**/*.html'
55+
--root-dir docs_build_output
56+
--exclude-path '.*/404\.html$'
57+
docs_build_output/**/*.html
5658
# Fail the action if broken links are found
5759
fail: true
5860

libsodium/CMakeLists.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ set(srcs
3434
"${SRC}/crypto_generichash/blake2b/ref/generichash_blake2b.c"
3535
"${SRC}/crypto_generichash/crypto_generichash.c"
3636
"${SRC}/crypto_hash/crypto_hash.c"
37-
"${SRC}/crypto_hash/sha256/cp/hash_sha256_cp.c"
3837
"${SRC}/crypto_hash/sha256/hash_sha256.c"
39-
"${SRC}/crypto_hash/sha512/cp/hash_sha512_cp.c"
4038
"${SRC}/crypto_hash/sha512/hash_sha512.c"
4139
"${SRC}/crypto_kdf/blake2b/kdf_blake2b.c"
4240
"${SRC}/crypto_kdf/crypto_kdf.c"
@@ -120,14 +118,16 @@ if(CONFIG_LIBSODIUM_USE_MBEDTLS_SHA)
120118
list(APPEND srcs
121119
"port/crypto_hash_mbedtls/crypto_hash_sha256_mbedtls.c"
122120
"port/crypto_hash_mbedtls/crypto_hash_sha512_mbedtls.c")
121+
set(include_dirs port_include ${SRC}/include)
122+
set(priv_include_dirs port port_include/sodium ${SRC}/include/sodium)
123123
else()
124124
list(APPEND srcs
125125
"${SRC}/crypto_hash/sha256/cp/hash_sha256_cp.c"
126126
"${SRC}/crypto_hash/sha512/cp/hash_sha512_cp.c")
127+
set(include_dirs ${SRC}/include port_include)
128+
set(priv_include_dirs ${SRC}/include/sodium port_include/sodium port)
127129
endif()
128130

129-
set(include_dirs ${SRC}/include port_include)
130-
set(priv_include_dirs ${SRC}/include/sodium port_include/sodium port)
131131
idf_component_register(SRCS "${srcs}"
132132
INCLUDE_DIRS "${include_dirs}"
133133
PRIV_INCLUDE_DIRS "${priv_include_dirs}"
@@ -177,6 +177,12 @@ set_source_files_properties(
177177

178178
target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-unused-function)
179179

180+
if(CONFIG_LIBSODIUM_USE_MBEDTLS_SHA)
181+
target_compile_options(${COMPONENT_LIB} PRIVATE
182+
"$<$<COMPILE_LANGUAGE:C>:SHELL:-include ${CMAKE_CURRENT_SOURCE_DIR}/port_include/sodium/crypto_hash_sha256.h>"
183+
"$<$<COMPILE_LANGUAGE:C>:SHELL:-include ${CMAKE_CURRENT_SOURCE_DIR}/port_include/sodium/crypto_hash_sha512.h>")
184+
endif()
185+
180186
if(CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE)
181187
# some libsodium variables are only used for asserts
182188
target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-unused-but-set-variable)

libsodium/Kconfig

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@ menu "libsodium"
33
config LIBSODIUM_USE_MBEDTLS_SHA
44
bool "Use mbedTLS SHA256 & SHA512 implementations"
55
default y
6-
depends on !MBEDTLS_HARDWARE_SHA
76
help
87
If this option is enabled, libsodium will use thin wrappers
98
around mbedTLS for SHA256 & SHA512 operations.
10-
11-
This saves some code size if mbedTLS is also used. However it
12-
is incompatible with hardware SHA acceleration (due to the
13-
way libsodium's API manages SHA state).
14-
9+
This saves some code size if mbedTLS is also used.
1510
endmenu # libsodium

libsodium/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "1.0.20~3"
1+
version: "1.0.20~4"
22
description: libsodium port to ESP
33
url: https://github.com/espressif/idf-extra-components/tree/master/libsodium
44
dependencies:

libsodium/port/crypto_hash_mbedtls/crypto_hash_sha256_mbedtls.c

Lines changed: 15 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -24,62 +24,9 @@
2424
#include "crypto_hash_sha256.h"
2525
#include <string.h>
2626

27-
#ifdef MBEDTLS_PSA_CRYPTO
28-
#include "psa/crypto.h"
29-
#else
30-
#include "mbedtls/sha256.h"
31-
#endif
32-
33-
#ifndef MBEDTLS_PSA_CRYPTO
34-
#ifdef MBEDTLS_SHA256_ALT
35-
/* Wrapper only works if the libsodium context structure can be mapped
36-
directly to the mbedTLS context structure.
37-
38-
See extended comments in crypto_hash_sha512_mbedtls.c
39-
*/
40-
#error "This wrapper only support standard software mbedTLS SHA"
41-
#endif
42-
43-
/* Sanity check that all the context fields have identical sizes
44-
(this should be more or less given from the SHA256 algorithm)
45-
46-
Note that the meaning of the fields is *not* all the same. In libsodium, SHA256 'count' is a 64-bit *bit* count. In
47-
mbedTLS, 'total' is a 2x32-bit *byte* count (count[0] == MSB).
48-
49-
For this implementation, we don't convert so the libsodium state structure actually holds a binary copy of the
50-
mbedTLS totals. This doesn't matter inside libsodium's documented API, but would matter if any callers try to use
51-
the state's bit count.
52-
*/
53-
_Static_assert(sizeof(((crypto_hash_sha256_state *)0)->state) == sizeof(((mbedtls_sha256_context *)0)->state), "state mismatch");
54-
_Static_assert(sizeof(((crypto_hash_sha256_state *)0)->count) == sizeof(((mbedtls_sha256_context *)0)->total), "count mismatch");
55-
_Static_assert(sizeof(((crypto_hash_sha256_state *)0)->buf) == sizeof(((mbedtls_sha256_context *)0)->buffer), "buf mismatch");
56-
57-
/* Inline functions to convert between mbedTLS & libsodium
58-
context structures
59-
*/
60-
61-
static void sha256_mbedtls_to_libsodium(crypto_hash_sha256_state *ls_state, const mbedtls_sha256_context *mb_ctx)
62-
{
63-
memcpy(&ls_state->count, mb_ctx->total, sizeof(ls_state->count));
64-
memcpy(ls_state->state, mb_ctx->state, sizeof(ls_state->state));
65-
memcpy(ls_state->buf, mb_ctx->buffer, sizeof(ls_state->buf));
66-
}
67-
68-
static void sha256_libsodium_to_mbedtls(mbedtls_sha256_context *mb_ctx, crypto_hash_sha256_state *ls_state)
69-
{
70-
memcpy(mb_ctx->total, &ls_state->count, sizeof(mb_ctx->total));
71-
memcpy(mb_ctx->state, ls_state->state, sizeof(mb_ctx->state));
72-
memcpy(mb_ctx->buffer, ls_state->buf, sizeof(mb_ctx->buffer));
73-
mb_ctx->is224 = 0;
74-
}
75-
#endif /* !MBEDTLS_PSA_CRYPTO */
76-
7727
int
7828
crypto_hash_sha256_init(crypto_hash_sha256_state *state)
7929
{
80-
if (state == NULL) {
81-
return -1;
82-
}
8330
#ifdef MBEDTLS_PSA_CRYPTO
8431
psa_status_t status;
8532

@@ -88,37 +35,23 @@ crypto_hash_sha256_init(crypto_hash_sha256_state *state)
8835
return -1;
8936
}
9037

91-
psa_hash_operation_t *operation;
92-
93-
/* Store PSA hash operation in the state buffer
94-
* The libsodium state structure is large enough to hold psa_hash_operation_t.
95-
* Ensure this is safe with respect to both size and alignment.
96-
*/
97-
_Static_assert(sizeof(crypto_hash_sha256_state) >= sizeof(psa_hash_operation_t),
98-
"crypto_hash_sha256_state too small for psa_hash_operation_t");
99-
_Static_assert(_Alignof(crypto_hash_sha256_state) >= _Alignof(psa_hash_operation_t),
100-
"crypto_hash_sha256_state alignment insufficient for psa_hash_operation_t");
101-
memset(state, 0, sizeof(*state));
102-
operation = (psa_hash_operation_t *)state;
103-
*operation = psa_hash_operation_init();
38+
state->_psa_op = psa_hash_operation_init();
10439

105-
status = psa_hash_setup(operation, PSA_ALG_SHA_256);
40+
status = psa_hash_setup(&state->_psa_op, PSA_ALG_SHA_256);
10641
if (status != PSA_SUCCESS) {
10742
return -1;
10843
}
10944
return 0;
11045
#else
111-
mbedtls_sha256_context ctx;
112-
mbedtls_sha256_init(&ctx);
46+
mbedtls_sha256_init(&state->ctx);
11347
#ifdef MBEDTLS_2_X_COMPAT
114-
int ret = mbedtls_sha256_starts_ret(&ctx, 0);
48+
int ret = mbedtls_sha256_starts_ret(&state->ctx, 0);
11549
#else
116-
int ret = mbedtls_sha256_starts(&ctx, 0);
50+
int ret = mbedtls_sha256_starts(&state->ctx, 0);
11751
#endif /* MBEDTLS_2_X_COMPAT */
11852
if (ret != 0) {
11953
return ret;
12054
}
121-
sha256_mbedtls_to_libsodium(state, &ctx);
12255
return 0;
12356
#endif /* !MBEDTLS_PSA_CRYPTO */
12457
}
@@ -127,59 +60,49 @@ int
12760
crypto_hash_sha256_update(crypto_hash_sha256_state *state,
12861
const unsigned char *in, unsigned long long inlen)
12962
{
130-
if (state == NULL || (in == NULL && inlen > 0)) {
63+
if (in == NULL && inlen > 0) {
13164
return -1;
13265
}
13366
#ifdef MBEDTLS_PSA_CRYPTO
134-
psa_hash_operation_t *operation = (psa_hash_operation_t *)state;
13567
psa_status_t status;
13668

137-
status = psa_hash_update(operation, in, inlen);
69+
status = psa_hash_update(&state->_psa_op, in, inlen);
13870
if (status != PSA_SUCCESS) {
139-
psa_hash_abort(operation);
71+
psa_hash_abort(&state->_psa_op);
14072
return -1;
14173
}
14274
return 0;
14375
#else
144-
mbedtls_sha256_context ctx;
145-
sha256_libsodium_to_mbedtls(&ctx, state);
14676
#ifdef MBEDTLS_2_X_COMPAT
147-
int ret = mbedtls_sha256_update_ret(&ctx, in, inlen);
77+
int ret = mbedtls_sha256_update_ret(&state->ctx, in, inlen);
14878
#else
149-
int ret = mbedtls_sha256_update(&ctx, in, inlen);
79+
int ret = mbedtls_sha256_update(&state->ctx, in, inlen);
15080
#endif /* MBEDTLS_2_X_COMPAT */
15181
if (ret != 0) {
15282
return ret;
15383
}
154-
sha256_mbedtls_to_libsodium(state, &ctx);
15584
return 0;
15685
#endif /* !MBEDTLS_PSA_CRYPTO */
15786
}
15887

15988
int
16089
crypto_hash_sha256_final(crypto_hash_sha256_state *state, unsigned char *out)
16190
{
162-
if (state == NULL || out == NULL) {
163-
return -1;
164-
}
16591
#ifdef MBEDTLS_PSA_CRYPTO
166-
psa_hash_operation_t *operation = (psa_hash_operation_t *)state;
16792
psa_status_t status;
16893
size_t hash_len;
16994

170-
status = psa_hash_finish(operation, out, crypto_hash_sha256_BYTES, &hash_len);
95+
status = psa_hash_finish(&state->_psa_op, out, crypto_hash_sha256_BYTES, &hash_len);
17196
if (status != PSA_SUCCESS || hash_len != crypto_hash_sha256_BYTES) {
172-
psa_hash_abort(operation);
97+
psa_hash_abort(&state->_psa_op);
17398
return -1;
17499
}
175100
return 0;
176101
#else
177-
mbedtls_sha256_context ctx;
178-
sha256_libsodium_to_mbedtls(&ctx, state);
179102
#ifdef MBEDTLS_2_X_COMPAT
180-
return mbedtls_sha256_finish_ret(&ctx, out);
103+
return mbedtls_sha256_finish_ret(&state->ctx, out);
181104
#else
182-
return mbedtls_sha256_finish(&ctx, out);
105+
return mbedtls_sha256_finish(&state->ctx, out);
183106
#endif /* MBEDTLS_2_X_COMPAT */
184107
#endif /* !MBEDTLS_PSA_CRYPTO */
185108
}
@@ -188,7 +111,7 @@ int
188111
crypto_hash_sha256(unsigned char *out, const unsigned char *in,
189112
unsigned long long inlen)
190113
{
191-
if (out == NULL || (in == NULL && inlen > 0)) {
114+
if (in == NULL && inlen > 0) {
192115
return -1;
193116
}
194117
#ifdef MBEDTLS_PSA_CRYPTO

0 commit comments

Comments
 (0)