81 lines
2.7 KiB
C
81 lines
2.7 KiB
C
|
/*
|
||
|
ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
|
||
|
|
||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
you may not use this file except in compliance with the License.
|
||
|
You may obtain a copy of the License at
|
||
|
|
||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||
|
|
||
|
Unless required by applicable law or agreed to in writing, software
|
||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
See the License for the specific language governing permissions and
|
||
|
limitations under the License.
|
||
|
*/
|
||
|
/*
|
||
|
* **** This file incorporates work covered by the following copyright and ****
|
||
|
* **** permission notice: ****
|
||
|
*
|
||
|
* Copyright (C) 2006-2017 wolfSSL Inc.
|
||
|
*
|
||
|
* This file is part of wolfSSL.
|
||
|
*
|
||
|
* wolfSSL is free software; you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License as published by
|
||
|
* the Free Software Foundation; either version 2 of the License, or
|
||
|
* (at your option) any later version.
|
||
|
*
|
||
|
* wolfSSL is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with this program; if not, write to the Free Software
|
||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||
|
*
|
||
|
*/
|
||
|
#include <ch.h>
|
||
|
#include <stdint.h>
|
||
|
#include "wolfssl_chibios.h"
|
||
|
#include "user_settings.h"
|
||
|
|
||
|
unsigned int chibios_rand_generate(void)
|
||
|
{
|
||
|
static unsigned int last_value=0;
|
||
|
static unsigned int new_value=0;
|
||
|
unsigned int error_bits = 0;
|
||
|
error_bits = RNG_SR_SEIS | RNG_SR_CEIS;
|
||
|
while (new_value==last_value) {
|
||
|
/* Check for error flags and if data is ready. */
|
||
|
if ( ((RNG->SR & error_bits) == 0) && ( (RNG->SR & RNG_SR_DRDY) == 1 ) )
|
||
|
new_value=RNG->DR;
|
||
|
}
|
||
|
last_value=new_value;
|
||
|
return new_value;
|
||
|
}
|
||
|
|
||
|
int custom_rand_generate_block(unsigned char* output, unsigned int sz)
|
||
|
{
|
||
|
uint32_t i = 0;
|
||
|
|
||
|
while (i < sz)
|
||
|
{
|
||
|
/* If not aligned or there is odd/remainder */
|
||
|
if( (i + sizeof(CUSTOM_RAND_TYPE)) > sz ||
|
||
|
((uint32_t)&output[i] % sizeof(CUSTOM_RAND_TYPE)) != 0
|
||
|
) {
|
||
|
/* Single byte at a time */
|
||
|
output[i++] = (unsigned char)chibios_rand_generate();
|
||
|
}
|
||
|
else {
|
||
|
/* Use native 8, 16, 32 or 64 copy instruction */
|
||
|
*((CUSTOM_RAND_TYPE*)&output[i]) = chibios_rand_generate();
|
||
|
i += sizeof(CUSTOM_RAND_TYPE);
|
||
|
}
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|