test_smp.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. /*
  7. Tests for the BLE SMP implementation
  8. */
  9. #include <esp_types.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <malloc.h>
  13. #include <string.h>
  14. #include <string.h>
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/task.h"
  17. #include "freertos/semphr.h"
  18. #include "freertos/queue.h"
  19. #include "freertos/xtensa_api.h"
  20. #include "unity.h"
  21. #include "esp_heap_caps.h"
  22. #include "esp_log.h"
  23. #include "freertos/ringbuf.h"
  24. #include "esp_random.h"
  25. #include "nvs_flash.h"
  26. #include "esp_bt.h"
  27. #include "esp_bt_main.h"
  28. #include "esp_bt_device.h"
  29. #include "esp_gap_ble_api.h"
  30. #define TAG "ble_smp_test"
  31. #define KEY_LENGTH_DWORDS_P256 8
  32. typedef unsigned long DWORD;
  33. typedef uint32_t UINT32;
  34. typedef struct {
  35. DWORD x[KEY_LENGTH_DWORDS_P256];
  36. DWORD y[KEY_LENGTH_DWORDS_P256];
  37. DWORD z[KEY_LENGTH_DWORDS_P256];
  38. } Point;
  39. typedef struct {
  40. // curve's coefficients
  41. DWORD a[KEY_LENGTH_DWORDS_P256];
  42. DWORD b[KEY_LENGTH_DWORDS_P256];
  43. //whether a is -3
  44. int a_minus3;
  45. // prime modulus
  46. DWORD p[KEY_LENGTH_DWORDS_P256];
  47. // Omega, p = 2^m -omega
  48. DWORD omega[KEY_LENGTH_DWORDS_P256];
  49. // base point, a point on E of order r
  50. Point G;
  51. } elliptic_curve_t;
  52. extern void ECC_PointMult_Bin_NAF(Point *q, Point *p, DWORD *n, uint32_t keyLength);
  53. extern bool ECC_CheckPointIsInElliCur_P256(Point *p);
  54. extern void p_256_init_curve(UINT32 keyLength);
  55. extern elliptic_curve_t curve_p256;
  56. static void bt_rand(void *buf, size_t len)
  57. {
  58. if (!len) {
  59. return;
  60. }
  61. // Reset the buf value to the fixed value.
  62. memset(buf, 0x55, len);
  63. for (int i = 0; i < (int)(len / sizeof(uint32_t)); i++) {
  64. uint32_t rand = esp_random();
  65. memcpy(buf + i*sizeof(uint32_t), &rand, sizeof(uint32_t));
  66. }
  67. return;
  68. }
  69. TEST_CASE("ble_smp_public_key_check", "[ble_smp]")
  70. {
  71. /* We wait init finish 200ms here */
  72. vTaskDelay(200 / portTICK_PERIOD_MS);
  73. Point public_key;
  74. DWORD private_key[KEY_LENGTH_DWORDS_P256] = {[0 ... (KEY_LENGTH_DWORDS_P256 - 1)] = 0x12345678};
  75. p_256_init_curve(KEY_LENGTH_DWORDS_P256);
  76. ECC_PointMult_Bin_NAF(&public_key, &(curve_p256.G), private_key, KEY_LENGTH_DWORDS_P256);
  77. /* Check Is the public key generated by the system on the given elliptic curve */
  78. TEST_ASSERT(ECC_CheckPointIsInElliCur_P256(&public_key));
  79. /* We simulate the attacker and set the y coordinate of the public key to 0. */
  80. for (int i = 0; i < KEY_LENGTH_DWORDS_P256; i++) {
  81. public_key.y[i] = 0x0;
  82. }
  83. /* At this point the public key should not be on the given elliptic curve. */
  84. TEST_ASSERT(!ECC_CheckPointIsInElliCur_P256(&public_key));
  85. /* Test whether the G point on the protocol is on a given elliptic curve */
  86. TEST_ASSERT(ECC_CheckPointIsInElliCur_P256(&(curve_p256.G)));
  87. /* test 100 times when the private key is generated by the random number. */
  88. for (int j = 0; j < 100; j++) {
  89. bt_rand(private_key, sizeof(DWORD)*KEY_LENGTH_DWORDS_P256);
  90. ECC_PointMult_Bin_NAF(&public_key, &(curve_p256.G), private_key, KEY_LENGTH_DWORDS_P256);
  91. /* Check Is the public key generated by the system on the given elliptic curve */
  92. TEST_ASSERT(ECC_CheckPointIsInElliCur_P256(&public_key));
  93. }
  94. }
  95. TEST_CASE("ble_smp_set_clear_static_passkey", "[ble_smp]")
  96. {
  97. /* We wait init finish 200ms here */
  98. vTaskDelay(200 / portTICK_PERIOD_MS);
  99. esp_ble_auth_req_t auth_req = ESP_LE_AUTH_BOND;
  100. uint32_t passkey = 123456;
  101. /* test len = 0 when type != ESP_BLE_SM_CLEAR_STATIC_PASSKEY */
  102. TEST_ASSERT(esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, 0) == ESP_ERR_INVALID_ARG);
  103. /* test function */
  104. TEST_ASSERT(esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(esp_ble_auth_req_t)) != ESP_ERR_INVALID_ARG);
  105. /* test type >= ESP_BLE_SM_MAX_PARAM */
  106. TEST_ASSERT(esp_ble_gap_set_security_param(ESP_BLE_SM_MAX_PARAM, &passkey, sizeof(uint32_t)) == ESP_ERR_INVALID_ARG);
  107. /* test len < sizeof(uint32_t) when type is ESP_BLE_SM_SET_STATIC_PASSKEY */
  108. TEST_ASSERT(esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, &passkey, sizeof(uint8_t)) != ESP_ERR_INVALID_ARG);
  109. /* test value is NULL when type != ESP_BLE_SM_CLEAR_STATIC_PASSKEY */
  110. TEST_ASSERT(esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, NULL, sizeof(uint8_t)) == ESP_ERR_INVALID_ARG);
  111. /* test value is NULL and len is 0 when type != ESP_BLE_SM_CLEAR_STATIC_PASSKEY */
  112. TEST_ASSERT(esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, NULL, 0) == ESP_ERR_INVALID_ARG);
  113. /* test function */
  114. TEST_ASSERT(esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, &passkey, sizeof(uint32_t)) != ESP_ERR_INVALID_ARG);
  115. /* test function */
  116. TEST_ASSERT(esp_ble_gap_set_security_param(ESP_BLE_SM_CLEAR_STATIC_PASSKEY, &passkey, sizeof(uint32_t)) != ESP_ERR_INVALID_ARG);
  117. /* test function */
  118. TEST_ASSERT(esp_ble_gap_set_security_param(ESP_BLE_SM_CLEAR_STATIC_PASSKEY, NULL, sizeof(uint32_t)) != ESP_ERR_INVALID_ARG);
  119. /* test function */
  120. TEST_ASSERT(esp_ble_gap_set_security_param(ESP_BLE_SM_CLEAR_STATIC_PASSKEY, NULL, 0) != ESP_ERR_INVALID_ARG);
  121. }