test_ds.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "unity.h"
  14. #include "esp32c3/rom/efuse.h"
  15. #include "esp32c3/rom/digital_signature.h"
  16. #include "esp32c3/rom/hmac.h"
  17. #include <string.h>
  18. #include "esp_ds.h"
  19. #define NUM_RESULTS 10
  20. #define DS_MAX_BITS (ETS_DS_MAX_BITS)
  21. typedef struct {
  22. uint8_t iv[ETS_DS_IV_LEN];
  23. ets_ds_p_data_t p_data;
  24. uint8_t expected_c[ETS_DS_C_LEN];
  25. uint8_t hmac_key_idx;
  26. uint32_t expected_results[NUM_RESULTS][DS_MAX_BITS/32];
  27. } encrypt_testcase_t;
  28. // Generated header (components/esp32s2/test/gen_digital_signature_tests.py) defines
  29. // NUM_HMAC_KEYS, test_hmac_keys, NUM_MESSAGES, NUM_CASES, test_messages[], test_cases[]
  30. #include "digital_signature_test_cases.h"
  31. _Static_assert(NUM_RESULTS == NUM_MESSAGES, "expected_results size should be the same as NUM_MESSAGES in generated header");
  32. TEST_CASE("Digital Signature Parameter Encryption data NULL", "[hw_crypto] [ds]")
  33. {
  34. const char iv [32];
  35. esp_ds_p_data_t p_data;
  36. const char key [32];
  37. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_encrypt_params(NULL, iv, &p_data, key));
  38. }
  39. TEST_CASE("Digital Signature Parameter Encryption iv NULL", "[hw_crypto] [ds]")
  40. {
  41. esp_ds_data_t data;
  42. esp_ds_p_data_t p_data;
  43. const char key [32];
  44. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_encrypt_params(&data, NULL, &p_data, key));
  45. }
  46. TEST_CASE("Digital Signature Parameter Encryption p_data NULL", "[hw_crypto] [ds]")
  47. {
  48. esp_ds_data_t data;
  49. const char iv [32];
  50. const char key [32];
  51. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_encrypt_params(&data, iv, NULL, key));
  52. }
  53. TEST_CASE("Digital Signature Parameter Encryption key NULL", "[hw_crypto] [ds]")
  54. {
  55. esp_ds_data_t data;
  56. const char iv [32];
  57. esp_ds_p_data_t p_data;
  58. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_encrypt_params(&data, iv, &p_data, NULL));
  59. }
  60. TEST_CASE("Digital Signature Parameter Encryption", "[hw_crypto] [ds]")
  61. {
  62. for (int i = 0; i < NUM_CASES; i++) {
  63. printf("Encrypting test case %d...\n", i);
  64. const encrypt_testcase_t *t = &test_cases[i];
  65. esp_ds_data_t result = { };
  66. esp_ds_p_data_t p_data;
  67. memcpy(p_data.Y, t->p_data.Y, ESP_DS_SIGNATURE_MAX_BIT_LEN/8);
  68. memcpy(p_data.M, t->p_data.M, ESP_DS_SIGNATURE_MAX_BIT_LEN/8);
  69. memcpy(p_data.Rb, t->p_data.Rb, ESP_DS_SIGNATURE_MAX_BIT_LEN/8);
  70. p_data.M_prime = t->p_data.M_prime;
  71. p_data.length = t->p_data.length;
  72. esp_err_t r = esp_ds_encrypt_params(&result, t->iv, &p_data,
  73. test_hmac_keys[t->hmac_key_idx]);
  74. printf("Encrypting test case %d done\n", i);
  75. TEST_ASSERT_EQUAL(ESP_OK, r);
  76. TEST_ASSERT_EQUAL(t->p_data.length, result.rsa_length);
  77. TEST_ASSERT_EQUAL_HEX8_ARRAY(t->iv, result.iv, ETS_DS_IV_LEN);
  78. TEST_ASSERT_EQUAL_HEX8_ARRAY(t->expected_c, result.c, ETS_DS_C_LEN);
  79. }
  80. }
  81. TEST_CASE("Digital Signature start Invalid message", "[hw_crypto] [ds]")
  82. {
  83. esp_ds_data_t ds_data = { };
  84. ds_data.rsa_length = ESP_DS_RSA_3072;
  85. esp_ds_context_t *ctx;
  86. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_start_sign(NULL, &ds_data, HMAC_KEY1, &ctx));
  87. }
  88. TEST_CASE("Digital Signature start Invalid data", "[hw_crypto] [ds]")
  89. {
  90. const char *message = "test";
  91. esp_ds_context_t *ctx;
  92. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_start_sign(message, NULL, HMAC_KEY1, &ctx));
  93. }
  94. TEST_CASE("Digital Signature start Invalid context", "[hw_crypto] [ds]")
  95. {
  96. esp_ds_data_t ds_data = {};
  97. ds_data.rsa_length = ESP_DS_RSA_3072;
  98. const char *message = "test";
  99. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_start_sign(message, &ds_data, HMAC_KEY1, NULL));
  100. }
  101. TEST_CASE("Digital Signature RSA length 0", "[hw_crypto] [ds]")
  102. {
  103. esp_ds_data_t ds_data = {};
  104. ds_data.rsa_length = 0;
  105. const char *message = "test";
  106. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_start_sign(message, &ds_data, HMAC_KEY1, NULL));
  107. }
  108. TEST_CASE("Digital Signature RSA length too long", "[hw_crypto] [ds]")
  109. {
  110. esp_ds_data_t ds_data = {};
  111. ds_data.rsa_length = 128;
  112. const char *message = "test";
  113. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_start_sign(message, &ds_data, HMAC_KEY1, NULL));
  114. }
  115. TEST_CASE("Digital Signature start HMAC key out of range", "[hw_crypto] [ds]")
  116. {
  117. esp_ds_data_t ds_data = {};
  118. ds_data.rsa_length = ESP_DS_RSA_3072;
  119. esp_ds_context_t *ctx;
  120. const char *message = "test";
  121. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_start_sign(message, &ds_data, HMAC_KEY5 + 1, &ctx));
  122. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_start_sign(message, &ds_data, HMAC_KEY0 - 1, &ctx));
  123. }
  124. TEST_CASE("Digital Signature finish Invalid signature ptr", "[hw_crypto] [ds]")
  125. {
  126. esp_ds_context_t *ctx = NULL;
  127. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_finish_sign(NULL, ctx));
  128. }
  129. TEST_CASE("Digital Signature finish Invalid context", "[hw_crypto] [ds]")
  130. {
  131. uint8_t signature_data [128 * 4];
  132. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_finish_sign(signature_data, NULL));
  133. }
  134. TEST_CASE("Digital Signature Blocking Invalid message", "[hw_crypto] [ds]")
  135. {
  136. esp_ds_data_t ds_data = { };
  137. ds_data.rsa_length = ESP_DS_RSA_3072;
  138. uint8_t signature_data [128 * 4];
  139. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_sign(NULL, &ds_data, HMAC_KEY1, signature_data));
  140. }
  141. TEST_CASE("Digital Signature Blocking Invalid data", "[hw_crypto] [ds]")
  142. {
  143. const char *message = "test";
  144. uint8_t signature_data [128 * 4];
  145. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_sign(message, NULL, HMAC_KEY1, signature_data));
  146. }
  147. TEST_CASE("Digital Signature Blocking Invalid signature ptr", "[hw_crypto] [ds]")
  148. {
  149. esp_ds_data_t ds_data = {};
  150. ds_data.rsa_length = ESP_DS_RSA_3072;
  151. const char *message = "test";
  152. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_sign(message, &ds_data, HMAC_KEY1, NULL));
  153. }
  154. TEST_CASE("Digital Signature Blocking RSA length 0", "[hw_crypto] [ds]")
  155. {
  156. esp_ds_data_t ds_data = {};
  157. ds_data.rsa_length = 0;
  158. const char *message = "test";
  159. uint8_t signature_data [128 * 4];
  160. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_sign(message, &ds_data, HMAC_KEY1, signature_data));
  161. }
  162. TEST_CASE("Digital Signature Blocking RSA length too long", "[hw_crypto] [ds]")
  163. {
  164. esp_ds_data_t ds_data = {};
  165. ds_data.rsa_length = 128;
  166. const char *message = "test";
  167. uint8_t signature_data [128 * 4];
  168. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_sign(message, &ds_data, HMAC_KEY1, signature_data));
  169. }
  170. TEST_CASE("Digital Signature Blocking HMAC key out of range", "[hw_crypto] [ds]")
  171. {
  172. esp_ds_data_t ds_data = {};
  173. ds_data.rsa_length = 127;
  174. const char *message = "test";
  175. uint8_t signature_data [128 * 4];
  176. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_sign(message, &ds_data, HMAC_KEY5 + 1, signature_data));
  177. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_sign(message, &ds_data, HMAC_KEY0 - 1, signature_data));
  178. }
  179. #if CONFIG_IDF_ENV_FPGA
  180. // Burn eFuse blocks 1, 2 and 3. Block 0 is used for HMAC tests already.
  181. static void burn_hmac_keys(void)
  182. {
  183. printf("Burning %d HMAC keys to efuse...\n", NUM_HMAC_KEYS);
  184. for (int i = 0; i < NUM_HMAC_KEYS; i++) {
  185. // TODO: vary the purpose across the keys
  186. ets_efuse_purpose_t purpose = ETS_EFUSE_KEY_PURPOSE_HMAC_DOWN_DIGITAL_SIGNATURE;
  187. ets_efuse_write_key(ETS_EFUSE_BLOCK_KEY1 + i,
  188. purpose,
  189. test_hmac_keys[i], 32);
  190. }
  191. /* verify the keys are what we expect (possibly they're already burned, doesn't matter but they have to match) */
  192. uint8_t block_compare[32];
  193. for (int i = 0; i < NUM_HMAC_KEYS; i++) {
  194. printf("Checking key %d...\n", i);
  195. memcpy(block_compare, (void *)ets_efuse_get_read_register_address(ETS_EFUSE_BLOCK_KEY1 + i), 32);
  196. TEST_ASSERT_EQUAL_HEX8_ARRAY(test_hmac_keys[i], block_compare, 32);
  197. }
  198. }
  199. // This test uses the HMAC_KEY0 eFuse key which hasn't been burned by burn_hmac_keys().
  200. // HMAC_KEY0 is usually used for HMAC upstream (user access) tests.
  201. TEST_CASE("Digital Signature wrong HMAC key purpose (FPGA only)", "[hw_crypto] [ds]")
  202. {
  203. esp_ds_data_t ds_data = {};
  204. ds_data.rsa_length = ESP_DS_RSA_3072;
  205. esp_ds_context_t *ctx;
  206. const char *message = "test";
  207. // HMAC fails in that case because it checks for the correct purpose
  208. TEST_ASSERT_EQUAL(ESP32C3_ERR_HW_CRYPTO_DS_HMAC_FAIL, esp_ds_start_sign(message, &ds_data, HMAC_KEY0, &ctx));
  209. }
  210. // This test uses the HMAC_KEY0 eFuse key which hasn't been burned by burn_hmac_keys().
  211. // HMAC_KEY0 is usually used for HMAC upstream (user access) tests.
  212. TEST_CASE("Digital Signature Blocking wrong HMAC key purpose (FPGA only)", "[hw_crypto] [ds]")
  213. {
  214. esp_ds_data_t ds_data = {};
  215. ds_data.rsa_length = ESP_DS_RSA_3072;
  216. const char *message = "test";
  217. uint8_t signature_data [128 * 4];
  218. // HMAC fails in that case because it checks for the correct purpose
  219. TEST_ASSERT_EQUAL(ESP32C3_ERR_HW_CRYPTO_DS_HMAC_FAIL, esp_ds_sign(message, &ds_data, HMAC_KEY0, signature_data));
  220. }
  221. TEST_CASE("Digital Signature Operation (FPGA only)", "[hw_crypto] [ds]")
  222. {
  223. burn_hmac_keys();
  224. for (int i = 0; i < NUM_CASES; i++) {
  225. printf("Running test case %d...\n", i);
  226. const encrypt_testcase_t *t = &test_cases[i];
  227. // copy encrypt parameter test case into ds_data structure
  228. esp_ds_data_t ds_data = { };
  229. memcpy(ds_data.iv, t->iv, ETS_DS_IV_LEN);
  230. memcpy(ds_data.c, t->expected_c, ETS_DS_C_LEN);
  231. ds_data.rsa_length = t->p_data.length;
  232. for (int j = 0; j < NUM_MESSAGES; j++) {
  233. uint8_t signature[DS_MAX_BITS/8] = { 0 };
  234. printf(" ... message %d\n", j);
  235. esp_ds_context_t *esp_ds_ctx;
  236. esp_err_t ds_r = esp_ds_start_sign(test_messages[j],
  237. &ds_data,
  238. t->hmac_key_idx + 1,
  239. &esp_ds_ctx);
  240. TEST_ASSERT_EQUAL(ESP_OK, ds_r);
  241. ds_r = esp_ds_finish_sign(signature, esp_ds_ctx);
  242. TEST_ASSERT_EQUAL(ESP_OK, ds_r);
  243. TEST_ASSERT_EQUAL_HEX8_ARRAY(t->expected_results[j], signature, sizeof(signature));
  244. }
  245. ets_hmac_invalidate_downstream(ETS_EFUSE_KEY_PURPOSE_HMAC_DOWN_DIGITAL_SIGNATURE);
  246. }
  247. }
  248. TEST_CASE("Digital Signature Blocking Operation (FPGA only)", "[hw_crypto] [ds]")
  249. {
  250. burn_hmac_keys();
  251. for (int i = 0; i < NUM_CASES; i++) {
  252. printf("Running test case %d...\n", i);
  253. const encrypt_testcase_t *t = &test_cases[i];
  254. // copy encrypt parameter test case into ds_data structure
  255. esp_ds_data_t ds_data = { };
  256. memcpy(ds_data.iv, t->iv, ETS_DS_IV_LEN);
  257. memcpy(ds_data.c, t->expected_c, ETS_DS_C_LEN);
  258. ds_data.rsa_length = t->p_data.length;
  259. uint8_t signature[DS_MAX_BITS/8] = { 0 };
  260. esp_err_t ds_r = esp_ds_sign(test_messages[0],
  261. &ds_data,
  262. t->hmac_key_idx + 1,
  263. signature);
  264. TEST_ASSERT_EQUAL(ESP_OK, ds_r);
  265. TEST_ASSERT_EQUAL_HEX8_ARRAY(t->expected_results[0], signature, sizeof(signature));
  266. }
  267. }
  268. TEST_CASE("Digital Signature Invalid Data (FPGA only)", "[hw_crypto] [ds]")
  269. {
  270. burn_hmac_keys();
  271. // Set up a valid test case
  272. const encrypt_testcase_t *t = &test_cases[0];
  273. esp_ds_data_t ds_data = { };
  274. memcpy(ds_data.iv, t->iv, ETS_DS_IV_LEN);
  275. memcpy(ds_data.c, t->expected_c, ETS_DS_C_LEN);
  276. ds_data.rsa_length = t->p_data.length;
  277. uint8_t signature[DS_MAX_BITS/8] = { 0 };
  278. const uint8_t zero[DS_MAX_BITS/8] = { 0 };
  279. // Corrupt the IV one bit at a time, rerun and expect failure
  280. for (int bit = 0; bit < 128; bit++) {
  281. printf("Corrupting IV bit %d...\n", bit);
  282. ds_data.iv[bit / 8] ^= 1 << (bit % 8);
  283. esp_ds_context_t *esp_ds_ctx;
  284. esp_err_t ds_r = esp_ds_start_sign(test_messages[0], &ds_data, t->hmac_key_idx + 1, &esp_ds_ctx);
  285. TEST_ASSERT_EQUAL(ESP_OK, ds_r);
  286. ds_r = esp_ds_finish_sign(signature, esp_ds_ctx);
  287. TEST_ASSERT_EQUAL(ESP32C3_ERR_HW_CRYPTO_DS_INVALID_DIGEST, ds_r);
  288. TEST_ASSERT_EQUAL_HEX8_ARRAY(zero, signature, DS_MAX_BITS/8);
  289. ds_data.iv[bit / 8] ^= 1 << (bit % 8);
  290. }
  291. // Corrupt encrypted key data one bit at a time, rerun and expect failure
  292. printf("Corrupting C...\n");
  293. for (int bit = 0; bit < ETS_DS_C_LEN * 8; bit++) {
  294. printf("Corrupting C bit %d...\n", bit);
  295. ds_data.c[bit / 8] ^= 1 << (bit % 8);
  296. esp_ds_context_t *esp_ds_ctx;
  297. esp_err_t ds_r = esp_ds_start_sign(test_messages[0], &ds_data, t->hmac_key_idx + 1, &esp_ds_ctx);
  298. TEST_ASSERT_EQUAL(ESP_OK, ds_r);
  299. ds_r = esp_ds_finish_sign(signature, esp_ds_ctx);
  300. TEST_ASSERT_EQUAL(ESP32C3_ERR_HW_CRYPTO_DS_INVALID_DIGEST, ds_r);
  301. TEST_ASSERT_EQUAL_HEX8_ARRAY(zero, signature, DS_MAX_BITS/8);
  302. ds_data.c[bit / 8] ^= 1 << (bit % 8);
  303. }
  304. }
  305. #endif // CONFIG_IDF_ENV_FPGA