-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxsha256_test.go
247 lines (223 loc) · 8.39 KB
/
xsha256_test.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package xsha256
import (
"encoding/hex"
"reflect"
"testing"
)
func Test_Addmod32(t *testing.T) {
if addMod32(1, 2) != 3 {
t.Error("addMod32 case 1 error")
}
if addMod32(4294967295, 1) != 0 {
t.Error("addMod32 case 2 error")
}
if addMod32(3050487260, 3710144918) != 2465664882 {
t.Error("addMod32 case 3 error")
}
}
func Test_RotR32(t *testing.T) {
if rotR32(2, 1) != 1 {
t.Error("rotR32 case 1 error")
}
if rotR32(1, 1) != 2147483648 {
t.Error("rotR32 case 2 error")
}
if rotR32(2919882184, 31) != 1544797073 {
t.Error("rotR32 case 3 error")
}
}
func Test_Little_Sigma0(t *testing.T) {
if little_Sigma0(1114723206) != 1345017931 {
t.Error("little_Sigma0 case 1 error")
}
}
func Test_Little_Sigma1(t *testing.T) {
if little_Sigma1(1232674167) != 2902922196 {
t.Error("little_Sigma1 case 1 error")
}
}
func Test_BytesToWords(t *testing.T) {
words := bytesToWords([]byte("iguana wombat dog kangaroo llama turkey yak unicorn sheep xenoce"))
if !reflect.DeepEqual(words,
[]uint32{1768387937, 1851859063, 1869439585, 1948279919, 1730177889, 1852268914, 1869553772, 1818324321,
544503154, 1801812256, 2036427552, 1970170211, 1869770272, 1936221541, 1881176165, 1852793701,
3002878561, 3711121932, 1520676164, 3002441970, 2935068969, 1610329529, 1904580351, 3219988740,
2337695268, 263015313, 2120931855, 131203777, 3818546915, 19163115, 3479924161, 2154860703,
1790169326, 516580487, 2414737634, 909025701, 2241053595, 1237268359, 3797503938, 1773623028,
2840671725, 2299292186, 1933596460, 2279513616, 514132674, 3245155609, 1753922983, 2241450350,
2449659630, 262239956, 773552098, 3253131632, 3863807927, 879696536, 3143654396, 3973063648,
509015903, 270850193, 1893431553, 719566283, 2310657204, 365781698, 3761063438, 1007484868}) {
t.Error("bytesToWords case 1 error")
}
}
func Test_Big_Sigma0(t *testing.T) {
if big_Sigma0(3536071395) != 3003388882 {
t.Error("big_Sigma0 case 1 error")
}
}
func Test_Big_Sigma1(t *testing.T) {
if big_Sigma1(651015076) != 2194029931 {
t.Error("big_Sigma1 case 1 error")
}
}
func Test_Choice(t *testing.T) {
if choice(2749825547, 776049372, 1213590135) != 1783753340 {
t.Error("choice case 1 error")
}
}
func Test_Majority(t *testing.T) {
if majority(3758166654, 2821345890, 1850678816) != 3893039714 {
t.Error("majority case 1 error")
}
}
func Test_Round(t *testing.T) {
state := State{list: [8]uint32{2739944672, 3126690193, 4191866847, 1163785745,
3714074692, 1172792371, 283469062, 826169706}}
round(&state, 961987163, 3221900128)
if !reflect.DeepEqual(state.list, [8]uint32{1724514418, 2739944672, 3126690193, 4191866847,
1638715774, 3714074692, 1172792371, 283469062}) {
t.Error("round case 1 error")
}
}
func Test_Compress(t *testing.T) {
state := State{list: [8]uint32{2918946378, 1679978889, 1678006433, 650957219,
379281712, 2112907926, 1775216060, 2152648190}}
msg := []byte("manatee fox unicorn octopus dog fox fox llama vulture jaguar xen")
compress(&state, msg)
if !reflect.DeepEqual(state.list, [8]uint32{1251501988, 1663226031, 2877128394, 4050467288,
2375501075, 1434687977, 2625842981, 650253644}) {
t.Error("compress case 1 error")
}
}
func Test_Padding(t *testing.T) {
pad := padding(0)
if hex.EncodeToString(pad) != "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" {
t.Error("padding case 1 error")
}
pad = padding(1)
if hex.EncodeToString(pad) != "800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008" {
t.Error("padding case 2 error")
}
pad = padding(55)
if hex.EncodeToString(pad) != "8000000000000001b8" {
t.Error("padding case 3 error")
}
pad = padding(56)
if hex.EncodeToString(pad) != "8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001c0" {
t.Error("padding case 4 error")
}
pad = padding(64)
if hex.EncodeToString(pad) != "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200" {
t.Error("padding case 5 error")
}
pad = padding(492022654431536432)
if hex.EncodeToString(pad) != "800000000000000036a01ffa96b12980" {
t.Error("padding case 6 error")
}
}
func Test_Hash(t *testing.T) {
hash := Hash([]byte(""))
if hex.EncodeToString(hash) != "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" {
t.Error("hash case 1 error")
}
hash = Hash([]byte("hello world"))
if hex.EncodeToString(hash) != "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9" {
t.Error("hash case 2 error")
}
hash = Hash([]byte("aardvark zebra yak pig jaguar aardvark rhinoceros butte"))
if hex.EncodeToString(hash) != "4b45e1bec21185865d1628a8a502eed789193a3c253a529983e4bc17fa65f32b" {
t.Error("hash case 3 error")
}
hash = Hash([]byte("narwhal dog llama llama giraffe narwhal octopus dog xeno"))
if hex.EncodeToString(hash) != "99069f1eba4c874aba649c17136a253e1dd504cda936ab77cf189c2cf9eb88ff" {
t.Error("hash case 4 error")
}
hash = Hash([]byte("John Jacob Jingleheimer Schmidt! His name is my name too. Whenever we go out the people always shout there goes John Jacob Jingleheimer Schmidt! Nanananananana..."))
if hex.EncodeToString(hash) != "68b74d91364475247c10bfee2621eaa13bcabb033ed1dee58b74c05e7944489a" {
t.Error("hash case 5 error")
}
}
// Length Extension Attack
//
// Some SHA-256 outputs are related to each other, that can be detected or exploited
// even when the input is unknown.
// SHA-256 hash is just a state input if there had been more input.
//
// We can compute the hash of input with added suffix from current
// hash of the input, without knowing the input.
// The limitation is the padding of current hash must be part of added suffix.
// The return value extended is hex encoded input+padding+suffix.
func lengthExtend(input, suffix []byte) []byte {
pad := padding(uint64(len(input)))
paddedInput := append(input, pad...)
extended := make([]byte, 2*(len(paddedInput)+len(suffix)))
suffixed := append(paddedInput, suffix...)
hex.Encode(extended, suffixed)
return extended
}
func Test_LengthExtend(t *testing.T) {
input := "fox elephant dog"
suffix := "pig jaguar iguana"
extended := lengthExtend([]byte(input), []byte(suffix))
expected := "666f7820656c657068616e7420646f67800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080706967206a616775617220696775616e61"
if !reflect.DeepEqual([]byte(expected), extended) {
t.Error("fail to LengthExtend")
}
}
// Get current State from given hash
func recoverState(hash []byte) State {
state := State{}
dst := make([]byte, 4)
for i := 0; i < 64; i += 8 {
hex.Decode(dst, hash[i:i+8])
// 4 bytes into uint32
state.list[i/8] = uint32(dst[0])<<24 | uint32(dst[1])<<16 | uint32(dst[2])<<8 | uint32(dst[3])
}
return state
}
func Test_RecoverState(t *testing.T) {
expected := [8]uint32{
3133871534, 4165468858, 2700423300, 1343465870,
3790528102, 1267814286, 3156890126, 1761909257,
}
hash := "bacb15aef84802baa0f530845013a98ee1eede664b914f8ebc2a520e69049a09"
state := recoverState([]byte(hash))
if !reflect.DeepEqual(expected, state.list) {
t.Error("fail to RecoverState")
}
}
// Get state from current hash. Create new block from padded suffix.
// Mix the block using the obtained state and block.
func _LEA(oriHash, suffix []byte, oriLen uint64) []byte {
state := recoverState(oriHash)
newLen := oriLen + uint64(len(padding(oriLen))) + uint64(len(suffix))
pad := padding(newLen)
paddedMsg := append(suffix, pad...)
// all below are from Hash() function.
for i := 0; i < len(paddedMsg); i += 64 {
block := paddedMsg[i : i+64]
compress(&state, block)
}
byteHash := make([]byte, 0, 32)
v := [4]byte{}
for i := 0; i < 8; i++ {
// uint32 to array of bytes
v[0] = byte(state.list[i] >> 24)
v[1] = byte(state.list[i] >> 16)
v[2] = byte(state.list[i] >> 8)
v[3] = byte(state.list[i])
byteHash = append(byteHash, v[:]...)
}
hash := make([]byte, 64)
hex.Encode(hash, byteHash)
return hash
}
func Test_LEA(t *testing.T) {
inputHash := []byte("27b82abe296f3ecd5174b6e6168ea683cd8ef94306d9abd9f81807f2fa587d2a")
inputLen := uint64(41)
suffix := []byte("manatee jaguar zebra zebra dog")
newHash := _LEA(inputHash, suffix, inputLen)
if string(newHash) != "50417b93404facb1b481990a7bf6ac963b1e1ee0ccced8b2a5938caa28b52b41" {
t.Error("Padding case 1 error")
}
}