test-crypto.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2011 Intel Corporation
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include "src/shared/crypto.h"
  14. #include "src/shared/util.h"
  15. #include "src/shared/tester.h"
  16. #include <string.h>
  17. #include <glib.h>
  18. static struct bt_crypto *crypto;
  19. static void print_debug(const char *str, void *user_data)
  20. {
  21. tester_debug("%s", str);
  22. }
  23. static void test_h6(gconstpointer data)
  24. {
  25. const uint8_t w[16] = {
  26. 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
  27. 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
  28. const uint8_t m[4] = { 0x72, 0x62, 0x65, 0x6c };
  29. const uint8_t exp[16] = {
  30. 0x99, 0x63, 0xb1, 0x80, 0xe2, 0xa9, 0xd3, 0xe8,
  31. 0x1c, 0xc9, 0x6d, 0xe7, 0x02, 0xe1, 0x9a, 0x2d };
  32. uint8_t res[16];
  33. tester_debug("W:");
  34. util_hexdump(' ', w, 16, print_debug, NULL);
  35. tester_debug("M:");
  36. util_hexdump(' ', m, 4, print_debug, NULL);
  37. if (!bt_crypto_h6(crypto, w, m, res)) {
  38. tester_test_failed();
  39. return;
  40. }
  41. tester_debug("Expected:");
  42. util_hexdump(' ', exp, 16, print_debug, NULL);
  43. tester_debug("Result:");
  44. util_hexdump(' ', res, 16, print_debug, NULL);
  45. if (memcmp(res, exp, 16)) {
  46. tester_test_failed();
  47. return;
  48. }
  49. tester_test_passed();
  50. }
  51. struct test_data {
  52. const uint8_t *msg;
  53. uint16_t msg_len;
  54. const uint8_t *t;
  55. const uint8_t *key;
  56. uint32_t cnt;
  57. };
  58. static const uint8_t key[] = {
  59. 0x3c, 0x4f, 0xcf, 0x09, 0x88, 0x15, 0xf7, 0xab, 0xa6, 0xd2, 0xae, 0x28,
  60. 0x16, 0x15, 0x7e, 0x2b
  61. };
  62. static const uint8_t msg_1[] = { 0x00 };
  63. static const uint8_t t_msg_1[] = {
  64. 0x00, 0x00, 0x00, 0x00, 0xb3, 0xa8, 0x59, 0x41, 0x27, 0xeb, 0xc2, 0xc0
  65. };
  66. static const struct test_data test_data_1 = {
  67. .msg = msg_1,
  68. .msg_len = 0,
  69. .t = t_msg_1,
  70. .key = key,
  71. };
  72. static const uint8_t msg_2[] = {
  73. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11,
  74. 0x73, 0x93, 0x17, 0x2a
  75. };
  76. static const uint8_t t_msg_2[] = {
  77. 0x00, 0x00, 0x00, 0x00, 0x27, 0x39, 0x74, 0xf4, 0x39, 0x2a, 0x23, 0x2a
  78. };
  79. static const struct test_data test_data_2 = {
  80. .msg = msg_2,
  81. .msg_len = 16,
  82. .t = t_msg_2,
  83. .key = key,
  84. };
  85. static const uint8_t msg_3[] = {
  86. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11,
  87. 0x73, 0x93, 0x17, 0x2a, 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  88. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, 0x30, 0xc8, 0x1c, 0x46,
  89. 0xa3, 0x5c, 0xe4, 0x11
  90. };
  91. static const uint8_t t_msg_3[12] = {
  92. 0x00, 0x00, 0x00, 0x00, 0xb7, 0xca, 0x94, 0xab, 0x87, 0xc7, 0x82, 0x18
  93. };
  94. static const struct test_data test_data_3 = {
  95. .msg = msg_3,
  96. .msg_len = 40,
  97. .t = t_msg_3,
  98. .key = key,
  99. };
  100. static const uint8_t msg_4[] = {
  101. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11,
  102. 0x73, 0x93, 0x17, 0x2a, 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
  103. 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, 0x30, 0xc8, 0x1c, 0x46,
  104. 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
  105. 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b,
  106. 0xe6, 0x6c, 0x37, 0x10
  107. };
  108. static const uint8_t t_msg_4[12] = {
  109. 0x00, 0x00, 0x00, 0x00, 0x44, 0xe1, 0xe6, 0xce, 0x1d, 0xf5, 0x13, 0x68
  110. };
  111. static const struct test_data test_data_4 = {
  112. .msg = msg_4,
  113. .msg_len = 64,
  114. .t = t_msg_4,
  115. .key = key,
  116. };
  117. static const uint8_t msg_5[] = {
  118. 0xd2, 0x12, 0x00, 0x13, 0x37
  119. };
  120. static const uint8_t key_5[] = {
  121. 0x50, 0x5E, 0x42, 0xDF, 0x96, 0x91, 0xEC, 0x72, 0xD3, 0x1F,
  122. 0xCD, 0xFB, 0xEB, 0x64, 0x1B, 0x61
  123. };
  124. static const uint8_t t_msg_5[] = {
  125. 0x01, 0x00, 0x00, 0x00, 0xF1, 0x87, 0x1E, 0x93, 0x3C, 0x90,
  126. 0x0F, 0xf2
  127. };
  128. static const struct test_data test_data_5 = {
  129. .msg = msg_5,
  130. .msg_len = sizeof(msg_5),
  131. .t = t_msg_5,
  132. .cnt = 1,
  133. .key = key_5,
  134. };
  135. static bool result_compare(const uint8_t exp[12], uint8_t res[12])
  136. {
  137. int i;
  138. for (i = 0; i < 12; i++)
  139. if (exp[i] != res[i])
  140. return false;
  141. return true;
  142. }
  143. static void test_sign(gconstpointer data)
  144. {
  145. uint8_t t[12];
  146. const struct test_data *d = data;
  147. memset(t, 0, 12);
  148. if (!bt_crypto_sign_att(crypto, d->key, d->msg, d->msg_len, d->cnt, t))
  149. g_assert(true);
  150. tester_debug("Result T:");
  151. util_hexdump(' ', t, 12, print_debug, NULL);
  152. tester_debug("Expected T:");
  153. util_hexdump(' ', d->t, 12, print_debug, NULL);
  154. g_assert(result_compare(d->t, t));
  155. tester_test_passed();
  156. }
  157. static void test_gatt_hash(gconstpointer data)
  158. {
  159. struct iovec iov[7];
  160. const uint8_t m[7][16] = {
  161. /* M0 */
  162. { 0x01, 0x00, 0x00, 0x28, 0x00, 0x18, 0x02, 0x00,
  163. 0x03, 0x28, 0x0A, 0x03, 0x00, 0x00, 0x2A, 0x04 },
  164. /* M1 */
  165. { 0x00, 0x03, 0x28, 0x02, 0x05, 0x00, 0x01, 0x2A,
  166. 0x06, 0x00, 0x00, 0x28, 0x01, 0x18, 0x07, 0x00 },
  167. /* M2 */
  168. { 0x03, 0x28, 0x20, 0x08, 0x00, 0x05, 0x2A, 0x09,
  169. 0x00, 0x02, 0x29, 0x0A, 0x00, 0x03, 0x28, 0x0A },
  170. /* M3 */
  171. { 0x0B, 0x00, 0x29, 0x2B, 0x0C, 0x00, 0x03, 0x28,
  172. 0x02, 0x0D, 0x00, 0x2A, 0x2B, 0x0E, 0x00, 0x00 },
  173. /* M4 */
  174. { 0x28, 0x08, 0x18, 0x0F, 0x00, 0x02, 0x28, 0x14,
  175. 0x00, 0x16, 0x00, 0x0F, 0x18, 0x10, 0x00, 0x03 },
  176. /* M5 */
  177. { 0x28, 0xA2, 0x11, 0x00, 0x18, 0x2A, 0x12, 0x00,
  178. 0x02, 0x29, 0x13, 0x00, 0x00, 0x29, 0x00, 0x00 },
  179. /* M6 */
  180. { 0x14, 0x00, 0x01, 0x28, 0x0F, 0x18, 0x15, 0x00,
  181. 0x03, 0x28, 0x02, 0x16, 0x00, 0x19, 0x2A }
  182. };
  183. const uint8_t exp[16] = {
  184. 0xF1, 0xCA, 0x2D, 0x48, 0xEC, 0xF5, 0x8B, 0xAC,
  185. 0x8A, 0x88, 0x30, 0xBB, 0xB9, 0xFB, 0xA9, 0x90
  186. };
  187. uint8_t res[16];
  188. int i;
  189. for (i = 0; i < 7; i++) {
  190. int len = sizeof(m[i]);
  191. if (i == 6)
  192. len -= 1;
  193. tester_debug("M%u:", i);
  194. util_hexdump(' ', m[i], len, print_debug, NULL);
  195. iov[i].iov_base = (void *) m[i];
  196. iov[i].iov_len = len;
  197. }
  198. if (!bt_crypto_gatt_hash(crypto, iov, 7, res)) {
  199. tester_test_failed();
  200. return;
  201. }
  202. tester_debug("Expected:");
  203. util_hexdump(' ', exp, 16, print_debug, NULL);
  204. tester_debug("Result:");
  205. util_hexdump(' ', res, 16, print_debug, NULL);
  206. if (memcmp(res, exp, 16)) {
  207. tester_test_failed();
  208. return;
  209. }
  210. tester_test_passed();
  211. }
  212. struct verify_sign_test_data {
  213. const uint8_t *msg;
  214. uint16_t msg_len;
  215. const uint8_t *key;
  216. bool match;
  217. };
  218. static const uint8_t msg_to_verify_pass[] = {
  219. 0xd2, 0x12, 0x00, 0x13, 0x37, 0x01, 0x00, 0x00, 0x00, 0xF1, 0x87, 0x1E,
  220. 0x93, 0x3C, 0x90, 0x0F, 0xf2
  221. };
  222. static const struct verify_sign_test_data verify_sign_pass_data = {
  223. .msg = msg_to_verify_pass,
  224. .msg_len = sizeof(msg_to_verify_pass),
  225. .key = key_5,
  226. .match = true,
  227. };
  228. static const uint8_t msg_to_verify_bad_sign[] = {
  229. 0xd2, 0x12, 0x00, 0x13, 0x37, 0x01, 0x00, 0x00, 0x00, 0xF1, 0x87, 0x1E,
  230. 0x93, 0x3C, 0x90, 0x0F, 0xf1
  231. };
  232. static const struct verify_sign_test_data verify_sign_bad_sign_data = {
  233. .msg = msg_to_verify_bad_sign,
  234. .msg_len = sizeof(msg_to_verify_bad_sign),
  235. .key = key_5,
  236. .match = false,
  237. };
  238. static const uint8_t msg_to_verify_too_short[] = {
  239. 0xd2, 0x12, 0x00, 0x13, 0x37
  240. };
  241. static const struct verify_sign_test_data verify_sign_too_short_data = {
  242. .msg = msg_to_verify_too_short,
  243. .msg_len = sizeof(msg_to_verify_too_short),
  244. .key = key_5,
  245. .match = false,
  246. };
  247. static void test_verify_sign(gconstpointer data)
  248. {
  249. const struct verify_sign_test_data *d = data;
  250. bool result = bt_crypto_verify_att_sign(crypto, d->key, d->msg,
  251. d->msg_len);
  252. g_assert(result == d->match);
  253. tester_test_passed();
  254. }
  255. int main(int argc, char *argv[])
  256. {
  257. int exit_status;
  258. crypto = bt_crypto_new();
  259. if (!crypto)
  260. return 0;
  261. tester_init(&argc, &argv);
  262. tester_add("/crypto/h6", NULL, NULL, test_h6, NULL);
  263. tester_add("/crypto/sign_att_1", &test_data_1, NULL, test_sign, NULL);
  264. tester_add("/crypto/sign_att_2", &test_data_2, NULL, test_sign, NULL);
  265. tester_add("/crypto/sign_att_3", &test_data_3, NULL, test_sign, NULL);
  266. tester_add("/crypto/sign_att_4", &test_data_4, NULL, test_sign, NULL);
  267. tester_add("/crypto/sign_att_5", &test_data_5, NULL, test_sign, NULL);
  268. tester_add("/crypto/gatt_hash", NULL, NULL, test_gatt_hash, NULL);
  269. tester_add("/crypto/verify_sign_pass", &verify_sign_pass_data,
  270. NULL, test_verify_sign, NULL);
  271. tester_add("/crypto/verify_sign_bad_sign", &verify_sign_bad_sign_data,
  272. NULL, test_verify_sign, NULL);
  273. tester_add("/crypto/verify_sign_too_short", &verify_sign_too_short_data,
  274. NULL, test_verify_sign, NULL);
  275. exit_status = tester_run();
  276. bt_crypto_unref(crypto);
  277. return exit_status;
  278. }