2018-07-06 16:24:35 +00:00
|
|
|
/*
|
2018-07-18 15:41:04 +00:00
|
|
|
* Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/
|
2018-07-06 16:24:35 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the copyright holder nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
/**
|
2018-07-19 14:44:39 +00:00
|
|
|
* \addtogroup cc13xx-cc26xx-trng
|
2018-07-18 15:41:04 +00:00
|
|
|
* @{
|
2018-07-06 16:24:35 +00:00
|
|
|
*
|
2018-07-18 15:41:04 +00:00
|
|
|
* \file
|
2018-07-19 14:44:39 +00:00
|
|
|
* Implementation of True Random Number Generator for CC13xx/CC26xx.
|
|
|
|
* \author
|
|
|
|
* Edvard Pettersen <e.pettersen@ti.com>
|
2018-07-06 16:24:35 +00:00
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#include "contiki.h"
|
|
|
|
|
2018-07-19 14:44:39 +00:00
|
|
|
#include "trng-arch.h"
|
2018-07-06 16:24:35 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-07-19 14:44:39 +00:00
|
|
|
#include <ti/drivers/TRNG.h>
|
|
|
|
#include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintext.h>
|
2018-07-12 08:44:14 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-07-19 14:44:39 +00:00
|
|
|
/*
|
2018-07-20 15:20:25 +00:00
|
|
|
* Very dirty workaround because the pre-compiled TI drivers library for
|
2018-08-30 15:09:25 +00:00
|
|
|
* CC13x0/CC26x0 is missing the CryptoKey object file. This can be removed
|
|
|
|
* when the pre-compiled library includes the missing object file.
|
2018-07-19 14:44:39 +00:00
|
|
|
*/
|
|
|
|
#include <ti/devices/DeviceFamily.h>
|
|
|
|
#if (DeviceFamily_PARENT == DeviceFamily_PARENT_CC13X0_CC26X0)
|
2018-09-12 07:50:19 +00:00
|
|
|
#include <ti/drivers/cryptoutils/cryptokey/CryptoKeyPlaintextCC26XX.c>
|
2018-07-19 14:44:39 +00:00
|
|
|
#endif
|
2018-07-06 16:24:35 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-07-19 14:44:39 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
2018-07-06 16:24:35 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-07-19 14:44:39 +00:00
|
|
|
bool
|
|
|
|
trng_rand(uint8_t *entropy_buf, size_t entropy_len, uint32_t timeout_us)
|
|
|
|
{
|
2018-07-25 16:16:27 +00:00
|
|
|
TRNG_Params trng_params;
|
|
|
|
TRNG_Handle trng_handle;
|
|
|
|
CryptoKey entropy_key;
|
2018-07-19 14:44:39 +00:00
|
|
|
int_fast16_t result;
|
2018-07-18 15:41:04 +00:00
|
|
|
|
2018-07-19 14:44:39 +00:00
|
|
|
TRNG_Params_init(&trng_params);
|
|
|
|
trng_params.returnBehavior = TRNG_RETURN_BEHAVIOR_BLOCKING;
|
|
|
|
if(timeout_us != TRNG_WAIT_FOREVER) {
|
|
|
|
trng_params.timeout = timeout_us;
|
|
|
|
}
|
2018-07-18 15:41:04 +00:00
|
|
|
|
2018-07-19 14:44:39 +00:00
|
|
|
trng_handle = TRNG_open(0, &trng_params);
|
2018-07-20 15:20:25 +00:00
|
|
|
if(!trng_handle) {
|
2018-07-19 14:44:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-07-18 15:41:04 +00:00
|
|
|
|
2018-07-19 14:44:39 +00:00
|
|
|
CryptoKeyPlaintext_initBlankKey(&entropy_key, entropy_buf, entropy_len);
|
|
|
|
|
|
|
|
result = TRNG_generateEntropy(trng_handle, &entropy_key);
|
|
|
|
|
|
|
|
TRNG_close(trng_handle);
|
|
|
|
|
2018-07-24 10:16:53 +00:00
|
|
|
return result == TRNG_STATUS_SUCCESS;
|
2018-07-19 14:44:39 +00:00
|
|
|
}
|
2018-07-18 15:41:04 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-07-19 14:44:39 +00:00
|
|
|
/** @} */
|