Add runtime.Random_Generator interface

This commit is contained in:
gingerBill
2024-06-15 14:45:57 +01:00
parent 94ec647923
commit dc4ec8638c
3 changed files with 66 additions and 0 deletions
+20
View File
@@ -4,6 +4,7 @@ helper routines.
*/
package crypto
import "base:runtime"
import "core:mem"
// compare_constant_time returns 1 iff a and b are equal, 0 otherwise.
@@ -58,3 +59,22 @@ rand_bytes :: proc (dst: []byte) {
_rand_bytes(dst)
}
to_random_generator :: proc() -> runtime.Random_Generator {
return {
procedure = proc(data: rawptr, mode: runtime.Random_Generator_Mode, p: []byte) {
switch mode {
case .Read:
rand_bytes(p)
case .Query_Info:
if len(p) != size_of(runtime.Random_Generator_Query_Info) {
return
}
info := (^runtime.Random_Generator_Query_Info)(raw_data(p))
info^ += {.Uniform, .Cryptographic, .External_Entropy}
}
},
data = nil,
}
}