Debian Codegraph @

counterecryptor_test.go

raw symbols SCIP
1 package goproxy_test
2
3 import (
4 "bytes"
5 "crypto/rsa"
6 "encoding/binary"
7 "io"
8 "math"
9 "math/rand"
10 "testing"
11
12 "github.com/elazarl/goproxy"
13 )
14
15 type RandSeedReader struct {
16 r rand.Rand
17 }
18
19 func (r *RandSeedReader) Read(b []byte) (n int, err error) {
20 for i := range b {
21 b[i] = byte(r.r.Int() & 0xFF)
22 }
23 return len(b), nil
24 }
25
26 func TestCounterEncDifferentConsecutive(t *testing.T) {
27 k, err := rsa.GenerateKey(&RandSeedReader{*rand.New(rand.NewSource(0xFF43109))}, 128)
28 fatalOnErr(err, "rsa.GenerateKey", t)
29 c, err := goproxy.NewCounterEncryptorRandFromKey(k, []byte("the quick brown fox run over the lazy dog"))
30 fatalOnErr(err, "NewCounterEncryptorRandFromKey", t)
31 for i := 0; i < 100*1000; i++ {
32 var a, b int64
33 binary.Read(&c, binary.BigEndian, &a)
34 binary.Read(&c, binary.BigEndian, &b)
35 if a == b {
36 t.Fatal("two consecutive equal int64", a, b)
37 }
38 }
39 }
40
41 func TestCounterEncIdenticalStreams(t *testing.T) {
42 k, err := rsa.GenerateKey(&RandSeedReader{*rand.New(rand.NewSource(0xFF43109))}, 128)
43 fatalOnErr(err, "rsa.GenerateKey", t)
44 c1, err := goproxy.NewCounterEncryptorRandFromKey(k, []byte("the quick brown fox run over the lazy dog"))
45 fatalOnErr(err, "NewCounterEncryptorRandFromKey", t)
46 c2, err := goproxy.NewCounterEncryptorRandFromKey(k, []byte("the quick brown fox run over the lazy dog"))
47 fatalOnErr(err, "NewCounterEncryptorRandFromKey", t)
48 nout := 1000
49 out1, out2 := make([]byte, nout), make([]byte, nout)
50 io.ReadFull(&c1, out1)
51 tmp := out2[:]
52 rand.Seed(0xFF43109)
53 for len(tmp) > 0 {
54 n := 1 + rand.Intn(256)
55 if n > len(tmp) {
56 n = len(tmp)
57 }
58 n, err := c2.Read(tmp[:n])
59 fatalOnErr(err, "CounterEncryptorRand.Read", t)
60 tmp = tmp[n:]
61 }
62 if !bytes.Equal(out1, out2) {
63 t.Error("identical CSPRNG does not produce the same output")
64 }
65 }
66
67 func stddev(data []int) float64 {
68 var sum, sum_sqr float64 = 0, 0
69 for _, h := range data {
70 sum += float64(h)
71 sum_sqr += float64(h) * float64(h)
72 }
73 n := float64(len(data))
74 variance := (sum_sqr - ((sum * sum) / n)) / (n - 1)
75 return math.Sqrt(variance)
76 }
77
78 func TestCounterEncStreamHistogram(t *testing.T) {
79 k, err := rsa.GenerateKey(&RandSeedReader{*rand.New(rand.NewSource(0xFF43109))}, 128)
80 fatalOnErr(err, "rsa.GenerateKey", t)
81 c, err := goproxy.NewCounterEncryptorRandFromKey(k, []byte("the quick brown fox run over the lazy dog"))
82 fatalOnErr(err, "NewCounterEncryptorRandFromKey", t)
83 nout := 100 * 1000
84 out := make([]byte, nout)
85 io.ReadFull(&c, out)
86 refhist := make([]int, 512)
87 for i := 0; i < nout; i++ {
88 refhist[rand.Intn(256)]++
89 }
90 hist := make([]int, 512)
91 for _, b := range out {
92 hist[int(b)]++
93 }
94 refstddev, stddev := stddev(refhist), stddev(hist)
95 // due to lack of time, I guestimate
96 t.Logf("ref:%v - act:%v = %v", refstddev, stddev, math.Abs(refstddev-stddev))
97 if math.Abs(refstddev-stddev) >= 1 {
98 t.Errorf("stddev of ref histogram different than regular PRNG: %v %v", refstddev, stddev)
99 }
100 }