123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606 |
- #include <stdbool.h>
- #include <stdint.h>
- #include "es_security.h"
- #include "app_timer.h"
- #include "es_flash.h"
- #include "es_stopwatch.h"
- #include "fds.h"
- #include "modes.h"
- #include "nrf_crypto.h"
- #include "nrf_soc.h"
- #define NONCE_SIZE (6)
- #define TAG_SIZE (2)
- #define SALT_SIZE (2)
- #define TLM_DATA_SIZE (ES_TLM_LENGTH - 2)
- #define EIK_SIZE (ESCS_AES_KEY_SIZE)
- #define AES_ECB_CIPHERTEXT_LENGTH (16)
- #define AES_ECB_CLEARTEXT_LENGTH (16)
- typedef struct
- {
- uint32_t time_counter;
- uint8_t k_scaler;
- } es_security_timing_t;
- typedef struct
- {
- nrf_ecb_hal_data_t aes_ecb_ik;
- nrf_ecb_hal_data_t aes_ecb_tk;
- uint8_t eid[ES_EID_ID_LENGTH];
- es_security_timing_t timing;
- bool is_occupied;
- } es_security_slot_t;
- typedef struct
- {
- nrf_crypto_ecc_private_key_t private;
- nrf_crypto_ecc_public_key_t public;
- } ecdh_key_pair_t;
- typedef struct
- {
- ecdh_key_pair_t ecdh_key_pair;
- } es_security_ecdh_t;
- static nrf_ecb_hal_data_t m_aes_ecb_lk;
- static es_security_slot_t m_security_slot[APP_MAX_EID_SLOTS];
- static es_security_ecdh_t m_ecdh;
- static es_security_msg_cb_t m_security_callback;
- static es_stopwatch_id_t m_seconds_passed_sw_id;
- static nrf_crypto_aes_context_t m_aes_context;
- static nrf_crypto_hmac_context_t m_hmac_context;
- static nrf_crypto_aead_context_t m_aead_context;
- static nrf_crypto_ecc_key_pair_generate_context_t ecc_key_pair_generate_context;
- static nrf_crypto_ecdh_context_t ecdh_context;
- static void temp_key_generate(uint8_t slot_no);
- static void eid_generate(uint8_t slot_no)
- {
- ret_code_t err_code;
- size_t ciphertext_size = AES_ECB_CIPHERTEXT_LENGTH;
- temp_key_generate(slot_no);
- memset(m_security_slot[slot_no].aes_ecb_tk.cleartext, 0, ESCS_AES_KEY_SIZE);
- m_security_slot[slot_no].aes_ecb_tk.cleartext[11] = m_security_slot[slot_no].timing.k_scaler;
- uint32_t k_bits_cleared_time =
- (m_security_slot[slot_no].timing.time_counter >> m_security_slot[slot_no].timing.k_scaler)
- << m_security_slot[slot_no].timing.k_scaler;
- m_security_slot[slot_no].aes_ecb_tk.cleartext[12] =
- (uint8_t)((k_bits_cleared_time >> 24) & 0xff);
- m_security_slot[slot_no].aes_ecb_tk.cleartext[13] =
- (uint8_t)((k_bits_cleared_time >> 16) & 0xff);
- m_security_slot[slot_no].aes_ecb_tk.cleartext[14] = (uint8_t)((k_bits_cleared_time >> 8) & 0xff);
- m_security_slot[slot_no].aes_ecb_tk.cleartext[15] = (uint8_t)((k_bits_cleared_time) & 0xff);
- err_code = nrf_crypto_aes_crypt(&m_aes_context,
- &g_nrf_crypto_aes_ecb_128_info,
- NRF_CRYPTO_ENCRYPT,
- m_security_slot[slot_no].aes_ecb_tk.key,
- NULL,
- m_security_slot[slot_no].aes_ecb_tk.cleartext,
- AES_ECB_CLEARTEXT_LENGTH,
- m_security_slot[slot_no].aes_ecb_tk.ciphertext,
- &ciphertext_size);
- APP_ERROR_CHECK(err_code);
- memcpy(m_security_slot[slot_no].eid,
- m_security_slot[slot_no].aes_ecb_tk.ciphertext,
- ES_EID_ID_LENGTH);
- m_security_callback(slot_no, ES_SECURITY_MSG_EID);
- }
- static void temp_key_generate(uint8_t slot_no)
- {
- ret_code_t err_code;
- size_t ciphertext_size = AES_ECB_CIPHERTEXT_LENGTH;
- memset(m_security_slot[slot_no].aes_ecb_ik.cleartext, 0, ESCS_AES_KEY_SIZE);
- m_security_slot[slot_no].aes_ecb_ik.cleartext[11] = 0xFF;
- m_security_slot[slot_no].aes_ecb_ik.cleartext[14] =
- (uint8_t)((m_security_slot[slot_no].timing.time_counter >> 24) & 0xff);
- m_security_slot[slot_no].aes_ecb_ik.cleartext[15] =
- (uint8_t)((m_security_slot[slot_no].timing.time_counter >> 16) & 0xff);
- err_code = nrf_crypto_aes_crypt(&m_aes_context,
- &g_nrf_crypto_aes_ecb_128_info,
- NRF_CRYPTO_ENCRYPT,
- m_security_slot[slot_no].aes_ecb_ik.key,
- NULL,
- m_security_slot[slot_no].aes_ecb_ik.cleartext,
- AES_ECB_CLEARTEXT_LENGTH,
- m_security_slot[slot_no].aes_ecb_ik.ciphertext,
- &ciphertext_size);
- APP_ERROR_CHECK(err_code);
- memcpy(m_security_slot[slot_no].aes_ecb_tk.key,
- m_security_slot[slot_no].aes_ecb_ik.ciphertext,
- ESCS_AES_KEY_SIZE);
- }
- static void check_rollovers_and_update_eid(uint8_t slot_no)
- {
- static uint32_t last_invocation_time_counter = 0;
- uint32_t scaler = 2 << (m_security_slot[slot_no].timing.k_scaler - 1);
- uint32_t diff;
- if (last_invocation_time_counter == 0)
- {
- last_invocation_time_counter = m_security_slot[slot_no].timing.time_counter;
- }
- diff = m_security_slot[slot_no].timing.time_counter - last_invocation_time_counter;
- if (diff >= scaler)
- {
-
- last_invocation_time_counter = (m_security_slot[slot_no].timing.time_counter / scaler) * scaler;
- eid_generate(slot_no);
- }
- }
- static void lock_code_init(uint8_t * p_lock_buff)
- {
- ret_code_t err_code;
- err_code = es_flash_access_lock_key(p_lock_buff, ES_FLASH_ACCESS_READ);
- FLASH_ACCES_ERROR_CHECK_ALLOW_NOT_FOUND(err_code);
-
- if (err_code == FDS_ERR_NOT_FOUND)
- {
- uint8_t lock_code[16] = APP_CONFIG_LOCK_CODE;
- memcpy(p_lock_buff, lock_code, sizeof(lock_code));
- err_code = es_flash_access_lock_key(p_lock_buff, ES_FLASH_ACCESS_WRITE);
- APP_ERROR_CHECK(err_code);
- }
- }
- void es_security_update_time(void)
- {
- static uint32_t timer_persist;
- uint32_t second_since_last_invocation = es_stopwatch_check(m_seconds_passed_sw_id);
- if (second_since_last_invocation > 0)
- {
- for (uint32_t i = 0; i < APP_MAX_EID_SLOTS; ++i)
- {
- if (m_security_slot[i].is_occupied)
- {
- m_security_slot[i].timing.time_counter += second_since_last_invocation;
- check_rollovers_and_update_eid(i);
- }
- }
-
- timer_persist += second_since_last_invocation;
- const uint32_t TWENTY_FOUR_HOURS = 60 * 60 * 24;
- if (timer_persist >= TWENTY_FOUR_HOURS)
- {
- for (uint32_t i = 0; i < APP_MAX_EID_SLOTS; ++i)
- {
- if (m_security_slot[i].is_occupied)
- {
- m_security_callback(i, ES_SECURITY_MSG_STORE_TIME);
- }
- }
- timer_persist = 0;
- }
- }
- }
- void es_security_eid_slots_restore(uint8_t slot_no,
- uint8_t k_scaler,
- uint32_t time_counter,
- const uint8_t * p_ik)
- {
- m_security_slot[slot_no].timing.k_scaler = k_scaler;
- m_security_slot[slot_no].timing.time_counter = time_counter;
- memcpy(m_security_slot[slot_no].aes_ecb_ik.key, p_ik, ESCS_AES_KEY_SIZE);
- m_security_slot[slot_no].is_occupied = true;
- m_security_callback(slot_no, ES_SECURITY_MSG_IK);
- eid_generate(slot_no);
- }
- ret_code_t es_security_lock_code_update(uint8_t * p_ecrypted_key)
- {
- ret_code_t err_code;
- uint8_t temp_buff[ESCS_AES_KEY_SIZE] = {0};
- size_t temp_buff_size = sizeof(temp_buff);
- err_code = nrf_crypto_aes_crypt(&m_aes_context,
- &g_nrf_crypto_aes_ecb_128_info,
- NRF_CRYPTO_DECRYPT,
- m_aes_ecb_lk.key,
- NULL,
- p_ecrypted_key,
- 16,
- temp_buff,
- &temp_buff_size);
- VERIFY_SUCCESS(err_code);
- memcpy(m_aes_ecb_lk.key, temp_buff, ESCS_AES_KEY_SIZE);
- return es_flash_access_lock_key(m_aes_ecb_lk.key, ES_FLASH_ACCESS_WRITE);
- }
- void es_security_unlock_prepare(uint8_t * p_challenge)
- {
- ret_code_t err_code;
- size_t ciphertext_size = AES_ECB_CIPHERTEXT_LENGTH;
- memcpy(m_aes_ecb_lk.cleartext, p_challenge, ESCS_AES_KEY_SIZE);
- err_code = nrf_crypto_aes_crypt(&m_aes_context,
- &g_nrf_crypto_aes_ecb_128_info,
- NRF_CRYPTO_ENCRYPT,
- m_aes_ecb_lk.key,
- NULL,
- m_aes_ecb_lk.cleartext,
- AES_ECB_CLEARTEXT_LENGTH,
- m_aes_ecb_lk.ciphertext,
- &ciphertext_size);
- APP_ERROR_CHECK(err_code);
- }
- void es_security_unlock_verify(uint8_t * p_unlock_token)
- {
- if (memcmp(p_unlock_token, m_aes_ecb_lk.ciphertext, ESCS_AES_KEY_SIZE) == 0)
- {
- m_security_callback(0, ES_SECURITY_MSG_UNLOCKED);
- }
- }
- ret_code_t es_security_random_challenge_generate(uint8_t * p_rand_chlg_buff)
- {
- return nrf_crypto_rng_vector_generate(p_rand_chlg_buff, ESCS_AES_KEY_SIZE);
- }
- void es_security_shared_ik_receive(uint8_t slot_no, uint8_t * p_encrypted_ik, uint8_t scaler_k)
- {
- ret_code_t err_code;
- size_t cleartext_size = AES_ECB_CLEARTEXT_LENGTH;
- m_security_slot[slot_no].is_occupied = true;
- m_security_slot[slot_no].timing.k_scaler = scaler_k;
- m_security_slot[slot_no].timing.time_counter = APP_CONFIG_TIMING_INIT_VALUE;
- err_code = nrf_crypto_aes_crypt(&m_aes_context,
- &g_nrf_crypto_aes_ecb_128_info,
- NRF_CRYPTO_DECRYPT,
- m_aes_ecb_lk.key,
- NULL,
- p_encrypted_ik,
- 16,
- m_security_slot[slot_no].aes_ecb_ik.key,
- &cleartext_size);
- APP_ERROR_CHECK(err_code);
- eid_generate(slot_no);
- m_security_callback(slot_no, ES_SECURITY_MSG_IK);
- }
- void es_security_client_pub_ecdh_receive(uint8_t slot_no, uint8_t * p_pub_ecdh, uint8_t scaler_k)
- {
- ret_code_t err_code;
- nrf_crypto_ecc_public_key_t phone_public;
- uint8_t beacon_public[ESCS_ECDH_KEY_SIZE];
- uint8_t shared[ESCS_ECDH_KEY_SIZE];
- uint8_t public_keys[64];
- uint8_t key_material[64];
- uint8_t empty_check[ESCS_ECDH_KEY_SIZE] = {0};
- size_t beacon_public_size = sizeof(beacon_public);
- size_t shared_size = sizeof(shared);
- size_t key_material_size = sizeof(key_material);
- m_security_slot[slot_no].is_occupied = true;
- m_security_slot[slot_no].timing.k_scaler = scaler_k;
- m_security_slot[slot_no].timing.time_counter = APP_CONFIG_TIMING_INIT_VALUE;
-
- err_code = nrf_crypto_ecc_public_key_from_raw(&g_nrf_crypto_ecc_curve25519_curve_info,
- &phone_public,
- p_pub_ecdh,
- ESCS_ECDH_KEY_SIZE);
- APP_ERROR_CHECK(err_code);
-
- err_code = nrf_crypto_ecc_key_pair_generate(&ecc_key_pair_generate_context,
- &g_nrf_crypto_ecc_curve25519_curve_info,
- &m_ecdh.ecdh_key_pair.private,
- &m_ecdh.ecdh_key_pair.public);
- APP_ERROR_CHECK(err_code);
-
- err_code = nrf_crypto_ecdh_compute(&ecdh_context,
- &m_ecdh.ecdh_key_pair.private,
- &phone_public,
- shared,
- &shared_size);
- APP_ERROR_CHECK(err_code);
-
- if (memcmp(empty_check, shared, ESCS_ECDH_KEY_SIZE) == 0)
- {
- APP_ERROR_CHECK(NRF_ERROR_INTERNAL);
- }
-
- err_code = nrf_crypto_ecc_public_key_to_raw(&m_ecdh.ecdh_key_pair.public,
- beacon_public,
- &beacon_public_size);
- APP_ERROR_CHECK(err_code);
- memcpy(public_keys, p_pub_ecdh, 32);
- memcpy(public_keys + 32, beacon_public, 32);
-
-
- err_code = nrf_crypto_hkdf_calculate(&m_hmac_context,
- &g_nrf_crypto_hmac_sha256_info,
- key_material,
- &key_material_size,
- shared,
- sizeof(shared),
- public_keys,
- sizeof(public_keys),
- NULL,
- 0,
- NRF_CRYPTO_HKDF_EXTRACT_AND_EXPAND);
- APP_ERROR_CHECK(err_code);
-
- memcpy(m_security_slot[slot_no].aes_ecb_ik.key, key_material, ESCS_AES_KEY_SIZE);
- eid_generate(slot_no);
- m_security_callback(slot_no, ES_SECURITY_MSG_ECDH);
- m_security_callback(slot_no, ES_SECURITY_MSG_IK);
- }
- void es_security_pub_ecdh_get(uint8_t slot_no, uint8_t * p_edch_buffer)
- {
- ret_code_t err_code;
- size_t buffer_size = ESCS_ECDH_KEY_SIZE;
- err_code = nrf_crypto_ecc_public_key_to_raw(&m_ecdh.ecdh_key_pair.public,
- p_edch_buffer,
- &buffer_size);
- APP_ERROR_CHECK(err_code);
- }
- uint32_t es_security_clock_get(uint8_t slot_no)
- {
- return m_security_slot[slot_no].timing.time_counter;
- }
- void es_security_eid_slot_destroy(uint8_t slot_no)
- {
- memset(&m_security_slot[slot_no], 0, sizeof(es_security_slot_t));
- }
- uint8_t es_security_scaler_get(uint8_t slot_no)
- {
- return m_security_slot[slot_no].timing.k_scaler;
- }
- void es_security_eid_get(uint8_t slot_no, uint8_t * p_eid_buffer)
- {
- memcpy(p_eid_buffer, m_security_slot[slot_no].eid, ES_EID_ID_LENGTH);
- }
- void es_security_encrypted_eid_id_key_get(uint8_t slot_no, uint8_t * p_key_buffer)
- {
- ret_code_t err_code;
- size_t ciphertext_size = AES_ECB_CIPHERTEXT_LENGTH;
- memcpy(m_aes_ecb_lk.cleartext, m_security_slot[slot_no].aes_ecb_ik.key, ESCS_AES_KEY_SIZE);
- err_code = nrf_crypto_aes_crypt(&m_aes_context,
- &g_nrf_crypto_aes_ecb_128_info,
- NRF_CRYPTO_ENCRYPT,
- m_aes_ecb_lk.key,
- NULL,
- m_aes_ecb_lk.cleartext,
- AES_ECB_CLEARTEXT_LENGTH,
- m_aes_ecb_lk.ciphertext,
- &ciphertext_size);
- APP_ERROR_CHECK(err_code);
- memcpy(p_key_buffer, m_aes_ecb_lk.ciphertext, ESCS_AES_KEY_SIZE);
- }
- void es_security_plain_eid_id_key_get(uint8_t slot_no, uint8_t * p_key_buffer)
- {
- memcpy(p_key_buffer, m_security_slot[slot_no].aes_ecb_ik.key, ESCS_AES_KEY_SIZE);
- }
- void es_security_tlm_to_etlm(uint8_t ik_slot_no, es_tlm_frame_t * p_tlm, es_etlm_frame_t * p_etlm)
- {
- ret_code_t err_code;
- uint8_t plain[TLM_DATA_SIZE] = {0};
- size_t nplain = TLM_DATA_SIZE;
-
- memcpy(plain, &p_tlm->vbatt[0], sizeof(plain));
- uint8_t key[EIK_SIZE] = {0};
- memcpy(key, &m_security_slot[ik_slot_no].aes_ecb_ik.key[0], EIK_SIZE);
-
- uint8_t nonce[NONCE_SIZE] = {0};
- size_t nnonce = NONCE_SIZE;
-
-
- uint32_t k_bits_cleared_time = (m_security_slot[ik_slot_no].timing.time_counter
- >> m_security_slot[ik_slot_no].timing.k_scaler)
- << m_security_slot[ik_slot_no].timing.k_scaler;
- nonce[0] = (uint8_t)((k_bits_cleared_time >> 24) & 0xff);
- nonce[1] = (uint8_t)((k_bits_cleared_time >> 16) & 0xff);
- nonce[2] = (uint8_t)((k_bits_cleared_time >> 8) & 0xff);
- nonce[3] = (uint8_t)((k_bits_cleared_time) & 0xff);
-
- uint8_t salt[SALT_SIZE] = {0};
- err_code = nrf_crypto_rng_vector_generate(salt, SALT_SIZE);
- APP_ERROR_CHECK(err_code);
- memcpy(&nonce[4], salt, SALT_SIZE);
- uint8_t cipher[ES_ETLM_ECRYPTED_LENGTH];
- uint8_t tag[TAG_SIZE] = {0};
- size_t ntag = TAG_SIZE;
-
-
- err_code = nrf_crypto_aead_init(&m_aead_context, &g_nrf_crypto_aes_eax_128_info, key);
- APP_ERROR_CHECK(err_code);
- err_code = nrf_crypto_aead_crypt(&m_aead_context,
- NRF_CRYPTO_ENCRYPT,
- nonce,
- nnonce,
- NULL,
- 0,
- plain,
- nplain,
- cipher,
- tag,
- ntag);
- APP_ERROR_CHECK(err_code);
- err_code = nrf_crypto_aead_uninit(&m_aead_context);
- APP_ERROR_CHECK(err_code);
-
-
- p_etlm->frame_type = p_tlm->frame_type;
- p_etlm->version = ES_TLM_VERSION_ETLM;
- memcpy(p_etlm->encrypted_tlm, cipher, ES_ETLM_ECRYPTED_LENGTH);
- memcpy((uint8_t *)&p_etlm->random_salt, salt, SALT_SIZE);
- memcpy((uint8_t *)&p_etlm->msg_integrity_check, tag, TAG_SIZE);
- }
- ret_code_t es_security_init(es_security_msg_cb_t security_callback)
- {
- ret_code_t err_code;
- if (security_callback == NULL)
- {
- return NRF_ERROR_INVALID_PARAM;
- }
-
- lock_code_init(m_aes_ecb_lk.key);
- m_security_callback = security_callback;
- memset(&m_ecdh, 0, sizeof(es_security_ecdh_t));
- for (uint32_t i = 0; i < APP_MAX_EID_SLOTS; ++i)
- {
- m_security_slot[i].timing.time_counter = APP_CONFIG_TIMING_INIT_VALUE;
- }
- err_code = es_stopwatch_create(&m_seconds_passed_sw_id, APP_TIMER_TICKS(1000));
- APP_ERROR_CHECK(err_code);
- err_code = nrf_crypto_init();
- APP_ERROR_CHECK(err_code);
- return NRF_SUCCESS;
- }
|