]> Cypherpunks.ru repositories - balloon.git/blob - balloon.go
f8257c4a88f220c90656751f095ffa2deef06b69
[balloon.git] / balloon.go
1 /*
2 balloon -- Balloon password hashing function
3 Copyright (C) 2016-2018 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation, either version 3 of the
8 License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this program.  If not, see
17 <http://www.gnu.org/licenses/>.
18 */
19
20 // Balloon password hashing.
21 //
22 // Look https://crypto.stanford.edu/balloon/ for more description.
23 package balloon
24
25 import (
26         "encoding/binary"
27         "hash"
28         "math/big"
29 )
30
31 const (
32         delta = 3
33 )
34
35 // This function takes hash, password, salt, space cost (buffer size,
36 // number of hash-output sized blocks), time cost (number of rounds) and
37 // performs the following:
38 //
39 //    # Expand input into buffer.
40 //    buf[0] = hash(cnt++ || passwd || salt)
41 //    for m from 1 to sCost-1:
42 //        buf[m] = hash(cnt++ || buf[m-1])
43 //    # Mix buffer contents.
44 //    for t from 0 to tCost-1:
45 //        for m from 0 to sCost-1:
46 //            # Hash last and current blocks.
47 //            prev = buf[(m-1) mod sCost]
48 //            buf[m] = hash(cnt++ || prev || buf[m])
49 //            # Hash in pseudorandomly chosen blocks.
50 //            for i from 0 to delta-1:
51 //                other = to_int(hash(cnt++ || salt || t || m || i)) mod sCost
52 //                buf[m] = hash(cnt++ || buf[m] || buf[other])
53 //    # Extract output from buffer.
54 //    return buf[sCost-1]
55 func B(h hash.Hash, passwd, salt []byte, sCost, tCost int) []byte {
56         var cnt uint64
57         intBuf := make([]byte, 8)
58         buf := make([][]byte, sCost)
59         // Expand input into buffer
60         binary.BigEndian.PutUint64(intBuf, cnt)
61         cnt++
62         h.Write(intBuf)
63         h.Write(passwd)
64         h.Write(salt)
65         buf[0] = h.Sum(nil)
66         var m int
67         for m = 1; m < sCost; m++ {
68                 binary.BigEndian.PutUint64(intBuf, cnt)
69                 cnt++
70                 h.Reset()
71                 h.Write(intBuf)
72                 h.Write(buf[m-1])
73                 buf[m] = h.Sum(nil)
74         }
75         // Mix buffer contents
76         var prev []byte
77         var i int
78         bi := big.NewInt(0)
79         bs := big.NewInt(int64(sCost))
80         biBuf := make([]byte, 0, h.Size())
81         var other int
82         for t := 0; t < tCost; t++ {
83                 for m = 0; m < sCost; m++ {
84                         // Hash last and current blocks
85                         if m == 0 {
86                                 prev = buf[len(buf)-1]
87                         } else {
88                                 prev = buf[m-1]
89                         }
90                         binary.BigEndian.PutUint64(intBuf, cnt)
91                         cnt++
92                         h.Reset()
93                         h.Write(intBuf)
94                         h.Write(prev)
95                         h.Write(buf[m])
96                         buf[m] = h.Sum(buf[m][:0])
97
98                         // Hash in pseudorandomly chosen blocks
99                         for i = 0; i < delta; i++ {
100                                 binary.BigEndian.PutUint64(intBuf, cnt)
101                                 cnt++
102                                 h.Reset()
103                                 h.Write(intBuf)
104                                 h.Write(salt)
105                                 binary.BigEndian.PutUint64(intBuf, uint64(t))
106                                 h.Write(intBuf)
107                                 binary.BigEndian.PutUint64(intBuf, uint64(m))
108                                 h.Write(intBuf)
109                                 binary.BigEndian.PutUint64(intBuf, uint64(i))
110                                 h.Write(intBuf)
111                                 biBuf = h.Sum(biBuf[:0])
112                                 bi.SetBytes(biBuf)
113                                 bi.Mod(bi, bs)
114                                 other = int(bi.Uint64())
115                                 binary.BigEndian.PutUint64(intBuf, cnt)
116                                 cnt++
117                                 h.Reset()
118                                 h.Write(intBuf)
119                                 h.Write(buf[m])
120                                 h.Write(buf[other])
121                                 buf[m] = h.Sum(buf[m][:0])
122                         }
123                 }
124         }
125         // Extract output from buffer
126         return buf[sCost-1]
127 }
128
129 // This function adds additional functionality over pure B(): ability to
130 // run several hashers (jobs) simultaneously and second-preimage resistant
131 // password double hashing.
132 //
133 //     H(p, s, jobs) = hash(p || s || (
134 //         B(p, s || "1") XOR
135 //         B(p, s || "2") XOR
136 //         B(p, s || jobs)
137 //     ))
138 func H(hasher func() hash.Hash, passwd, salt []byte, sCost, tCost int, jobs int) []byte {
139         var i int
140         results := make(chan []byte)
141         for ; i < jobs; i++ {
142                 go func(i int) {
143                         saltBuf := make([]byte, 8)
144                         binary.BigEndian.PutUint64(saltBuf, uint64(i))
145                         results <- B(hasher(), passwd, append(salt, saltBuf...), sCost, tCost)
146                 }(i)
147         }
148         h := hasher()
149         h.Write(passwd)
150         h.Write(salt)
151         result := make([]byte, h.Size())
152         for i = 0; i < jobs; i++ {
153                 for n, e := range <-results {
154                         result[n] ^= e
155                 }
156         }
157         close(results)
158         h.Write(result)
159         return h.Sum(result[:0])
160 }