Skip to content

Commit 1af7cfe

Browse files
author
Mika Leppänen
authored
Updated nanostack to be compatible with mbed TLS 3.0 (ARMmbed#2657)
Updated functions names for SHA256 and MD5, updated export keys function for EAP-TLS key material export, disabled extended Wi-SUN certification field checks (other name, extended key usage) for now, made the SSL state to refer to private state for now. Do not include config, instead include version.h that is present in both 2.0 and 3.0 and will include config and define version macros.
1 parent 29744e0 commit 1af7cfe

File tree

6 files changed

+165
-16
lines changed

6 files changed

+165
-16
lines changed

nanostack/ns_sha256.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,45 +61,77 @@ static inline void ns_sha256_clone(ns_sha256_context *dst,
6161

6262
static inline void ns_sha256_starts(ns_sha256_context *ctx)
6363
{
64+
#if (MBEDTLS_VERSION_MAJOR >= 3)
65+
(void)mbedtls_sha256_starts(ctx, 0);
66+
#else
6467
(void)mbedtls_sha256_starts_ret(ctx, 0);
68+
#endif
6569
}
6670

6771
static inline void ns_sha256_update(ns_sha256_context *ctx, const void *input,
6872
size_t ilen)
6973
{
74+
#if (MBEDTLS_VERSION_MAJOR >= 3)
75+
(void)mbedtls_sha256_update(ctx, input, ilen);
76+
#else
7077
(void)mbedtls_sha256_update_ret(ctx, input, ilen);
78+
#endif
7179
}
7280

7381
static inline void ns_sha256_finish(ns_sha256_context *ctx, void *output)
7482
{
83+
#if (MBEDTLS_VERSION_MAJOR >= 3)
84+
(void)mbedtls_sha256_finish(ctx, output);
85+
#else
7586
(void)mbedtls_sha256_finish_ret(ctx, output);
87+
#endif
7688
}
7789

7890
static inline void ns_sha256(const void *input, size_t ilen, void *output)
7991
{
92+
#if (MBEDTLS_VERSION_MAJOR >= 3)
93+
(void)mbedtls_sha256(input, ilen, output, 0);
94+
#else
8095
(void)mbedtls_sha256_ret(input, ilen, output, 0);
96+
#endif
8197
}
8298

8399
/* Extensions to standard mbed TLS - output the first bits of a hash only */
84100
/* Number of bits must be a multiple of 32, and <=256 */
85101
static inline void ns_sha256_finish_nbits(ns_sha256_context *ctx, void *output, unsigned obits)
86102
{
87103
if (obits == 256) {
104+
#if (MBEDTLS_VERSION_MAJOR >= 3)
105+
(void)mbedtls_sha256_finish(ctx, output);
106+
#else
88107
(void)mbedtls_sha256_finish_ret(ctx, output);
108+
#endif
89109
} else {
90110
uint8_t sha256[32];
111+
#if (MBEDTLS_VERSION_MAJOR >= 3)
112+
(void)mbedtls_sha256_finish(ctx, sha256);
113+
#else
91114
(void)mbedtls_sha256_finish_ret(ctx, sha256);
115+
#endif
92116
memcpy(output, sha256, obits / 8);
93117
}
94118
}
95119

96120
static inline void ns_sha256_nbits(const void *input, size_t ilen, void *output, unsigned obits)
97121
{
98122
if (obits == 256) {
123+
#if (MBEDTLS_VERSION_MAJOR >= 3)
124+
(void)mbedtls_sha256(input, ilen, output, 0);
125+
#else
99126
(void)mbedtls_sha256_ret(input, ilen, output, 0);
127+
#endif
100128
} else {
101129
uint8_t sha256[32];
130+
#if (MBEDTLS_VERSION_MAJOR >= 3)
131+
(void)mbedtls_sha256(input, ilen, sha256, 0);
132+
#else
102133
(void)mbedtls_sha256_ret(input, ilen, sha256, 0);
134+
#endif
103135
memcpy(output, sha256, obits / 8);
104136
}
105137
}

source/6LoWPAN/ws/ws_pae_controller.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,19 +546,31 @@ static int8_t ws_pae_controller_gak_from_gtk(uint8_t *gak, uint8_t *gtk, char *n
546546

547547
mbedtls_sha256_init(&ctx);
548548

549+
#if (MBEDTLS_VERSION_MAJOR >= 3)
550+
if (mbedtls_sha256_starts(&ctx, 0) != 0) {
551+
#else
549552
if (mbedtls_sha256_starts_ret(&ctx, 0) != 0) {
553+
#endif
550554
ret_val = -1;
551555
goto error;
552556
}
553557

558+
#if (MBEDTLS_VERSION_MAJOR >= 3)
559+
if (mbedtls_sha256_update(&ctx, input, network_name_len + GTK_LEN) != 0) {
560+
#else
554561
if (mbedtls_sha256_update_ret(&ctx, input, network_name_len + GTK_LEN) != 0) {
562+
#endif
555563
ret_val = -1;
556564
goto error;
557565
}
558566

559567
uint8_t output[32];
560568

569+
#if (MBEDTLS_VERSION_MAJOR >= 3)
570+
if (mbedtls_sha256_finish(&ctx, output) != 0) {
571+
#else
561572
if (mbedtls_sha256_finish_ret(&ctx, output) != 0) {
573+
#endif
562574
ret_val = -1;
563575
goto error;
564576
}

source/Security/protocols/radius_sec_prot/radius_client_sec_prot.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,19 +786,31 @@ static int8_t radius_client_sec_prot_eui_64_hash_generate(uint8_t *eui_64, uint8
786786

787787
mbedtls_sha256_init(&ctx);
788788

789+
#if (MBEDTLS_VERSION_MAJOR >= 3)
790+
if (mbedtls_sha256_starts(&ctx, 0) != 0) {
791+
#else
789792
if (mbedtls_sha256_starts_ret(&ctx, 0) != 0) {
793+
#endif
790794
ret_val = -1;
791795
goto error;
792796
}
793797

798+
#if (MBEDTLS_VERSION_MAJOR >= 3)
799+
if (mbedtls_sha256_update(&ctx, hashed_string, 24) != 0) {
800+
#else
794801
if (mbedtls_sha256_update_ret(&ctx, hashed_string, 24) != 0) {
802+
#endif
795803
ret_val = -1;
796804
goto error;
797805
}
798806

799807
uint8_t output[32];
800808

809+
#if (MBEDTLS_VERSION_MAJOR >= 3)
810+
if (mbedtls_sha256_finish(&ctx, output) != 0) {
811+
#else
801812
if (mbedtls_sha256_finish_ret(&ctx, output) != 0) {
813+
#endif
802814
ret_val = -1;
803815
goto error;
804816
}
@@ -872,19 +884,35 @@ static int8_t radius_client_sec_prot_response_authenticator_calc(sec_prot_t *pro
872884

873885
mbedtls_md5_init(&ctx);
874886

887+
#if (MBEDTLS_VERSION_MAJOR >= 3)
888+
if (mbedtls_md5_starts(&ctx) != 0) {
889+
#else
875890
if (mbedtls_md5_starts_ret(&ctx) != 0) {
891+
#endif
876892
goto end;
877893
}
878894

895+
#if (MBEDTLS_VERSION_MAJOR >= 3)
896+
if (mbedtls_md5_update(&ctx, msg_ptr, msg_len) != 0) {
897+
#else
879898
if (mbedtls_md5_update_ret(&ctx, msg_ptr, msg_len) != 0) {
899+
#endif
880900
goto end;
881901
}
882902

903+
#if (MBEDTLS_VERSION_MAJOR >= 3)
904+
if (mbedtls_md5_update(&ctx, key, key_len) != 0) {
905+
#else
883906
if (mbedtls_md5_update_ret(&ctx, key, key_len) != 0) {
907+
#endif
884908
goto end;
885909
}
886910

911+
#if (MBEDTLS_VERSION_MAJOR >= 3)
912+
if (mbedtls_md5_finish(&ctx, auth_ptr) != 0) {
913+
#else
887914
if (mbedtls_md5_finish_ret(&ctx, auth_ptr) != 0) {
915+
#endif
888916
goto end;
889917
}
890918

@@ -940,35 +968,59 @@ static int8_t radius_client_sec_prot_ms_mppe_recv_key_pmk_decrypt(sec_prot_t *pr
940968
while (cipher_text_len >= MS_MPPE_RECV_KEY_BLOCK_LEN) {
941969
mbedtls_md5_init(&ctx);
942970

971+
#if (MBEDTLS_VERSION_MAJOR >= 3)
972+
if (mbedtls_md5_starts(&ctx) != 0) {
973+
#else
943974
if (mbedtls_md5_starts_ret(&ctx) != 0) {
975+
#endif
944976
md5_failed = true;
945977
break;
946978
}
947979

980+
#if (MBEDTLS_VERSION_MAJOR >= 3)
981+
if (mbedtls_md5_update(&ctx, key, key_len) != 0) {
982+
#else
948983
if (mbedtls_md5_update_ret(&ctx, key, key_len) != 0) {
984+
#endif
949985
md5_failed = true;
950986
break;
951987
}
952988

953989
if (first_interm_b_value) {
954990
// b(1) = MD5(secret + request-authenticator + salt)
991+
#if (MBEDTLS_VERSION_MAJOR >= 3)
992+
if (mbedtls_md5_update(&ctx, request_authenticator, MS_MPPE_RECV_KEY_BLOCK_LEN) != 0) {
993+
#else
955994
if (mbedtls_md5_update_ret(&ctx, request_authenticator, MS_MPPE_RECV_KEY_BLOCK_LEN) != 0) {
995+
#endif
956996
md5_failed = true;
957997
break;
958998
}
999+
#if (MBEDTLS_VERSION_MAJOR >= 3)
1000+
if (mbedtls_md5_update(&ctx, salt_ptr, MS_MPPE_RECV_KEY_SALT_LEN) != 0) {
1001+
#else
9591002
if (mbedtls_md5_update_ret(&ctx, salt_ptr, MS_MPPE_RECV_KEY_SALT_LEN) != 0) {
1003+
#endif
9601004
md5_failed = true;
9611005
break;
9621006
}
9631007
} else {
9641008
// b(i) = MD5(secret + cipher_text(i - 1))
1009+
#if (MBEDTLS_VERSION_MAJOR >= 3)
1010+
if (mbedtls_md5_update(&ctx, cipher_text_ptr - MS_MPPE_RECV_KEY_BLOCK_LEN, MS_MPPE_RECV_KEY_BLOCK_LEN) != 0) {
1011+
#else
9651012
if (mbedtls_md5_update_ret(&ctx, cipher_text_ptr - MS_MPPE_RECV_KEY_BLOCK_LEN, MS_MPPE_RECV_KEY_BLOCK_LEN) != 0) {
1013+
#endif
9661014
md5_failed = true;
9671015
break;
9681016
}
9691017
}
9701018

1019+
#if (MBEDTLS_VERSION_MAJOR >= 3)
1020+
if (mbedtls_md5_finish(&ctx, interm_b_val) != 0) {
1021+
#else
9711022
if (mbedtls_md5_finish_ret(&ctx, interm_b_val) != 0) {
1023+
#endif
9721024
md5_failed = true;
9731025
break;
9741026
}

source/Security/protocols/sec_prot_lib.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,19 +514,31 @@ int8_t sec_prot_lib_gtkhash_generate(uint8_t *gtk, uint8_t *gtk_hash)
514514

515515
mbedtls_sha256_init(&ctx);
516516

517+
#if (MBEDTLS_VERSION_MAJOR >= 3)
518+
if (mbedtls_sha256_starts(&ctx, 0) != 0) {
519+
#else
517520
if (mbedtls_sha256_starts_ret(&ctx, 0) != 0) {
521+
#endif
518522
ret_val = -1;
519523
goto error;
520524
}
521525

526+
#if (MBEDTLS_VERSION_MAJOR >= 3)
527+
if (mbedtls_sha256_update(&ctx, gtk, 16) != 0) {
528+
#else
522529
if (mbedtls_sha256_update_ret(&ctx, gtk, 16) != 0) {
530+
#endif
523531
ret_val = -1;
524532
goto error;
525533
}
526534

527535
uint8_t output[32];
528536

537+
#if (MBEDTLS_VERSION_MAJOR >= 3)
538+
if (mbedtls_sha256_finish(&ctx, output) != 0) {
539+
#else
529540
if (mbedtls_sha256_finish_ret(&ctx, output) != 0) {
541+
#endif
530542
ret_val = -1;
531543
goto error;
532544
}

0 commit comments

Comments
 (0)