-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmt_stream_cipher.go
56 lines (43 loc) · 913 Bytes
/
mt_stream_cipher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package set3
import (
"github.com/dbalan/cryptopals/common"
"math/rand"
)
type MTStream struct {
mt *mt19937
cur uint32
index int
}
func NewMTStream(seed uint32) *MTStream {
mt := NewMT19937(seed)
return &MTStream{mt, 0, 4}
}
func (mts *MTStream) Read(p []byte) (int, error) {
nbuf := len(p)
for i := 0; i < nbuf; i++ {
if mts.index%4 == 0 {
mts.cur = mts.mt.Rand()
mts.index = 0
}
p[i] = byte(mts.cur & 0xff)
mts.cur = mts.cur >> 8
mts.index++
}
return nbuf, nil
}
func MTStreamCipher(in []byte, seed uint32) (out []byte, err error) {
ks := make([]byte, len(in))
mts := NewMTStream(seed)
_, err = mts.Read(ks)
if err != nil {
return
}
return common.XORBlk(in, ks)
}
func MTSOracle(in []byte, key uint32) []byte {
num := rand.Uint32() % 0xff
prefix, _ := common.RandBytes(int(num))
pt := append(prefix, in...)
ct, _ := MTStreamCipher(pt, key)
return ct
}