2 balloon -- Balloon password hashing function
3 Copyright (C) 2016-2018 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 published by the Free Software Foundation, either version 3 of the
8 License, or (at your option) any later version.
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.
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/>.
20 // Balloon password hashing.
22 // Look https://crypto.stanford.edu/balloon/ for more description.
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:
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 {
57 intBuf := make([]byte, 8)
58 buf := make([][]byte, sCost)
59 // Expand input into buffer
60 binary.BigEndian.PutUint64(intBuf, cnt)
67 for m = 1; m < sCost; m++ {
68 binary.BigEndian.PutUint64(intBuf, cnt)
75 // Mix buffer contents
79 bs := big.NewInt(int64(sCost))
80 biBuf := make([]byte, 0, h.Size())
82 for t := 0; t < tCost; t++ {
83 for m = 0; m < sCost; m++ {
84 // Hash last and current blocks
86 prev = buf[len(buf)-1]
90 binary.BigEndian.PutUint64(intBuf, cnt)
96 buf[m] = h.Sum(buf[m][:0])
98 // Hash in pseudorandomly chosen blocks
99 for i = 0; i < delta; i++ {
100 binary.BigEndian.PutUint64(intBuf, cnt)
105 binary.BigEndian.PutUint64(intBuf, uint64(t))
107 binary.BigEndian.PutUint64(intBuf, uint64(m))
109 binary.BigEndian.PutUint64(intBuf, uint64(i))
111 biBuf = h.Sum(biBuf[:0])
114 other = int(bi.Uint64())
115 binary.BigEndian.PutUint64(intBuf, cnt)
121 buf[m] = h.Sum(buf[m][:0])
125 // Extract output from buffer
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, 8)
144 binary.BigEndian.PutUint64(saltBuf, uint64(i))
145 results <- B(hasher(), passwd, append(salt, saltBuf...), sCost, tCost)
151 result := make([]byte, h.Size())
152 for i = 0; i < jobs; i++ {
153 for n, e := range <-results {
159 return h.Sum(result[:0])