Skip to content

Commit 21d6620

Browse files
committed
amp feedback
https://ampcode.com/threads/T-019c77b0-4180-77bd-aef8-37428d27e085 Signed-off-by: Peter M <petermm@gmail.com>
1 parent ddfa3a0 commit 21d6620

File tree

5 files changed

+49
-41
lines changed

5 files changed

+49
-41
lines changed

src/libAtomVM/otp_crypto.c

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
#include <otp_crypto.h>
2323

24-
#include <stdio.h>
25-
2624
#include <context.h>
2725
#include <defaultatoms.h>
2826
#include <globalcontext.h>
@@ -299,7 +297,7 @@ static term nif_crypto_hash(Context *ctx, int argc, term argv[])
299297
psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
300298
psa_status_t status = psa_hash_setup(&operation, alg);
301299
if (UNLIKELY(status != PSA_SUCCESS)) {
302-
fprintf(stderr, "crypto:hash psa_hash_setup failed with status %d for alg 0x%08lx\n", (int) status, (unsigned long) alg);
300+
TRACE("crypto:hash psa_hash_setup failed with status %d for alg 0x%08lx\n", (int) status, (unsigned long) alg);
303301
RAISE_ERROR(BADARG_ATOM);
304302
}
305303

@@ -611,6 +609,9 @@ static term nif_crypto_crypto_one_time(Context *ctx, int argc, term argv[])
611609
#if MBEDTLS_VERSION_NUMBER >= 0x04000000
612610
bool encrypt = true;
613611
bool padding_pkcs7 = false;
612+
psa_key_id_t key_id = 0;
613+
void *temp_buf = NULL;
614+
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
614615

615616
if (term_is_list(flag_or_options)) {
616617
term encrypt_flag = interop_kv_get_value_default(
@@ -660,59 +661,45 @@ static term nif_crypto_crypto_one_time(Context *ctx, int argc, term argv[])
660661
psa_set_key_type(&attributes, key_type);
661662
psa_set_key_bits(&attributes, key_bits);
662663

663-
psa_key_id_t key_id;
664664
psa_status_t status = psa_import_key(&attributes, key_data, key_len, &key_id);
665665
if (UNLIKELY(status != PSA_SUCCESS)) {
666-
free(allocated_key_data);
667-
free(allocated_iv_data);
668-
free(allocated_data_data);
669666
char err_msg[48];
670667
snprintf(err_msg, sizeof(err_msg), "key import err %d", (int) status);
671-
RAISE_ERROR(make_crypto_error(__FILE__, __LINE__, err_msg, ctx));
668+
error_atom = make_crypto_error(__FILE__, __LINE__, err_msg, ctx);
669+
goto psa_error;
672670
}
673671

674672
size_t output_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(key_type, alg, data_size);
675673
if (!encrypt) {
676674
output_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE(key_type, alg, data_size);
677675
}
678-
void *temp_buf = malloc(output_size);
676+
temp_buf = malloc(output_size);
679677
if (IS_NULL_PTR(temp_buf)) {
680-
psa_destroy_key(key_id);
681678
error_atom = OUT_OF_MEMORY_ATOM;
682-
goto raise_error;
679+
goto psa_error;
683680
}
684681

685682
size_t output_len;
686-
psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
687683
if (encrypt) {
688684
status = psa_cipher_encrypt_setup(&operation, key_id, alg);
689685
} else {
690686
status = psa_cipher_decrypt_setup(&operation, key_id, alg);
691687
}
692688
if (UNLIKELY(status != PSA_SUCCESS)) {
693-
psa_destroy_key(key_id);
694-
free(temp_buf);
695-
free(allocated_key_data);
696-
free(allocated_iv_data);
697-
free(allocated_data_data);
698689
char err_msg[48];
699690
snprintf(err_msg, sizeof(err_msg), "cipher setup err %d", (int) status);
700-
RAISE_ERROR(make_crypto_error(__FILE__, __LINE__, err_msg, ctx));
691+
error_atom = make_crypto_error(__FILE__, __LINE__, err_msg, ctx);
692+
goto psa_error;
701693
}
702694

703695
// PSA rejects IVs for ECB; ignore IV to preserve legacy behavior.
704696
if (iv_len > 0 && alg != PSA_ALG_ECB_NO_PADDING) {
705697
status = psa_cipher_set_iv(&operation, iv_data, iv_len);
706698
if (UNLIKELY(status != PSA_SUCCESS)) {
707-
psa_cipher_abort(&operation);
708-
psa_destroy_key(key_id);
709-
free(temp_buf);
710-
free(allocated_key_data);
711-
free(allocated_iv_data);
712-
free(allocated_data_data);
713699
char err_msg[24];
714700
snprintf(err_msg, sizeof(err_msg), "IV err %d", (int) status);
715-
RAISE_ERROR(make_crypto_error(__FILE__, __LINE__, err_msg, ctx));
701+
error_atom = make_crypto_error(__FILE__, __LINE__, err_msg, ctx);
702+
goto psa_error;
716703
}
717704
}
718705

@@ -728,6 +715,9 @@ static term nif_crypto_crypto_one_time(Context *ctx, int argc, term argv[])
728715
psa_cipher_abort(&operation);
729716
psa_destroy_key(key_id);
730717
free(temp_buf);
718+
if (allocated_key_data) {
719+
memset(allocated_key_data, 0, key_len);
720+
}
731721
free(allocated_key_data);
732722
free(allocated_iv_data);
733723
free(allocated_data_data);
@@ -742,34 +732,27 @@ static term nif_crypto_crypto_one_time(Context *ctx, int argc, term argv[])
742732
size_t update_len = 0;
743733
status = psa_cipher_update(&operation, data_data, process_size, temp_buf, output_size, &update_len);
744734
if (UNLIKELY(status != PSA_SUCCESS)) {
745-
psa_cipher_abort(&operation);
746-
psa_destroy_key(key_id);
747-
free(temp_buf);
748-
free(allocated_key_data);
749-
free(allocated_iv_data);
750-
free(allocated_data_data);
751735
char err_msg[24];
752736
snprintf(err_msg, sizeof(err_msg), "update err %d", (int) status);
753-
RAISE_ERROR(make_crypto_error(__FILE__, __LINE__, err_msg, ctx));
737+
error_atom = make_crypto_error(__FILE__, __LINE__, err_msg, ctx);
738+
goto psa_error;
754739
}
755740

756741
size_t finish_len = 0;
757742
status = psa_cipher_finish(&operation, (uint8_t *) temp_buf + update_len, output_size - update_len, &finish_len);
758743
if (UNLIKELY(status != PSA_SUCCESS)) {
759-
psa_cipher_abort(&operation);
760-
psa_destroy_key(key_id);
761-
free(temp_buf);
762-
free(allocated_key_data);
763-
free(allocated_iv_data);
764-
free(allocated_data_data);
765744
char err_msg[24];
766745
snprintf(err_msg, sizeof(err_msg), "finish err %d", (int) status);
767-
RAISE_ERROR(make_crypto_error(__FILE__, __LINE__, err_msg, ctx));
746+
error_atom = make_crypto_error(__FILE__, __LINE__, err_msg, ctx);
747+
goto psa_error;
768748
}
769749
output_len = update_len + finish_len;
770750

771751
psa_destroy_key(key_id);
772752

753+
if (allocated_key_data) {
754+
memset(allocated_key_data, 0, key_len);
755+
}
773756
free(allocated_key_data);
774757
free(allocated_iv_data);
775758
free(allocated_data_data);
@@ -783,6 +766,17 @@ static term nif_crypto_crypto_one_time(Context *ctx, int argc, term argv[])
783766
term out = term_from_literal_binary(temp_buf, output_len, &ctx->heap, ctx->global);
784767
free(temp_buf);
785768
return out;
769+
770+
psa_error:
771+
psa_cipher_abort(&operation);
772+
if (key_id != 0) {
773+
psa_destroy_key(key_id);
774+
}
775+
free(temp_buf);
776+
if (allocated_key_data) {
777+
memset(allocated_key_data, 0, key_len);
778+
}
779+
goto raise_error;
786780
#else
787781
mbedtls_operation_t operation;
788782
mbedtls_cipher_padding_t padding = MBEDTLS_PADDING_NONE;

src/platforms/emscripten/src/lib/sys.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ void sys_init_platform(GlobalContext *glb)
183183
}
184184
#endif
185185

186+
#if MBEDTLS_VERSION_NUMBER < 0x04000000
186187
#ifndef AVM_NO_SMP
187188
platform->entropy_mutex = smp_mutex_create();
188189
if (IS_NULL_PTR(platform->entropy_mutex)) {
@@ -195,6 +196,7 @@ void sys_init_platform(GlobalContext *glb)
195196
#endif
196197
platform->entropy_is_initialized = false;
197198
platform->random_is_initialized = false;
199+
#endif
198200

199201
glb->platform_data = platform;
200202
}
@@ -204,12 +206,14 @@ void sys_free_platform(GlobalContext *glb)
204206
struct EmscriptenPlatformData *platform = glb->platform_data;
205207
pthread_cond_destroy(&platform->poll_cond);
206208
pthread_mutex_destroy(&platform->poll_mutex);
209+
#if MBEDTLS_VERSION_NUMBER < 0x04000000
207210
if (platform->random_is_initialized) {
208211
mbedtls_ctr_drbg_free(&platform->random_ctx);
209212
}
210213
if (platform->entropy_is_initialized) {
211214
mbedtls_entropy_free(&platform->entropy_ctx);
212215
}
216+
#endif
213217
free(platform);
214218
}
215219

src/platforms/generic_unix/lib/sys.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,6 @@ void sys_init_platform(GlobalContext *global)
584584
AVM_ABORT();
585585
}
586586
#else
587-
#if MBEDTLS_VERSION_NUMBER < 0x04000000
588587
#ifndef AVM_NO_SMP
589588
platform->entropy_mutex = smp_mutex_create();
590589
if (IS_NULL_PTR(platform->entropy_mutex)) {
@@ -597,7 +596,6 @@ void sys_init_platform(GlobalContext *global)
597596
#endif
598597
platform->entropy_is_initialized = false;
599598
platform->random_is_initialized = false;
600-
#endif
601599
#endif
602600
otp_ssl_init(global);
603601
#endif

src/platforms/rp2/src/lib/rp2_sys.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@
3030
#include <pico/cond.h>
3131
#include <pico/util/queue.h>
3232

33+
#include <mbedtls/version.h>
34+
#if MBEDTLS_VERSION_NUMBER < 0x04000000
3335
#include <mbedtls/ctr_drbg.h>
3436
#include <mbedtls/entropy.h>
37+
#endif
3538

3639
#pragma GCC diagnostic pop
3740

@@ -82,6 +85,7 @@ struct RP2PlatformData
8285
#endif
8386
queue_t event_queue;
8487

88+
#if MBEDTLS_VERSION_NUMBER < 0x04000000
8589
#ifndef AVM_NO_SMP
8690
Mutex *entropy_mutex;
8791
#endif
@@ -93,6 +97,10 @@ struct RP2PlatformData
9397
#endif
9498
mbedtls_ctr_drbg_context random_ctx;
9599
bool random_is_initialized;
100+
#else
101+
char entropy_ctx;
102+
char random_ctx;
103+
#endif
96104
};
97105

98106
#endif

src/platforms/rp2/src/lib/sys.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ void sys_init_platform(GlobalContext *glb)
9898
}
9999
#endif
100100

101+
#if MBEDTLS_VERSION_NUMBER < 0x04000000
101102
#ifndef AVM_NO_SMP
102103
platform->entropy_mutex = smp_mutex_create();
103104
if (IS_NULL_PTR(platform->entropy_mutex)) {
@@ -111,6 +112,7 @@ void sys_init_platform(GlobalContext *glb)
111112

112113
platform->entropy_is_initialized = false;
113114
platform->random_is_initialized = false;
115+
#endif
114116
}
115117

116118
void sys_free_platform(GlobalContext *glb)
@@ -122,6 +124,7 @@ void sys_free_platform(GlobalContext *glb)
122124
struct RP2PlatformData *platform = glb->platform_data;
123125
queue_free(&platform->event_queue);
124126

127+
#if MBEDTLS_VERSION_NUMBER < 0x04000000
125128
if (platform->random_is_initialized) {
126129
mbedtls_ctr_drbg_free(&platform->random_ctx);
127130
}
@@ -133,6 +136,7 @@ void sys_free_platform(GlobalContext *glb)
133136
#ifndef AVM_NO_SMP
134137
smp_mutex_destroy(platform->entropy_mutex);
135138
smp_mutex_destroy(platform->random_mutex);
139+
#endif
136140
#endif
137141

138142
free(platform);

0 commit comments

Comments
 (0)