|
|
@@ -4,9 +4,7 @@
|
|
|
|
|
|
package blowfish
|
|
|
|
|
|
-import (
|
|
|
- "testing"
|
|
|
-)
|
|
|
+import "testing"
|
|
|
|
|
|
type CryptTest struct {
|
|
|
key []byte
|
|
|
@@ -202,3 +200,75 @@ func TestSaltedCipherKeyLength(t *testing.T) {
|
|
|
t.Errorf("NewSaltedCipher with long key, gave error %#v", err)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+// Test vectors generated with Blowfish from OpenSSH.
|
|
|
+var saltedVectors = [][8]byte{
|
|
|
+ {0x0c, 0x82, 0x3b, 0x7b, 0x8d, 0x01, 0x4b, 0x7e},
|
|
|
+ {0xd1, 0xe1, 0x93, 0xf0, 0x70, 0xa6, 0xdb, 0x12},
|
|
|
+ {0xfc, 0x5e, 0xba, 0xde, 0xcb, 0xf8, 0x59, 0xad},
|
|
|
+ {0x8a, 0x0c, 0x76, 0xe7, 0xdd, 0x2c, 0xd3, 0xa8},
|
|
|
+ {0x2c, 0xcb, 0x7b, 0xee, 0xac, 0x7b, 0x7f, 0xf8},
|
|
|
+ {0xbb, 0xf6, 0x30, 0x6f, 0xe1, 0x5d, 0x62, 0xbf},
|
|
|
+ {0x97, 0x1e, 0xc1, 0x3d, 0x3d, 0xe0, 0x11, 0xe9},
|
|
|
+ {0x06, 0xd7, 0x4d, 0xb1, 0x80, 0xa3, 0xb1, 0x38},
|
|
|
+ {0x67, 0xa1, 0xa9, 0x75, 0x0e, 0x5b, 0xc6, 0xb4},
|
|
|
+ {0x51, 0x0f, 0x33, 0x0e, 0x4f, 0x67, 0xd2, 0x0c},
|
|
|
+ {0xf1, 0x73, 0x7e, 0xd8, 0x44, 0xea, 0xdb, 0xe5},
|
|
|
+ {0x14, 0x0e, 0x16, 0xce, 0x7f, 0x4a, 0x9c, 0x7b},
|
|
|
+ {0x4b, 0xfe, 0x43, 0xfd, 0xbf, 0x36, 0x04, 0x47},
|
|
|
+ {0xb1, 0xeb, 0x3e, 0x15, 0x36, 0xa7, 0xbb, 0xe2},
|
|
|
+ {0x6d, 0x0b, 0x41, 0xdd, 0x00, 0x98, 0x0b, 0x19},
|
|
|
+ {0xd3, 0xce, 0x45, 0xce, 0x1d, 0x56, 0xb7, 0xfc},
|
|
|
+ {0xd9, 0xf0, 0xfd, 0xda, 0xc0, 0x23, 0xb7, 0x93},
|
|
|
+ {0x4c, 0x6f, 0xa1, 0xe4, 0x0c, 0xa8, 0xca, 0x57},
|
|
|
+ {0xe6, 0x2f, 0x28, 0xa7, 0x0c, 0x94, 0x0d, 0x08},
|
|
|
+ {0x8f, 0xe3, 0xf0, 0xb6, 0x29, 0xe3, 0x44, 0x03},
|
|
|
+ {0xff, 0x98, 0xdd, 0x04, 0x45, 0xb4, 0x6d, 0x1f},
|
|
|
+ {0x9e, 0x45, 0x4d, 0x18, 0x40, 0x53, 0xdb, 0xef},
|
|
|
+ {0xb7, 0x3b, 0xef, 0x29, 0xbe, 0xa8, 0x13, 0x71},
|
|
|
+ {0x02, 0x54, 0x55, 0x41, 0x8e, 0x04, 0xfc, 0xad},
|
|
|
+ {0x6a, 0x0a, 0xee, 0x7c, 0x10, 0xd9, 0x19, 0xfe},
|
|
|
+ {0x0a, 0x22, 0xd9, 0x41, 0xcc, 0x23, 0x87, 0x13},
|
|
|
+ {0x6e, 0xff, 0x1f, 0xff, 0x36, 0x17, 0x9c, 0xbe},
|
|
|
+ {0x79, 0xad, 0xb7, 0x40, 0xf4, 0x9f, 0x51, 0xa6},
|
|
|
+ {0x97, 0x81, 0x99, 0xa4, 0xde, 0x9e, 0x9f, 0xb6},
|
|
|
+ {0x12, 0x19, 0x7a, 0x28, 0xd0, 0xdc, 0xcc, 0x92},
|
|
|
+ {0x81, 0xda, 0x60, 0x1e, 0x0e, 0xdd, 0x65, 0x56},
|
|
|
+ {0x7d, 0x76, 0x20, 0xb2, 0x73, 0xc9, 0x9e, 0xee},
|
|
|
+}
|
|
|
+
|
|
|
+func TestSaltedCipher(t *testing.T) {
|
|
|
+ var key, salt [32]byte
|
|
|
+ for i := range key {
|
|
|
+ key[i] = byte(i)
|
|
|
+ salt[i] = byte(i + 32)
|
|
|
+ }
|
|
|
+ for i, v := range saltedVectors {
|
|
|
+ c, err := NewSaltedCipher(key[:], salt[:i])
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ var buf [8]byte
|
|
|
+ c.Encrypt(buf[:], buf[:])
|
|
|
+ if v != buf {
|
|
|
+ t.Errorf("%d: expected %x, got %x", i, v, buf)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func BenchmarkExpandKeyWithSalt(b *testing.B) {
|
|
|
+ key := make([]byte, 32)
|
|
|
+ salt := make([]byte, 16)
|
|
|
+ c, _ := NewCipher(key)
|
|
|
+ for i := 0; i < b.N; i++ {
|
|
|
+ expandKeyWithSalt(key, salt, c)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func BenchmarkExpandKey(b *testing.B) {
|
|
|
+ key := make([]byte, 32)
|
|
|
+ c, _ := NewCipher(key)
|
|
|
+ for i := 0; i < b.N; i++ {
|
|
|
+ ExpandKey(key, c)
|
|
|
+ }
|
|
|
+}
|