/*
 * Checks whether key is initialized and triggers key initialization if necessary.
 * Returns 1 if key is initialized and 0 otherwise.
 */
static int keyInitialization() {
  if (checkedInitializedKey == 0) {
    #ifdef RESET_KEY
    key_flash_erase_keying_material();
    #endif
    key_flash_restore_keying_material(&initializedKey, 1, AES_128_KEY_LENGTH);
    if (initializedKey != 1) initializedKey = 0;
    checkedInitializedKey = 1;
  }

  // Check whether key was already initialized
  if (initializedKey == 1) {
    return 1;
  }

  // Check whether key initialization was already triggered
  if (keyInitializationTriggered == 0) {
    printf("[Adaptivesec_Driver_Wrapper] Trigger key initialization.\n");
    // Trigger light app
    process_start(&light_app_process, NULL);

    keyInitializationTriggered = 1;
    return 0;
  }

  // Check whether key was initialized
  uint8_t initialized = 0;
  key_flash_restore_keying_material(&initialized, 1, AES_128_KEY_LENGTH);
  if (initialized == 1) {
    printf("[Adaptivesec_Driver_Wrapper] Key is initialized.\n");
    initializedKey = 1;
    // Causes a recursive call to keyInitialization, which is fine as initializedKey is now 1
    init();
    return 1;
  }

  // Key initialization was already triggered but key is not initialized yet
  return 0;
}
Esempio n. 2
0
/*---------------------------------------------------------------------------*/
static uint8_t *
get_secret_with(const linkaddr_t *addr)
{
#if LINKADDR_SIZE == 2
  if(addr->u16 > FULLY_MAX_NODES) {
    return NULL;
  }
  key_flash_restore_keying_material(key,
      FULLY_PAIRWISE_KEY_LEN,
      addr->u16 * FULLY_PAIRWISE_KEY_LEN);
  return key;
#else /* LINKADDR_SIZE == 2 */
  return NULL;
#endif /* LINKADDR_SIZE == 2 */
}