2 balloon -- Balloon password hashing function
3 Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
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 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program. If not, see
16 <http://www.gnu.org/licenses/>.
19 // Balloon password hashing.
21 // Look https://crypto.stanford.edu/balloon/ for more description.
34 // This function takes hash, password, salt, space cost (buffer size,
35 // number of hash-output sized blocks), time cost (number of rounds) and
36 // performs the following:
38 // # Expand input into buffer.
39 // buf[0] = hash(cnt++ || passwd || salt)
40 // for m from 1 to sCost-1:
41 // buf[m] = hash(cnt++ || buf[m-1])
42 // # Mix buffer contents.
43 // for t from 0 to tCost-1:
44 // for m from 0 to sCost-1:
45 // # Hash last and current blocks.
46 // prev = buf[(m-1) mod sCost]
47 // buf[m] = hash(cnt++ || prev || buf[m])
48 // # Hash in pseudorandomly chosen blocks.
49 // for i from 0 to delta-1:
50 // other = to_int(hash(cnt++ || salt || t || m || i)) mod sCost
51 // buf[m] = hash(cnt++ || buf[m] || buf[other])
52 // # Extract output from buffer.
53 // return buf[sCost-1]
54 func B(h hash.Hash, passwd, salt []byte, sCost, tCost uint64) []byte {
56 intBuf := make([]byte, 8)
57 hSize := uint64(h.Size())
58 buf := make([]byte, 0, sCost*hSize)
59 // Expand input into buffer
60 binary.BigEndian.PutUint64(intBuf, cnt)
67 for m = 1; m < sCost; m++ {
68 binary.BigEndian.PutUint64(intBuf, cnt)
72 h.Write(buf[(m-1)*hSize : m*hSize])
75 // Mix buffer contents
80 bs := big.NewInt(int64(sCost))
81 biBuf := make([]byte, 0, h.Size())
82 for t := uint64(0); t < tCost; t++ {
83 for m = 0; m < sCost; m++ {
84 // Hash last and current blocks
86 prev = buf[(sCost-1)*hSize:]
88 prev = buf[(m-1)*hSize : m*hSize]
90 binary.BigEndian.PutUint64(intBuf, cnt)
95 h.Write(buf[m*hSize : (m+1)*hSize])
96 buf = h.Sum(buf[:m*hSize])
98 // Hash in pseudorandomly chosen blocks
99 for i = 0; i < delta; i++ {
100 binary.BigEndian.PutUint64(intBuf, cnt)
105 binary.BigEndian.PutUint64(intBuf, t)
107 binary.BigEndian.PutUint64(intBuf, m)
109 binary.BigEndian.PutUint64(intBuf, i)
111 biBuf = h.Sum(biBuf[:0])
114 binary.BigEndian.PutUint64(intBuf, cnt)
118 h.Write(buf[m*hSize : (m+1)*hSize])
120 h.Write(buf[neigh*hSize : (neigh+1)*hSize])
121 buf = h.Sum(buf[:m*hSize])
125 // Extract output from buffer
126 return buf[(sCost-1)*hSize:]
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.
133 // H(p, s, jobs) = hash(p || s || (
134 // B(p, s || "1") XOR
135 // B(p, s || "2") XOR
138 func H(hasher func() hash.Hash, passwd, salt []byte, sCost, tCost int, jobs int) []byte {
140 results := make(chan []byte)
141 for ; i < jobs; i++ {
143 saltBuf := make([]byte, len(salt)+8)
145 binary.BigEndian.PutUint64(saltBuf[len(salt):], uint64(i))
146 results <- B(hasher(), passwd, saltBuf, uint64(sCost), uint64(tCost))
152 result := make([]byte, h.Size())
153 for i = 0; i < jobs; i++ {
154 for n, e := range <-results {
160 return h.Sum(result[:0])