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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package bcrypt implements Provos and Mazières's bcrypt adaptive hashing
// algorithm. See http://www.usenix.org/event/usenix99/provos/provos.pdf
package bcrypt
// The code is a port of Provos and Mazières's C implementation.
import (
"crypto/rand"
"crypto/subtle"
"fmt"
"io"
"strconv"
"github.com/go-crypt/x/blowfish"
)
type hashed struct {
hash []byte
salt []byte
cost int // allowed range is MinCost to MaxCost
major byte
minor byte
}
// GenerateFromPassword returns the bcrypt hash of the password at the given
// cost. If the cost given is less than MinCost, the cost will be set to
// DefaultCost, instead. Use CompareHashAndPassword, as defined in this package,
// to compare the returned hashed password with its cleartext version.
func GenerateFromPassword(password []byte, cost int) ([]byte, error) {
p, err := newFromPassword(password, cost)
if err != nil {
return nil, err
}
return p.Hash(), nil
}
// GenerateFromPasswordSalt returns the bcrypt hash of the password at the given
// cost. If the cost given is less than MinCost, the cost will be set to
// DefaultCost, instead. Use CompareHashAndPassword, as defined in this package,
// to compare the returned hashed password with its cleartext version.
func GenerateFromPasswordSalt(password, salt []byte, cost int) ([]byte, error) {
p, err := newFromPasswordSalt(password, salt, cost)
if err != nil {
return nil, err
}
return p.Hash(), nil
}
// Key returns a new key from password/salt combination. Salt must be 16 bytes. For storage the salt needs to be encoded
// with bcrypt.Base64Encode.
func Key(password, salt []byte, cost int) ([]byte, error) {
p, err := newFromPasswordSalt(password, salt, cost)
if err != nil {
return nil, err
}
return p.Key(), nil
}
// CompareHashAndPassword compares a bcrypt hashed password with its possible
// plaintext equivalent. Returns nil on success, or an error on failure.
func CompareHashAndPassword(hashedPassword, password []byte) error {
p, err := newFromHash(hashedPassword)
if err != nil {
return err
}
otherHash, err := bcrypt(password, p.cost, p.salt)
if err != nil {
return err
}
otherP := &hashed{otherHash, p.salt, p.cost, p.major, p.minor}
if subtle.ConstantTimeCompare(p.Hash(), otherP.Hash()) == 1 {
return nil
}
return ErrMismatchedHashAndPassword
}
// Cost returns the hashing cost used to create the given hashed
// password. When, in the future, the hashing cost of a password system needs
// to be increased in order to adjust for greater computational power, this
// function allows one to establish which passwords need to be updated.
func Cost(hashedPassword []byte) (int, error) {
p, _, err := newFromHashPartial(hashedPassword)
if err != nil {
return 0, err
}
return p.cost, nil
}
// NewSalt generates a salt with an appropriate length.
func NewSalt() (salt []byte, err error) {
salt = make([]byte, maxSaltSize)
_, err = io.ReadFull(rand.Reader, salt)
return salt, err
}
func newFromPasswordSalt(password, salt []byte, cost int) (p *hashed, err error) {
if cost < MinCost {
cost = DefaultCost
}
p = new(hashed)
p.major = majorVersion
p.minor = minorVersion
if err = checkSalt(salt); err != nil {
return nil, err
}
p.salt = Base64Encode(salt)
if err = checkCost(cost); err != nil {
return nil, err
}
p.cost = cost
hash, err := bcrypt(password, p.cost, p.salt)
if err != nil {
return nil, err
}
p.hash = hash
return p, err
}
func newFromPassword(password []byte, cost int) (p *hashed, err error) {
var salt []byte
if salt, err = NewSalt(); err != nil {
return nil, err
}
return newFromPasswordSalt(password, salt, cost)
}
func newFromHashPartial(hashedSecret []byte) (p *hashed, secret []byte, err error) {
if len(hashedSecret) < minHashSize {
return nil, nil, ErrHashTooShort
}
p = new(hashed)
n, err := p.decodeVersion(hashedSecret)
if err != nil {
return nil, nil, err
}
hashedSecret = hashedSecret[n:]
n, err = p.decodeCost(hashedSecret)
if err != nil {
return nil, nil, err
}
hashedSecret = hashedSecret[n:]
return p, hashedSecret, nil
}
func newFromHash(hashedSecret []byte) (*hashed, error) {
p, hashedSecret, err := newFromHashPartial(hashedSecret)
if err != nil {
return nil, err
}
if len(hashedSecret) != EncodedSaltSize+EncodedHashSize {
return nil, ErrSecretInvalidLength
}
p.salt, p.hash = DecodeSecret(hashedSecret)
return p, nil
}
// DecodeSecret decodes a valid bcrypt secret into a salt and key. This function will panic if the secret does not have
// the len of EncodedSaltSize + EncodedHashSize.
func DecodeSecret(secret []byte) (salt, key []byte) {
if len(secret) != EncodedSaltSize+EncodedHashSize {
panic("secret is malformed as it does not have the expected size")
}
salt, key = make([]byte, EncodedSaltSize, EncodedSaltSize), make([]byte, EncodedHashSize, EncodedHashSize)
copy(salt, secret[:EncodedSaltSize])
copy(key, secret[EncodedSaltSize:])
return
}
func bcrypt(password []byte, cost int, salt []byte) ([]byte, error) {
cipherData := make([]byte, len(magicCipherData))
copy(cipherData, magicCipherData)
c, err := expensiveBlowfishSetup(password, uint32(cost), salt)
if err != nil {
return nil, err
}
for i := 0; i < 24; i += 8 {
for j := 0; j < 64; j++ {
c.Encrypt(cipherData[i:i+8], cipherData[i:i+8])
}
}
// Bug compatibility with C bcrypt implementations. We only encode 23 of
// the 24 bytes encrypted.
hsh := Base64Encode(cipherData[:maxCryptedHashSize])
return hsh, nil
}
func expensiveBlowfishSetup(key []byte, cost uint32, salt []byte) (*blowfish.Cipher, error) {
csalt, err := Base64Decode(salt)
if err != nil {
return nil, err
}
// Bug compatibility with C bcrypt implementations. They use the trailing
// NULL in the key string during expansion.
// We copy the key to prevent changing the underlying array.
ckey := append(key[:len(key):len(key)], 0)
c, err := blowfish.NewSaltedCipher(ckey, csalt)
if err != nil {
return nil, err
}
var i, rounds uint64
rounds = 1 << cost
for i = 0; i < rounds; i++ {
blowfish.ExpandKey(ckey, c)
blowfish.ExpandKey(csalt, c)
}
return c, nil
}
func (p *hashed) Key() []byte {
return p.hash
}
func (p *hashed) Hash() []byte {
arr := make([]byte, 60)
arr[0] = '$'
arr[1] = p.major
n := 2
if p.minor != 0 {
arr[2] = p.minor
n = 3
}
arr[n] = '$'
n++
copy(arr[n:], fmt.Sprintf("%02d", p.cost))
n += 2
arr[n] = '$'
n++
copy(arr[n:], p.salt)
n += EncodedSaltSize
copy(arr[n:], p.hash)
n += EncodedHashSize
return arr[:n]
}
func (p *hashed) decodeVersion(sbytes []byte) (int, error) {
if sbytes[0] != '$' {
return -1, InvalidHashPrefixError(sbytes[0])
}
if sbytes[1] > majorVersion {
return -1, HashVersionTooNewError(sbytes[1])
}
p.major = sbytes[1]
n := 3
if sbytes[2] != '$' {
p.minor = sbytes[2]
n++
}
return n, nil
}
// sbytes should begin where decodeVersion left off.
func (p *hashed) decodeCost(sbytes []byte) (int, error) {
cost, err := strconv.Atoi(string(sbytes[0:2]))
if err != nil {
return -1, err
}
err = checkCost(cost)
if err != nil {
return -1, err
}
p.cost = cost
return 3, nil
}
func (p *hashed) String() string {
return fmt.Sprintf("&{hash: %#v, salt: %#v, cost: %d, major: %c, minor: %c}", string(p.hash), p.salt, p.cost, p.major, p.minor)
}
func checkSalt(salt []byte) error {
if len(salt) != maxSaltSize {
return InvalidSaltSizeError{salt}
}
return nil
}
func checkCost(cost int) error {
if cost < MinCost || cost > MaxCost {
return InvalidCostError(cost)
}
return nil
}
|