test_ds.c 13 KB

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