test_smp.c 5.0 KB

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