NXP / Mbed 2 deprecated mcr20_connectivity_test

Dependencies:   fsl_phy_mcr20a fsl_smac mbed-rtos mbed

Fork of mcr20_connectivity_test by Freescale

Modes of Operation

The MCR20A Connectivity Test application has five main features:

Continuous Tests

This menu option displays several test suites

  • IDLE: This option sets the transceiver and all the state machines to idle.
  • Burst PRBS Transmission using packet mode: This option continuously sends packets which contain a pseudo-random payload of fixed length.
  • Continuous Modulated Transmission: This option allows the user to select between modulating 1’s, 0’s, or a pseudo-random sequence (PN) and sending them OTA continuously (in continuous mode).
  • Continuous Unmodulated Transmission: This option allows the user to send an unmodulated signal OTA having the frequency equal to the central frequency of the currently selected channel.
  • Continuous Reception: This test places the transceiver in reception and dumps the payload bytes of the received packets to the TERM in ASCII-converted hexadecimal characters.
  • Continuous Energy Detect: This option launches consecutive energy detect requests at fixed hard-coded intervals for the current channel, and prints their values to the TERM.
  • Continuous Scan: This option is similar to the previous one, except that at each iteration it obtains the energy values on all channels.
  • Continuous CCA: This option launches consecutive CCA requests for the currently selected channel at a fixed. hard-coded interval, and prints “Idle” or “Busy” depending on the CCA result.

Packet Error Rate

This menu option displays a configuration menu for testing the packet error rate. The menu displayed also depends on the ‘r’ or ‘t’ shortcut key. If ‘r’ is pressed, the following menu is for PER RX, otherwise it is for PER TX. For example, if two MCR20A platforms have Connectivity Test loaded, one of the boards can be set in RX and the other in TX as in the following figures.

Range Test

This test displays a configuration menu that performs a ‘ping-pong’ test to aid the user in determining the range (as distance between two platforms) in which the MCR20A platform can function properly. The sub-menu also depends on the ‘r’ and ‘t’ shortcuts so that one of the platforms can be the initializer (first to start a TX) and the other can respond to requests. The test is started and stopped only by user intervention and during its execution it will display the signal strength for each received packet. At the end of the test, the platform configured as the initializer (TX) displays a summary of how many packets were lost and what was the average RSSI.

Radio Registers Edit

This menu allows the user to read-write transceiver registers and to dump all address-value pairs from the transceiver registers to the TERM. The described features are accessible through the entries of this menu. For each access request (read or write) to a certain register, the register address is validated partially, and it is the responsibility of the user to access an existing register. For example, if the last accessible register is at 0xFD, the application only validates that the address is in the unsigned char range, but the user has the possibility to request register 0xFF. To ensure that a proper range is used, the user should first use the dump register feature to see the valid address ranges.

Carrier Sense and Transmission Control

This menu allows the user to choose between two tests. The former is the Carrier Sense test, which performs ED continuously until the ED value is above the CCA threshold (configured using ‘k’ and ‘l’ shortcuts) and then transmits a packet which contains pseudo-random data with the payload size configured using ‘n’ and ‘m’ shortcuts. The latter is the Transmission Control test, which displays a selection menu for number of packets identical with the one in PER TX test, then prompts the user to enter a decimal value resembling the inter-packet delay in milliseconds. After that, the application starts sending the selected number of packets with the selected inter-packet delay, using pseudo-random data for the payload with the size configured with ‘n’ and ‘m’ shortcuts.

Keys Usage

The following keys have the effect described below:

  • ‘t’ : Brings up the configuration menu for the transmitter in both PER and Range tests.
  • ‘r’ : Brings up the configuration menu for the receiver in both PER and Range tests.
  • ‘q’ : Increments channel number. If pressed when the current channel is 26, the channel number changes to 11.
  • ‘w’ : Decrements channel number. If pressed when the current channel is 11, the channel number will change to 26.
  • ‘a’ : Increments output power value. If output power is at maximum and this key is pressed, the output power goes to the minimum (in this case 0x03).
  • ‘s’ : Decrements output power value. If output power is at minimum and this key is pressed, the output power goes to the maximum (in this case 0x1F). These are not directly mapped to dBm values. Instead the output power value is written to the appropriate register. The user should consult the reference manual to determine the relationship between selected value and power in dBm.
  • ‘n’ : Increments the length of the payload. This value is used in both PER TX test to build-up the payload and in Transmission Control test for the same reason.
  • ‘m’ : Decrements the length of the payload. Incrementation and decrementation are performed in the [17, 116] interval. All overflows at one end lead to setting the other end’s value.
  • ‘k’ : Increments the CCA threshold for the Carrier Sense test. In this test the CCA before TX algorithm is implemented at application level, and the channel idle threshold is established using this parameter.
  • ‘l’ : Decrements the CCA threshold for the Carrier Sense test.

Documentation

SMAC Demo Applications User Guide

Committer:
andreikovacs
Date:
Thu Jul 16 07:29:15 2015 +0000
Revision:
0:4eb2240dbd22
Initial commit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andreikovacs 0:4eb2240dbd22 1 /*!
andreikovacs 0:4eb2240dbd22 2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
andreikovacs 0:4eb2240dbd22 3 * All rights reserved.
andreikovacs 0:4eb2240dbd22 4 *
andreikovacs 0:4eb2240dbd22 5 * \file RNG.c
andreikovacs 0:4eb2240dbd22 6 * RNG implementation file for the ARM CORTEX-M4 processor
andreikovacs 0:4eb2240dbd22 7 *
andreikovacs 0:4eb2240dbd22 8 * Redistribution and use in source and binary forms, with or without modification,
andreikovacs 0:4eb2240dbd22 9 * are permitted provided that the following conditions are met:
andreikovacs 0:4eb2240dbd22 10 *
andreikovacs 0:4eb2240dbd22 11 * o Redistributions of source code must retain the above copyright notice, this list
andreikovacs 0:4eb2240dbd22 12 * of conditions and the following disclaimer.
andreikovacs 0:4eb2240dbd22 13 *
andreikovacs 0:4eb2240dbd22 14 * o Redistributions in binary form must reproduce the above copyright notice, this
andreikovacs 0:4eb2240dbd22 15 * list of conditions and the following disclaimer in the documentation and/or
andreikovacs 0:4eb2240dbd22 16 * other materials provided with the distribution.
andreikovacs 0:4eb2240dbd22 17 *
andreikovacs 0:4eb2240dbd22 18 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
andreikovacs 0:4eb2240dbd22 19 * contributors may be used to endorse or promote products derived from this
andreikovacs 0:4eb2240dbd22 20 * software without specific prior written permission.
andreikovacs 0:4eb2240dbd22 21 *
andreikovacs 0:4eb2240dbd22 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
andreikovacs 0:4eb2240dbd22 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
andreikovacs 0:4eb2240dbd22 24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
andreikovacs 0:4eb2240dbd22 25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
andreikovacs 0:4eb2240dbd22 26 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
andreikovacs 0:4eb2240dbd22 27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
andreikovacs 0:4eb2240dbd22 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
andreikovacs 0:4eb2240dbd22 29 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
andreikovacs 0:4eb2240dbd22 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
andreikovacs 0:4eb2240dbd22 31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
andreikovacs 0:4eb2240dbd22 32 */
andreikovacs 0:4eb2240dbd22 33
andreikovacs 0:4eb2240dbd22 34
andreikovacs 0:4eb2240dbd22 35 #include "RNG_Interface.h"
andreikovacs 0:4eb2240dbd22 36 #include "fsl_device_registers.h"
andreikovacs 0:4eb2240dbd22 37 #include "fsl_clock_manager.h"
andreikovacs 0:4eb2240dbd22 38 //#include "SecLib.h"
andreikovacs 0:4eb2240dbd22 39 #include "FunctionLib.h"
andreikovacs 0:4eb2240dbd22 40
andreikovacs 0:4eb2240dbd22 41 #if (gSecLib_HWSupport_d == gSecLib_MMCAUSupport_d)
andreikovacs 0:4eb2240dbd22 42 //#include "mmcau_interface.h"
andreikovacs 0:4eb2240dbd22 43 #endif
andreikovacs 0:4eb2240dbd22 44
andreikovacs 0:4eb2240dbd22 45 #ifndef gRNG_UsePhyRngForInitialSeed_d
andreikovacs 0:4eb2240dbd22 46 #define gRNG_UsePhyRngForInitialSeed_d 0
andreikovacs 0:4eb2240dbd22 47 #endif
andreikovacs 0:4eb2240dbd22 48
andreikovacs 0:4eb2240dbd22 49 #if (gRNG_HWSupport_d == gRNG_NoHWSupport_d)
andreikovacs 0:4eb2240dbd22 50 uint32_t mRandomNumber;
andreikovacs 0:4eb2240dbd22 51
andreikovacs 0:4eb2240dbd22 52 #if gRNG_UsePhyRngForInitialSeed_d
andreikovacs 0:4eb2240dbd22 53 extern void PhyGetRandomNo(uint32_t *pRandomNo);
andreikovacs 0:4eb2240dbd22 54 #endif
andreikovacs 0:4eb2240dbd22 55 #endif
andreikovacs 0:4eb2240dbd22 56
andreikovacs 0:4eb2240dbd22 57 /************************************************************************************
andreikovacs 0:4eb2240dbd22 58 *************************************************************************************
andreikovacs 0:4eb2240dbd22 59 * Private macros
andreikovacs 0:4eb2240dbd22 60 *************************************************************************************
andreikovacs 0:4eb2240dbd22 61 ************************************************************************************/
andreikovacs 0:4eb2240dbd22 62 #define mPRNG_NoOfBits_c (160)
andreikovacs 0:4eb2240dbd22 63 #define mPRNG_NoOfBytes_c (mPRNG_NoOfBits_c/8)
andreikovacs 0:4eb2240dbd22 64 #define mPRNG_NoOfLongWords_c (mPRNG_NoOfBits_c/32)
andreikovacs 0:4eb2240dbd22 65
andreikovacs 0:4eb2240dbd22 66 /************************************************************************************
andreikovacs 0:4eb2240dbd22 67 *************************************************************************************
andreikovacs 0:4eb2240dbd22 68 * Private memory declarations
andreikovacs 0:4eb2240dbd22 69 *************************************************************************************
andreikovacs 0:4eb2240dbd22 70 ************************************************************************************/
andreikovacs 0:4eb2240dbd22 71 static uint32_t XKEY[mPRNG_NoOfLongWords_c];
andreikovacs 0:4eb2240dbd22 72 static uint32_t mPRNG_Requests = gRngMaxRequests_d;
andreikovacs 0:4eb2240dbd22 73
andreikovacs 0:4eb2240dbd22 74 /************************************************************************************
andreikovacs 0:4eb2240dbd22 75 *************************************************************************************
andreikovacs 0:4eb2240dbd22 76 * Public functions
andreikovacs 0:4eb2240dbd22 77 *************************************************************************************
andreikovacs 0:4eb2240dbd22 78 ************************************************************************************/
andreikovacs 0:4eb2240dbd22 79
andreikovacs 0:4eb2240dbd22 80 /******************************************************************************
andreikovacs 0:4eb2240dbd22 81 * Name: RNG_Init()
andreikovacs 0:4eb2240dbd22 82 * Description: Initialize the RNG HW module
andreikovacs 0:4eb2240dbd22 83 * Parameter(s): -
andreikovacs 0:4eb2240dbd22 84 * Return: Status of the RNG module
andreikovacs 0:4eb2240dbd22 85 ******************************************************************************/
andreikovacs 0:4eb2240dbd22 86 uint8_t RNG_Init(void)
andreikovacs 0:4eb2240dbd22 87 {
andreikovacs 0:4eb2240dbd22 88 #if gRNG_HWSupport_d == gRNG_RNGAHWSupport_d
andreikovacs 0:4eb2240dbd22 89 CLOCK_SYS_EnableRngaClock(0);
andreikovacs 0:4eb2240dbd22 90 /* Mask Interrupts and start RNG */
andreikovacs 0:4eb2240dbd22 91 RNG_CR = RNG_CR_INTM_MASK | RNG_CR_HA_MASK | RNG_CR_GO_MASK;
andreikovacs 0:4eb2240dbd22 92
andreikovacs 0:4eb2240dbd22 93 #elif gRNG_HWSupport_d == gRNG_RNGBHWSupport_d
andreikovacs 0:4eb2240dbd22 94 CLOCK_SYS_EnableRngbClock(0);
andreikovacs 0:4eb2240dbd22 95 /* Execute a SW Reset */
andreikovacs 0:4eb2240dbd22 96 RNG_CMD |= RNG_CMD_SR_MASK;
andreikovacs 0:4eb2240dbd22 97
andreikovacs 0:4eb2240dbd22 98 /* Mask Interrupts */
andreikovacs 0:4eb2240dbd22 99 RNG_CR = RNG_CR_MASKDONE_MASK | RNG_CR_MASKERR_MASK;
andreikovacs 0:4eb2240dbd22 100
andreikovacs 0:4eb2240dbd22 101 /* Start Self Test and Seed Generation */
andreikovacs 0:4eb2240dbd22 102 RNG_CMD = RNG_CMD_ST_MASK | RNG_CMD_GS_MASK;
andreikovacs 0:4eb2240dbd22 103
andreikovacs 0:4eb2240dbd22 104 /* Wait for Self Test and Seed Generation to be done*/
andreikovacs 0:4eb2240dbd22 105 while (RNG_CMD & (RNG_CMD_ST_MASK | RNG_CMD_GS_MASK));
andreikovacs 0:4eb2240dbd22 106
andreikovacs 0:4eb2240dbd22 107 /* Enable RNG Auto-Reseed */
andreikovacs 0:4eb2240dbd22 108 RNG_CR |= RNG_CR_AR_MASK;
andreikovacs 0:4eb2240dbd22 109
andreikovacs 0:4eb2240dbd22 110 /* Check for Errors */
andreikovacs 0:4eb2240dbd22 111 if ( RNG_SR & RNG_SR_ERR_MASK )
andreikovacs 0:4eb2240dbd22 112 {
andreikovacs 0:4eb2240dbd22 113 return (uint8_t)(RNG_ESR);
andreikovacs 0:4eb2240dbd22 114 }
andreikovacs 0:4eb2240dbd22 115 #elif gRNG_HWSupport_d == gRNG_TRNGHWSupport_d
andreikovacs 0:4eb2240dbd22 116 // uint32_t temp;
andreikovacs 0:4eb2240dbd22 117 //
andreikovacs 0:4eb2240dbd22 118 // SIM_SCGC6 |= SIM_SCGC6_TRNG_MASK;
andreikovacs 0:4eb2240dbd22 119 //
andreikovacs 0:4eb2240dbd22 120 // /* Reset TRNG registers to default values */
andreikovacs 0:4eb2240dbd22 121 // HW_TRNG_RTMCTL_WR(gTRNG_BaseAddr_c, BM_TRNG_RTMCTL_RST_DEF | BM_TRNG_RTMCTL_PRGM);
andreikovacs 0:4eb2240dbd22 122 //
andreikovacs 0:4eb2240dbd22 123 // /* Enable Entropy Valid IRQ */
andreikovacs 0:4eb2240dbd22 124 // //disallow device to sleep, allow device to sleep in ISR
andreikovacs 0:4eb2240dbd22 125 // //HW_TRNG_SA_TRNG_INT_MASK_SET(gTRNG_BaseAddr_c, BM_TRNG_SA_TRNG_INT_MASK_SA_TRNG_SBS_ENTROPY_VALID);
andreikovacs 0:4eb2240dbd22 126 //
andreikovacs 0:4eb2240dbd22 127 // /* Set TRNG in Run mode, and enable entropy read access */
andreikovacs 0:4eb2240dbd22 128 // temp = HW_TRNG_RTMCTL_RD(gTRNG_BaseAddr_c);
andreikovacs 0:4eb2240dbd22 129 // temp &= ~(BM_TRNG_RTMCTL_PRGM | BM_TRNG_RTMCTL_ERR);
andreikovacs 0:4eb2240dbd22 130 // temp |= BM_TRNG_RTMCTL_TRNG_ACC;
andreikovacs 0:4eb2240dbd22 131 // HW_TRNG_RTMCTL_WR(gTRNG_BaseAddr_c, temp);
andreikovacs 0:4eb2240dbd22 132 #else
andreikovacs 0:4eb2240dbd22 133 #if gRNG_UsePhyRngForInitialSeed_d
andreikovacs 0:4eb2240dbd22 134 PhyGetRandomNo(&mRandomNumber);
andreikovacs 0:4eb2240dbd22 135 #else
andreikovacs 0:4eb2240dbd22 136 mRandomNumber = HW_SIM_UIDL_RD(SIM_BASE);
andreikovacs 0:4eb2240dbd22 137 #endif
andreikovacs 0:4eb2240dbd22 138 #endif /* gRNG_HwSupport_d == 1 */
andreikovacs 0:4eb2240dbd22 139
andreikovacs 0:4eb2240dbd22 140 /* Init Successfull */
andreikovacs 0:4eb2240dbd22 141 return gRngSuccess_d;
andreikovacs 0:4eb2240dbd22 142 }
andreikovacs 0:4eb2240dbd22 143
andreikovacs 0:4eb2240dbd22 144 /******************************************************************************
andreikovacs 0:4eb2240dbd22 145 * Name: RNG_HwGetRandomNo()
andreikovacs 0:4eb2240dbd22 146 * Description: Read a random number from the HW RNG module
andreikovacs 0:4eb2240dbd22 147 * Parameter(s): [OUT] pRandomNo - pointer to location where the RN will be stored
andreikovacs 0:4eb2240dbd22 148 * Return: status of the RNG module
andreikovacs 0:4eb2240dbd22 149 ******************************************************************************/
andreikovacs 0:4eb2240dbd22 150 #if (gRNG_HWSupport_d != gRNG_NoHWSupport_d)
andreikovacs 0:4eb2240dbd22 151 static uint8_t RNG_HwGetRandomNo(uint32_t* pRandomNo)
andreikovacs 0:4eb2240dbd22 152 {
andreikovacs 0:4eb2240dbd22 153 #if gRNG_HWSupport_d == gRNG_RNGAHWSupport_d
andreikovacs 0:4eb2240dbd22 154 /* If output register is empty, wait for a new random number */
andreikovacs 0:4eb2240dbd22 155 while ( ((RNG_SR & RNG_SR_OREG_LVL_MASK) >> RNG_SR_OREG_LVL_SHIFT) == 0 );
andreikovacs 0:4eb2240dbd22 156
andreikovacs 0:4eb2240dbd22 157 /* Copy the output of RNG module */
andreikovacs 0:4eb2240dbd22 158 *pRandomNo = RNG_OR;
andreikovacs 0:4eb2240dbd22 159
andreikovacs 0:4eb2240dbd22 160 #elif gRNG_HWSupport_d == gRNG_RNGBHWSupport_d
andreikovacs 0:4eb2240dbd22 161 /* Check for Errors */
andreikovacs 0:4eb2240dbd22 162 if ( RNG_SR & RNG_SR_ERR_MASK )
andreikovacs 0:4eb2240dbd22 163 return (uint8_t)(RNG_ESR);
andreikovacs 0:4eb2240dbd22 164
andreikovacs 0:4eb2240dbd22 165 /* If output FIFO is empty, wait for a new random number */
andreikovacs 0:4eb2240dbd22 166 while (((RNG_SR & RNG_SR_FIFO_LVL_MASK) >> RNG_SR_FIFO_LVL_SHIFT) == 0 );
andreikovacs 0:4eb2240dbd22 167
andreikovacs 0:4eb2240dbd22 168 /* Copy the output of RNG module */
andreikovacs 0:4eb2240dbd22 169 *pRandomNo = RNG_OUT;
andreikovacs 0:4eb2240dbd22 170
andreikovacs 0:4eb2240dbd22 171 #elif gRNG_HWSupport_d == gRNG_TRNGHWSupport_d
andreikovacs 0:4eb2240dbd22 172 static uint8_t entropyIdx = 0;
andreikovacs 0:4eb2240dbd22 173
andreikovacs 0:4eb2240dbd22 174 /* wait for entropy to be generated or for an error */
andreikovacs 0:4eb2240dbd22 175 while( !(HW_TRNG_RTMCTL_RD(gTRNG_BaseAddr_c) & (BM_TRNG_RTMCTL_ENT_VAL | BM_TRNG_RTMCTL_ERR)) );
andreikovacs 0:4eb2240dbd22 176
andreikovacs 0:4eb2240dbd22 177 if( HW_TRNG_RTMCTL_RD(gTRNG_BaseAddr_c) & BM_TRNG_RTMCTL_ERR )
andreikovacs 0:4eb2240dbd22 178 return gRngInternalError_d;
andreikovacs 0:4eb2240dbd22 179
andreikovacs 0:4eb2240dbd22 180 *pRandomNo = HW_TRNG_RTENTan_RD(gTRNG_BaseAddr_c, entropyIdx);
andreikovacs 0:4eb2240dbd22 181 if( ++entropyIdx == 16 )
andreikovacs 0:4eb2240dbd22 182 {
andreikovacs 0:4eb2240dbd22 183 entropyIdx = 0;
andreikovacs 0:4eb2240dbd22 184 //disallow device to sleep
andreikovacs 0:4eb2240dbd22 185 }
andreikovacs 0:4eb2240dbd22 186 #endif
andreikovacs 0:4eb2240dbd22 187
andreikovacs 0:4eb2240dbd22 188 return gRngSuccess_d;
andreikovacs 0:4eb2240dbd22 189 }
andreikovacs 0:4eb2240dbd22 190 #endif /* gRNG_HwSupport_d */
andreikovacs 0:4eb2240dbd22 191
andreikovacs 0:4eb2240dbd22 192
andreikovacs 0:4eb2240dbd22 193 /******************************************************************************
andreikovacs 0:4eb2240dbd22 194 * Name: RNG_GetRandomNo()
andreikovacs 0:4eb2240dbd22 195 * Description: Read a random number from RNG module or from 802.15.4 PHY
andreikovacs 0:4eb2240dbd22 196 * Parameter(s): [OUT] pRandomNo - pointer to location where the RN will be stored
andreikovacs 0:4eb2240dbd22 197 * Return: none
andreikovacs 0:4eb2240dbd22 198 ******************************************************************************/
andreikovacs 0:4eb2240dbd22 199 void RNG_GetRandomNo(uint32_t* pRandomNo)
andreikovacs 0:4eb2240dbd22 200 {
andreikovacs 0:4eb2240dbd22 201 /* Check for NULL pointers */
andreikovacs 0:4eb2240dbd22 202 if (NULL == pRandomNo)
andreikovacs 0:4eb2240dbd22 203 return;
andreikovacs 0:4eb2240dbd22 204
andreikovacs 0:4eb2240dbd22 205 #if (gRNG_HWSupport_d == gRNG_NoHWSupport_d)
andreikovacs 0:4eb2240dbd22 206 mRandomNumber = (mRandomNumber * 6075) + 1283;
andreikovacs 0:4eb2240dbd22 207 FLib_MemCpy(pRandomNo, &mRandomNumber, sizeof(uint32_t));
andreikovacs 0:4eb2240dbd22 208 #else
andreikovacs 0:4eb2240dbd22 209 (void)RNG_HwGetRandomNo(pRandomNo);
andreikovacs 0:4eb2240dbd22 210 #endif
andreikovacs 0:4eb2240dbd22 211 }
andreikovacs 0:4eb2240dbd22 212
andreikovacs 0:4eb2240dbd22 213 /******************************************************************************
andreikovacs 0:4eb2240dbd22 214 * Name: RNG_SetPseudoRandomNoSeed()
andreikovacs 0:4eb2240dbd22 215 * Description: Initialize seed for the PRNG algorithm.
andreikovacs 0:4eb2240dbd22 216 * Parameter(s):
andreikovacs 0:4eb2240dbd22 217 * pSeed - pointer to a buffer containing 20 bytes (160 bits).
andreikovacs 0:4eb2240dbd22 218 * Can be set using the RNG_GetRandomNo() function.
andreikovacs 0:4eb2240dbd22 219 * Return: None
andreikovacs 0:4eb2240dbd22 220 ******************************************************************************/
andreikovacs 0:4eb2240dbd22 221 void RNG_SetPseudoRandomNoSeed(uint8_t* pSeed)
andreikovacs 0:4eb2240dbd22 222 {
andreikovacs 0:4eb2240dbd22 223 mPRNG_Requests = 1;
andreikovacs 0:4eb2240dbd22 224 FLib_MemCpy( XKEY, pSeed, mPRNG_NoOfBytes_c );
andreikovacs 0:4eb2240dbd22 225 }
andreikovacs 0:4eb2240dbd22 226
andreikovacs 0:4eb2240dbd22 227 #if 0
andreikovacs 0:4eb2240dbd22 228 /******************************************************************************
andreikovacs 0:4eb2240dbd22 229 * Name: RNG_GetRandomNo()
andreikovacs 0:4eb2240dbd22 230 *
andreikovacs 0:4eb2240dbd22 231 * Description: Pseudo Random Number Generator (PRNG) implementation
andreikovacs 0:4eb2240dbd22 232 * according to NIST FIPS Publication 186-2, APPENDIX 3
andreikovacs 0:4eb2240dbd22 233 *
andreikovacs 0:4eb2240dbd22 234 * Let x be the signer's private key. The following may be used to generate m values of x:
andreikovacs 0:4eb2240dbd22 235 * Step 1. Choose a new, secret value for the seed-key, XKEY.
andreikovacs 0:4eb2240dbd22 236 * Step 2. In hexadecimal notation let
andreikovacs 0:4eb2240dbd22 237 * t = 67452301 EFCDAB89 98BADCFE 10325476 C3D2E1F0.
andreikovacs 0:4eb2240dbd22 238 * This is the initial value for H0 || H1 || H2 || H3 || H4 in the SHS.
andreikovacs 0:4eb2240dbd22 239 * Step 3. For j = 0 to m - 1 do
andreikovacs 0:4eb2240dbd22 240 * a. XSEEDj = optional user input.
andreikovacs 0:4eb2240dbd22 241 * b. XVAL = (XKEY + XSEEDj) mod 2^b
andreikovacs 0:4eb2240dbd22 242 * c. xj = G(t,XVAL) mod q
andreikovacs 0:4eb2240dbd22 243 * d. XKEY = (1 + XKEY + xj) mod 2^b
andreikovacs 0:4eb2240dbd22 244 *
andreikovacs 0:4eb2240dbd22 245 * Parameter(s):
andreikovacs 0:4eb2240dbd22 246 * pOut - pointer to the output buffer
andreikovacs 0:4eb2240dbd22 247 * outBytes - the number of bytes to be copyed (1-20)
andreikovacs 0:4eb2240dbd22 248 * pXSEED - optional user SEED. Should be NULL if not used.
andreikovacs 0:4eb2240dbd22 249 *
andreikovacs 0:4eb2240dbd22 250 * Return: The number of bytes copied or -1 if reseed is needed
andreikovacs 0:4eb2240dbd22 251 ******************************************************************************/
andreikovacs 0:4eb2240dbd22 252 int16_t RNG_GetPseudoRandomNo(uint8_t* pOut, uint8_t outBytes, uint8_t* pXSEED)
andreikovacs 0:4eb2240dbd22 253 {
andreikovacs 0:4eb2240dbd22 254 uint32_t i;
andreikovacs 0:4eb2240dbd22 255 sha1Context_t ctx;
andreikovacs 0:4eb2240dbd22 256
andreikovacs 0:4eb2240dbd22 257 if (mPRNG_Requests == gRngMaxRequests_d)
andreikovacs 0:4eb2240dbd22 258 return -1;
andreikovacs 0:4eb2240dbd22 259
andreikovacs 0:4eb2240dbd22 260 mPRNG_Requests++;
andreikovacs 0:4eb2240dbd22 261
andreikovacs 0:4eb2240dbd22 262 /* a. XSEEDj = optional user input. */
andreikovacs 0:4eb2240dbd22 263 if (pXSEED)
andreikovacs 0:4eb2240dbd22 264 {
andreikovacs 0:4eb2240dbd22 265 /* b. XVAL = (XKEY + XSEEDj) mod 2^b */
andreikovacs 0:4eb2240dbd22 266 for (i=0; i<mPRNG_NoOfBytes_c; i++)
andreikovacs 0:4eb2240dbd22 267 {
andreikovacs 0:4eb2240dbd22 268 ctx.buffer[i] = ((uint8_t*)XKEY)[i] + pXSEED[i];
andreikovacs 0:4eb2240dbd22 269 }
andreikovacs 0:4eb2240dbd22 270 }
andreikovacs 0:4eb2240dbd22 271 else
andreikovacs 0:4eb2240dbd22 272 {
andreikovacs 0:4eb2240dbd22 273 for (i=0; i<mPRNG_NoOfBytes_c; i++)
andreikovacs 0:4eb2240dbd22 274 {
andreikovacs 0:4eb2240dbd22 275 ctx.buffer[i] = ((uint8_t*)XKEY)[i];
andreikovacs 0:4eb2240dbd22 276 }
andreikovacs 0:4eb2240dbd22 277 }
andreikovacs 0:4eb2240dbd22 278
andreikovacs 0:4eb2240dbd22 279 /* c. xj = G(t,XVAL) mod q
andreikovacs 0:4eb2240dbd22 280 ***************************/
andreikovacs 0:4eb2240dbd22 281 SHA1_Hash(&ctx, ctx.buffer, mPRNG_NoOfBytes_c);
andreikovacs 0:4eb2240dbd22 282
andreikovacs 0:4eb2240dbd22 283 /* d. XKEY = (1 + XKEY + xj) mod 2^b */
andreikovacs 0:4eb2240dbd22 284 XKEY[0] += 1;
andreikovacs 0:4eb2240dbd22 285 for (i=0; i<mPRNG_NoOfLongWords_c; i++)
andreikovacs 0:4eb2240dbd22 286 {
andreikovacs 0:4eb2240dbd22 287 XKEY[i] += ctx.hash[i];
andreikovacs 0:4eb2240dbd22 288 }
andreikovacs 0:4eb2240dbd22 289
andreikovacs 0:4eb2240dbd22 290 /* Check if the length provided exceeds the output data size */
andreikovacs 0:4eb2240dbd22 291 if (outBytes > mPRNG_NoOfBytes_c)
andreikovacs 0:4eb2240dbd22 292 {
andreikovacs 0:4eb2240dbd22 293 outBytes = mPRNG_NoOfBytes_c;
andreikovacs 0:4eb2240dbd22 294 }
andreikovacs 0:4eb2240dbd22 295
andreikovacs 0:4eb2240dbd22 296 /* Copy the generated number */
andreikovacs 0:4eb2240dbd22 297 for (i=0; i < outBytes; i++)
andreikovacs 0:4eb2240dbd22 298 {
andreikovacs 0:4eb2240dbd22 299 pOut[i] = ((uint8_t*)ctx.hash)[i];
andreikovacs 0:4eb2240dbd22 300 }
andreikovacs 0:4eb2240dbd22 301
andreikovacs 0:4eb2240dbd22 302 return outBytes;
andreikovacs 0:4eb2240dbd22 303 }
andreikovacs 0:4eb2240dbd22 304 #endif
andreikovacs 0:4eb2240dbd22 305
andreikovacs 0:4eb2240dbd22 306 /********************************** EOF ***************************************/