xref: /freebsd/crypto/libecc/src/utils/utils_rand.c (revision f0865ec9906d5a18fa2a3b61381f22ce16e606ad)
1 /*
2  *  Copyright (C) 2023 - This file is part of libecc project
3  *
4  *  Authors:
5  *      Ryad BENADJILA <ryadbenadjila@gmail.com>
6  *      Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
7  *
8  *  This software is licensed under a dual BSD and GPL v2 license.
9  *  See LICENSE file at the root folder of the project.
10  */
11 #include <libecc/utils/utils_rand.h>
12 
13 /* Unsafe random source:
14  * Initial seeding is performed using good entropy, then
15  * a congruential linear system is used.
16  */
17 static u64 seed = 0;
get_unsafe_random(unsigned char * buf,u16 len)18 int get_unsafe_random(unsigned char *buf, u16 len)
19 {
20         int ret;
21         u64 a, b;
22         u16 i, j;
23         a = (u64)2862933555777941757;
24         b = (u64)3037000493;
25 
26         if(seed == 0){
27                 ret = get_random((u8*)&seed, sizeof(seed));
28                 if(ret){
29                         ret = -1;
30                         goto err;
31                 }
32         }
33 
34         i = 0;
35         while(i < len){
36                 /* Use a congruential linear generator */
37                 seed = ((a * seed) + b);
38 
39                 for(j = 0; j < sizeof(seed); j++){
40                         if((i + j) < len){
41                                 buf[i + j] = (u8)((seed >> (j * 8)) & 0xff);
42                         }
43                 }
44                 i = (u16)(i + sizeof(seed));
45         }
46 
47         ret = 0;
48 
49 err:
50         return ret;
51 }
52