77package main
88
99import (
10+ "crypto/rand"
1011 "io"
1112 "log"
13+ "math"
1214 "os"
1315
1416 "github.com/edgelesssys/ego/ego/test"
@@ -28,6 +30,7 @@ func main() {
2830 testFileSystemMounts (assert , require )
2931 testEnvVars (assert , require )
3032 testCpuid (assert , require )
33+ testRand (assert , require )
3134}
3235
3336func testFileSystemMounts (assert * assert.Assertions , require * require.Assertions ) {
@@ -104,3 +107,32 @@ func testEnvVars(assert *assert.Assertions, require *require.Assertions) {
104107func testCpuid (assert * assert.Assertions , require * require.Assertions ) {
105108 assert .True (cpuid .CPU .Has (cpuid .CMOV ))
106109}
110+
111+ func testRand (assert * assert.Assertions , require * require.Assertions ) {
112+ // This test
113+ // - does a sanity check of returned randomness
114+ // - implicitly verifies that FIPS entropy initialization succeeds when built with GOFIPS140
115+ buf := make ([]byte , 8192 )
116+ n , err := rand .Read (buf )
117+ require .NoError (err )
118+ require .Equal (8192 , n )
119+ assert .Greater (entropy (buf ), 7.9 )
120+ }
121+
122+ func entropy (data []byte ) float64 {
123+ var freq [256 ]int
124+ for _ , b := range data {
125+ freq [b ]++
126+ }
127+
128+ lenData := float64 (len (data ))
129+ var entropy float64
130+ for _ , n := range freq {
131+ if n > 0 {
132+ p := float64 (n ) / lenData
133+ entropy -= p * math .Log2 (p )
134+ }
135+ }
136+
137+ return entropy
138+ }
0 commit comments