test_protocomm.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. /*
  2. * SPDX-FileCopyrightText: 2018-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdbool.h>
  9. #include <esp_err.h>
  10. #include <esp_log.h>
  11. #include <esp_system.h>
  12. #include <sys/random.h>
  13. #include <unistd.h>
  14. #include <unity.h>
  15. /* TODO: Currently MBEDTLS_ECDH_LEGACY_CONTEXT is enabled by default
  16. * when MBEDTLS_ECP_RESTARTABLE is enabled.
  17. * This is a temporary workaround to allow that.
  18. *
  19. * The legacy option is soon going to be removed in future mbedtls
  20. * versions and this workaround will be removed once the appropriate
  21. * solution is available.
  22. */
  23. #ifdef CONFIG_MBEDTLS_ECDH_LEGACY_CONTEXT
  24. #define ACCESS_ECDH(S, var) S.MBEDTLS_PRIVATE(var)
  25. #else
  26. #define ACCESS_ECDH(S, var) S.MBEDTLS_PRIVATE(ctx).MBEDTLS_PRIVATE(mbed_ecdh).MBEDTLS_PRIVATE(var)
  27. #endif
  28. #include <mbedtls/aes.h>
  29. #include <mbedtls/sha256.h>
  30. #include <mbedtls/entropy.h>
  31. #include <mbedtls/ctr_drbg.h>
  32. #include <mbedtls/ecdh.h>
  33. #include <mbedtls/error.h>
  34. #include <protocomm.h>
  35. #include <protocomm_security.h>
  36. #include <protocomm_security0.h>
  37. #include <protocomm_security1.h>
  38. #include "test_utils.h"
  39. #include "session.pb-c.h"
  40. #ifdef CONFIG_HEAP_TRACING
  41. #include <esp_heap_trace.h>
  42. #define NUM_RECORDS 100
  43. static heap_trace_record_t trace_record[NUM_RECORDS]; // This buffer must be in internal RAM
  44. #endif
  45. #define PUBLIC_KEY_LEN 32
  46. #define SZ_RANDOM 16
  47. typedef struct {
  48. uint32_t id;
  49. uint8_t sec_ver;
  50. uint8_t weak;
  51. const protocomm_security1_params_t *pop;
  52. uint8_t device_pubkey[PUBLIC_KEY_LEN];
  53. uint8_t client_pubkey[PUBLIC_KEY_LEN];
  54. uint8_t sym_key[PUBLIC_KEY_LEN];
  55. uint8_t rand[SZ_RANDOM];
  56. /* mbedtls context data for Curve25519 */
  57. mbedtls_ecdh_context ctx_client;
  58. mbedtls_entropy_context entropy;
  59. mbedtls_ctr_drbg_context ctr_drbg;
  60. /* mbedtls context data for AES */
  61. mbedtls_aes_context ctx_aes;
  62. unsigned char stb[16];
  63. size_t nc_off;
  64. } session_t;
  65. static const char *TAG = "protocomm_test";
  66. static protocomm_t *test_pc = NULL;
  67. static const protocomm_security_t *test_sec = NULL;
  68. protocomm_security_handle_t sec_inst = NULL;
  69. static uint32_t test_priv_data = 1234;
  70. static void flip_endian(uint8_t *data, size_t len)
  71. {
  72. uint8_t swp_buf;
  73. for (int i = 0; i < len/2; i++) {
  74. swp_buf = data[i];
  75. data[i] = data[len - i - 1];
  76. data[len - i - 1] = swp_buf;
  77. }
  78. }
  79. static void hexdump(const char *msg, uint8_t *buf, int len)
  80. {
  81. ESP_LOGI(TAG, "%s:", msg);
  82. ESP_LOG_BUFFER_HEX(TAG, buf, len);
  83. }
  84. static esp_err_t prepare_command0(session_t *session, SessionData *req)
  85. {
  86. Sec1Payload *in = (Sec1Payload *) malloc(sizeof(Sec1Payload));
  87. if (in == NULL) {
  88. ESP_LOGE(TAG, "Error allocating memory for request");
  89. return ESP_ERR_NO_MEM;
  90. }
  91. SessionCmd0 *in_req = (SessionCmd0 *) malloc(sizeof(SessionCmd0));
  92. if (in_req == NULL) {
  93. ESP_LOGE(TAG, "Error allocating memory for request");
  94. free(in);
  95. return ESP_ERR_NO_MEM;
  96. }
  97. sec1_payload__init(in);
  98. session_cmd0__init(in_req);
  99. in_req->client_pubkey.data = session->client_pubkey;
  100. in_req->client_pubkey.len = PUBLIC_KEY_LEN;
  101. in->msg = SEC1_MSG_TYPE__Session_Command0;
  102. in->payload_case = SEC1_PAYLOAD__PAYLOAD_SC0;
  103. in->sc0 = in_req;
  104. req->proto_case = SESSION_DATA__PROTO_SEC1;
  105. req->sec_ver = protocomm_security1.ver;
  106. req->sec1 = in;
  107. return ESP_OK;
  108. }
  109. static void cleanup_command0(SessionData *req)
  110. {
  111. free(req->sec1->sc0);
  112. free(req->sec1);
  113. }
  114. static esp_err_t verify_response0(session_t *session, SessionData *resp)
  115. {
  116. if ((resp->proto_case != SESSION_DATA__PROTO_SEC1) ||
  117. (resp->sec1->msg != SEC1_MSG_TYPE__Session_Response0)) {
  118. ESP_LOGE(TAG, "Invalid response type");
  119. return ESP_ERR_INVALID_ARG;
  120. }
  121. int ret;
  122. Sec1Payload *in = (Sec1Payload *) resp->sec1;
  123. if (in->sr0->device_pubkey.len != PUBLIC_KEY_LEN) {
  124. ESP_LOGE(TAG, "Device public key length as not as expected");
  125. return ESP_FAIL;
  126. }
  127. if (in->sr0->device_random.len != SZ_RANDOM) {
  128. ESP_LOGE(TAG, "Device random data length is not as expected");
  129. return ESP_FAIL;
  130. }
  131. uint8_t *cli_pubkey = session->client_pubkey;
  132. uint8_t *dev_pubkey = session->device_pubkey;
  133. memcpy(session->device_pubkey, in->sr0->device_pubkey.data, in->sr0->device_pubkey.len);
  134. hexdump("Device pubkey", dev_pubkey, PUBLIC_KEY_LEN);
  135. hexdump("Client pubkey", cli_pubkey, PUBLIC_KEY_LEN);
  136. ret = mbedtls_mpi_lset(ACCESS_ECDH(&session->ctx_client, Qp).MBEDTLS_PRIVATE(Z), 1);
  137. if (ret != 0) {
  138. ESP_LOGE(TAG, "Failed at mbedtls_mpi_lset with error code : %d", ret);
  139. return ESP_FAIL;
  140. }
  141. flip_endian(session->device_pubkey, PUBLIC_KEY_LEN);
  142. ret = mbedtls_mpi_read_binary(ACCESS_ECDH(&session->ctx_client, Qp).MBEDTLS_PRIVATE(X), dev_pubkey, PUBLIC_KEY_LEN);
  143. flip_endian(session->device_pubkey, PUBLIC_KEY_LEN);
  144. if (ret != 0) {
  145. ESP_LOGE(TAG, "Failed at mbedtls_mpi_read_binary with error code : %d", ret);
  146. return ESP_FAIL;
  147. }
  148. ret = mbedtls_ecdh_compute_shared(ACCESS_ECDH(&session->ctx_client, grp),
  149. ACCESS_ECDH(&session->ctx_client, z),
  150. ACCESS_ECDH(&session->ctx_client, Qp),
  151. ACCESS_ECDH(&session->ctx_client, d),
  152. mbedtls_ctr_drbg_random,
  153. &session->ctr_drbg);
  154. if (ret != 0) {
  155. ESP_LOGE(TAG, "Failed at mbedtls_ecdh_compute_shared with error code : %d", ret);
  156. return ESP_FAIL;
  157. }
  158. ret = mbedtls_mpi_write_binary(ACCESS_ECDH(&session->ctx_client, z), session->sym_key, PUBLIC_KEY_LEN);
  159. if (ret != 0) {
  160. ESP_LOGE(TAG, "Failed at mbedtls_mpi_write_binary with error code : %d", ret);
  161. return ESP_FAIL;
  162. }
  163. flip_endian(session->sym_key, PUBLIC_KEY_LEN);
  164. const protocomm_security1_params_t *pop = session->pop;
  165. if (pop != NULL && pop->data != NULL && pop->len != 0) {
  166. ESP_LOGD(TAG, "Adding proof of possession");
  167. uint8_t sha_out[PUBLIC_KEY_LEN];
  168. ret = mbedtls_sha256((const unsigned char *) pop->data, pop->len, sha_out, 0);
  169. if (ret != 0) {
  170. ESP_LOGE(TAG, "Failed at mbedtls_sha256_ret with error code : %d", ret);
  171. return ESP_FAIL;
  172. }
  173. for (int i = 0; i < PUBLIC_KEY_LEN; i++) {
  174. session->sym_key[i] ^= sha_out[i];
  175. }
  176. }
  177. hexdump("Shared key", session->sym_key, PUBLIC_KEY_LEN);
  178. memcpy(session->rand, in->sr0->device_random.data, in->sr0->device_random.len);
  179. hexdump("Dev random", session->rand, sizeof(session->rand));
  180. return ESP_OK;
  181. }
  182. static esp_err_t prepare_command1(session_t *session, SessionData *req)
  183. {
  184. int ret;
  185. uint8_t *outbuf = (uint8_t *) malloc(PUBLIC_KEY_LEN);
  186. if (!outbuf) {
  187. ESP_LOGE(TAG, "Error allocating ciphertext buffer");
  188. return ESP_ERR_NO_MEM;
  189. }
  190. /* Initialise crypto context */
  191. mbedtls_aes_init(&session->ctx_aes);
  192. memset(session->stb, 0, sizeof(session->stb));
  193. session->nc_off = 0;
  194. ret = mbedtls_aes_setkey_enc(&session->ctx_aes, session->sym_key,
  195. sizeof(session->sym_key)*8);
  196. if (ret != 0) {
  197. ESP_LOGE(TAG, "Failed at mbedtls_aes_setkey_enc with error code : %d", ret);
  198. free(outbuf);
  199. return ESP_FAIL;
  200. }
  201. ret = mbedtls_aes_crypt_ctr(&session->ctx_aes, PUBLIC_KEY_LEN,
  202. &session->nc_off, session->rand,
  203. session->stb, session->device_pubkey, outbuf);
  204. if (ret != 0) {
  205. ESP_LOGE(TAG, "Failed at mbedtls_aes_crypt_ctr with error code : %d", ret);
  206. free(outbuf);
  207. return ESP_FAIL;
  208. }
  209. Sec1Payload *out = (Sec1Payload *) malloc(sizeof(Sec1Payload));
  210. if (!out) {
  211. ESP_LOGE(TAG, "Error allocating out buffer");
  212. free(outbuf);
  213. return ESP_ERR_NO_MEM;
  214. }
  215. sec1_payload__init(out);
  216. SessionCmd1 *out_req = (SessionCmd1 *) malloc(sizeof(SessionCmd1));
  217. if (!out_req) {
  218. ESP_LOGE(TAG, "Error allocating out_req buffer");
  219. free(outbuf);
  220. free(out);
  221. return ESP_ERR_NO_MEM;
  222. }
  223. session_cmd1__init(out_req);
  224. out_req->client_verify_data.data = outbuf;
  225. out_req->client_verify_data.len = PUBLIC_KEY_LEN;
  226. hexdump("Client verify data", outbuf, PUBLIC_KEY_LEN);
  227. out->msg = SEC1_MSG_TYPE__Session_Command1;
  228. out->payload_case = SEC1_PAYLOAD__PAYLOAD_SC1;
  229. out->sc1 = out_req;
  230. req->proto_case = SESSION_DATA__PROTO_SEC1;
  231. req->sec_ver = protocomm_security1.ver;
  232. req->sec1 = out;
  233. return ESP_OK;
  234. }
  235. static void cleanup_command1(SessionData *req)
  236. {
  237. free(req->sec1->sc1->client_verify_data.data);
  238. free(req->sec1->sc1);
  239. free(req->sec1);
  240. }
  241. static esp_err_t verify_response1(session_t *session, SessionData *resp)
  242. {
  243. uint8_t *cli_pubkey = session->client_pubkey;
  244. uint8_t *dev_pubkey = session->device_pubkey;
  245. hexdump("Device pubkey", dev_pubkey, PUBLIC_KEY_LEN);
  246. hexdump("Client pubkey", cli_pubkey, PUBLIC_KEY_LEN);
  247. if ((resp->proto_case != SESSION_DATA__PROTO_SEC1) ||
  248. (resp->sec1->msg != SEC1_MSG_TYPE__Session_Response1)) {
  249. ESP_LOGE(TAG, "Invalid response type");
  250. return ESP_ERR_INVALID_ARG;
  251. }
  252. uint8_t check_buf[PUBLIC_KEY_LEN];
  253. Sec1Payload *in = (Sec1Payload *) resp->sec1;
  254. int ret = mbedtls_aes_crypt_ctr(&session->ctx_aes, PUBLIC_KEY_LEN,
  255. &session->nc_off, session->rand, session->stb,
  256. in->sr1->device_verify_data.data, check_buf);
  257. if (ret != 0) {
  258. ESP_LOGE(TAG, "Failed at mbedtls_aes_crypt_ctr with error code : %d", ret);
  259. return ESP_FAIL;
  260. }
  261. hexdump("Dec Device verifier", check_buf, sizeof(check_buf));
  262. if (memcmp(check_buf, session->client_pubkey, sizeof(session->client_pubkey)) != 0) {
  263. ESP_LOGE(TAG, "Key mismatch. Close connection");
  264. return ESP_FAIL;
  265. }
  266. return ESP_OK;
  267. }
  268. static esp_err_t test_new_session(session_t *session)
  269. {
  270. if (session->sec_ver == 0) {
  271. return ESP_OK;
  272. }
  273. if (!test_sec) {
  274. return ESP_ERR_INVALID_STATE;
  275. }
  276. if (test_sec->init && (test_sec->init(&sec_inst) != ESP_OK)) {
  277. return ESP_ERR_NO_MEM;
  278. }
  279. uint32_t session_id = session->id;
  280. if (test_sec->new_transport_session &&
  281. (test_sec->new_transport_session(sec_inst, session_id) != ESP_OK)) {
  282. ESP_LOGE(TAG, "Failed to launch new transport session");
  283. return ESP_FAIL;
  284. }
  285. if (protocomm_open_session(test_pc, session_id) != ESP_OK) {
  286. ESP_LOGE(TAG, "Failed to open new protocomm session");
  287. return ESP_FAIL;
  288. }
  289. return ESP_OK;
  290. }
  291. static esp_err_t test_delete_session(session_t *session)
  292. {
  293. if (!test_sec) {
  294. return ESP_ERR_INVALID_STATE;
  295. }
  296. if (test_sec->cleanup && (test_sec->cleanup(sec_inst) != ESP_OK)) {
  297. return ESP_FAIL;
  298. }
  299. return ESP_OK;
  300. }
  301. static esp_err_t test_sec_endpoint(session_t *session)
  302. {
  303. if (session->sec_ver == 0) {
  304. return ESP_OK;
  305. }
  306. uint32_t session_id = session->id;
  307. int ret = ESP_FAIL;
  308. SessionData req;
  309. SessionData *resp;
  310. ssize_t inlen = 0;
  311. uint8_t *inbuf = NULL;
  312. ssize_t outlen = 0;
  313. uint8_t *outbuf = NULL;
  314. mbedtls_ecdh_init(&session->ctx_client);
  315. mbedtls_ecdh_setup(&session->ctx_client, MBEDTLS_ECP_DP_CURVE25519);
  316. mbedtls_ctr_drbg_init(&session->ctr_drbg);
  317. mbedtls_entropy_init(&session->entropy);
  318. ret = mbedtls_ctr_drbg_seed(&session->ctr_drbg, mbedtls_entropy_func,
  319. &session->entropy, NULL, 0);
  320. if (ret != 0) {
  321. ESP_LOGE(TAG, "Failed at mbedtls_ctr_drbg_seed with error code : %d", ret);
  322. goto abort_test_sec_endpoint;
  323. }
  324. ret = mbedtls_ecp_group_load(ACCESS_ECDH(&session->ctx_client, grp), MBEDTLS_ECP_DP_CURVE25519);
  325. if (ret != 0) {
  326. ESP_LOGE(TAG, "Failed at mbedtls_ecp_group_load with error code : %d", ret);
  327. goto abort_test_sec_endpoint;
  328. }
  329. ret = mbedtls_ecdh_gen_public(ACCESS_ECDH(&session->ctx_client, grp),
  330. ACCESS_ECDH(&session->ctx_client, d),
  331. ACCESS_ECDH(&session->ctx_client, Q),
  332. mbedtls_ctr_drbg_random,
  333. &session->ctr_drbg);
  334. if (ret != 0) {
  335. ESP_LOGE(TAG, "Failed at mbedtls_ecdh_gen_public with error code : %d", ret);
  336. goto abort_test_sec_endpoint;
  337. }
  338. if (session->weak) {
  339. /* Read zero client public key */
  340. ret = mbedtls_mpi_read_binary(ACCESS_ECDH(&session->ctx_client, Q).MBEDTLS_PRIVATE(X),
  341. session->client_pubkey,
  342. PUBLIC_KEY_LEN);
  343. if (ret != 0) {
  344. ESP_LOGE(TAG, "Failed at mbedtls_mpi_read_binary with error code : %d", ret);
  345. goto abort_test_sec_endpoint;
  346. }
  347. }
  348. ret = mbedtls_mpi_write_binary(ACCESS_ECDH(&session->ctx_client, Q).MBEDTLS_PRIVATE(X),
  349. session->client_pubkey,
  350. PUBLIC_KEY_LEN);
  351. if (ret != 0) {
  352. ESP_LOGE(TAG, "Failed at mbedtls_mpi_write_binary with error code : %d", ret);
  353. goto abort_test_sec_endpoint;
  354. }
  355. flip_endian(session->client_pubkey, PUBLIC_KEY_LEN);
  356. /*********** Transaction0 = SessionCmd0 + SessionResp0 ****************/
  357. session_data__init(&req);
  358. if (prepare_command0(session, &req) != ESP_OK) {
  359. ESP_LOGE(TAG, "Failed in prepare_command0");
  360. goto abort_test_sec_endpoint;
  361. }
  362. inlen = session_data__get_packed_size(&req);
  363. inbuf = (uint8_t *) malloc(inlen);
  364. if (!inbuf) {
  365. ESP_LOGE(TAG, "Failed to allocate inbuf");
  366. goto abort_test_sec_endpoint;
  367. }
  368. session_data__pack(&req, inbuf);
  369. cleanup_command0(&req);
  370. outlen = 0;
  371. outbuf = NULL;
  372. ret = protocomm_req_handle(test_pc, "test-sec", session_id,
  373. inbuf, inlen, &outbuf, &outlen);
  374. free(inbuf);
  375. if (ret != ESP_OK) {
  376. ESP_LOGE(TAG, "test-sec handler failed");
  377. free(outbuf);
  378. goto abort_test_sec_endpoint;
  379. }
  380. resp = session_data__unpack(NULL, outlen, outbuf);
  381. free(outbuf);
  382. if (!resp) {
  383. ESP_LOGE(TAG, "Unable to unpack SessionResp0");
  384. goto abort_test_sec_endpoint;
  385. }
  386. if (verify_response0(session, resp) != ESP_OK) {
  387. ESP_LOGE(TAG, "Invalid response 0");
  388. session_data__free_unpacked(resp, NULL);
  389. goto abort_test_sec_endpoint;
  390. }
  391. session_data__free_unpacked(resp, NULL);
  392. /*********** Transaction1 = SessionCmd1 + SessionResp1 ****************/
  393. session_data__init(&req);
  394. if (prepare_command1(session, &req) != ESP_OK) {
  395. ESP_LOGE(TAG, "Failed in prepare_command1");
  396. goto abort_test_sec_endpoint;
  397. }
  398. inlen = session_data__get_packed_size(&req);
  399. inbuf = (uint8_t *) malloc(inlen);
  400. if (!inbuf) {
  401. ESP_LOGE(TAG, "Failed to allocate inbuf");
  402. goto abort_test_sec_endpoint;
  403. }
  404. session_data__pack(&req, inbuf);
  405. cleanup_command1(&req);
  406. outlen = 0;
  407. outbuf = NULL;
  408. ret = protocomm_req_handle(test_pc, "test-sec", session_id,
  409. inbuf, inlen, &outbuf, &outlen);
  410. free(inbuf);
  411. if (ret != ESP_OK) {
  412. ESP_LOGE(TAG, "test-sec handler failed");
  413. free(outbuf);
  414. goto abort_test_sec_endpoint;
  415. }
  416. resp = session_data__unpack(NULL, outlen, outbuf);
  417. free(outbuf);
  418. if (!resp) {
  419. ESP_LOGE(TAG, "Unable to unpack SessionResp0");
  420. goto abort_test_sec_endpoint;
  421. }
  422. if (verify_response1(session, resp) != ESP_OK) {
  423. ESP_LOGE(TAG, "Invalid response 1");
  424. session_data__free_unpacked(resp, NULL);
  425. goto abort_test_sec_endpoint;
  426. }
  427. session_data__free_unpacked(resp, NULL);
  428. mbedtls_ecdh_free(&session->ctx_client);
  429. mbedtls_ctr_drbg_free(&session->ctr_drbg);
  430. mbedtls_entropy_free(&session->entropy);
  431. return ESP_OK;
  432. abort_test_sec_endpoint:
  433. mbedtls_ecdh_free(&session->ctx_client);
  434. mbedtls_ctr_drbg_free(&session->ctr_drbg);
  435. mbedtls_entropy_free(&session->entropy);
  436. return ESP_FAIL;
  437. }
  438. #define TEST_VER_STR "<some version string>"
  439. static esp_err_t test_ver_endpoint(session_t *session)
  440. {
  441. ssize_t ver_data_len = 0;
  442. uint8_t *ver_data = NULL;
  443. esp_err_t ret = protocomm_req_handle(test_pc, "test-ver", session->id,
  444. NULL, 0, &ver_data, &ver_data_len);
  445. if (ret != ESP_OK) {
  446. ESP_LOGE(TAG, "test-ver handler failed");
  447. return ESP_FAIL;
  448. }
  449. if (ver_data_len != strlen(TEST_VER_STR) || memcmp(TEST_VER_STR, ver_data, ver_data_len)) {
  450. ESP_LOGE(TAG, "incorrect response data from test-ver");
  451. free(ver_data);
  452. return ESP_FAIL;
  453. }
  454. free(ver_data);
  455. return ESP_OK;
  456. }
  457. static esp_err_t test_req_endpoint(session_t *session)
  458. {
  459. uint32_t session_id = session->id;
  460. uint8_t rand_test_data[512], enc_test_data[512];
  461. getrandom(rand_test_data, sizeof(rand_test_data), 0);
  462. if (session->sec_ver == 0) {
  463. memcpy(enc_test_data, rand_test_data, sizeof(rand_test_data));
  464. }
  465. else if (session->sec_ver == 1) {
  466. #if !CONFIG_MBEDTLS_HARDWARE_AES
  467. // Check if the AES key is correctly set before calling the software encryption
  468. // API. Without this check, the code will crash, resulting in a test case failure.
  469. // For hardware AES, portability layer takes care of this.
  470. if (session->ctx_aes.MBEDTLS_PRIVATE(nr) > 0) {
  471. #endif
  472. mbedtls_aes_crypt_ctr(&session->ctx_aes, sizeof(rand_test_data), &session->nc_off,
  473. session->rand, session->stb, rand_test_data, enc_test_data);
  474. #if !CONFIG_MBEDTLS_HARDWARE_AES
  475. }
  476. #endif
  477. }
  478. ssize_t verify_data_len = 0;
  479. uint8_t *enc_verify_data = NULL;
  480. esp_err_t ret = protocomm_req_handle(test_pc, "test-ep", session_id,
  481. enc_test_data, sizeof(enc_test_data),
  482. &enc_verify_data, &verify_data_len);
  483. if (ret != ESP_OK || !verify_data_len) {
  484. ESP_LOGE(TAG, "test-ep handler failed");
  485. return ESP_FAIL;
  486. }
  487. uint8_t *verify_data = malloc(verify_data_len);
  488. if (!verify_data) {
  489. ESP_LOGE(TAG, "error allocating memory for decrypted data");
  490. free(enc_verify_data);
  491. return ESP_FAIL;
  492. }
  493. if (session->sec_ver == 0) {
  494. memcpy(verify_data, enc_verify_data, verify_data_len);
  495. }
  496. else if (session->sec_ver == 1) {
  497. mbedtls_aes_crypt_ctr(&session->ctx_aes, verify_data_len, &session->nc_off,
  498. session->rand, session->stb, enc_verify_data, verify_data);
  499. }
  500. free(enc_verify_data);
  501. hexdump("Sent data", rand_test_data, sizeof(rand_test_data));
  502. hexdump("Recv data", verify_data, verify_data_len);
  503. ESP_LOGI(TAG, "verify data len : %d", verify_data_len);
  504. ESP_LOGI(TAG, "expected data len : %d", sizeof(rand_test_data));
  505. if (verify_data_len != sizeof(rand_test_data)) {
  506. ESP_LOGE(TAG, "incorrect response length from test-ep");
  507. free(verify_data);
  508. return ESP_FAIL;
  509. }
  510. if (memcmp(rand_test_data, verify_data, verify_data_len)) {
  511. ESP_LOGE(TAG, "incorrect response data from test-ep");
  512. free(verify_data);
  513. return ESP_FAIL;
  514. }
  515. free(verify_data);
  516. return ESP_OK;
  517. }
  518. esp_err_t test_req_handler (uint32_t session_id,
  519. const uint8_t *inbuf, ssize_t inlen,
  520. uint8_t **outbuf, ssize_t *outlen,
  521. void *priv_data)
  522. {
  523. *outbuf = malloc(inlen);
  524. if (*outbuf) {
  525. *outlen = inlen;
  526. memcpy(*outbuf, inbuf, inlen);
  527. } else {
  528. ESP_LOGE(TAG, "Error allocating response outbuf");
  529. *outbuf = NULL;
  530. *outlen = 0;
  531. }
  532. uint32_t *priv = (uint32_t *) priv_data;
  533. if ((&test_priv_data != priv) || (test_priv_data != *priv)) {
  534. ESP_LOGE(TAG, "Handler private data doesn't match");
  535. return ESP_FAIL;
  536. }
  537. return ESP_OK;
  538. }
  539. static esp_err_t start_test_service(uint8_t sec_ver, const protocomm_security1_params_t *pop)
  540. {
  541. test_pc = protocomm_new();
  542. if (test_pc == NULL) {
  543. ESP_LOGE(TAG, "Failed to create new protocomm instance");
  544. return ESP_FAIL;
  545. }
  546. if (sec_ver == 0) {
  547. if (protocomm_set_security(test_pc, "test-sec", &protocomm_security0, NULL) != ESP_OK) {
  548. ESP_LOGE(TAG, "Failed to set Security0");
  549. return ESP_FAIL;
  550. }
  551. test_sec = &protocomm_security0;
  552. } else if (sec_ver == 1) {
  553. if (protocomm_set_security(test_pc, "test-sec", &protocomm_security1, pop) != ESP_OK) {
  554. ESP_LOGE(TAG, "Failed to set Security1");
  555. return ESP_FAIL;
  556. }
  557. test_sec = &protocomm_security1;
  558. }
  559. if (protocomm_set_version(test_pc, "test-ver", TEST_VER_STR) != ESP_OK) {
  560. ESP_LOGE(TAG, "Failed to set version");
  561. return ESP_FAIL;
  562. }
  563. if (protocomm_add_endpoint(test_pc, "test-ep",
  564. test_req_handler,
  565. (void *) &test_priv_data) != ESP_OK) {
  566. ESP_LOGE(TAG, "Failed to set test-ep endpoint handler");
  567. return ESP_FAIL;
  568. }
  569. return ESP_OK;
  570. }
  571. static void stop_test_service(void)
  572. {
  573. test_sec = NULL;
  574. protocomm_delete(test_pc);
  575. test_pc = NULL;
  576. }
  577. static esp_err_t test_security1_no_encryption (void)
  578. {
  579. ESP_LOGI(TAG, "Starting Security 1 no encryption test");
  580. const char *pop_data = "test pop";
  581. protocomm_security1_params_t pop = {
  582. .data = (const uint8_t *)pop_data,
  583. .len = strlen(pop_data)
  584. };
  585. session_t *session = calloc(1, sizeof(session_t));
  586. if (session == NULL) {
  587. ESP_LOGE(TAG, "Error allocating session");
  588. return ESP_ERR_NO_MEM;
  589. }
  590. session->id = 1;
  591. session->sec_ver = 1;
  592. session->pop = &pop;
  593. // Start protocomm service
  594. if (start_test_service(1, &pop) != ESP_OK) {
  595. ESP_LOGE(TAG, "Error starting test");
  596. free(session);
  597. return ESP_ERR_INVALID_STATE;
  598. }
  599. // Intialise protocomm session with zero public keys
  600. if (test_new_session(session) != ESP_OK) {
  601. ESP_LOGE(TAG, "Error creating new session");
  602. stop_test_service();
  603. free(session);
  604. return ESP_FAIL;
  605. }
  606. // Perform 25519 security handshake to set public keys
  607. if (test_sec_endpoint(session) != ESP_OK) {
  608. ESP_LOGE(TAG, "Error testing security endpoint");
  609. test_delete_session(session);
  610. stop_test_service();
  611. free(session);
  612. return ESP_FAIL;
  613. }
  614. // Force endpoint with un-encrypted data
  615. session->sec_ver = 0;
  616. // Send unencrypted request data to echo endpoint.
  617. // Response would be encrypted causing echoed back
  618. // data to not match that which was sent, hence failing.
  619. if (test_req_endpoint(session) == ESP_OK) {
  620. ESP_LOGE(TAG, "Error testing request endpoint");
  621. session->sec_ver = 1;
  622. test_delete_session(session);
  623. stop_test_service();
  624. free(session);
  625. return ESP_FAIL;
  626. }
  627. session->sec_ver = 1;
  628. test_delete_session(session);
  629. stop_test_service();
  630. free(session);
  631. ESP_LOGI(TAG, "Protocomm test successful");
  632. return ESP_OK;
  633. }
  634. static esp_err_t test_security1_session_overflow (void)
  635. {
  636. ESP_LOGI(TAG, "Starting Security 1 session overflow test");
  637. const char *pop_data = "test pop";
  638. protocomm_security1_params_t pop = {
  639. .data = (const uint8_t *)pop_data,
  640. .len = strlen(pop_data)
  641. };
  642. session_t *session1 = calloc(1, sizeof(session_t));
  643. if (session1 == NULL) {
  644. ESP_LOGE(TAG, "Error allocating session");
  645. return ESP_ERR_NO_MEM;
  646. }
  647. session1->id = 2;
  648. session1->sec_ver = 1;
  649. session1->pop = &pop;
  650. session_t *session2 = calloc(1, sizeof(session_t));
  651. if (session2 == NULL) {
  652. ESP_LOGE(TAG, "Error allocating session");
  653. free(session1);
  654. return ESP_ERR_NO_MEM;
  655. }
  656. session2->id = 3;
  657. session2->sec_ver = 1;
  658. session2->pop = NULL;
  659. // Start protocomm service
  660. if (start_test_service(1, &pop) != ESP_OK) {
  661. ESP_LOGE(TAG, "Error starting test");
  662. free(session1);
  663. free(session2);
  664. return ESP_FAIL;
  665. }
  666. // Intialise protocomm session with zero public keys
  667. if (test_new_session(session1) != ESP_OK) {
  668. ESP_LOGE(TAG, "Error creating new session");
  669. stop_test_service();
  670. free(session1);
  671. free(session2);
  672. return ESP_FAIL;
  673. }
  674. // Perform 25519 security handshake to set public keys
  675. if (test_sec_endpoint(session1) != ESP_OK) {
  676. ESP_LOGE(TAG, "Error testing security endpoint");
  677. test_delete_session(session1);
  678. stop_test_service();
  679. free(session1);
  680. free(session2);
  681. return ESP_FAIL;
  682. }
  683. // Try to perform security handshake again with different
  684. // session ID without registering new session, hence failing
  685. if (test_sec_endpoint(session2) == ESP_OK) {
  686. ESP_LOGE(TAG, "Error testing security endpoint");
  687. test_delete_session(session1);
  688. stop_test_service();
  689. free(session1);
  690. free(session2);
  691. return ESP_FAIL;
  692. }
  693. test_delete_session(session1);
  694. stop_test_service();
  695. free(session1);
  696. free(session2);
  697. ESP_LOGI(TAG, "Protocomm test successful");
  698. return ESP_OK;
  699. }
  700. static esp_err_t test_security1_wrong_pop (void)
  701. {
  702. ESP_LOGI(TAG, "Starting Security 1 wrong auth test");
  703. const char *pop_data = "test pop";
  704. protocomm_security1_params_t pop = {
  705. .data = (const uint8_t *)pop_data,
  706. .len = strlen(pop_data)
  707. };
  708. session_t *session = calloc(1, sizeof(session_t));
  709. if (session == NULL) {
  710. ESP_LOGE(TAG, "Error allocating session");
  711. return ESP_ERR_NO_MEM;
  712. }
  713. session->id = 4;
  714. session->sec_ver = 1;
  715. session->pop = &pop;
  716. // Start protocomm service
  717. if (start_test_service(1, &pop) != ESP_OK) {
  718. ESP_LOGE(TAG, "Error starting test");
  719. free(session);
  720. return ESP_FAIL;
  721. }
  722. // Intialise protocomm session with zero public keys
  723. if (test_new_session(session) != ESP_OK) {
  724. ESP_LOGE(TAG, "Error creating new session");
  725. stop_test_service();
  726. free(session);
  727. return ESP_FAIL;
  728. }
  729. const char *wrong_pop_data = "wrong pop";
  730. protocomm_security1_params_t wrong_pop = {
  731. .data = (const uint8_t *)wrong_pop_data,
  732. .len = strlen(wrong_pop_data)
  733. };
  734. // Force wrong pop during authentication
  735. session->pop = &wrong_pop;
  736. // Perform 25519 security handshake with
  737. // wrong pop, hence failing
  738. if (test_sec_endpoint(session) == ESP_OK) {
  739. ESP_LOGE(TAG, "Error testing security endpoint");
  740. test_delete_session(session);
  741. stop_test_service();
  742. free(session);
  743. return ESP_FAIL;
  744. }
  745. test_delete_session(session);
  746. stop_test_service();
  747. free(session);
  748. ESP_LOGI(TAG, "Protocomm test successful");
  749. return ESP_OK;
  750. }
  751. static esp_err_t test_security1_insecure_client (void)
  752. {
  753. ESP_LOGI(TAG, "Starting Security 1 insecure client test");
  754. const char *pop_data = "test pop";
  755. protocomm_security1_params_t pop = {
  756. .data = (const uint8_t *)pop_data,
  757. .len = strlen(pop_data)
  758. };
  759. session_t *session = calloc(1, sizeof(session_t));
  760. if (session == NULL) {
  761. ESP_LOGE(TAG, "Error allocating session");
  762. return ESP_ERR_NO_MEM;
  763. }
  764. session->id = 5;
  765. session->sec_ver = 1;
  766. session->pop = &pop;
  767. // Start protocomm service
  768. if (start_test_service(1, &pop) != ESP_OK) {
  769. ESP_LOGE(TAG, "Error starting test");
  770. free(session);
  771. return ESP_FAIL;
  772. }
  773. // Perform 25519 security handshake without
  774. // initialising session, hence failing
  775. if (test_sec_endpoint(session) == ESP_OK) {
  776. ESP_LOGE(TAG, "Error testing security endpoint");
  777. stop_test_service();
  778. free(session);
  779. return ESP_FAIL;
  780. }
  781. // Communicating with request endpoint without
  782. // initialising session, hence failing
  783. if (test_req_endpoint(session) == ESP_OK) {
  784. ESP_LOGE(TAG, "Error testing request endpoint");
  785. stop_test_service();
  786. free(session);
  787. return ESP_FAIL;
  788. }
  789. stop_test_service();
  790. free(session);
  791. ESP_LOGI(TAG, "Protocomm test successful");
  792. return ESP_OK;
  793. }
  794. static esp_err_t test_security1_weak_session (void)
  795. {
  796. ESP_LOGI(TAG, "Starting Security 1 weak session test");
  797. const char *pop_data = "test pop";
  798. protocomm_security1_params_t pop = {
  799. .data = (const uint8_t *)pop_data,
  800. .len = strlen(pop_data)
  801. };
  802. session_t *session = calloc(1, sizeof(session_t));
  803. if (session == NULL) {
  804. ESP_LOGE(TAG, "Error allocating session");
  805. return ESP_ERR_NO_MEM;
  806. }
  807. session->id = 6;
  808. session->sec_ver = 1;
  809. session->pop = &pop;
  810. session->weak = 1;
  811. // Start protocomm service
  812. if (start_test_service(1, &pop) != ESP_OK) {
  813. ESP_LOGE(TAG, "Error starting test");
  814. free(session);
  815. return ESP_FAIL;
  816. }
  817. // Intialise protocomm session with zero public keys
  818. if (test_new_session(session) != ESP_OK) {
  819. ESP_LOGE(TAG, "Error creating new session");
  820. stop_test_service();
  821. free(session);
  822. return ESP_FAIL;
  823. }
  824. // Perform 25519 security handshake with weak (zero)
  825. // client public key, hence failing
  826. if (test_sec_endpoint(session) == ESP_OK) {
  827. ESP_LOGE(TAG, "Error testing security endpoint");
  828. test_delete_session(session);
  829. stop_test_service();
  830. free(session);
  831. return ESP_FAIL;
  832. }
  833. // Sending request data to echo endpoint encrypted with zero
  834. // public keys on both client and server side should fail
  835. if (test_req_endpoint(session) == ESP_OK) {
  836. ESP_LOGE(TAG, "Error testing request endpoint");
  837. test_delete_session(session);
  838. stop_test_service();
  839. free(session);
  840. return ESP_FAIL;
  841. }
  842. test_delete_session(session);
  843. stop_test_service();
  844. free(session);
  845. ESP_LOGI(TAG, "Protocomm test successful");
  846. return ESP_OK;
  847. }
  848. static esp_err_t test_protocomm (session_t *session)
  849. {
  850. ESP_LOGI(TAG, "Starting Protocomm test");
  851. // Start protocomm service
  852. if (start_test_service(session->sec_ver, session->pop) != ESP_OK) {
  853. ESP_LOGE(TAG, "Error starting test");
  854. return ESP_FAIL;
  855. }
  856. // Check version endpoint
  857. if (test_ver_endpoint(session) != ESP_OK) {
  858. ESP_LOGE(TAG, "Error testing version endpoint");
  859. stop_test_service();
  860. return ESP_FAIL;
  861. }
  862. // Intialise protocomm session with zero public keys
  863. if (test_new_session(session) != ESP_OK) {
  864. ESP_LOGE(TAG, "Error creating new session");
  865. stop_test_service();
  866. return ESP_FAIL;
  867. }
  868. // Perform 25519 security handshake to set public keys
  869. if (test_sec_endpoint(session) != ESP_OK) {
  870. ESP_LOGE(TAG, "Error testing security endpoint");
  871. test_delete_session(session);
  872. stop_test_service();
  873. return ESP_FAIL;
  874. }
  875. // Send request data to echo endpoint encrypted with
  876. // the set public keys on both client and server side
  877. if (test_req_endpoint(session) != ESP_OK) {
  878. ESP_LOGE(TAG, "Error testing request endpoint");
  879. test_delete_session(session);
  880. stop_test_service();
  881. return ESP_FAIL;
  882. }
  883. // Stop protocomm service
  884. test_delete_session(session);
  885. stop_test_service();
  886. ESP_LOGI(TAG, "Protocomm test successful");
  887. return ESP_OK;
  888. }
  889. static esp_err_t test_security1 (void)
  890. {
  891. ESP_LOGI(TAG, "Starting Sec1 test");
  892. const char *pop_data = "test pop";
  893. protocomm_security1_params_t pop = {
  894. .data = (const uint8_t *)pop_data,
  895. .len = strlen(pop_data)
  896. };
  897. session_t *session = calloc(1, sizeof(session_t));
  898. if (session == NULL) {
  899. ESP_LOGE(TAG, "Error allocating session");
  900. return ESP_ERR_NO_MEM;
  901. }
  902. session->id = 7;
  903. session->sec_ver = 1;
  904. session->pop = &pop;
  905. if (test_protocomm (session) != ESP_OK) {
  906. ESP_LOGE(TAG, "Sec1 test failed");
  907. free(session);
  908. return ESP_FAIL;
  909. }
  910. ESP_LOGI(TAG, "Sec1 test successful");
  911. free(session);
  912. return ESP_OK;
  913. }
  914. static esp_err_t test_security0 (void)
  915. {
  916. ESP_LOGI(TAG, "Starting Sec0 test");
  917. session_t *session = calloc(1, sizeof(session_t));
  918. if (session == NULL) {
  919. ESP_LOGE(TAG, "Error allocating session");
  920. return ESP_ERR_NO_MEM;
  921. }
  922. session->id = 8;
  923. session->sec_ver = 0;
  924. session->pop = NULL;
  925. if (test_protocomm (session) != ESP_OK) {
  926. ESP_LOGE(TAG, "Sec0 test failed");
  927. free(session);
  928. return ESP_FAIL;
  929. }
  930. ESP_LOGI(TAG, "Sec0 test successful");
  931. free(session);
  932. return ESP_OK;
  933. }
  934. TEST_CASE("leak test", "[PROTOCOMM]")
  935. {
  936. #ifdef CONFIG_HEAP_TRACING
  937. heap_trace_init_standalone(trace_record, NUM_RECORDS);
  938. heap_trace_start(HEAP_TRACE_LEAKS);
  939. #endif
  940. /* Run basic tests for the first time to allow for internal long
  941. * time allocations to happen (not related to protocomm) */
  942. test_security0();
  943. test_security1();
  944. usleep(1000);
  945. #ifdef CONFIG_HEAP_TRACING
  946. heap_trace_stop();
  947. heap_trace_dump();
  948. #endif
  949. /* Run all tests passively. Any leaks due
  950. * to protocomm should show up now */
  951. unsigned pre_start_mem = esp_get_free_heap_size();
  952. test_security0();
  953. test_security1();
  954. test_security1_no_encryption();
  955. test_security1_session_overflow();
  956. test_security1_wrong_pop();
  957. test_security1_insecure_client();
  958. test_security1_weak_session();
  959. usleep(1000);
  960. unsigned post_stop_mem = esp_get_free_heap_size();
  961. if (pre_start_mem != post_stop_mem) {
  962. ESP_LOGE(TAG, "Mismatch in free heap size : %d bytes", post_stop_mem - pre_start_mem);
  963. }
  964. TEST_ASSERT(pre_start_mem == post_stop_mem);
  965. }
  966. TEST_CASE("security 0 basic test", "[PROTOCOMM]")
  967. {
  968. TEST_ASSERT(test_security0() == ESP_OK);
  969. }
  970. TEST_CASE("security 1 basic test", "[PROTOCOMM]")
  971. {
  972. TEST_ASSERT(test_security1() == ESP_OK);
  973. }
  974. TEST_CASE("security 1 no encryption test", "[PROTOCOMM]")
  975. {
  976. TEST_ASSERT(test_security1_no_encryption() == ESP_OK);
  977. }
  978. TEST_CASE("security 1 session overflow test", "[PROTOCOMM]")
  979. {
  980. TEST_ASSERT(test_security1_session_overflow() == ESP_OK);
  981. }
  982. TEST_CASE("security 1 wrong pop test", "[PROTOCOMM]")
  983. {
  984. TEST_ASSERT(test_security1_wrong_pop() == ESP_OK);
  985. }
  986. TEST_CASE("security 1 insecure client test", "[PROTOCOMM]")
  987. {
  988. TEST_ASSERT(test_security1_insecure_client() == ESP_OK);
  989. }
  990. TEST_CASE("security 1 weak session test", "[PROTOCOMM]")
  991. {
  992. TEST_ASSERT(test_security1_weak_session() == ESP_OK);
  993. }
  994. void app_main(void)
  995. {
  996. unity_run_menu();
  997. }