1 /* 2 * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 #include <stdlib.h> 7 #include <time.h> 8 #include <stdint.h> 9 #include <assert.h> 10 #include <string.h> 11 #include <sys/param.h> 12 13 #include "esp_log.h" 14 15 static const char* TAG = "esp-random"; 16 esp_random_init(void)17static void esp_random_init(void) 18 { 19 srand(time(NULL)); 20 ESP_LOGW(TAG, "esp_random do not provide a cryptographically secure numbers on Linux, and should never be used for anything security related"); 21 } 22 esp_random(void)23uint32_t esp_random(void) 24 { 25 /* Adding INT32_MAX to shift the results such that after conversion to uint32_t we still get 32 bits of random data */ 26 return (rand() + INT32_MAX); 27 } 28 esp_fill_random(void * buf,size_t len)29void esp_fill_random(void *buf, size_t len) 30 { 31 assert(buf != NULL); 32 uint8_t *buf_bytes = (uint8_t *)buf; 33 while (len > 0) { 34 uint32_t word = esp_random(); 35 uint32_t to_copy = MIN(sizeof(word), len); 36 memcpy(buf_bytes, &word, to_copy); 37 buf_bytes += to_copy; 38 len -= to_copy; 39 } 40 } 41