#include "randombytes.h"
#include "main.h"
#include "stm32f7xx.h"
#include <stdint.h>


RNG_HandleTypeDef hrng;

void init_rng(void)
{
  hrng.Instance = RNG;
  if (HAL_RNG_Init(&hrng) != HAL_OK) {
    Error_Handler();
  }
}

uint32_t rand32(void) {
  uint32_t myrndnum=0;
  HAL_RNG_GenerateRandomNumber(&hrng, &myrndnum);

  return myrndnum;
}

void randombytes(unsigned char* x, unsigned long long len) {
  while (len >= 4) {
    *(uint32_t*)x = rand32();
    x += 4;
    len -= 4;
  }
  if (len == 0) return;
  uint32_t rnd = rand32();
  while (len) {
    *x = rnd;
    rnd >>= 8;
    x++;
    len--;
  }
}
