forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/crypto/bcrypt/bcrypt.go

507 lines
8.3 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Copyright 2011 The Go Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
// Use of this source code is governed by a BSD-style
2024-02-18 10:42:21 +00:00
// license that can be found in the LICENSE file.
// Package bcrypt implements Provos and Mazières's bcrypt adaptive hashing
2024-02-18 10:42:21 +00:00
// algorithm. See http://www.usenix.org/event/usenix99/provos/provos.pdf
2024-02-18 10:42:21 +00:00
package bcrypt // import "golang.org/x/crypto/bcrypt"
// The code is a port of Provos and Mazières's C implementation.
2024-02-18 10:42:21 +00:00
import (
"crypto/rand"
"crypto/subtle"
"errors"
"fmt"
"io"
"strconv"
"golang.org/x/crypto/blowfish"
)
const (
MinCost int = 4 // the minimum allowable cost as passed in to GenerateFromPassword
MaxCost int = 31 // the maximum allowable cost as passed in to GenerateFromPassword
2024-02-18 10:42:21 +00:00
DefaultCost int = 10 // the cost that will actually be set if a cost below MinCost is passed into GenerateFromPassword
2024-02-18 10:42:21 +00:00
)
// The error returned from CompareHashAndPassword when a password and hash do
2024-02-18 10:42:21 +00:00
// not match.
2024-02-18 10:42:21 +00:00
var ErrMismatchedHashAndPassword = errors.New("crypto/bcrypt: hashedPassword is not the hash of the given password")
// The error returned from CompareHashAndPassword when a hash is too short to
2024-02-18 10:42:21 +00:00
// be a bcrypt hash.
2024-02-18 10:42:21 +00:00
var ErrHashTooShort = errors.New("crypto/bcrypt: hashedSecret too short to be a bcrypted password")
// The error returned from CompareHashAndPassword when a hash was created with
2024-02-18 10:42:21 +00:00
// a bcrypt algorithm newer than this implementation.
2024-02-18 10:42:21 +00:00
type HashVersionTooNewError byte
func (hv HashVersionTooNewError) Error() string {
2024-02-18 10:42:21 +00:00
return fmt.Sprintf("crypto/bcrypt: bcrypt algorithm version '%c' requested is newer than current version '%c'", byte(hv), majorVersion)
2024-02-18 10:42:21 +00:00
}
// The error returned from CompareHashAndPassword when a hash starts with something other than '$'
2024-02-18 10:42:21 +00:00
type InvalidHashPrefixError byte
func (ih InvalidHashPrefixError) Error() string {
2024-02-18 10:42:21 +00:00
return fmt.Sprintf("crypto/bcrypt: bcrypt hashes must start with '$', but hashedSecret started with '%c'", byte(ih))
2024-02-18 10:42:21 +00:00
}
type InvalidCostError int
func (ic InvalidCostError) Error() string {
2024-02-18 10:42:21 +00:00
return fmt.Sprintf("crypto/bcrypt: cost %d is outside allowed range (%d,%d)", int(ic), MinCost, MaxCost)
2024-02-18 10:42:21 +00:00
}
const (
majorVersion = '2'
minorVersion = 'a'
maxSaltSize = 16
2024-02-18 10:42:21 +00:00
maxCryptedHashSize = 23
encodedSaltSize = 22
encodedHashSize = 31
minHashSize = 59
2024-02-18 10:42:21 +00:00
)
// magicCipherData is an IV for the 64 Blowfish encryption calls in
2024-02-18 10:42:21 +00:00
// bcrypt(). It's the string "OrpheanBeholderScryDoubt" in big-endian bytes.
2024-02-18 10:42:21 +00:00
var magicCipherData = []byte{
2024-02-18 10:42:21 +00:00
0x4f, 0x72, 0x70, 0x68,
2024-02-18 10:42:21 +00:00
0x65, 0x61, 0x6e, 0x42,
2024-02-18 10:42:21 +00:00
0x65, 0x68, 0x6f, 0x6c,
2024-02-18 10:42:21 +00:00
0x64, 0x65, 0x72, 0x53,
2024-02-18 10:42:21 +00:00
0x63, 0x72, 0x79, 0x44,
2024-02-18 10:42:21 +00:00
0x6f, 0x75, 0x62, 0x74,
}
type hashed struct {
hash []byte
salt []byte
cost int // allowed range is MinCost to MaxCost
2024-02-18 10:42:21 +00:00
major byte
2024-02-18 10:42:21 +00:00
minor byte
}
// ErrPasswordTooLong is returned when the password passed to
2024-02-18 10:42:21 +00:00
// GenerateFromPassword is too long (i.e. > 72 bytes).
2024-02-18 10:42:21 +00:00
var ErrPasswordTooLong = errors.New("bcrypt: password length exceeds 72 bytes")
// GenerateFromPassword returns the bcrypt hash of the password at the given
2024-02-18 10:42:21 +00:00
// cost. If the cost given is less than MinCost, the cost will be set to
2024-02-18 10:42:21 +00:00
// DefaultCost, instead. Use CompareHashAndPassword, as defined in this package,
2024-02-18 10:42:21 +00:00
// to compare the returned hashed password with its cleartext version.
2024-02-18 10:42:21 +00:00
// GenerateFromPassword does not accept passwords longer than 72 bytes, which
2024-02-18 10:42:21 +00:00
// is the longest password bcrypt will operate on.
2024-02-18 10:42:21 +00:00
func GenerateFromPassword(password []byte, cost int) ([]byte, error) {
2024-02-18 10:42:21 +00:00
if len(password) > 72 {
2024-02-18 10:42:21 +00:00
return nil, ErrPasswordTooLong
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
p, err := newFromPassword(password, cost)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return p.Hash(), nil
2024-02-18 10:42:21 +00:00
}
// CompareHashAndPassword compares a bcrypt hashed password with its possible
2024-02-18 10:42:21 +00:00
// plaintext equivalent. Returns nil on success, or an error on failure.
2024-02-18 10:42:21 +00:00
func CompareHashAndPassword(hashedPassword, password []byte) error {
2024-02-18 10:42:21 +00:00
p, err := newFromHash(hashedPassword)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
otherHash, err := bcrypt(password, p.cost, p.salt)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
otherP := &hashed{otherHash, p.salt, p.cost, p.major, p.minor}
2024-02-18 10:42:21 +00:00
if subtle.ConstantTimeCompare(p.Hash(), otherP.Hash()) == 1 {
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
return ErrMismatchedHashAndPassword
2024-02-18 10:42:21 +00:00
}
// Cost returns the hashing cost used to create the given hashed
2024-02-18 10:42:21 +00:00
// password. When, in the future, the hashing cost of a password system needs
2024-02-18 10:42:21 +00:00
// to be increased in order to adjust for greater computational power, this
2024-02-18 10:42:21 +00:00
// function allows one to establish which passwords need to be updated.
2024-02-18 10:42:21 +00:00
func Cost(hashedPassword []byte) (int, error) {
2024-02-18 10:42:21 +00:00
p, err := newFromHash(hashedPassword)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return 0, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return p.cost, nil
2024-02-18 10:42:21 +00:00
}
func newFromPassword(password []byte, cost int) (*hashed, error) {
2024-02-18 10:42:21 +00:00
if cost < MinCost {
2024-02-18 10:42:21 +00:00
cost = DefaultCost
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
p := new(hashed)
2024-02-18 10:42:21 +00:00
p.major = majorVersion
2024-02-18 10:42:21 +00:00
p.minor = minorVersion
err := checkCost(cost)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
p.cost = cost
unencodedSalt := make([]byte, maxSaltSize)
2024-02-18 10:42:21 +00:00
_, err = io.ReadFull(rand.Reader, unencodedSalt)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
p.salt = base64Encode(unencodedSalt)
2024-02-18 10:42:21 +00:00
hash, err := bcrypt(password, p.cost, p.salt)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
p.hash = hash
2024-02-18 10:42:21 +00:00
return p, err
2024-02-18 10:42:21 +00:00
}
func newFromHash(hashedSecret []byte) (*hashed, error) {
2024-02-18 10:42:21 +00:00
if len(hashedSecret) < minHashSize {
2024-02-18 10:42:21 +00:00
return nil, ErrHashTooShort
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
p := new(hashed)
2024-02-18 10:42:21 +00:00
n, err := p.decodeVersion(hashedSecret)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
hashedSecret = hashedSecret[n:]
2024-02-18 10:42:21 +00:00
n, err = p.decodeCost(hashedSecret)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
hashedSecret = hashedSecret[n:]
// The "+2" is here because we'll have to append at most 2 '=' to the salt
2024-02-18 10:42:21 +00:00
// when base64 decoding it in expensiveBlowfishSetup().
2024-02-18 10:42:21 +00:00
p.salt = make([]byte, encodedSaltSize, encodedSaltSize+2)
2024-02-18 10:42:21 +00:00
copy(p.salt, hashedSecret[:encodedSaltSize])
hashedSecret = hashedSecret[encodedSaltSize:]
2024-02-18 10:42:21 +00:00
p.hash = make([]byte, len(hashedSecret))
2024-02-18 10:42:21 +00:00
copy(p.hash, hashedSecret)
return p, nil
2024-02-18 10:42:21 +00:00
}
func bcrypt(password []byte, cost int, salt []byte) ([]byte, error) {
2024-02-18 10:42:21 +00:00
cipherData := make([]byte, len(magicCipherData))
2024-02-18 10:42:21 +00:00
copy(cipherData, magicCipherData)
c, err := expensiveBlowfishSetup(password, uint32(cost), salt)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
for i := 0; i < 24; i += 8 {
2024-02-18 10:42:21 +00:00
for j := 0; j < 64; j++ {
2024-02-18 10:42:21 +00:00
c.Encrypt(cipherData[i:i+8], cipherData[i:i+8])
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Bug compatibility with C bcrypt implementations. We only encode 23 of
2024-02-18 10:42:21 +00:00
// the 24 bytes encrypted.
2024-02-18 10:42:21 +00:00
hsh := base64Encode(cipherData[:maxCryptedHashSize])
2024-02-18 10:42:21 +00:00
return hsh, nil
2024-02-18 10:42:21 +00:00
}
func expensiveBlowfishSetup(key []byte, cost uint32, salt []byte) (*blowfish.Cipher, error) {
2024-02-18 10:42:21 +00:00
csalt, err := base64Decode(salt)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
// Bug compatibility with C bcrypt implementations. They use the trailing
2024-02-18 10:42:21 +00:00
// NULL in the key string during expansion.
2024-02-18 10:42:21 +00:00
// We copy the key to prevent changing the underlying array.
2024-02-18 10:42:21 +00:00
ckey := append(key[:len(key):len(key)], 0)
c, err := blowfish.NewSaltedCipher(ckey, csalt)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
var i, rounds uint64
2024-02-18 10:42:21 +00:00
rounds = 1 << cost
2024-02-18 10:42:21 +00:00
for i = 0; i < rounds; i++ {
2024-02-18 10:42:21 +00:00
blowfish.ExpandKey(ckey, c)
2024-02-18 10:42:21 +00:00
blowfish.ExpandKey(csalt, c)
2024-02-18 10:42:21 +00:00
}
return c, nil
2024-02-18 10:42:21 +00:00
}
func (p *hashed) Hash() []byte {
2024-02-18 10:42:21 +00:00
arr := make([]byte, 60)
2024-02-18 10:42:21 +00:00
arr[0] = '$'
2024-02-18 10:42:21 +00:00
arr[1] = p.major
2024-02-18 10:42:21 +00:00
n := 2
2024-02-18 10:42:21 +00:00
if p.minor != 0 {
2024-02-18 10:42:21 +00:00
arr[2] = p.minor
2024-02-18 10:42:21 +00:00
n = 3
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
arr[n] = '$'
2024-02-18 10:42:21 +00:00
n++
2024-02-18 10:42:21 +00:00
copy(arr[n:], []byte(fmt.Sprintf("%02d", p.cost)))
2024-02-18 10:42:21 +00:00
n += 2
2024-02-18 10:42:21 +00:00
arr[n] = '$'
2024-02-18 10:42:21 +00:00
n++
2024-02-18 10:42:21 +00:00
copy(arr[n:], p.salt)
2024-02-18 10:42:21 +00:00
n += encodedSaltSize
2024-02-18 10:42:21 +00:00
copy(arr[n:], p.hash)
2024-02-18 10:42:21 +00:00
n += encodedHashSize
2024-02-18 10:42:21 +00:00
return arr[:n]
2024-02-18 10:42:21 +00:00
}
func (p *hashed) decodeVersion(sbytes []byte) (int, error) {
2024-02-18 10:42:21 +00:00
if sbytes[0] != '$' {
2024-02-18 10:42:21 +00:00
return -1, InvalidHashPrefixError(sbytes[0])
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if sbytes[1] > majorVersion {
2024-02-18 10:42:21 +00:00
return -1, HashVersionTooNewError(sbytes[1])
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
p.major = sbytes[1]
2024-02-18 10:42:21 +00:00
n := 3
2024-02-18 10:42:21 +00:00
if sbytes[2] != '$' {
2024-02-18 10:42:21 +00:00
p.minor = sbytes[2]
2024-02-18 10:42:21 +00:00
n++
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return n, nil
2024-02-18 10:42:21 +00:00
}
// sbytes should begin where decodeVersion left off.
2024-02-18 10:42:21 +00:00
func (p *hashed) decodeCost(sbytes []byte) (int, error) {
2024-02-18 10:42:21 +00:00
cost, err := strconv.Atoi(string(sbytes[0:2]))
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return -1, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
err = checkCost(cost)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return -1, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
p.cost = cost
2024-02-18 10:42:21 +00:00
return 3, nil
2024-02-18 10:42:21 +00:00
}
func (p *hashed) String() string {
2024-02-18 10:42:21 +00:00
return fmt.Sprintf("&{hash: %#v, salt: %#v, cost: %d, major: %c, minor: %c}", string(p.hash), p.salt, p.cost, p.major, p.minor)
2024-02-18 10:42:21 +00:00
}
func checkCost(cost int) error {
2024-02-18 10:42:21 +00:00
if cost < MinCost || cost > MaxCost {
2024-02-18 10:42:21 +00:00
return InvalidCostError(cost)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}