From dc67354e864179a90a9bec14aa1c9af1344e1394 Mon Sep 17 00:00:00 2001 From: Dmitry Chestnykh Date: Sat, 6 Oct 2012 10:03:43 -0400 Subject: [PATCH] go.crypto/blowfish: document that only 16 bytes of salt are used. Also fix salt expansion code to fill the properly sized array, since the following code never uses more than 4 uints. Also remove unused expandedKey array. R=golang-dev, agl CC=golang-dev https://golang.org/cl/6622050 --- blowfish/block.go | 12 +++++------- blowfish/cipher.go | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/blowfish/block.go b/blowfish/block.go index 326292d..7b14dec 100644 --- a/blowfish/block.go +++ b/blowfish/block.go @@ -14,7 +14,7 @@ func ExpandKey(key []byte, c *Cipher) { for i := 0; i < 18; i++ { var d uint32 for k := 0; k < 4; k++ { - d = d<<8 | uint32(key[j])&0x000000FF + d = d<<8 | uint32(key[j]) j++ if j >= len(key) { j = 0 @@ -53,26 +53,24 @@ func ExpandKey(key []byte, c *Cipher) { // and specializing it here is useful. func expandKeyWithSalt(key []byte, salt []byte, c *Cipher) { j := 0 - expandedKey := make([]uint32, 18) for i := 0; i < 18; i++ { var d uint32 for k := 0; k < 4; k++ { - d = d<<8 | uint32(key[j])&0x000000FF + d = d<<8 | uint32(key[j]) j++ if j >= len(key) { j = 0 } } - expandedKey[i] = d c.p[i] ^= d } j = 0 - expandedSalt := make([]uint32, 18) - for i := 0; i < 18; i++ { + var expandedSalt [4]uint32 + for i := range expandedSalt { var d uint32 for k := 0; k < 4; k++ { - d = d<<8 | uint32(salt[j])&0x000000FF + d = d<<8 | uint32(salt[j]) j++ if j >= len(salt) { j = 0 diff --git a/blowfish/cipher.go b/blowfish/cipher.go index 94e10f0..2f712ad 100644 --- a/blowfish/cipher.go +++ b/blowfish/cipher.go @@ -41,7 +41,7 @@ func NewCipher(key []byte) (*Cipher, error) { // NewSaltedCipher creates a returns a Cipher that folds a salt into its key // schedule. For most purposes, NewCipher, instead of NewSaltedCipher, is // sufficient and desirable. For bcrypt compatiblity, the key can be over 56 -// bytes. +// bytes. Only the first 16 bytes of salt are used. func NewSaltedCipher(key, salt []byte) (*Cipher, error) { var result Cipher k := len(key)