-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathballoon.go
137 lines (125 loc) · 4.11 KB
/
balloon.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
// balloon.go - implementation of Balloon memory-hard hashing.
//
// To the extent possible under law, Ivan Markin waived all copyright
// and related or neighboring rights to this module of balloon, using the creative
// commons "CC0" public domain dedication. See LICENSE or
// <http://creativecommons.org/publicdomain/zero/1.0/> for full details.
package balloon
import (
"encoding/binary"
"hash"
"math/big"
)
const (
delta = 3
)
// Instance represents Ballon instance (its internal state).
type Instance struct {
Buffer []byte
LastBlock []byte
Cnt uint64
}
// Balloon uses non-memory-hard cryptographic hash function h
// and calculates memory-hard Ballon hash of passphrase with salt.
// sCost is the number of digest-sized blocks in buffer (space cost).
// tCost is the number of rounds (time cost).
func Balloon(h hash.Hash, passphrase, salt []byte, sCost, tCost uint64) []byte {
b := &Instance{Buffer: make([]byte, sCost*uint64(h.Size()))}
b.Expand(h, passphrase, salt, sCost)
b.Mix(h, salt, sCost, tCost)
return b.LastBlock
}
// BalloonM runs M concurrent Balloon instances and returns
// XOR of their outputs. All other parameters are the same as in Balloon.
func BalloonM(hr func() hash.Hash, passphrase, salt []byte, sCost, tCost uint64, M uint64) []byte {
out := make([]byte, hr().Size())
bouts := make(chan []byte)
for m := uint64(0); m < M; m++ {
go func(core uint64) {
binaryM := make([]byte, 8)
binary.BigEndian.PutUint64(binaryM, core)
bouts <- Balloon(hr(), passphrase, append(salt, binaryM...), sCost, tCost)
}(m + 1)
}
for m := uint64(0); m < M; m++ {
for i, v := range <-bouts {
out[i] ^= v
}
}
return FinalHash(hr(), passphrase, salt, out)
}
// FinalHash hashes output of Balloon function in order to guarantee
// collision and second-preimage resistance (if hash function h provides
// these properties).
func FinalHash(h hash.Hash, passphrase, salt, balloonOut []byte) []byte {
h.Reset()
h.Write(passphrase)
h.Write(salt)
h.Write(balloonOut)
return h.Sum(nil)
}
// Expand performs Balloon expansion of (passphrase, salt) using hash function h
// and fills b.Buffer with this output. It panics if size of b.Buffer
// is not sCost*h.Size().
func (b *Instance) Expand(h hash.Hash, passphrase, salt []byte, sCost uint64) {
blockSize := uint64(h.Size())
if len(b.Buffer) != int(sCost*blockSize) {
panic("balloon: internal buffer has wrong length")
}
h.Reset()
binary.Write(h, binary.BigEndian, b.Cnt)
b.Cnt++
h.Write(passphrase)
h.Write(salt)
b.LastBlock = h.Sum(nil)
copy(b.Buffer, b.LastBlock)
for m := uint64(1); m < sCost; m++ {
h.Reset()
binary.Write(h, binary.BigEndian, b.Cnt)
h.Write(b.LastBlock)
b.LastBlock = h.Sum(nil)
copy(b.Buffer[b.Cnt*blockSize:], b.LastBlock)
b.Cnt++
}
}
// Mix performs Balloon mixing of b.Buffer contents using hash function h and
// salt salt. Mixing parameters are the same as in Balloon: sCost for space cost,
// tCost for number of rounds. It panics if size of b.Buffer is not sCost*h.Size().
func (b *Instance) Mix(h hash.Hash, salt []byte, sCost, tCost uint64) {
blockSize := uint64(h.Size())
if len(b.Buffer) != int(sCost*blockSize) {
panic("balloon: internal buffer has wrong length")
}
sCostInt := big.NewInt(int64(sCost))
otherInt := big.NewInt(0)
for t := uint64(0); t < tCost; t++ {
for m := uint64(0); m < sCost; m++ {
h.Reset()
binary.Write(h, binary.BigEndian, b.Cnt)
b.Cnt++
h.Write(b.LastBlock)
h.Write(b.Buffer[m*blockSize : (m+1)*blockSize])
b.LastBlock = h.Sum(nil)
copy(b.Buffer[m*blockSize:], b.LastBlock)
for i := uint64(0); i < delta; i++ {
h.Reset()
binary.Write(h, binary.BigEndian, b.Cnt)
b.Cnt++
h.Write(salt)
binary.Write(h, binary.BigEndian, t)
binary.Write(h, binary.BigEndian, m)
binary.Write(h, binary.BigEndian, i)
otherInt.SetBytes(h.Sum(nil))
otherInt.Mod(otherInt, sCostInt)
other := otherInt.Uint64()
h.Reset()
binary.Write(h, binary.BigEndian, b.Cnt)
b.Cnt++
h.Write(b.LastBlock)
h.Write(b.Buffer[other*blockSize : (other+1)*blockSize])
b.LastBlock = h.Sum(nil)
copy(b.Buffer[m*blockSize:], b.LastBlock)
}
}
}
}