es_security.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /**
  2. * Copyright (c) 2016 - 2018, 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. #define TK_ROLLOVER 0x10000
  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 EID with the Temporary Key*/
  95. static void eid_generate(uint8_t slot_no)
  96. {
  97. ret_code_t err_code;
  98. size_t ciphertext_size = AES_ECB_CIPHERTEXT_LENGTH;
  99. memset(m_security_slot[slot_no].aes_ecb_tk.cleartext, 0, ESCS_AES_KEY_SIZE);
  100. m_security_slot[slot_no].aes_ecb_tk.cleartext[11] = m_security_slot[slot_no].timing.k_scaler;
  101. uint32_t k_bits_cleared_time =
  102. (m_security_slot[slot_no].timing.time_counter >> m_security_slot[slot_no].timing.k_scaler)
  103. << m_security_slot[slot_no].timing.k_scaler;
  104. m_security_slot[slot_no].aes_ecb_tk.cleartext[12] =
  105. (uint8_t)((k_bits_cleared_time >> 24) & 0xff);
  106. m_security_slot[slot_no].aes_ecb_tk.cleartext[13] =
  107. (uint8_t)((k_bits_cleared_time >> 16) & 0xff);
  108. m_security_slot[slot_no].aes_ecb_tk.cleartext[14] = (uint8_t)((k_bits_cleared_time >> 8) & 0xff);
  109. m_security_slot[slot_no].aes_ecb_tk.cleartext[15] = (uint8_t)((k_bits_cleared_time) & 0xff);
  110. err_code = nrf_crypto_aes_crypt(&m_aes_context,
  111. &g_nrf_crypto_aes_ecb_128_info,
  112. NRF_CRYPTO_ENCRYPT, // Operation
  113. m_security_slot[slot_no].aes_ecb_tk.key, // Key
  114. NULL, // IV
  115. m_security_slot[slot_no].aes_ecb_tk.cleartext, // Data in
  116. AES_ECB_CLEARTEXT_LENGTH, // Data in size
  117. m_security_slot[slot_no].aes_ecb_tk.ciphertext, // Data out
  118. &ciphertext_size); // Data out size
  119. APP_ERROR_CHECK(err_code);
  120. memcpy(m_security_slot[slot_no].eid,
  121. m_security_slot[slot_no].aes_ecb_tk.ciphertext,
  122. ES_EID_ID_LENGTH);
  123. m_security_callback(slot_no, ES_SECURITY_MSG_EID);
  124. }
  125. /**@brief Generates a temporary key with the Identity key. */
  126. static void temp_key_generate(uint8_t slot_no)
  127. {
  128. ret_code_t err_code;
  129. size_t ciphertext_size = AES_ECB_CIPHERTEXT_LENGTH;
  130. memset(m_security_slot[slot_no].aes_ecb_ik.cleartext, 0, ESCS_AES_KEY_SIZE);
  131. m_security_slot[slot_no].aes_ecb_ik.cleartext[11] = 0xFF;
  132. m_security_slot[slot_no].aes_ecb_ik.cleartext[14] =
  133. (uint8_t)((m_security_slot[slot_no].timing.time_counter >> 24) & 0xff);
  134. m_security_slot[slot_no].aes_ecb_ik.cleartext[15] =
  135. (uint8_t)((m_security_slot[slot_no].timing.time_counter >> 16) & 0xff);
  136. err_code = nrf_crypto_aes_crypt(&m_aes_context,
  137. &g_nrf_crypto_aes_ecb_128_info,
  138. NRF_CRYPTO_ENCRYPT, // Operation
  139. m_security_slot[slot_no].aes_ecb_ik.key, // Key
  140. NULL, // IV
  141. m_security_slot[slot_no].aes_ecb_ik.cleartext, // Data in
  142. AES_ECB_CLEARTEXT_LENGTH, // Data in size
  143. m_security_slot[slot_no].aes_ecb_ik.ciphertext, // Data out
  144. &ciphertext_size); // Data out size
  145. APP_ERROR_CHECK(err_code);
  146. memcpy(m_security_slot[slot_no].aes_ecb_tk.key,
  147. m_security_slot[slot_no].aes_ecb_ik.ciphertext,
  148. ESCS_AES_KEY_SIZE);
  149. }
  150. static void check_rollovers_and_update_eid(uint8_t slot_no)
  151. {
  152. if (m_security_slot[slot_no].timing.time_counter % TK_ROLLOVER == 0)
  153. {
  154. temp_key_generate(slot_no);
  155. }
  156. /*lint -save -e573 */
  157. if ((m_security_slot[slot_no].timing.time_counter %
  158. (2 << (m_security_slot[slot_no].timing.k_scaler - 1))) == 0)
  159. {
  160. eid_generate(slot_no);
  161. }
  162. /*lint -restore */
  163. }
  164. /**@brief Initialize lock code from flash. If it does not exist, copy from APP_CONFIG_LOCK_CODE.
  165. */
  166. static void lock_code_init(uint8_t * p_lock_buff)
  167. {
  168. ret_code_t err_code;
  169. err_code = es_flash_access_lock_key(p_lock_buff, ES_FLASH_ACCESS_READ);
  170. FLASH_ACCES_ERROR_CHECK_ALLOW_NOT_FOUND(err_code);
  171. // If no lock keys exist, then generate one and copy it to buffer.
  172. if (err_code == FDS_ERR_NOT_FOUND)
  173. {
  174. uint8_t lock_code[16] = APP_CONFIG_LOCK_CODE;
  175. memcpy(p_lock_buff, lock_code, sizeof(lock_code));
  176. err_code = es_flash_access_lock_key(p_lock_buff, ES_FLASH_ACCESS_WRITE);
  177. APP_ERROR_CHECK(err_code);
  178. }
  179. }
  180. void es_security_update_time(void)
  181. {
  182. static uint32_t timer_persist;
  183. uint32_t second_since_last_invocation = es_stopwatch_check(m_seconds_passed_sw_id);
  184. if (second_since_last_invocation > 0)
  185. {
  186. for (uint32_t i = 0; i < APP_MAX_EID_SLOTS; ++i)
  187. {
  188. if (m_security_slot[i].is_occupied)
  189. {
  190. m_security_slot[i].timing.time_counter += second_since_last_invocation;
  191. check_rollovers_and_update_eid(i);
  192. }
  193. }
  194. // Every 24 hr, write the new EID timer to flash.
  195. timer_persist += second_since_last_invocation;
  196. const uint32_t TWENTY_FOUR_HOURS = 60 * 60 * 24;
  197. if (timer_persist >= TWENTY_FOUR_HOURS)
  198. {
  199. for (uint32_t i = 0; i < APP_MAX_EID_SLOTS; ++i)
  200. {
  201. if (m_security_slot[i].is_occupied)
  202. {
  203. m_security_callback(i, ES_SECURITY_MSG_STORE_TIME);
  204. }
  205. }
  206. timer_persist = 0;
  207. }
  208. }
  209. }
  210. void es_security_eid_slots_restore(uint8_t slot_no,
  211. uint8_t k_scaler,
  212. uint32_t time_counter,
  213. const uint8_t * p_ik)
  214. {
  215. m_security_slot[slot_no].timing.k_scaler = k_scaler;
  216. m_security_slot[slot_no].timing.time_counter = time_counter;
  217. memcpy(m_security_slot[slot_no].aes_ecb_ik.key, p_ik, ESCS_AES_KEY_SIZE);
  218. m_security_slot[slot_no].is_occupied = true;
  219. m_security_callback(slot_no, ES_SECURITY_MSG_IK);
  220. temp_key_generate(slot_no);
  221. eid_generate(slot_no);
  222. }
  223. ret_code_t es_security_lock_code_update(uint8_t * p_ecrypted_key)
  224. {
  225. ret_code_t err_code;
  226. uint8_t temp_buff[ESCS_AES_KEY_SIZE] = {0};
  227. size_t temp_buff_size = sizeof(temp_buff);
  228. err_code = nrf_crypto_aes_crypt(&m_aes_context,
  229. &g_nrf_crypto_aes_ecb_128_info,
  230. NRF_CRYPTO_DECRYPT, // Operation
  231. m_aes_ecb_lk.key, // Key
  232. NULL, // IV
  233. p_ecrypted_key, // Data in
  234. 16, // Data in size
  235. temp_buff, // Data out
  236. &temp_buff_size); // Data out size
  237. VERIFY_SUCCESS(err_code);
  238. memcpy(m_aes_ecb_lk.key, temp_buff, ESCS_AES_KEY_SIZE);
  239. return es_flash_access_lock_key(m_aes_ecb_lk.key, ES_FLASH_ACCESS_WRITE);
  240. }
  241. void es_security_unlock_prepare(uint8_t * p_challenge)
  242. {
  243. ret_code_t err_code;
  244. size_t ciphertext_size = AES_ECB_CIPHERTEXT_LENGTH;
  245. memcpy(m_aes_ecb_lk.cleartext, p_challenge, ESCS_AES_KEY_SIZE);
  246. err_code = nrf_crypto_aes_crypt(&m_aes_context,
  247. &g_nrf_crypto_aes_ecb_128_info,
  248. NRF_CRYPTO_ENCRYPT, // Operation
  249. m_aes_ecb_lk.key, // Key
  250. NULL, // IV
  251. m_aes_ecb_lk.cleartext, // Data in
  252. AES_ECB_CLEARTEXT_LENGTH, // Data in size
  253. m_aes_ecb_lk.ciphertext, // Data out
  254. &ciphertext_size); // Data out size
  255. APP_ERROR_CHECK(err_code);
  256. }
  257. void es_security_unlock_verify(uint8_t * p_unlock_token)
  258. {
  259. if (memcmp(p_unlock_token, m_aes_ecb_lk.ciphertext, ESCS_AES_KEY_SIZE) == 0)
  260. {
  261. m_security_callback(0, ES_SECURITY_MSG_UNLOCKED);
  262. }
  263. }
  264. ret_code_t es_security_random_challenge_generate(uint8_t * p_rand_chlg_buff)
  265. {
  266. return nrf_crypto_rng_vector_generate(p_rand_chlg_buff, ESCS_AES_KEY_SIZE);
  267. }
  268. void es_security_shared_ik_receive(uint8_t slot_no, uint8_t * p_encrypted_ik, uint8_t scaler_k)
  269. {
  270. ret_code_t err_code;
  271. size_t cleartext_size = AES_ECB_CLEARTEXT_LENGTH;
  272. m_security_slot[slot_no].is_occupied = true;
  273. m_security_slot[slot_no].timing.k_scaler = scaler_k;
  274. m_security_slot[slot_no].timing.time_counter = APP_CONFIG_TIMING_INIT_VALUE;
  275. err_code = nrf_crypto_aes_crypt(&m_aes_context,
  276. &g_nrf_crypto_aes_ecb_128_info,
  277. NRF_CRYPTO_DECRYPT, // Operation
  278. m_aes_ecb_lk.key, // Key
  279. NULL, // IV
  280. p_encrypted_ik, // Data in
  281. 16, // Data in size
  282. m_security_slot[slot_no].aes_ecb_ik.key, // Data out
  283. &cleartext_size); // Data out size
  284. APP_ERROR_CHECK(err_code);
  285. temp_key_generate(slot_no);
  286. eid_generate(slot_no);
  287. m_security_callback(slot_no, ES_SECURITY_MSG_IK);
  288. }
  289. void es_security_client_pub_ecdh_receive(uint8_t slot_no, uint8_t * p_pub_ecdh, uint8_t scaler_k)
  290. {
  291. ret_code_t err_code;
  292. nrf_crypto_ecc_public_key_t phone_public; // Phone public ECDH key
  293. uint8_t beacon_public[ESCS_ECDH_KEY_SIZE]; // Beacon public ECDH key
  294. uint8_t shared[ESCS_ECDH_KEY_SIZE]; // Shared secret ECDH key
  295. uint8_t public_keys[64]; // Buffer for concatenated public keys
  296. uint8_t key_material[64]; // Buffer for holding key material
  297. uint8_t empty_check[ESCS_ECDH_KEY_SIZE] = {0};
  298. size_t beacon_public_size = sizeof(beacon_public);
  299. size_t shared_size = sizeof(shared);
  300. size_t key_material_size = sizeof(key_material);
  301. m_security_slot[slot_no].is_occupied = true;
  302. m_security_slot[slot_no].timing.k_scaler = scaler_k;
  303. m_security_slot[slot_no].timing.time_counter = APP_CONFIG_TIMING_INIT_VALUE;
  304. // Get public 32-byte service ECDH key from phone.
  305. err_code = nrf_crypto_ecc_public_key_from_raw(&g_nrf_crypto_ecc_curve25519_curve_info,
  306. &phone_public,
  307. p_pub_ecdh,
  308. ESCS_ECDH_KEY_SIZE);
  309. APP_ERROR_CHECK(err_code);
  310. // Generate key pair.
  311. err_code = nrf_crypto_ecc_key_pair_generate(&ecc_key_pair_generate_context,
  312. &g_nrf_crypto_ecc_curve25519_curve_info,
  313. &m_ecdh.ecdh_key_pair.private,
  314. &m_ecdh.ecdh_key_pair.public);
  315. APP_ERROR_CHECK(err_code);
  316. // Generate shared 32-byte ECDH secret from beacon private service ECDH key and phone public ECDH key.
  317. err_code = nrf_crypto_ecdh_compute(&ecdh_context,
  318. &m_ecdh.ecdh_key_pair.private,
  319. &phone_public,
  320. shared,
  321. &shared_size);
  322. APP_ERROR_CHECK(err_code);
  323. // Verify that the shared secret is not zero at this point, and report an error/reset if it is.
  324. if (memcmp(empty_check, shared, ESCS_ECDH_KEY_SIZE) == 0)
  325. {
  326. APP_ERROR_CHECK(NRF_ERROR_INTERNAL);
  327. }
  328. // Concatenate the resolver's public key and beacon's public key
  329. err_code = nrf_crypto_ecc_public_key_to_raw(&m_ecdh.ecdh_key_pair.public,
  330. beacon_public,
  331. &beacon_public_size);
  332. APP_ERROR_CHECK(err_code);
  333. memcpy(public_keys, p_pub_ecdh, 32);
  334. memcpy(public_keys + 32, beacon_public, 32);
  335. // Convert the shared secret to key material using HKDF-SHA256. HKDF is used with the salt set
  336. // to a concatenation of the resolver's public key and beacon's public key
  337. err_code = nrf_crypto_hkdf_calculate(&m_hmac_context,
  338. &g_nrf_crypto_hmac_sha256_info,
  339. key_material, // Output key
  340. &key_material_size, // Output key size
  341. shared, // Input key
  342. sizeof(shared), // Input key size
  343. public_keys, // Salt
  344. sizeof(public_keys), // Salt size
  345. NULL, // Additional info
  346. 0, // Additional info size
  347. NRF_CRYPTO_HKDF_EXTRACT_AND_EXPAND); // Mode
  348. APP_ERROR_CHECK(err_code);
  349. // Truncate the key material to 128 bits to convert it to an AES-128 secret key (Identity key).
  350. memcpy(m_security_slot[slot_no].aes_ecb_ik.key, key_material, ESCS_AES_KEY_SIZE);
  351. temp_key_generate(slot_no);
  352. eid_generate(slot_no);
  353. m_security_callback(slot_no, ES_SECURITY_MSG_ECDH);
  354. m_security_callback(slot_no, ES_SECURITY_MSG_IK);
  355. }
  356. void es_security_pub_ecdh_get(uint8_t slot_no, uint8_t * p_edch_buffer)
  357. {
  358. ret_code_t err_code;
  359. size_t buffer_size = ESCS_ECDH_KEY_SIZE;
  360. err_code = nrf_crypto_ecc_public_key_to_raw(&m_ecdh.ecdh_key_pair.public,
  361. p_edch_buffer,
  362. &buffer_size);
  363. APP_ERROR_CHECK(err_code);
  364. }
  365. uint32_t es_security_clock_get(uint8_t slot_no)
  366. {
  367. return m_security_slot[slot_no].timing.time_counter;
  368. }
  369. void es_security_eid_slot_destroy(uint8_t slot_no)
  370. {
  371. memset(&m_security_slot[slot_no], 0, sizeof(es_security_slot_t));
  372. }
  373. uint8_t es_security_scaler_get(uint8_t slot_no)
  374. {
  375. return m_security_slot[slot_no].timing.k_scaler;
  376. }
  377. void es_security_eid_get(uint8_t slot_no, uint8_t * p_eid_buffer)
  378. {
  379. memcpy(p_eid_buffer, m_security_slot[slot_no].eid, ES_EID_ID_LENGTH);
  380. }
  381. void es_security_encrypted_eid_id_key_get(uint8_t slot_no, uint8_t * p_key_buffer)
  382. {
  383. ret_code_t err_code;
  384. size_t ciphertext_size = AES_ECB_CIPHERTEXT_LENGTH;
  385. memcpy(m_aes_ecb_lk.cleartext, m_security_slot[slot_no].aes_ecb_ik.key, ESCS_AES_KEY_SIZE);
  386. err_code = nrf_crypto_aes_crypt(&m_aes_context,
  387. &g_nrf_crypto_aes_ecb_128_info,
  388. NRF_CRYPTO_ENCRYPT, // Operation
  389. m_aes_ecb_lk.key, // Key
  390. NULL, // IV
  391. m_aes_ecb_lk.cleartext, // Data in
  392. AES_ECB_CLEARTEXT_LENGTH, // Data in size
  393. m_aes_ecb_lk.ciphertext, // Data out
  394. &ciphertext_size); // Data out size
  395. APP_ERROR_CHECK(err_code);
  396. memcpy(p_key_buffer, m_aes_ecb_lk.ciphertext, ESCS_AES_KEY_SIZE);
  397. }
  398. void es_security_plain_eid_id_key_get(uint8_t slot_no, uint8_t * p_key_buffer)
  399. {
  400. memcpy(p_key_buffer, m_security_slot[slot_no].aes_ecb_ik.key, ESCS_AES_KEY_SIZE);
  401. }
  402. void es_security_tlm_to_etlm(uint8_t ik_slot_no, es_tlm_frame_t * p_tlm, es_etlm_frame_t * p_etlm)
  403. {
  404. ret_code_t err_code;
  405. uint8_t plain[TLM_DATA_SIZE] = {0}; // Plaintext tlm, without the frame byte and version.
  406. size_t nplain = TLM_DATA_SIZE; // Length of message plaintext.
  407. /*lint -save -e420 */
  408. memcpy(plain, &p_tlm->vbatt[0], sizeof(plain));
  409. uint8_t key[EIK_SIZE] = {0}; // Encryption/decryption key: EIK.
  410. memcpy(key, &m_security_slot[ik_slot_no].aes_ecb_ik.key[0], EIK_SIZE);
  411. /*lint -restore */
  412. uint8_t nonce[NONCE_SIZE] = {0}; // Nonce. This must not repeat for a given key.
  413. size_t nnonce = NONCE_SIZE; // Length of nonce.First 4 bytes are beacon time base with k-bits cleared.
  414. // Last two bits are randomly generated
  415. // Take the current timestamp and clear the lowest K bits, use it as nonce.
  416. uint32_t k_bits_cleared_time = (m_security_slot[ik_slot_no].timing.time_counter
  417. >> m_security_slot[ik_slot_no].timing.k_scaler)
  418. << m_security_slot[ik_slot_no].timing.k_scaler;
  419. nonce[0] = (uint8_t)((k_bits_cleared_time >> 24) & 0xff);
  420. nonce[1] = (uint8_t)((k_bits_cleared_time >> 16) & 0xff);
  421. nonce[2] = (uint8_t)((k_bits_cleared_time >> 8) & 0xff);
  422. nonce[3] = (uint8_t)((k_bits_cleared_time) & 0xff);
  423. // Generate random salt.
  424. uint8_t salt[SALT_SIZE] = {0};
  425. err_code = nrf_crypto_rng_vector_generate(salt, SALT_SIZE);
  426. APP_ERROR_CHECK(err_code);
  427. memcpy(&nonce[4], salt, SALT_SIZE);
  428. uint8_t cipher[ES_ETLM_ECRYPTED_LENGTH]; // Ciphertext output. nplain bytes are written.
  429. uint8_t tag[TAG_SIZE] = {0}; // Authentication tag. ntag bytes are written.
  430. size_t ntag = TAG_SIZE; // Length of authentication tag.
  431. // Encryption
  432. // --------------------------------------------------------------------------
  433. err_code = nrf_crypto_aead_init(&m_aead_context, &g_nrf_crypto_aes_eax_128_info, key);
  434. APP_ERROR_CHECK(err_code);
  435. err_code = nrf_crypto_aead_crypt(&m_aead_context,
  436. NRF_CRYPTO_ENCRYPT, // Operation
  437. nonce, // Nonce
  438. nnonce, // Nonce size
  439. NULL, // Additional authenticated data (adata)
  440. 0, // Additional authenticated data size
  441. plain, // Input data
  442. nplain, // Input data size
  443. cipher, // Output data
  444. tag, // MAC result output
  445. ntag); // MAC size
  446. APP_ERROR_CHECK(err_code);
  447. err_code = nrf_crypto_aead_uninit(&m_aead_context);
  448. APP_ERROR_CHECK(err_code);
  449. // Construct the eTLM.
  450. // --------------------------------------------------------------------------
  451. p_etlm->frame_type = p_tlm->frame_type;
  452. p_etlm->version = ES_TLM_VERSION_ETLM;
  453. memcpy(p_etlm->encrypted_tlm, cipher, ES_ETLM_ECRYPTED_LENGTH);
  454. memcpy((uint8_t *)&p_etlm->random_salt, salt, SALT_SIZE);
  455. memcpy((uint8_t *)&p_etlm->msg_integrity_check, tag, TAG_SIZE);
  456. }
  457. ret_code_t es_security_init(es_security_msg_cb_t security_callback)
  458. {
  459. ret_code_t err_code;
  460. if (security_callback == NULL)
  461. {
  462. return NRF_ERROR_INVALID_PARAM;
  463. }
  464. // Get lock code from 'es_app_config.h', or fetch it from flash if exists.
  465. lock_code_init(m_aes_ecb_lk.key);
  466. m_security_callback = security_callback;
  467. memset(&m_ecdh, 0, sizeof(es_security_ecdh_t));
  468. for (uint32_t i = 0; i < APP_MAX_EID_SLOTS; ++i)
  469. {
  470. m_security_slot[i].timing.time_counter = APP_CONFIG_TIMING_INIT_VALUE;
  471. }
  472. err_code = es_stopwatch_create(&m_seconds_passed_sw_id, APP_TIMER_TICKS(1000));
  473. APP_ERROR_CHECK(err_code);
  474. err_code = nrf_crypto_init();
  475. APP_ERROR_CHECK(err_code);
  476. return NRF_SUCCESS;
  477. }