예제 #1
0
/**@brief Decode an incoming Control Point write.
 *
 * @param[in]    p_rcvd_val Received write value.
 * @param[in]    len        Value length.
 * @param[out]   p_ctrlpt   Decoded control point structure.
 *
 * @retval NRF_ERROR_INVALID_LENGTH The supplied value length is invalid.
 * @retval NRF_ERROR_NOT_SUPPORTED  The supplied op code is not supported.
 * @retval NRF_SUCCESS              Operation successful.
 */
static ret_code_t ctrlpt_decode(uint8_t * p_rcvd_val, uint16_t len, nrf_ble_bms_ctrlpt_t * p_ctrlpt)
{
    uint16_t pos = 0;

    VERIFY_TRUE(len >= NRF_BLE_BMS_CTRLPT_MIN_LEN, NRF_ERROR_INVALID_LENGTH);
    VERIFY_TRUE(len <= NRF_BLE_BMS_CTRLPT_MAX_LEN, NRF_ERROR_INVALID_LENGTH);

    p_ctrlpt->op_code       = (nrf_ble_bms_op_t) p_rcvd_val[pos++];
    p_ctrlpt->auth_code.len = (len - pos);
    memcpy(p_ctrlpt->auth_code.code, &(p_rcvd_val[pos]), p_ctrlpt->auth_code.len);

    return NRF_SUCCESS;
}
예제 #2
0
ret_code_t nrf_crypto_rng_backend_vector_generate(void      * const p_context,
                                                  uint8_t   * const p_target,
                                                  size_t            size,
                                                  bool              use_mutex)
{
    bool                mutex_locked;
    CRYSError_t         err_code;
    ret_code_t          ret_val;
    CRYS_RND_State_t  * p_crys_rnd_state =
        &((nrf_crypto_backend_rng_context_t *)p_context)->crys_rnd_state;

    if (use_mutex)
    {
        mutex_locked = cc310_backend_mutex_trylock();
        VERIFY_TRUE(mutex_locked, NRF_ERROR_CRYPTO_BUSY);
    }

    cc310_backend_enable();

    err_code = CRYS_RND_GenerateVector(p_crys_rnd_state, size, p_target);

    cc310_backend_disable();

    ret_val = result_get(err_code);

    if (use_mutex)
    {
        cc310_backend_mutex_unlock();
    }

    return ret_val;
}
예제 #3
0
ret_code_t nrf_crypto_rng_backend_init(void * const p_context,
                                       void * const p_temp_buffer)
{
    bool                                mutex_locked;
    CRYSError_t                         err_code;
    ret_code_t                          ret_val;
    CRYS_RND_WorkBuff_t               * p_work_buffer   = (CRYS_RND_WorkBuff_t *)p_temp_buffer;
    nrf_crypto_backend_rng_context_t  * p_ctx = (nrf_crypto_backend_rng_context_t *)p_context;

    // Save time by not reinitializing an already valid CC310 RNG context.
    // (Useful for example in case the context was stored in retained memory during system OFF.)
    if (p_ctx->header.init_value == NRF_CRYPTO_RNG_CONTEXT_INIT_MAGIC_VALUE)
    {
        return NRF_SUCCESS;
    }

    mutex_locked = cc310_backend_mutex_trylock();
    VERIFY_TRUE(mutex_locked, NRF_ERROR_CRYPTO_BUSY);

    cc310_backend_enable();

    err_code = CRYS_RndInit(&p_ctx->crys_rnd_state, p_work_buffer);
    ret_val = result_get(err_code);

    cc310_backend_disable();

    cc310_backend_mutex_unlock();

    return ret_val;
}
예제 #4
0
uint16_t nrf_ble_bms_on_qwr_evt(nrf_ble_bms_t     * p_bms,
                                nrf_ble_qwr_t     * p_qwr,
                                nrf_ble_qwr_evt_t * p_evt)
{
    VERIFY_TRUE(p_bms != NULL, (NRF_BLE_QWR_REJ_REQUEST_ERR_CODE));
    VERIFY_TRUE(p_qwr != NULL, (NRF_BLE_QWR_REJ_REQUEST_ERR_CODE));
    VERIFY_TRUE(p_evt != NULL, (NRF_BLE_QWR_REJ_REQUEST_ERR_CODE));
    VERIFY_TRUE(p_evt->attr_handle == p_bms->ctrlpt_handles.value_handle,
            (NRF_BLE_QWR_REJ_REQUEST_ERR_CODE));

    if (p_evt->evt_type == NRF_BLE_QWR_EVT_AUTH_REQUEST)
    {
        return on_qwr_auth_req(p_bms, p_qwr, p_evt);
    }
    else if ((p_evt->evt_type == NRF_BLE_QWR_EVT_EXECUTE_WRITE) &&
             (p_bms->auth_status == NRF_BLE_BMS_AUTH_STATUS_ALLOWED))
    {
        return on_qwr_exec_write(p_bms, p_qwr, p_evt);
    }

    return BLE_GATT_STATUS_SUCCESS;
}
예제 #5
0
ret_code_t nrf_crypto_rng_backend_reseed(void   * const p_context,
                                         void         * p_temp_buffer,
                                         uint8_t      * p_input_data,
                                         size_t         size)
{
    bool                    mutex_locked;
    CRYSError_t             err_code;
    ret_code_t              ret_val = NRF_SUCCESS;
    CRYS_RND_WorkBuff_t   * p_work_buffer = (CRYS_RND_WorkBuff_t *)p_temp_buffer;
    CRYS_RND_State_t      * p_crys_rnd_state =
        &((nrf_crypto_backend_rng_context_t *)p_context)->crys_rnd_state;

    VERIFY_TRUE(size <= CRYS_RND_ADDITINAL_INPUT_MAX_SIZE_WORDS, NRF_ERROR_CRYPTO_INPUT_LENGTH);
    VERIFY_TRUE((size & 0x3) == 0, NRF_ERROR_CRYPTO_INTERNAL);

    mutex_locked = cc310_backend_mutex_trylock();
    VERIFY_TRUE(mutex_locked, NRF_ERROR_CRYPTO_BUSY);

    cc310_backend_enable();

    if (size > 0)
    {
        err_code = CRYS_RND_AddAdditionalInput(p_crys_rnd_state, p_input_data, size);
        ret_val = result_get(err_code);
        if (ret_val != NRF_SUCCESS)
        {
            goto exit;
        }
    }

    err_code = CRYS_RND_Reseeding(p_crys_rnd_state, p_work_buffer);
    ret_val = result_get(err_code);

exit:
    cc310_backend_disable();
    cc310_backend_mutex_unlock();
    return ret_val;
}
예제 #6
0
/**
 * @brief Function for validating all possible types of File Control TLV.
 */
__STATIC_INLINE ret_code_t nfc_t4t_file_control_tl_validate(nfc_t4t_tlv_block_t * p_file_control_tlv)
{
    switch (p_file_control_tlv->type)
    {
        case NDEF_FILE_CONTROL_TLV:
            VERIFY_TRUE(p_file_control_tlv->length == NDEF_FILE_CONTROL_TLV_LEN,
                        NRF_ERROR_INVALID_DATA);
            return NRF_SUCCESS;

        case PROPRIETARY_FILE_CONTROL_TLV:
            VERIFY_TRUE(p_file_control_tlv->length == PROPRIETARY_FILE_CONTROL_TLV_LEN,
                        NRF_ERROR_INVALID_DATA);
            return NRF_SUCCESS;

        case EXTENDED_NDEF_FILE_CONTROL_TLV:
            VERIFY_TRUE(p_file_control_tlv->length == EXTENDED_NDEF_FILE_CONTROL_TLV_LEN,
                        NRF_ERROR_INVALID_DATA);
            return NRF_SUCCESS;

        default:
            return NRF_ERROR_INVALID_DATA;
    }
}
예제 #7
0
ret_code_t nrf_ble_bms_auth_response(nrf_ble_bms_t * p_bms, bool authorize)
{
    VERIFY_PARAM_NOT_NULL(p_bms);
    VERIFY_TRUE(p_bms->auth_status == NRF_BLE_BMS_AUTH_STATUS_PENDING, NRF_ERROR_INVALID_STATE);

    if (authorize)
    {
        p_bms->auth_status = NRF_BLE_BMS_AUTH_STATUS_ALLOWED;
    }
    else
    {
        p_bms->auth_status = NRF_BLE_BMS_AUTH_STATUS_DENIED;
    }

    return NRF_SUCCESS;
}
예제 #8
0
ret_code_t nrf_crypto_backend_secp256r1_verify(
    void           * p_context,
    void     const * p_public_key,
    uint8_t  const * p_data,
    size_t           data_size,
    uint8_t  const * p_signature)
{
    ret_code_t  result;
    CRYSError_t crys_error;
    bool        mutex_locked;
    

    nrf_crypto_backend_secp256r1_verify_context_t * p_ctx =
        (nrf_crypto_backend_secp256r1_verify_context_t *)p_context;

    nrf_crypto_backend_secp256r1_public_key_t * p_pub =
        (nrf_crypto_backend_secp256r1_public_key_t *)p_public_key;

    p_ctx->user_context.init_val = NRF_CC310_BL_ECDSA_CONTEXT_INITIALIZED;

    mutex_locked = cc310_backend_mutex_trylock();
    VERIFY_TRUE(mutex_locked, NRF_ERROR_CRYPTO_BUSY);

    cc310_bl_backend_enable();

    crys_error = nrf_cc310_bl_ecdsa_verify_secp256r1(
        &p_ctx->user_context,
        &p_pub->public_key,
        (nrf_cc310_bl_ecc_signature_secp256r1_t const *)p_signature,
        p_data,
        data_size);

    cc310_bl_backend_disable();

    cc310_backend_mutex_unlock();

    result = crys_error_to_ret_code(crys_error);

    return result;
}
예제 #9
0
ret_code_t nrf_crypto_rng_backend_uninit(void   * const p_context)
{
    bool                mutex_locked;
    CRYSError_t         err_code;
    ret_code_t          ret_val;
    CRYS_RND_State_t  * p_crys_rnd_state =
        &((nrf_crypto_backend_rng_context_t *)p_context)->crys_rnd_state;


    mutex_locked = cc310_backend_mutex_trylock();
    VERIFY_TRUE(mutex_locked, NRF_ERROR_CRYPTO_BUSY);

    cc310_backend_enable();

    err_code = CRYS_RND_UnInstantiation(p_crys_rnd_state);

    cc310_backend_disable();

    ret_val = result_get(err_code);

    cc310_backend_mutex_unlock();

    return ret_val;
}
예제 #10
0
HighResolutionClock::TimePoint HighResolutionClock::Now() {
  LARGE_INTEGER large_integer;
  VERIFY_TRUE(QueryPerformanceCounter(&large_integer), "QueryPerformanceCounter failed");
  int64 nanosecond_count = (1000 * 1000 * 1000 * (large_integer.QuadPart - g_start_time_)) / g_frequency_;
  return TimePoint(HighResolutionClock::Duration(nanosecond_count));
}
예제 #11
0
HighResolutionClock::Repr HighResolutionClock::TimeSinceEpoch() {
  LARGE_INTEGER large_integer;
  VERIFY_TRUE(QueryPerformanceCounter(&large_integer), "QueryPerformanceCounter failed");
  int64 nanosecond_count = (1000 * 1000 * 1000 * (large_integer.QuadPart - g_start_time_)) / g_frequency_;
  return nanosecond_count;
}
예제 #12
0
int64 GetStartTime() {
  LARGE_INTEGER large_integer;
  VERIFY_TRUE(QueryPerformanceCounter(&large_integer), "QueryPerformanceCounter failed");
  return large_integer.QuadPart;
}
예제 #13
0
int64 GetFrequency() {
  LARGE_INTEGER large_integer;
  VERIFY_TRUE(QueryPerformanceFrequency(&large_integer), "QueryPerformanceFrequency failed");
  return large_integer.QuadPart;
}