-
Notifications
You must be signed in to change notification settings - Fork 3
/
isaac.go
216 lines (193 loc) · 4.8 KB
/
isaac.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
------------------------------------------------------------------------------
isaac.go: an implementation of Bob Jenkins' random number generator ISAAC based on 'readable.c'
* 18 Aug 2014 -- direct port of readable.c to Go
* 10 Sep 2014 -- updated to be more idiomatic Go
------------------------------------------------------------------------------
*/
package isaac
import (
"bytes"
"crypto/cipher"
"encoding/binary"
)
type ISAAC struct {
/* external results */
randrsl [256]uint32
randcnt uint32
/* internal state */
mm [256]uint32
aa, bb, cc uint32
}
func (r *ISAAC) isaac() {
r.cc = r.cc + 1 /* cc just gets incremented once per 256 results */
r.bb = r.bb + r.cc /* then combined with bb */
for i := 0; i < 256; i++ {
x := r.mm[i]
switch i % 4 {
case 0:
r.aa = r.aa ^ (r.aa << 13)
case 1:
r.aa = r.aa ^ (r.aa >> 6)
case 2:
r.aa = r.aa ^ (r.aa << 2)
case 3:
r.aa = r.aa ^ (r.aa >> 16)
}
r.aa = r.mm[(i+128)%256] + r.aa
y := r.mm[(x>>2)%256] + r.aa + r.bb
r.mm[i] = y
r.bb = r.mm[(y>>10)%256] + x
r.randrsl[i] = r.bb
}
/* Note that bits 2..9 are chosen from x but 10..17 are chosen
from y. The only important thing here is that 2..9 and 10..17
don't overlap. 2..9 and 10..17 were then chosen for speed in
the optimized version (rand.c) */
/* See http://burtleburtle.net/bob/rand/isaac.html
for further explanations and analysis. */
}
func mix(a, b, c, d, e, f, g, h uint32) (uint32, uint32, uint32, uint32, uint32, uint32, uint32, uint32) {
a ^= b << 11
d += a
b += c
b ^= c >> 2
e += b
c += d
c ^= d << 8
f += c
d += e
d ^= e >> 16
g += d
e += f
e ^= f << 10
h += e
f += g
f ^= g >> 4
a += f
g += h
g ^= h << 8
b += g
h += a
h ^= a >> 9
c += h
a += b
return a, b, c, d, e, f, g, h
}
/* if (flag==true), then use the contents of randrsl[] to initialize mm[]. */
func (r *ISAAC) randInit(flag bool) {
var a, b, c, d, e, f, g, h uint32
a, b, c, d, e, f, g, h = 0x9e3779b9, 0x9e3779b9, 0x9e3779b9, 0x9e3779b9, 0x9e3779b9, 0x9e3779b9, 0x9e3779b9, 0x9e3779b9
for i := 0; i < 4; i++ {
a, b, c, d, e, f, g, h = mix(a, b, c, d, e, f, g, h)
}
for i := 0; i < 256; i += 8 { /* fill mm[] with messy stuff */
if flag { /* use all the information in the seed */
a += r.randrsl[i]
b += r.randrsl[i+1]
c += r.randrsl[i+2]
d += r.randrsl[i+3]
e += r.randrsl[i+4]
f += r.randrsl[i+5]
g += r.randrsl[i+6]
h += r.randrsl[i+7]
}
a, b, c, d, e, f, g, h = mix(a, b, c, d, e, f, g, h)
r.mm[i] = a
r.mm[i+1] = b
r.mm[i+2] = c
r.mm[i+3] = d
r.mm[i+4] = e
r.mm[i+5] = f
r.mm[i+6] = g
r.mm[i+7] = h
}
if flag { /* do a second pass to make all of the seed affect all of mm */
for i := 0; i < 256; i += 8 {
a += r.mm[i]
b += r.mm[i+1]
c += r.mm[i+2]
d += r.mm[i+3]
e += r.mm[i+4]
f += r.mm[i+5]
g += r.mm[i+6]
h += r.mm[i+7]
a, b, c, d, e, f, g, h = mix(a, b, c, d, e, f, g, h)
r.mm[i] = a
r.mm[i+1] = b
r.mm[i+2] = c
r.mm[i+3] = d
r.mm[i+4] = e
r.mm[i+5] = f
r.mm[i+6] = g
r.mm[i+7] = h
}
}
r.isaac() /* fill in the first set of results */
r.randcnt = 256 /* reset the counter for the first set of results */
}
/* there is no official method for doing this
* the challenge code just memcpys the string to the top of the output array
* and this is the best equivalent I could come up with in Go */
func (r *ISAAC) Seed(key string) {
keyBuf := bytes.NewBuffer([]byte(key))
// this padding should be equivalent to the behavior of memcpy-ing a shorter string
// into a zeroed output array (per randtest.c)
var padding = 0
if keyBuf.Len()%4 != 0 {
padding = 4 - (keyBuf.Len() % 4)
}
for i := 0; i < padding; i++ {
keyBuf.WriteByte(0x00)
}
var count = keyBuf.Len() / 4 // separate counter since keyBuf is being consumed
for i := 0; i < count; i++ {
if i == len(r.randrsl) {
break
}
// packing
var num uint32
if err := binary.Read(keyBuf, binary.LittleEndian, &num); err == nil {
r.randrsl[i] = num
}
}
r.randInit(true)
}
/* retrieve the next number in the sequence */
func (r *ISAAC) Rand() (number uint32) {
r.randcnt--
number = r.randrsl[r.randcnt]
if r.randcnt == 0 {
r.isaac()
r.randcnt = 256
}
return number
}
/* implementation based on http://golang.org/src/pkg/crypto/cipher/ctr.go */
func (r *ISAAC) XORKeyStream(dst, src []byte) {
keyStream := new(bytes.Buffer)
for len(src) > 0 {
keyStream.Reset()
// unpacking
nextUint32 := r.Rand()
binary.Write(keyStream, binary.BigEndian, &nextUint32)
n := safeXORBytes(dst, src, keyStream.Bytes())
dst = dst[n:]
src = src[n:]
}
}
func safeXORBytes(dst, a, b []byte) int {
n := len(a)
if len(b) < n {
n = len(b)
}
for i := 0; i < n; i++ {
dst[i] = a[i] ^ b[i]
}
return n
}
func NewISAACStream(key string) cipher.Stream {
stream := new(ISAAC)
stream.Seed(key)
return stream
}