es_security.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /**
  2. * Copyright (c) 2016 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include <stdbool.h>
  41. #include <stdint.h>
  42. #include "es_security.h"
  43. #include "app_timer.h"
  44. #include "es_flash.h"
  45. #include "es_stopwatch.h"
  46. #include "fds.h"
  47. #include "modes.h"
  48. #include "nrf_crypto.h"
  49. #include "nrf_soc.h"
  50. #define NONCE_SIZE (6)
  51. #define TAG_SIZE (2)
  52. #define SALT_SIZE (2)
  53. #define TLM_DATA_SIZE (ES_TLM_LENGTH - 2)
  54. #define EIK_SIZE (ESCS_AES_KEY_SIZE)
  55. #define AES_ECB_CIPHERTEXT_LENGTH (16)
  56. #define AES_ECB_CLEARTEXT_LENGTH (16)
  57. /**@brief Timing structure. */
  58. typedef struct
  59. {
  60. uint32_t time_counter;
  61. uint8_t k_scaler;
  62. } es_security_timing_t;
  63. /**@brief Security slot structure. */
  64. typedef struct
  65. {
  66. nrf_ecb_hal_data_t aes_ecb_ik;
  67. nrf_ecb_hal_data_t aes_ecb_tk;
  68. uint8_t eid[ES_EID_ID_LENGTH];
  69. es_security_timing_t timing;
  70. bool is_occupied;
  71. } es_security_slot_t;
  72. /**@brief Key pair structure. */
  73. typedef struct
  74. {
  75. nrf_crypto_ecc_private_key_t private;
  76. nrf_crypto_ecc_public_key_t public;
  77. } ecdh_key_pair_t;
  78. /**@brief ECDH structure. */
  79. typedef struct
  80. {
  81. ecdh_key_pair_t ecdh_key_pair;
  82. } es_security_ecdh_t;
  83. static nrf_ecb_hal_data_t m_aes_ecb_lk;
  84. static es_security_slot_t m_security_slot[APP_MAX_EID_SLOTS];
  85. static es_security_ecdh_t m_ecdh;
  86. static es_security_msg_cb_t m_security_callback;
  87. static es_stopwatch_id_t m_seconds_passed_sw_id;
  88. // Use static context variables to avoid stack allocation.
  89. static nrf_crypto_aes_context_t m_aes_context;
  90. static nrf_crypto_hmac_context_t m_hmac_context;
  91. static nrf_crypto_aead_context_t m_aead_context;
  92. static nrf_crypto_ecc_key_pair_generate_context_t ecc_key_pair_generate_context;
  93. static nrf_crypto_ecdh_context_t ecdh_context;
  94. /**@brief Generates a temporary key with the Identity key. */
  95. static void temp_key_generate(uint8_t slot_no);
  96. /**@brief Generates a EID with the Temporary Key*/
  97. static void eid_generate(uint8_t slot_no)
  98. {
  99. ret_code_t err_code;
  100. size_t ciphertext_size = AES_ECB_CIPHERTEXT_LENGTH;
  101. temp_key_generate(slot_no);
  102. memset(m_security_slot[slot_no].aes_ecb_tk.cleartext, 0, ESCS_AES_KEY_SIZE);
  103. m_security_slot[slot_no].aes_ecb_tk.cleartext[11] = m_security_slot[slot_no].timing.k_scaler;
  104. uint32_t k_bits_cleared_time =
  105. (m_security_slot[slot_no].timing.time_counter >> m_security_slot[slot_no].timing.k_scaler)
  106. << m_security_slot[slot_no].timing.k_scaler;
  107. m_security_slot[slot_no].aes_ecb_tk.cleartext[12] =
  108. (uint8_t)((k_bits_cleared_time >> 24) & 0xff);
  109. m_security_slot[slot_no].aes_ecb_tk.cleartext[13] =
  110. (uint8_t)((k_bits_cleared_time >> 16) & 0xff);
  111. m_security_slot[slot_no].aes_ecb_tk.cleartext[14] = (uint8_t)((k_bits_cleared_time >> 8) & 0xff);
  112. m_security_slot[slot_no].aes_ecb_tk.cleartext[15] = (uint8_t)((k_bits_cleared_time) & 0xff);
  113. err_code = nrf_crypto_aes_crypt(&m_aes_context,
  114. &g_nrf_crypto_aes_ecb_128_info,
  115. NRF_CRYPTO_ENCRYPT, // Operation
  116. m_security_slot[slot_no].aes_ecb_tk.key, // Key
  117. NULL, // IV
  118. m_security_slot[slot_no].aes_ecb_tk.cleartext, // Data in
  119. AES_ECB_CLEARTEXT_LENGTH, // Data in size
  120. m_security_slot[slot_no].aes_ecb_tk.ciphertext, // Data out
  121. &ciphertext_size); // Data out size
  122. APP_ERROR_CHECK(err_code);
  123. memcpy(m_security_slot[slot_no].eid,
  124. m_security_slot[slot_no].aes_ecb_tk.ciphertext,
  125. ES_EID_ID_LENGTH);
  126. m_security_callback(slot_no, ES_SECURITY_MSG_EID);
  127. }
  128. /**@brief Generates a temporary key with the Identity key. */
  129. static void temp_key_generate(uint8_t slot_no)
  130. {
  131. ret_code_t err_code;
  132. size_t ciphertext_size = AES_ECB_CIPHERTEXT_LENGTH;
  133. memset(m_security_slot[slot_no].aes_ecb_ik.cleartext, 0, ESCS_AES_KEY_SIZE);
  134. m_security_slot[slot_no].aes_ecb_ik.cleartext[11] = 0xFF;
  135. m_security_slot[slot_no].aes_ecb_ik.cleartext[14] =
  136. (uint8_t)((m_security_slot[slot_no].timing.time_counter >> 24) & 0xff);
  137. m_security_slot[slot_no].aes_ecb_ik.cleartext[15] =
  138. (uint8_t)((m_security_slot[slot_no].timing.time_counter >> 16) & 0xff);
  139. err_code = nrf_crypto_aes_crypt(&m_aes_context,
  140. &g_nrf_crypto_aes_ecb_128_info,
  141. NRF_CRYPTO_ENCRYPT, // Operation
  142. m_security_slot[slot_no].aes_ecb_ik.key, // Key
  143. NULL, // IV
  144. m_security_slot[slot_no].aes_ecb_ik.cleartext, // Data in
  145. AES_ECB_CLEARTEXT_LENGTH, // Data in size
  146. m_security_slot[slot_no].aes_ecb_ik.ciphertext, // Data out
  147. &ciphertext_size); // Data out size
  148. APP_ERROR_CHECK(err_code);
  149. memcpy(m_security_slot[slot_no].aes_ecb_tk.key,
  150. m_security_slot[slot_no].aes_ecb_ik.ciphertext,
  151. ESCS_AES_KEY_SIZE);
  152. }
  153. /**@brief See if EID should be re-calculated.
  154. */
  155. static void check_rollovers_and_update_eid(uint8_t slot_no)
  156. {
  157. static uint32_t last_invocation_time_counter = 0;
  158. uint32_t scaler = 2 << (m_security_slot[slot_no].timing.k_scaler - 1);
  159. uint32_t diff;
  160. if (last_invocation_time_counter == 0)
  161. {
  162. last_invocation_time_counter = m_security_slot[slot_no].timing.time_counter;
  163. }
  164. diff = m_security_slot[slot_no].timing.time_counter - last_invocation_time_counter;
  165. if (diff >= scaler)
  166. {
  167. // Store to last scaler-aligned time.
  168. last_invocation_time_counter = (m_security_slot[slot_no].timing.time_counter / scaler) * scaler;
  169. eid_generate(slot_no);
  170. }
  171. }
  172. /**@brief Initialize lock code from flash. If it does not exist, copy from APP_CONFIG_LOCK_CODE.
  173. */
  174. static void lock_code_init(uint8_t * p_lock_buff)
  175. {
  176. ret_code_t err_code;
  177. err_code = es_flash_access_lock_key(p_lock_buff, ES_FLASH_ACCESS_READ);
  178. FLASH_ACCES_ERROR_CHECK_ALLOW_NOT_FOUND(err_code);
  179. // If no lock keys exist, then generate one and copy it to buffer.
  180. if (err_code == FDS_ERR_NOT_FOUND)
  181. {
  182. uint8_t lock_code[16] = APP_CONFIG_LOCK_CODE;
  183. memcpy(p_lock_buff, lock_code, sizeof(lock_code));
  184. err_code = es_flash_access_lock_key(p_lock_buff, ES_FLASH_ACCESS_WRITE);
  185. APP_ERROR_CHECK(err_code);
  186. }
  187. }
  188. void es_security_update_time(void)
  189. {
  190. static uint32_t timer_persist;
  191. uint32_t second_since_last_invocation = es_stopwatch_check(m_seconds_passed_sw_id);
  192. if (second_since_last_invocation > 0)
  193. {
  194. for (uint32_t i = 0; i < APP_MAX_EID_SLOTS; ++i)
  195. {
  196. if (m_security_slot[i].is_occupied)
  197. {
  198. m_security_slot[i].timing.time_counter += second_since_last_invocation;
  199. check_rollovers_and_update_eid(i);
  200. }
  201. }
  202. // Every 24 hr, write the new EID timer to flash.
  203. timer_persist += second_since_last_invocation;
  204. const uint32_t TWENTY_FOUR_HOURS = 60 * 60 * 24;
  205. if (timer_persist >= TWENTY_FOUR_HOURS)
  206. {
  207. for (uint32_t i = 0; i < APP_MAX_EID_SLOTS; ++i)
  208. {
  209. if (m_security_slot[i].is_occupied)
  210. {
  211. m_security_callback(i, ES_SECURITY_MSG_STORE_TIME);
  212. }
  213. }
  214. timer_persist = 0;
  215. }
  216. }
  217. }
  218. void es_security_eid_slots_restore(uint8_t slot_no,
  219. uint8_t k_scaler,
  220. uint32_t time_counter,
  221. const uint8_t * p_ik)
  222. {
  223. m_security_slot[slot_no].timing.k_scaler = k_scaler;
  224. m_security_slot[slot_no].timing.time_counter = time_counter;
  225. memcpy(m_security_slot[slot_no].aes_ecb_ik.key, p_ik, ESCS_AES_KEY_SIZE);
  226. m_security_slot[slot_no].is_occupied = true;
  227. m_security_callback(slot_no, ES_SECURITY_MSG_IK);
  228. eid_generate(slot_no);
  229. }
  230. ret_code_t es_security_lock_code_update(uint8_t * p_ecrypted_key)
  231. {
  232. ret_code_t err_code;
  233. uint8_t temp_buff[ESCS_AES_KEY_SIZE] = {0};
  234. size_t temp_buff_size = sizeof(temp_buff);
  235. err_code = nrf_crypto_aes_crypt(&m_aes_context,
  236. &g_nrf_crypto_aes_ecb_128_info,
  237. NRF_CRYPTO_DECRYPT, // Operation
  238. m_aes_ecb_lk.key, // Key
  239. NULL, // IV
  240. p_ecrypted_key, // Data in
  241. 16, // Data in size
  242. temp_buff, // Data out
  243. &temp_buff_size); // Data out size
  244. VERIFY_SUCCESS(err_code);
  245. memcpy(m_aes_ecb_lk.key, temp_buff, ESCS_AES_KEY_SIZE);
  246. return es_flash_access_lock_key(m_aes_ecb_lk.key, ES_FLASH_ACCESS_WRITE);
  247. }
  248. void es_security_unlock_prepare(uint8_t * p_challenge)
  249. {
  250. ret_code_t err_code;
  251. size_t ciphertext_size = AES_ECB_CIPHERTEXT_LENGTH;
  252. memcpy(m_aes_ecb_lk.cleartext, p_challenge, ESCS_AES_KEY_SIZE);
  253. err_code = nrf_crypto_aes_crypt(&m_aes_context,
  254. &g_nrf_crypto_aes_ecb_128_info,
  255. NRF_CRYPTO_ENCRYPT, // Operation
  256. m_aes_ecb_lk.key, // Key
  257. NULL, // IV
  258. m_aes_ecb_lk.cleartext, // Data in
  259. AES_ECB_CLEARTEXT_LENGTH, // Data in size
  260. m_aes_ecb_lk.ciphertext, // Data out
  261. &ciphertext_size); // Data out size
  262. APP_ERROR_CHECK(err_code);
  263. }
  264. void es_security_unlock_verify(uint8_t * p_unlock_token)
  265. {
  266. if (memcmp(p_unlock_token, m_aes_ecb_lk.ciphertext, ESCS_AES_KEY_SIZE) == 0)
  267. {
  268. m_security_callback(0, ES_SECURITY_MSG_UNLOCKED);
  269. }
  270. }
  271. ret_code_t es_security_random_challenge_generate(uint8_t * p_rand_chlg_buff)
  272. {
  273. return nrf_crypto_rng_vector_generate(p_rand_chlg_buff, ESCS_AES_KEY_SIZE);
  274. }
  275. void es_security_shared_ik_receive(uint8_t slot_no, uint8_t * p_encrypted_ik, uint8_t scaler_k)
  276. {
  277. ret_code_t err_code;
  278. size_t cleartext_size = AES_ECB_CLEARTEXT_LENGTH;
  279. m_security_slot[slot_no].is_occupied = true;
  280. m_security_slot[slot_no].timing.k_scaler = scaler_k;
  281. m_security_slot[slot_no].timing.time_counter = APP_CONFIG_TIMING_INIT_VALUE;
  282. err_code = nrf_crypto_aes_crypt(&m_aes_context,
  283. &g_nrf_crypto_aes_ecb_128_info,
  284. NRF_CRYPTO_DECRYPT, // Operation
  285. m_aes_ecb_lk.key, // Key
  286. NULL, // IV
  287. p_encrypted_ik, // Data in
  288. 16, // Data in size
  289. m_security_slot[slot_no].aes_ecb_ik.key, // Data out
  290. &cleartext_size); // Data out size
  291. APP_ERROR_CHECK(err_code);
  292. eid_generate(slot_no);
  293. m_security_callback(slot_no, ES_SECURITY_MSG_IK);
  294. }
  295. void es_security_client_pub_ecdh_receive(uint8_t slot_no, uint8_t * p_pub_ecdh, uint8_t scaler_k)
  296. {
  297. ret_code_t err_code;
  298. nrf_crypto_ecc_public_key_t phone_public; // Phone public ECDH key
  299. uint8_t beacon_public[ESCS_ECDH_KEY_SIZE]; // Beacon public ECDH key
  300. uint8_t shared[ESCS_ECDH_KEY_SIZE]; // Shared secret ECDH key
  301. uint8_t public_keys[64]; // Buffer for concatenated public keys
  302. uint8_t key_material[64]; // Buffer for holding key material
  303. uint8_t empty_check[ESCS_ECDH_KEY_SIZE] = {0};
  304. size_t beacon_public_size = sizeof(beacon_public);
  305. size_t shared_size = sizeof(shared);
  306. size_t key_material_size = sizeof(key_material);
  307. m_security_slot[slot_no].is_occupied = true;
  308. m_security_slot[slot_no].timing.k_scaler = scaler_k;
  309. m_security_slot[slot_no].timing.time_counter = APP_CONFIG_TIMING_INIT_VALUE;
  310. // Get public 32-byte service ECDH key from phone.
  311. err_code = nrf_crypto_ecc_public_key_from_raw(&g_nrf_crypto_ecc_curve25519_curve_info,
  312. &phone_public,
  313. p_pub_ecdh,
  314. ESCS_ECDH_KEY_SIZE);
  315. APP_ERROR_CHECK(err_code);
  316. // Generate key pair.
  317. err_code = nrf_crypto_ecc_key_pair_generate(&ecc_key_pair_generate_context,
  318. &g_nrf_crypto_ecc_curve25519_curve_info,
  319. &m_ecdh.ecdh_key_pair.private,
  320. &m_ecdh.ecdh_key_pair.public);
  321. APP_ERROR_CHECK(err_code);
  322. // Generate shared 32-byte ECDH secret from beacon private service ECDH key and phone public ECDH key.
  323. err_code = nrf_crypto_ecdh_compute(&ecdh_context,
  324. &m_ecdh.ecdh_key_pair.private,
  325. &phone_public,
  326. shared,
  327. &shared_size);
  328. APP_ERROR_CHECK(err_code);
  329. // Verify that the shared secret is not zero at this point, and report an error/reset if it is.
  330. if (memcmp(empty_check, shared, ESCS_ECDH_KEY_SIZE) == 0)
  331. {
  332. APP_ERROR_CHECK(NRF_ERROR_INTERNAL);
  333. }
  334. // Concatenate the resolver's public key and beacon's public key
  335. err_code = nrf_crypto_ecc_public_key_to_raw(&m_ecdh.ecdh_key_pair.public,
  336. beacon_public,
  337. &beacon_public_size);
  338. APP_ERROR_CHECK(err_code);
  339. memcpy(public_keys, p_pub_ecdh, 32);
  340. memcpy(public_keys + 32, beacon_public, 32);
  341. // Convert the shared secret to key material using HKDF-SHA256. HKDF is used with the salt set
  342. // to a concatenation of the resolver's public key and beacon's public key
  343. err_code = nrf_crypto_hkdf_calculate(&m_hmac_context,
  344. &g_nrf_crypto_hmac_sha256_info,
  345. key_material, // Output key
  346. &key_material_size, // Output key size
  347. shared, // Input key
  348. sizeof(shared), // Input key size
  349. public_keys, // Salt
  350. sizeof(public_keys), // Salt size
  351. NULL, // Additional info
  352. 0, // Additional info size
  353. NRF_CRYPTO_HKDF_EXTRACT_AND_EXPAND); // Mode
  354. APP_ERROR_CHECK(err_code);
  355. // Truncate the key material to 128 bits to convert it to an AES-128 secret key (Identity key).
  356. memcpy(m_security_slot[slot_no].aes_ecb_ik.key, key_material, ESCS_AES_KEY_SIZE);
  357. eid_generate(slot_no);
  358. m_security_callback(slot_no, ES_SECURITY_MSG_ECDH);
  359. m_security_callback(slot_no, ES_SECURITY_MSG_IK);
  360. }
  361. void es_security_pub_ecdh_get(uint8_t slot_no, uint8_t * p_edch_buffer)
  362. {
  363. ret_code_t err_code;
  364. size_t buffer_size = ESCS_ECDH_KEY_SIZE;
  365. err_code = nrf_crypto_ecc_public_key_to_raw(&m_ecdh.ecdh_key_pair.public,
  366. p_edch_buffer,
  367. &buffer_size);
  368. APP_ERROR_CHECK(err_code);
  369. }
  370. uint32_t es_security_clock_get(uint8_t slot_no)
  371. {
  372. return m_security_slot[slot_no].timing.time_counter;
  373. }
  374. void es_security_eid_slot_destroy(uint8_t slot_no)
  375. {
  376. memset(&m_security_slot[slot_no], 0, sizeof(es_security_slot_t));
  377. }
  378. uint8_t es_security_scaler_get(uint8_t slot_no)
  379. {
  380. return m_security_slot[slot_no].timing.k_scaler;
  381. }
  382. void es_security_eid_get(uint8_t slot_no, uint8_t * p_eid_buffer)
  383. {
  384. memcpy(p_eid_buffer, m_security_slot[slot_no].eid, ES_EID_ID_LENGTH);
  385. }
  386. void es_security_encrypted_eid_id_key_get(uint8_t slot_no, uint8_t * p_key_buffer)
  387. {
  388. ret_code_t err_code;
  389. size_t ciphertext_size = AES_ECB_CIPHERTEXT_LENGTH;
  390. memcpy(m_aes_ecb_lk.cleartext, m_security_slot[slot_no].aes_ecb_ik.key, ESCS_AES_KEY_SIZE);
  391. err_code = nrf_crypto_aes_crypt(&m_aes_context,
  392. &g_nrf_crypto_aes_ecb_128_info,
  393. NRF_CRYPTO_ENCRYPT, // Operation
  394. m_aes_ecb_lk.key, // Key
  395. NULL, // IV
  396. m_aes_ecb_lk.cleartext, // Data in
  397. AES_ECB_CLEARTEXT_LENGTH, // Data in size
  398. m_aes_ecb_lk.ciphertext, // Data out
  399. &ciphertext_size); // Data out size
  400. APP_ERROR_CHECK(err_code);
  401. memcpy(p_key_buffer, m_aes_ecb_lk.ciphertext, ESCS_AES_KEY_SIZE);
  402. }
  403. void es_security_plain_eid_id_key_get(uint8_t slot_no, uint8_t * p_key_buffer)
  404. {
  405. memcpy(p_key_buffer, m_security_slot[slot_no].aes_ecb_ik.key, ESCS_AES_KEY_SIZE);
  406. }
  407. void es_security_tlm_to_etlm(uint8_t ik_slot_no, es_tlm_frame_t * p_tlm, es_etlm_frame_t * p_etlm)
  408. {
  409. ret_code_t err_code;
  410. uint8_t plain[TLM_DATA_SIZE] = {0}; // Plaintext tlm, without the frame byte and version.
  411. size_t nplain = TLM_DATA_SIZE; // Length of message plaintext.
  412. /*lint -save -e420 */
  413. memcpy(plain, &p_tlm->vbatt[0], sizeof(plain));
  414. uint8_t key[EIK_SIZE] = {0}; // Encryption/decryption key: EIK.
  415. memcpy(key, &m_security_slot[ik_slot_no].aes_ecb_ik.key[0], EIK_SIZE);
  416. /*lint -restore */
  417. uint8_t nonce[NONCE_SIZE] = {0}; // Nonce. This must not repeat for a given key.
  418. size_t nnonce = NONCE_SIZE; // Length of nonce.First 4 bytes are beacon time base with k-bits cleared.
  419. // Last two bits are randomly generated
  420. // Take the current timestamp and clear the lowest K bits, use it as nonce.
  421. uint32_t k_bits_cleared_time = (m_security_slot[ik_slot_no].timing.time_counter
  422. >> m_security_slot[ik_slot_no].timing.k_scaler)
  423. << m_security_slot[ik_slot_no].timing.k_scaler;
  424. nonce[0] = (uint8_t)((k_bits_cleared_time >> 24) & 0xff);
  425. nonce[1] = (uint8_t)((k_bits_cleared_time >> 16) & 0xff);
  426. nonce[2] = (uint8_t)((k_bits_cleared_time >> 8) & 0xff);
  427. nonce[3] = (uint8_t)((k_bits_cleared_time) & 0xff);
  428. // Generate random salt.
  429. uint8_t salt[SALT_SIZE] = {0};
  430. err_code = nrf_crypto_rng_vector_generate(salt, SALT_SIZE);
  431. APP_ERROR_CHECK(err_code);
  432. memcpy(&nonce[4], salt, SALT_SIZE);
  433. uint8_t cipher[ES_ETLM_ECRYPTED_LENGTH]; // Ciphertext output. nplain bytes are written.
  434. uint8_t tag[TAG_SIZE] = {0}; // Authentication tag. ntag bytes are written.
  435. size_t ntag = TAG_SIZE; // Length of authentication tag.
  436. // Encryption
  437. // --------------------------------------------------------------------------
  438. err_code = nrf_crypto_aead_init(&m_aead_context, &g_nrf_crypto_aes_eax_128_info, key);
  439. APP_ERROR_CHECK(err_code);
  440. err_code = nrf_crypto_aead_crypt(&m_aead_context,
  441. NRF_CRYPTO_ENCRYPT, // Operation
  442. nonce, // Nonce
  443. nnonce, // Nonce size
  444. NULL, // Additional authenticated data (adata)
  445. 0, // Additional authenticated data size
  446. plain, // Input data
  447. nplain, // Input data size
  448. cipher, // Output data
  449. tag, // MAC result output
  450. ntag); // MAC size
  451. APP_ERROR_CHECK(err_code);
  452. err_code = nrf_crypto_aead_uninit(&m_aead_context);
  453. APP_ERROR_CHECK(err_code);
  454. // Construct the eTLM.
  455. // --------------------------------------------------------------------------
  456. p_etlm->frame_type = p_tlm->frame_type;
  457. p_etlm->version = ES_TLM_VERSION_ETLM;
  458. memcpy(p_etlm->encrypted_tlm, cipher, ES_ETLM_ECRYPTED_LENGTH);
  459. memcpy((uint8_t *)&p_etlm->random_salt, salt, SALT_SIZE);
  460. memcpy((uint8_t *)&p_etlm->msg_integrity_check, tag, TAG_SIZE);
  461. }
  462. ret_code_t es_security_init(es_security_msg_cb_t security_callback)
  463. {
  464. ret_code_t err_code;
  465. if (security_callback == NULL)
  466. {
  467. return NRF_ERROR_INVALID_PARAM;
  468. }
  469. // Get lock code from 'es_app_config.h', or fetch it from flash if exists.
  470. lock_code_init(m_aes_ecb_lk.key);
  471. m_security_callback = security_callback;
  472. memset(&m_ecdh, 0, sizeof(es_security_ecdh_t));
  473. for (uint32_t i = 0; i < APP_MAX_EID_SLOTS; ++i)
  474. {
  475. m_security_slot[i].timing.time_counter = APP_CONFIG_TIMING_INIT_VALUE;
  476. }
  477. err_code = es_stopwatch_create(&m_seconds_passed_sw_id, APP_TIMER_TICKS(1000));
  478. APP_ERROR_CHECK(err_code);
  479. err_code = nrf_crypto_init();
  480. APP_ERROR_CHECK(err_code);
  481. return NRF_SUCCESS;
  482. }