Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions balloon.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func BalloonM(hr func() hash.Hash, passphrase, salt []byte, sCost, tCost uint64,
for m := uint64(0); m < M; m++ {
go func(core uint64) {
binaryM := make([]byte, 8)
binary.BigEndian.PutUint64(binaryM, core)
binary.LittleEndian.PutUint64(binaryM, core)
bouts <- Balloon(hr(), passphrase, append(salt, binaryM...), sCost, tCost)
}(m + 1)
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func (b *Instance) Expand(h hash.Hash, passphrase, salt []byte, sCost uint64) {
panic("balloon: internal buffer has wrong length")
}
h.Reset()
binary.Write(h, binary.BigEndian, b.Cnt)
binary.Write(h, binary.LittleEndian, b.Cnt)
b.Cnt++
h.Write(passphrase)
h.Write(salt)
Expand All @@ -84,7 +84,7 @@ func (b *Instance) Expand(h hash.Hash, passphrase, salt []byte, sCost uint64) {

for m := uint64(1); m < sCost; m++ {
h.Reset()
binary.Write(h, binary.BigEndian, b.Cnt)
binary.Write(h, binary.LittleEndian, b.Cnt)
h.Write(b.LastBlock)
b.LastBlock = h.Sum(nil)
copy(b.Buffer[b.Cnt*blockSize:], b.LastBlock)
Expand All @@ -106,7 +106,7 @@ func (b *Instance) Mix(h hash.Hash, salt []byte, sCost, tCost uint64) {
for t := uint64(0); t < tCost; t++ {
for m := uint64(0); m < sCost; m++ {
h.Reset()
binary.Write(h, binary.BigEndian, b.Cnt)
binary.Write(h, binary.LittleEndian, b.Cnt)
b.Cnt++
h.Write(b.LastBlock)
h.Write(b.Buffer[m*blockSize : (m+1)*blockSize])
Expand All @@ -115,17 +115,26 @@ func (b *Instance) Mix(h hash.Hash, salt []byte, sCost, tCost uint64) {

for i := uint64(0); i < delta; i++ {
h.Reset()
binary.Write(h, binary.BigEndian, b.Cnt)
binary.Write(h, binary.LittleEndian, t)
binary.Write(h, binary.LittleEndian, m)
binary.Write(h, binary.LittleEndian, i)
idxBlock := h.Sum(nil)
h.Reset()
binary.Write(h, binary.LittleEndian, 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))
h.Write(idxBlock)
otherBytes := h.Sum(nil)

for left, right := 0, len(otherBytes)-1; left < right; left, right = left+1, right-1 {
otherBytes[left], otherBytes[right] = otherBytes[right], otherBytes[left]
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

math/big only takes big-endian, I copied this reverse slice implementation from here:
https://github.com/golang/go/wiki/SliceTricks#reversing

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was messing around with this project and found out that there exists a function in math/big that takes little-endian.

I've submitted a pull request on your fork a while ago, but sadly I think this original repo has gotten stale. If there's more interest, I might spend some time to optimize the implementation.

khonsulabs#1

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just saw your comment here, sorry for the delay!
@nogoegst has to look at this PR, I really don't feel comfortable reviewing unsafe Go code.

@darenliang personally I have no stake in this library, I was only trying to help multiple Balloon implementations be compatible with each other, specifically the Rust library I contributed to. So if you are more interested in using Balloon with Go and want to have common compatibility, feel free to contact me.


otherInt.SetBytes(otherBytes)
otherInt.Mod(otherInt, sCostInt)
other := otherInt.Uint64()
h.Reset()
binary.Write(h, binary.BigEndian, b.Cnt)
binary.Write(h, binary.LittleEndian, b.Cnt)
b.Cnt++
h.Write(b.LastBlock)
h.Write(b.Buffer[other*blockSize : (other+1)*blockSize])
Expand Down
65 changes: 65 additions & 0 deletions balloon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ package balloon

import (
"crypto/rand"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
"reflect"
"testing"
)

Expand All @@ -30,3 +33,65 @@ func BenchmarkBalloonM(b *testing.B) {
BalloonM(sha512.New, ps[:8], ps[8:], 16, 16, 4)
}
}

func TestVectors(t *testing.T) {
type test_vector struct {
password []byte
salt []byte
s_cost uint64
t_cost uint64
output string
}

test_vectors := []test_vector{
{
password: []byte("hunter42"),
salt: []byte("examplesalt"),
s_cost: 1024,
t_cost: 3,
output: "716043dff777b44aa7b88dcbab12c078abecfac9d289c5b5195967aa63440dfb",
},
{
password: []byte(""),
salt: []byte("salt"),
s_cost: 3,
t_cost: 3,
output: "5f02f8206f9cd212485c6bdf85527b698956701ad0852106f94b94ee94577378",
},
{
password: []byte("password"),
salt: []byte(""),
s_cost: 3,
t_cost: 3,
output: "20aa99d7fe3f4df4bd98c655c5480ec98b143107a331fd491deda885c4d6a6cc",
},
{
password: []byte("\000"),
salt: []byte("\000"),
s_cost: 3,
t_cost: 3,
output: "4fc7e302ffa29ae0eac31166cee7a552d1d71135f4e0da66486fb68a749b73a4",
},
{
password: []byte("password"),
salt: []byte("salt"),
s_cost: 1,
t_cost: 1,
output: "eefda4a8a75b461fa389c1dcfaf3e9dfacbc26f81f22e6f280d15cc18c417545",
},
}

for _, vector := range test_vectors {
output := Balloon(sha256.New(), vector.password, vector.salt, vector.s_cost, vector.t_cost)
data, err := hex.DecodeString(vector.output)

if err != nil {
panic(err)
}

if !reflect.DeepEqual(output, data) {
t.Log("Expected: ", vector.output, " Output: ", hex.EncodeToString(output))
t.Fail()
}
}
}