hwrng.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /*
  14. * **** This file incorporates work covered by the following copyright and ****
  15. * **** permission notice: ****
  16. *
  17. * Copyright (C) 2006-2017 wolfSSL Inc.
  18. *
  19. * This file is part of wolfSSL.
  20. *
  21. * wolfSSL is free software; you can redistribute it and/or modify
  22. * it under the terms of the GNU General Public License as published by
  23. * the Free Software Foundation; either version 2 of the License, or
  24. * (at your option) any later version.
  25. *
  26. * wolfSSL is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU General Public License
  32. * along with this program; if not, write to the Free Software
  33. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  34. *
  35. */
  36. #include <ch.h>
  37. #include <stdint.h>
  38. #include "wolfssl_chibios.h"
  39. #include "user_settings.h"
  40. unsigned int chibios_rand_generate(void)
  41. {
  42. static unsigned int last_value=0;
  43. static unsigned int new_value=0;
  44. unsigned int error_bits = 0;
  45. error_bits = RNG_SR_SEIS | RNG_SR_CEIS;
  46. while (new_value==last_value) {
  47. /* Check for error flags and if data is ready. */
  48. if ( ((RNG->SR & error_bits) == 0) && ( (RNG->SR & RNG_SR_DRDY) == 1 ) )
  49. new_value=RNG->DR;
  50. }
  51. last_value=new_value;
  52. return new_value;
  53. }
  54. int custom_rand_generate_block(unsigned char* output, unsigned int sz)
  55. {
  56. uint32_t i = 0;
  57. while (i < sz)
  58. {
  59. /* If not aligned or there is odd/remainder */
  60. if( (i + sizeof(CUSTOM_RAND_TYPE)) > sz ||
  61. ((uint32_t)&output[i] % sizeof(CUSTOM_RAND_TYPE)) != 0
  62. ) {
  63. /* Single byte at a time */
  64. output[i++] = (unsigned char)chibios_rand_generate();
  65. }
  66. else {
  67. /* Use native 8, 16, 32 or 64 copy instruction */
  68. *((CUSTOM_RAND_TYPE*)&output[i]) = chibios_rand_generate();
  69. i += sizeof(CUSTOM_RAND_TYPE);
  70. }
  71. }
  72. return 0;
  73. }