test_smp.c 4.9 KB

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