Https://test.mosquitto.org/

I observe incorrect work only when communicating with my server. Incorrect work is to switch to HardFault when issuing a message => write client key exchange. As far as I could step through the steps in the debugger, I determined that the stack was being destroyed along the chain:
ssl_cli.c(2936) → ecdh.c(542) → ecdh.c(514) → ecdh.c(102) → ecdh.c(84) → ecp.c(2372) → ecp. c(1934) → ecp.c(1823) → ecp.c(1237)

Return back from subroutine ecp.c(1237) → ecp.c(1823) → ecp.c(1934) and then HardFault. I also tried to print the passed in data:

		ret = mbedtls_ecdh_make_public( &ssl->handshake->ecdh_ctx,
								&n,
								&ssl->out_msg[i], 1000,
								ssl->conf->f_rng, ssl->conf->p_rng );

Here’s what happened. When working correctly with the server https://test.mosquitto.org/

0--------------olen 7
0b 00 00 03 00 00 00
1---------------n 40004800
2ssl->conf->f_rng 800e263
3ssl->conf->p_rng 20001a98
ssl->handshake->ecdh_ctx:
4------ctx.d.n 0
5------ctx.d.p 0
6------ctx.d.s 1
7------ctx._d.n 0
8------ctx._d.p 0
9------ctx._d.s 1
10-----ctx.Q.X.n 0
11-----ctx.Q.X.p 0
12-----ctx.Q.X.s 1
13-----ctx.Qp.X.n 8
14-----ctx.Qp.X.p 2000bcd0
15-----ctx.Qp.X.s 1
16-----ctx.z.n 0
17-----ctx.z.p 0
18-----ctx.z.s 1
19–ctx.Vi.X.n 0
20–ctx.Vi.X.p 0
21–ctx.Vi.X.p 1
22–ctx.Vf.X.n 0
23–ctx.Vf.X.p 0
24–ctx.Vf.X.s 1
25–ctx.point_format 0
26–ctx.grp.A.n 0
27–ctx.grp.A.p 0
28–ctx.grp.A.s 0
29–ctx.grp.B.n 8
30–ctx.grp.B.p 80251ec
31–ctx.grp.B.s 1
32–ctx.grp.N.n 8
33–ctx.grp.N.p 802524c
34–ctx.grp.N.s 1
35–ctx.grp.P.n 8
36–ctx.grp.P.p 80251cc
37–ctx.grp.P.s 8
39–ctx.grp.G.X.p 802520c
40–ctx.grp.G.X.s 1
42–ctx.grp.G.Y.n 8
42–ctx.grp.G.Y.p 802522c
43–ctx.grp.G.Y.s 1
44–ctx.grp.G.Z.n 1
45–ctx.grp.G.Z. 2000000c
46–ctx.grp.G.Z.s 1
47–ctx.grp.T->X.n 4
48–ctx.grp.T->X.p 8
49–ctx.grp.T->X.s 0
50–ctx.grp.T->Y.n 10
51–ctx.grp.T->Y.p 14
52–ctx.grp.T->Y.s c
53–ctx.grp.T->Z.n 1c
54–ctx.grp.T->Z.p 20
55–ctx.grp.T->Z.s 18
56–ctx.grp.pbits 100
57–ctx.grp.nbits 100
58–ctx.grp.h 1
59–ctx.grp.modp 80098e1
60–ctx.grp.t_pre 0
61–ctx.grp.t_post 0
62–ctx.grp.t_data 0
63–ctx.grp.T_size 0
64–ctx.grp.i 2000ab40

In case of working with my server:

0--------------olen 113
01 00 00 6d 03 03 1c 2d 21 57 92 ef 70 1f 74 b3
bf 58 e3 67 99 5c 5a 9b 5e 53 70 66 e8 f8 5d 89
82 e0 f0 35 43 a8 00 00 08 c0 30 00 9d 00 9c 00
ff 01 00 00 3c 00 0d 00 12 00 10 06 03 06 01 05
03 05 01 04 03 04 01 03 03 03 01 00 0a 00 18 00
16 00 19 00 1c 00 18 00 1b 00 17 00 16 00 1a 00
15 00 14 00 13 00 12 00 0b 00 02 01 00 00 17 00
00
1---------------n 40004800
2ssl->conf->f_rng 800e263
3ssl->conf->p_rng 20001a98
ssl->handshake->ecdh_ctx:
4------ctx.d.n 0
5------ctx.d.p 0
6------ctx.d.s 1
7------ctx._d.n 0
8------ctx._d.p 0
9------ctx._d.s 1
10-----ctx.Q.X.n 0
11-----ctx.Q.X.p 0
12-----ctx.Q.X.s 1
13-----ctx.Qp.X.n 11
14-----ctx.Qp.X.p 2000c340
15-----ctx.Qp.X.s 1
16-----ctx.z.n 0
17-----ctx.z.p 0
18-----ctx.z.s 1
19–ctx.Vi.X.n 0
20–ctx.Vi.X.p 0
21–ctx.Vi.X.p 1
22–ctx.Vf.X.n 0
23–ctx.Vf.X.p 0
24–ctx.Vf.X.s 1
25–ctx.point_format 0
26–ctx.grp.A.n 0
27–ctx.grp.A.p 0
28–ctx.grp.A.s 0
29–ctx.grp.B.n 11
30–ctx.grp.B.p 80253a0
31–ctx.grp.B.s 1
32–ctx.grp.N.n 11
33–ctx.grp.N.p 802546c
34–ctx.grp.N.s 1
35–ctx.grp.P.n 11
36–ctx.grp.P.p 802535c
37–ctx.grp.P.s 11
39–ctx.grp.G.X.p 80253e4
40–ctx.grp.G.X.s 1
42–ctx.grp.G.Y.n 11
42–ctx.grp.G.Y.p 8025428
43–ctx.grp.G.Y.s 1
44–ctx.grp.G.Z.n 1
45–ctx.grp.G.Z. 2000000c
46–ctx.grp.G.Z.s 1
47–ctx.grp.T->X.n 4
48–ctx.grp.T->X.p 8
49–ctx.grp.T->X.s 0
50–ctx.grp.T->Y.n 10
51–ctx.grp.T->Y.p 14
52–ctx.grp.T->Y.s c
53–ctx.grp.T->Z.n 1c
54–ctx.grp.T->Z.p 20
55–ctx.grp.T->Z.s 18
56–ctx.grp.pbits 209
57–ctx.grp.nbits 209
58–ctx.grp.h 1
59–ctx.grp.modp 800a491
60–ctx.grp.t_pre 0
61–ctx.grp.t_post 0
62–ctx.grp.t_data 0
63–ctx.grp.T_size 0
64–ctx.grp.i 2000a8b0

I saw a significant difference only in the ssl->out_msg message. This message in the first case has a length (ssl->out_msglen) = 7, in the case of working with my server, the length is 113 bytes. Why such a big difference?

Code of print:

extern void MyPrintHex(unsigned char* BUF, unsigned int LEN);
printf(“0--------------olen %d\r”, ssl->out_msglen);
MyPrintHex(ssl->out_msg, ssl->out_msglen);
printf(“1---------------n %x\r”, (unsigned int)n);
printf(“2ssl->conf->f_rng %x\r”, (int)ssl->conf->f_rng);
printf(“3ssl->conf->p_rng %x\r”, (int)ssl->conf->p_rng);
printf(“ssl->handshake->ecdh_ctx:\r”);
printf(“4------ctx.d.n %x\r”, (int)ssl->handshake->ecdh_ctx.d.n);
printf(“5------ctx.d.p %x\r”, (int)ssl->handshake->ecdh_ctx.d.p);
printf(“6------ctx.d.s %x\r”, (int)ssl->handshake->ecdh_ctx.d.s);
printf(“7------ctx._d.n %x\r”, (int)ssl->handshake->ecdh_ctx._d.n);
printf(“8------ctx._d.p %x\r”, (int)ssl->handshake->ecdh_ctx._d.p);
printf(“9------ctx._d.s %x\r”, (int)ssl->handshake->ecdh_ctx._d.s);
printf(“10-----ctx.Q.X.n %x\r”, (int)ssl->handshake->ecdh_ctx.Q.X.n);
printf(“11-----ctx.Q.X.p %x\r”, (int)ssl->handshake->ecdh_ctx.Q.X.p);
printf(“12-----ctx.Q.X.s %x\r”, (int)ssl->handshake->ecdh_ctx.Q.X.s);
printf(“13-----ctx.Qp.X.n %x\r”, (int)ssl->handshake->ecdh_ctx.Qp.X.n);
printf(“14-----ctx.Qp.X.p %x\r”, (int)ssl->handshake->ecdh_ctx.Qp.X.p);
printf(“15-----ctx.Qp.X.s %x\r”, (int)ssl->handshake->ecdh_ctx.Qp.X.s);
printf(“16-----ctx.z.n %x\r”, (int)ssl->handshake->ecdh_ctx.z.n);
printf(“17-----ctx.z.p %x\r”, (int)ssl->handshake->ecdh_ctx.z.p);
printf(“18-----ctx.z.s %x\r”, (int)ssl->handshake->ecdh_ctx.z.s);
printf(“19–ctx.Vi.X.n %x\r”, (int)ssl->handshake->ecdh_ctx.Vi.X.n);
printf(“20–ctx.Vi.X.p %x\r”, (int)ssl->handshake->ecdh_ctx.Vi.X.p);
printf(“21–ctx.Vi.X.p %x\r”, (int)ssl->handshake->ecdh_ctx.Vi.X.s);
printf(“22–ctx.Vf.X.n %x\r”, (int)ssl->handshake->ecdh_ctx.Vf.X.n);
printf(“23–ctx.Vf.X.p %x\r”, (int)ssl->handshake->ecdh_ctx.Vf.X.p);
printf(“24–ctx.Vf.X.s %x\r”, (int)ssl->handshake->ecdh_ctx.Vf.X.s);
printf(“25–ctx.point_format %x\r”,(int)ssl->handshake->ecdh_ctx.point_format);
printf(“26–ctx.grp.A.n %x\r”, (int)ssl->handshake->ecdh_ctx.grp.A.n);
printf(“27–ctx.grp.A.p %x\r”, (int)ssl->handshake->ecdh_ctx.grp.A.p);
printf(“28–ctx.grp.A.s %x\r”, (int)ssl->handshake->ecdh_ctx.grp.A.s);
printf(“29–ctx.grp.B.n %x\r”, (int)ssl->handshake->ecdh_ctx.grp.B.n);
printf(“30–ctx.grp.B.p %x\r”, (int)ssl->handshake->ecdh_ctx.grp.B.p);
printf(“31–ctx.grp.B.s %x\r”, (int)ssl->handshake->ecdh_ctx.grp.B.s);
printf(“32–ctx.grp.N.n %x\r”, (int)ssl->handshake->ecdh_ctx.grp.N.n);
printf(“33–ctx.grp.N.p %x\r”, (int)ssl->handshake->ecdh_ctx.grp.N.p);
printf(“34–ctx.grp.N.s %x\r”, (int)ssl->handshake->ecdh_ctx.grp.N.s);
printf(“35–ctx.grp.P.n %x\r”, (int)ssl->handshake->ecdh_ctx.grp.P.n);
printf(“36–ctx.grp.P.p %x\r”, (int)ssl->handshake->ecdh_ctx.grp.P.p);
printf(“37–ctx.grp.P.s %x\r”, (int)ssl->handshake->ecdh_ctx.grp.G.X.n);
printf(“39–ctx.grp.G.X.p %x\r”, (int)ssl->handshake->ecdh_ctx.grp.G.X.p);
printf(“40–ctx.grp.G.X.s %x\r”, (int)ssl->handshake->ecdh_ctx.grp.G.X.s);
printf(“42–ctx.grp.G.Y.n %x\r”, (int)ssl->handshake->ecdh_ctx.grp.G.Y.n);
printf(“42–ctx.grp.G.Y.p %x\r”, (int)ssl->handshake->ecdh_ctx.grp.G.Y.p);
printf(“43–ctx.grp.G.Y.s %x\r”, (int)ssl->handshake->ecdh_ctx.grp.G.Y.s);
printf(“44–ctx.grp.G.Z.n %x\r”, (int)ssl->handshake->ecdh_ctx.grp.G.Z.n);
printf(“45–ctx.grp.G.Z. %x\r”, (int)ssl->handshake->ecdh_ctx.grp.G.Z.p);
printf(“46–ctx.grp.G.Z.s %x\r”, (int)ssl->handshake->ecdh_ctx.grp.G.Z.s);
printf(“47–ctx.grp.T->X.n %x\r”, (int)&ssl->handshake->ecdh_ctx.grp.T->X.n);
printf(“48–ctx.grp.T->X.p %x\r”, (int)&ssl->handshake->ecdh_ctx.grp.T->X.p);
printf(“49–ctx.grp.T->X.s %x\r”, (int)&ssl->handshake->ecdh_ctx.grp.T->X.s);
HAL_Delay(300);
printf(“50–ctx.grp.T->Y.n %x\r”, (int)&ssl->handshake->ecdh_ctx.grp.T->Y.n);
printf(“51–ctx.grp.T->Y.p %x\r”, (int)&ssl->handshake->ecdh_ctx.grp.T->Y.p);
printf(“52–ctx.grp.T->Y.s %x\r”, (int)&ssl->handshake->ecdh_ctx.grp.T->Y.s);
printf(“53–ctx.grp.T->Z.n %x\r”, (int)&ssl->handshake->ecdh_ctx.grp.T->Z.n);
printf(“54–ctx.grp.T->Z.p %x\r”, (int)&ssl->handshake->ecdh_ctx.grp.T->Z.p);
printf(“55–ctx.grp.T->Z.s %x\r”, (int)&ssl->handshake->ecdh_ctx.grp.T->Z.s);
printf(“56–ctx.grp.pbits %x\r”, (int)ssl->handshake->ecdh_ctx.grp.pbits);
printf(“57–ctx.grp.nbits %x\r”, (int)ssl->handshake->ecdh_ctx.grp.nbits);
printf(“58–ctx.grp.h %x\r”, (int)ssl->handshake->ecdh_ctx.grp.h);
printf(“59–ctx.grp.modp %x\r”, (int)ssl->handshake->ecdh_ctx.grp.modp);
printf(“60–ctx.grp.t_pre %x\r”, (int)ssl->handshake->ecdh_ctx.grp.t_pre);
printf(“61–ctx.grp.t_post %x\r”, (int)ssl->handshake->ecdh_ctx.grp.t_post);
printf(“62–ctx.grp.t_data %x\r”, (int)ssl->handshake->ecdh_ctx.grp.t_data);
printf(“63–ctx.grp.T_size %x\r”, (int)ssl->handshake->ecdh_ctx.grp.T_size);
printf(“64–ctx.grp.i %x\r”, (int)&ssl->handshake->ecdh_ctx.grp.id);
HAL_Delay(300);