test_ds.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 "esp32s2/rom/efuse.h"
  15. #include "esp32s2/rom/digital_signature.h"
  16. #include "esp32s2/rom/aes.h"
  17. #include "esp32s2/rom/sha.h"
  18. #include <string.h>
  19. #include "esp_ds.h"
  20. #define NUM_RESULTS 10
  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][4096/32];
  27. } encrypt_testcase_t;
  28. // Generated header (gen_digital_signature_tests.py) defines
  29. // NUM_HMAC_KEYS, test_hmac_keys, NUM_MESSAGES, NUM_CASES, test_messages[], test_cases[]
  30. // Some adaptations were made: removed the 512 bit case and changed RSA lengths to the enums from esp_ds.h
  31. #include "digital_signature_test_cases.h"
  32. _Static_assert(NUM_RESULTS == NUM_MESSAGES, "expected_results size should be the same as NUM_MESSAGES in generated header");
  33. TEST_CASE("Digital Signature Parameter Encryption data NULL", "[hw_crypto]")
  34. {
  35. const char iv [32];
  36. esp_ds_p_data_t p_data;
  37. const char key [32];
  38. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_encrypt_params(NULL, iv, &p_data, key));
  39. }
  40. TEST_CASE("Digital Signature Parameter Encryption iv NULL", "[hw_crypto]")
  41. {
  42. esp_ds_data_t data;
  43. esp_ds_p_data_t p_data;
  44. const char key [32];
  45. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_encrypt_params(&data, NULL, &p_data, key));
  46. }
  47. TEST_CASE("Digital Signature Parameter Encryption p_data NULL", "[hw_crypto]")
  48. {
  49. esp_ds_data_t data;
  50. const char iv [32];
  51. const char key [32];
  52. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_encrypt_params(&data, iv, NULL, key));
  53. }
  54. TEST_CASE("Digital Signature Parameter Encryption key NULL", "[hw_crypto]")
  55. {
  56. esp_ds_data_t data;
  57. const char iv [32];
  58. esp_ds_p_data_t p_data;
  59. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_encrypt_params(&data, iv, &p_data, NULL));
  60. }
  61. TEST_CASE("Digital Signature Parameter Encryption", "[hw_crypto]")
  62. {
  63. for (int i = 0; i < NUM_CASES; i++) {
  64. printf("Encrypting test case %d...\n", i);
  65. const encrypt_testcase_t *t = &test_cases[i];
  66. esp_ds_data_t result = { };
  67. esp_ds_p_data_t p_data;
  68. memcpy(p_data.Y, t->p_data.Y, 4096/8);
  69. memcpy(p_data.M, t->p_data.M, 4096/8);
  70. memcpy(p_data.Rb, t->p_data.Rb, 4096/8);
  71. p_data.M_prime = t->p_data.M_prime;
  72. p_data.length = t->p_data.length;
  73. esp_err_t r = esp_ds_encrypt_params(&result, t->iv, &p_data,
  74. test_hmac_keys[t->hmac_key_idx]);
  75. printf("Encrypting test case %d done\n", i);
  76. TEST_ASSERT_EQUAL(ESP_OK, r);
  77. TEST_ASSERT_EQUAL(t->p_data.length, result.rsa_length);
  78. TEST_ASSERT_EQUAL_HEX8_ARRAY(t->iv, result.iv, ETS_DS_IV_LEN);
  79. TEST_ASSERT_EQUAL_HEX8_ARRAY(t->expected_c, result.c, ETS_DS_C_LEN);
  80. }
  81. }
  82. TEST_CASE("Digital Signature start Invalid message", "[hw_crypto]")
  83. {
  84. esp_ds_data_t ds_data = { };
  85. ds_data.rsa_length = ESP_DS_RSA_4096;
  86. esp_ds_context_t *ctx;
  87. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_start_sign(NULL, &ds_data, HMAC_KEY1, &ctx));
  88. }
  89. TEST_CASE("Digital Signature start Invalid data", "[hw_crypto]")
  90. {
  91. const char *message = "test";
  92. esp_ds_context_t *ctx;
  93. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_start_sign(message, NULL, HMAC_KEY1, &ctx));
  94. }
  95. TEST_CASE("Digital Signature start Invalid context", "[hw_crypto]")
  96. {
  97. esp_ds_data_t ds_data = {};
  98. ds_data.rsa_length = ESP_DS_RSA_4096;
  99. const char *message = "test";
  100. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_start_sign(message, &ds_data, HMAC_KEY1, NULL));
  101. }
  102. TEST_CASE("Digital Signature RSA length 0", "[hw_crypto]")
  103. {
  104. esp_ds_data_t ds_data = {};
  105. ds_data.rsa_length = 0;
  106. const char *message = "test";
  107. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_start_sign(message, &ds_data, HMAC_KEY1, NULL));
  108. }
  109. TEST_CASE("Digital Signature RSA length too long", "[hw_crypto]")
  110. {
  111. esp_ds_data_t ds_data = {};
  112. ds_data.rsa_length = 128;
  113. const char *message = "test";
  114. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_start_sign(message, &ds_data, HMAC_KEY1, NULL));
  115. }
  116. TEST_CASE("Digital Signature start HMAC key out of range", "[hw_crypto]")
  117. {
  118. esp_ds_data_t ds_data = {};
  119. ds_data.rsa_length = ESP_DS_RSA_4096;
  120. esp_ds_context_t *ctx;
  121. const char *message = "test";
  122. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_start_sign(message, &ds_data, HMAC_KEY5 + 1, &ctx));
  123. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_start_sign(message, &ds_data, HMAC_KEY0 - 1, &ctx));
  124. }
  125. TEST_CASE("Digital Signature finish Invalid signature ptr", "[hw_crypto]")
  126. {
  127. esp_ds_context_t *ctx = NULL;
  128. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_finish_sign(NULL, ctx));
  129. }
  130. TEST_CASE("Digital Signature finish Invalid context", "[hw_crypto]")
  131. {
  132. uint8_t signature_data [128 * 4];
  133. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_finish_sign(signature_data, NULL));
  134. }
  135. TEST_CASE("Digital Signature Blocking Invalid message", "[hw_crypto]")
  136. {
  137. esp_ds_data_t ds_data = { };
  138. ds_data.rsa_length = ESP_DS_RSA_4096;
  139. uint8_t signature_data [128 * 4];
  140. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_sign(NULL, &ds_data, HMAC_KEY1, signature_data));
  141. }
  142. TEST_CASE("Digital Signature Blocking Invalid data", "[hw_crypto]")
  143. {
  144. const char *message = "test";
  145. uint8_t signature_data [128 * 4];
  146. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_sign(message, NULL, HMAC_KEY1, signature_data));
  147. }
  148. TEST_CASE("Digital Signature Blocking Invalid signature ptr", "[hw_crypto]")
  149. {
  150. esp_ds_data_t ds_data = {};
  151. ds_data.rsa_length = ESP_DS_RSA_4096;
  152. const char *message = "test";
  153. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_sign(message, &ds_data, HMAC_KEY1, NULL));
  154. }
  155. TEST_CASE("Digital Signature Blocking RSA length 0", "[hw_crypto]")
  156. {
  157. esp_ds_data_t ds_data = {};
  158. ds_data.rsa_length = 0;
  159. const char *message = "test";
  160. uint8_t signature_data [128 * 4];
  161. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_sign(message, &ds_data, HMAC_KEY1, signature_data));
  162. }
  163. TEST_CASE("Digital Signature Blocking RSA length too long", "[hw_crypto]")
  164. {
  165. esp_ds_data_t ds_data = {};
  166. ds_data.rsa_length = 128;
  167. const char *message = "test";
  168. uint8_t signature_data [128 * 4];
  169. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_sign(message, &ds_data, HMAC_KEY1, signature_data));
  170. }
  171. TEST_CASE("Digital Signature Blocking HMAC key out of range", "[hw_crypto]")
  172. {
  173. esp_ds_data_t ds_data = {};
  174. ds_data.rsa_length = 127;
  175. const char *message = "test";
  176. uint8_t signature_data [128 * 4];
  177. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_sign(message, &ds_data, HMAC_KEY5 + 1, signature_data));
  178. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, esp_ds_sign(message, &ds_data, HMAC_KEY0 - 1, signature_data));
  179. }
  180. #if CONFIG_IDF_ENV_FPGA
  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. // starting from block 1, block 0 occupied with HMAC upstream test key
  188. int ets_status = ets_efuse_write_key(ETS_EFUSE_BLOCK_KEY1 + i,
  189. purpose,
  190. test_hmac_keys[i], 32);
  191. if (ets_status == ESP_OK) {
  192. printf("written DS test key to block [%d]!\n", ETS_EFUSE_BLOCK_KEY1 + i);
  193. } else {
  194. printf("writing DS test key to block [%d] failed, maybe written already\n", ETS_EFUSE_BLOCK_KEY1 + i);
  195. }
  196. }
  197. }
  198. TEST_CASE("Digital Signature wrong HMAC key purpose (FPGA only)", "[hw_crypto]")
  199. {
  200. esp_ds_data_t ds_data = {};
  201. ds_data.rsa_length = ESP_DS_RSA_4096;
  202. esp_ds_context_t *ctx;
  203. const char *message = "test";
  204. // HMAC fails in that case because it checks for the correct purpose
  205. TEST_ASSERT_EQUAL(ESP_ERR_HW_CRYPTO_DS_HMAC_FAIL, esp_ds_start_sign(message, &ds_data, HMAC_KEY0, &ctx));
  206. }
  207. TEST_CASE("Digital Signature Blocking wrong HMAC key purpose (FPGA only)", "[hw_crypto]")
  208. {
  209. esp_ds_data_t ds_data = {};
  210. ds_data.rsa_length = ESP_DS_RSA_4096;
  211. const char *message = "test";
  212. uint8_t signature_data [128 * 4];
  213. // HMAC fails in that case because it checks for the correct purpose
  214. TEST_ASSERT_EQUAL(ESP_ERR_HW_CRYPTO_DS_HMAC_FAIL, esp_ds_sign(message, &ds_data, HMAC_KEY0, signature_data));
  215. }
  216. TEST_CASE("Digital Signature Operation (FPGA only)", "[hw_crypto]")
  217. {
  218. burn_hmac_keys();
  219. for (int i = 0; i < NUM_CASES; i++) {
  220. printf("Running test case %d...\n", i);
  221. const encrypt_testcase_t *t = &test_cases[i];
  222. // copy encrypt parameter test case into ds_data structure
  223. esp_ds_data_t ds_data = { };
  224. memcpy(ds_data.iv, t->iv, ETS_DS_IV_LEN);
  225. memcpy(ds_data.c, t->expected_c, ETS_DS_C_LEN);
  226. ds_data.rsa_length = t->p_data.length;
  227. for (int j = 0; j < NUM_MESSAGES; j++) {
  228. uint8_t signature[4096/8] = { 0 };
  229. printf(" ... message %d\n", j);
  230. esp_ds_context_t *esp_ds_ctx;
  231. esp_err_t ds_r = esp_ds_start_sign(test_messages[j],
  232. &ds_data,
  233. t->hmac_key_idx + 1,
  234. &esp_ds_ctx);
  235. TEST_ASSERT_EQUAL(ESP_OK, ds_r);
  236. ds_r = esp_ds_finish_sign(signature, esp_ds_ctx);
  237. TEST_ASSERT_EQUAL(ESP_OK, ds_r);
  238. TEST_ASSERT_EQUAL_HEX8_ARRAY(t->expected_results[j], signature, sizeof(signature));
  239. }
  240. }
  241. }
  242. TEST_CASE("Digital Signature Blocking Operation (FPGA only)", "[hw_crypto]")
  243. {
  244. burn_hmac_keys();
  245. for (int i = 0; i < NUM_CASES; i++) {
  246. printf("Running test case %d...\n", i);
  247. const encrypt_testcase_t *t = &test_cases[i];
  248. // copy encrypt parameter test case into ds_data structure
  249. esp_ds_data_t ds_data = { };
  250. memcpy(ds_data.iv, t->iv, ETS_DS_IV_LEN);
  251. memcpy(ds_data.c, t->expected_c, ETS_DS_C_LEN);
  252. ds_data.rsa_length = t->p_data.length;
  253. uint8_t signature[4096/8] = { 0 };
  254. esp_ds_context_t *esp_ds_ctx;
  255. esp_err_t ds_r = esp_ds_start_sign(test_messages[0],
  256. &ds_data,
  257. t->hmac_key_idx + 1,
  258. &esp_ds_ctx);
  259. TEST_ASSERT_EQUAL(ESP_OK, ds_r);
  260. ds_r = esp_ds_finish_sign(signature, esp_ds_ctx);
  261. TEST_ASSERT_EQUAL(ESP_OK, ds_r);
  262. TEST_ASSERT_EQUAL_HEX8_ARRAY(t->expected_results[0], signature, sizeof(signature));
  263. }
  264. }
  265. TEST_CASE("Digital Signature Invalid Data (FPGA only)", "[hw_crypto]")
  266. {
  267. burn_hmac_keys();
  268. // Set up a valid test case
  269. const encrypt_testcase_t *t = &test_cases[0];
  270. esp_ds_data_t ds_data = { };
  271. memcpy(ds_data.iv, t->iv, ETS_DS_IV_LEN);
  272. memcpy(ds_data.c, t->expected_c, ETS_DS_C_LEN);
  273. ds_data.rsa_length = t->p_data.length;
  274. uint8_t signature[4096/8] = { 0 };
  275. const uint8_t zero[4096/8] = { 0 };
  276. // Corrupt the IV one bit at a time, rerun and expect failure
  277. for (int bit = 0; bit < 128; bit++) {
  278. printf("Corrupting IV bit %d...\n", bit);
  279. ds_data.iv[bit / 8] ^= 1 << (bit % 8);
  280. esp_ds_context_t *esp_ds_ctx;
  281. esp_err_t ds_r = esp_ds_start_sign(test_messages[0], &ds_data, t->hmac_key_idx + 1, &esp_ds_ctx);
  282. TEST_ASSERT_EQUAL(ESP_OK, ds_r);
  283. ds_r = esp_ds_finish_sign(signature, esp_ds_ctx);
  284. TEST_ASSERT_EQUAL(ESP_ERR_HW_CRYPTO_DS_INVALID_DIGEST, ds_r);
  285. TEST_ASSERT_EQUAL_HEX8_ARRAY(zero, signature, 4096/8);
  286. ds_data.iv[bit / 8] ^= 1 << (bit % 8);
  287. }
  288. // Corrupt encrypted key data one bit at a time, rerun and expect failure
  289. printf("Corrupting C...\n");
  290. for (int bit = 0; bit < ETS_DS_C_LEN * 8; bit++) {
  291. printf("Corrupting C bit %d...\n", bit);
  292. ds_data.c[bit / 8] ^= 1 << (bit % 8);
  293. esp_ds_context_t *esp_ds_ctx;
  294. esp_err_t ds_r = esp_ds_start_sign(test_messages[0], &ds_data, t->hmac_key_idx + 1, &esp_ds_ctx);
  295. TEST_ASSERT_EQUAL(ESP_OK, ds_r);
  296. ds_r = esp_ds_finish_sign(signature, esp_ds_ctx);
  297. TEST_ASSERT_EQUAL(ESP_ERR_HW_CRYPTO_DS_INVALID_DIGEST, ds_r);
  298. TEST_ASSERT_EQUAL_HEX8_ARRAY(zero, signature, 4096/8);
  299. ds_data.c[bit / 8] ^= 1 << (bit % 8);
  300. }
  301. }
  302. #endif // CONFIG_IDF_ENV_FPGA