Example #1
0
/**
 * @brief Initialize the CPU, the board and the peripherals
 *
 * This function is called by ESP8266 SDK when all system initializations
 * has been finished.
 */
void system_init(void)
{
    LOG_INFO("\nStarting ESP8266 CPU with ID: %08x", system_get_chip_id());
    LOG_INFO("\nSDK Version %s\n\n", system_get_sdk_version());

    /* avoid reconnection all the time */
    wifi_station_disconnect();

    /* set exception handlers */
    init_exceptions ();

    /* init random number generator */
    srand(hwrand());

    /* init flash drive */
    extern void flash_drive_init (void);
    flash_drive_init();

    /* trigger static peripheral initialization */
    periph_init();

    /* trigger board initialization */
    board_init();

    /* print the board config */
    board_print_config();
}
Example #2
0
static void rand_get_seed(struct rand_state *state,
                          uint8_t seed[CTR_DRBG_ENTROPY_LEN]) {
  if (!state->last_block_valid) {
    if (!hwrand(state->last_block, sizeof(state->last_block))) {
      CRYPTO_sysrand(state->last_block, sizeof(state->last_block));
    }
    state->last_block_valid = 1;
  }

  // We overread from /dev/urandom or RDRAND by a factor of 10 and XOR to
  // whiten.
#define FIPS_OVERREAD 10
  uint8_t entropy[CTR_DRBG_ENTROPY_LEN * FIPS_OVERREAD];

  if (!hwrand(entropy, sizeof(entropy))) {
    CRYPTO_sysrand(entropy, sizeof(entropy));
  }

  // See FIPS 140-2, section 4.9.2. This is the “continuous random number
  // generator test” which causes the program to randomly abort. Hopefully the
  // rate of failure is small enough not to be a problem in practice.
  if (CRYPTO_memcmp(state->last_block, entropy, CRNGT_BLOCK_SIZE) == 0) {
    fprintf(stderr, "CRNGT failed.\n");
    BORINGSSL_FIPS_abort();
  }

  for (size_t i = CRNGT_BLOCK_SIZE; i < sizeof(entropy);
       i += CRNGT_BLOCK_SIZE) {
    if (CRYPTO_memcmp(entropy + i - CRNGT_BLOCK_SIZE, entropy + i,
                      CRNGT_BLOCK_SIZE) == 0) {
      fprintf(stderr, "CRNGT failed.\n");
      BORINGSSL_FIPS_abort();
    }
  }
  OPENSSL_memcpy(state->last_block,
                 entropy + sizeof(entropy) - CRNGT_BLOCK_SIZE,
                 CRNGT_BLOCK_SIZE);

  OPENSSL_memcpy(seed, entropy, CTR_DRBG_ENTROPY_LEN);

  for (size_t i = 1; i < FIPS_OVERREAD; i++) {
    for (size_t j = 0; j < CTR_DRBG_ENTROPY_LEN; j++) {
      seed[j] ^= entropy[CTR_DRBG_ENTROPY_LEN * i + j];
    }
  }
}
Example #3
0
void RAND_bytes_with_additional_data(uint8_t *out, size_t out_len,
                                     const uint8_t user_additional_data[32]) {
  if (out_len == 0) {
    return;
  }

  // Additional data is mixed into every CTR-DRBG call to protect, as best we
  // can, against forks & VM clones. We do not over-read this information and
  // don't reseed with it so, from the point of view of FIPS, this doesn't
  // provide “prediction resistance”. But, in practice, it does.
  uint8_t additional_data[32];
  if (!hwrand(additional_data, sizeof(additional_data))) {
    // Without a hardware RNG to save us from address-space duplication, the OS
    // entropy is used. This can be expensive (one read per |RAND_bytes| call)
    // and so can be disabled by applications that we have ensured don't fork
    // and aren't at risk of VM cloning.
    if (!rand_fork_unsafe_buffering_enabled()) {
      CRYPTO_sysrand(additional_data, sizeof(additional_data));
    } else {
      OPENSSL_memset(additional_data, 0, sizeof(additional_data));
    }
  }

  for (size_t i = 0; i < sizeof(additional_data); i++) {
    additional_data[i] ^= user_additional_data[i];
  }

  struct rand_state stack_state;
  struct rand_state *state = rand_state_get();

  if (state == NULL) {
    // If the system is out of memory, use an ephemeral state on the
    // stack.
    state = &stack_state;
    rand_state_init(state);
  }

  if (state->calls >= kReseedInterval) {
    uint8_t seed[CTR_DRBG_ENTROPY_LEN];
    rand_get_seed(state, seed);
#if defined(BORINGSSL_FIPS)
    // Take a read lock around accesses to |state->drbg|. This is needed to
    // avoid returning bad entropy if we race with
    // |rand_state_clear_all|.
    //
    // This lock must be taken after any calls to |CRYPTO_sysrand| to avoid a
    // bug on ppc64le. glibc may implement pthread locks by wrapping user code
    // in a hardware transaction, but, on some older versions of glibc and the
    // kernel, syscalls made with |syscall| did not abort the transaction.
    CRYPTO_STATIC_MUTEX_lock_read(rand_drbg_lock_bss_get());
#endif
    if (!CTR_DRBG_reseed(&state->drbg, seed, NULL, 0)) {
      abort();
    }
    state->calls = 0;
  } else {
#if defined(BORINGSSL_FIPS)
    CRYPTO_STATIC_MUTEX_lock_read(rand_drbg_lock_bss_get());
#endif
  }

  int first_call = 1;
  while (out_len > 0) {
    size_t todo = out_len;
    if (todo > CTR_DRBG_MAX_GENERATE_LENGTH) {
      todo = CTR_DRBG_MAX_GENERATE_LENGTH;
    }

    if (!CTR_DRBG_generate(&state->drbg, out, todo, additional_data,
                           first_call ? sizeof(additional_data) : 0)) {
      abort();
    }

    out += todo;
    out_len -= todo;
    state->calls++;
    first_call = 0;
  }

  if (state == &stack_state) {
    CTR_DRBG_clear(&state->drbg);
  }

#if defined(BORINGSSL_FIPS)
  CRYPTO_STATIC_MUTEX_unlock_read(rand_drbg_lock_bss_get());
#endif

  if (state != &stack_state) {
    rand_state_put(state);
  }
}
Example #4
0
int RAND_bytes(uint8_t *buf, size_t len) {
  if (len == 0) {
    return 1;
  }

  if (!hwrand(buf, len)) {
    /* Without a hardware RNG to save us from address-space duplication, the OS
     * entropy is used directly. */
    CRYPTO_sysrand(buf, len);
    return 1;
  }

  struct rand_thread_state *state =
      CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_RAND);
  if (state == NULL) {
    state = OPENSSL_malloc(sizeof(struct rand_thread_state));
    if (state == NULL ||
        !CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_RAND, state,
                                 rand_thread_state_free)) {
      CRYPTO_sysrand(buf, len);
      return 1;
    }

    memset(state->partial_block, 0, sizeof(state->partial_block));
    state->calls_used = kMaxCallsPerRefresh;
  }

  if (state->calls_used >= kMaxCallsPerRefresh ||
      state->bytes_used >= kMaxBytesPerRefresh) {
    CRYPTO_sysrand(state->key, sizeof(state->key));
    state->calls_used = 0;
    state->bytes_used = 0;
    state->partial_block_used = sizeof(state->partial_block);
  }

  if (len >= sizeof(state->partial_block)) {
    size_t remaining = len;
    while (remaining > 0) {
      /* kMaxBytesPerCall is only 2GB, while ChaCha can handle 256GB. But this
       * is sufficient and easier on 32-bit. */
      static const size_t kMaxBytesPerCall = 0x80000000;
      size_t todo = remaining;
      if (todo > kMaxBytesPerCall) {
        todo = kMaxBytesPerCall;
      }
      uint8_t nonce[12];
      memset(nonce, 0, 4);
      memcpy(nonce + 4, &state->calls_used, sizeof(state->calls_used));
      CRYPTO_chacha_20(buf, buf, todo, state->key, nonce, 0);
      buf += todo;
      remaining -= todo;
      state->calls_used++;
    }
  } else {
    if (sizeof(state->partial_block) - state->partial_block_used < len) {
      uint8_t nonce[12];
      memset(nonce, 0, 4);
      memcpy(nonce + 4, &state->calls_used, sizeof(state->calls_used));
      CRYPTO_chacha_20(state->partial_block, state->partial_block,
                       sizeof(state->partial_block), state->key, nonce, 0);
      state->partial_block_used = 0;
    }

    unsigned i;
    for (i = 0; i < len; i++) {
      buf[i] ^= state->partial_block[state->partial_block_used++];
    }
    state->calls_used++;
  }
  state->bytes_used += len;

  return 1;
}
Example #5
0
uint32_t homekit_random() {
    return hwrand();
}
Example #6
0
uint_t random()
{
	return hwrand();
}
Example #7
0
void user_init(void) {
    uart_set_baud(0, 921600);

    printf("\n\nESP8266 UMAC\n\n");

    bridge_init();
    umac_cfg um_cfg = {
        .timer_fn = umac_impl_request_future_tick,
        .cancel_timer_fn = umac_impl_cancel_future_tick,
        .now_fn = umac_impl_now_tick,
        .tx_byte_fn = umac_impl_tx_byte,
        .tx_buf_fn = umac_impl_tx_buf,
        .rx_pkt_fn = umac_impl_rx_pkt,
        .rx_pkt_ack_fn = bridge_pkt_acked,
        .timeout_fn = bridge_timeout,
        .nonprotocol_data_fn = NULL
    };
    umac_init(&um, &um_cfg, rx_buf);

    um_tim_hdl = xTimerCreate(
                     (signed char *)"umac_tim",
                     10,
                     false,
                     (void *)um_tim_id, um_tim_cb);

    char ap_cred[128];
    struct sdk_station_config config;
    bool setup_ap = true;

    systask_init();
    device_id = 0;
    fs_init();
    if (fs_mount() >= 0) {
#if 0
        spiffs_DIR d;
        struct spiffs_dirent e;
        struct spiffs_dirent *pe = &e;

        fs_opendir("/", &d);
        while ((pe = fs_readdir(&d, pe))) {
            printf("%s [%04x] size:%i\n", pe->name, pe->obj_id, pe->size);
        }
        fs_closedir(&d);
#endif

        // read preferred ssid
        spiffs_file fd_ssid = fs_open(".ssid", SPIFFS_RDONLY, 0);
        if (fd_ssid > 0) {
            if (fs_read(fd_ssid, (uint8_t *)ap_cred, sizeof(ap_cred)) > 0) {
                fs_close(fd_ssid);
                char *nl_ix = strchr(ap_cred, '\n');
                if (nl_ix > 0) {
                    memset(&config, 0, sizeof(struct sdk_station_config));
                    strncpy((char *)&config.ssid, ap_cred, nl_ix - ap_cred);
                    char *nl_ix2 = strchr(nl_ix + 1, '\n');
                    if (nl_ix2 > 0) {
                        strncpy((char *)&config.password, nl_ix + 1, nl_ix2 - (nl_ix + 1));
                        setup_ap = false;
                    }
                }
                printf("ssid:%s\n", config.ssid);
            } // if read
            else {
                printf("could not read .ssid\n");
            }
        } // if fs_ssid
        else {
            printf("no .ssid found, running softAP\n");
        }
        // find device id or create one
        spiffs_file fd_devid = fs_open(".devid", SPIFFS_RDONLY, 0);
        if (fd_devid < 0) {
            device_id = hwrand();
            fd_devid = fs_open(".devid", SPIFFS_O_CREAT | SPIFFS_O_TRUNC | SPIFFS_O_WRONLY | SPIFFS_O_APPEND, 0);
            fs_write(fd_devid, &device_id, 4);
            printf("create devid\n");
        } else {
            fs_read(fd_devid, &device_id, 4);
        }
        fs_close(fd_devid);
        printf("devid %08x\n", device_id);

        // remove previous scan results
        fs_remove(SYSTASK_AP_SCAN_FILENAME);
    } // if mount

    if (setup_ap) {
        sdk_wifi_set_opmode(SOFTAP_MODE);

        struct ip_info ap_ip;
        IP4_ADDR(&ap_ip.ip, 192, 169, 1, 1);
        IP4_ADDR(&ap_ip.gw, 0, 0, 0, 0);
        IP4_ADDR(&ap_ip.netmask, 255, 255, 255, 0);
        sdk_wifi_set_ip_info(1, &ap_ip);


        struct sdk_softap_config ap_cfg = {
            .ssid = "WISLEEP",
            .password = "",
            .ssid_len = 7,
            .channel = 1,
            .authmode = AUTH_OPEN,
            .ssid_hidden = false,
            .max_connection = 255,
            .beacon_interval = 100
        };

        sdk_wifi_softap_set_config(&ap_cfg);

        ip_addr_t first_client_ip;
        IP4_ADDR(&first_client_ip, 192, 169, 1, 100);
        dhcpserver_start(&first_client_ip, 4);
    } else {
        // required to call wifi_set_opmode before station_set_config
        sdk_wifi_set_opmode(STATION_MODE);
        sdk_wifi_station_set_config(&config);
    }

    server_init(server_actions);
    um_mutex = xSemaphoreCreateMutex();
    xTaskCreate(uart_task, (signed char * )"uart_task", 512, NULL, 2, NULL);
    xTaskCreate(server_task, (signed char *)"server_task", 1024, NULL, 2, NULL);
}