// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-package des
+package des_test
import (
"bytes"
+ "crypto/cipher"
+ "crypto/des"
"testing"
)
[]byte{0x63, 0xfa, 0xc0, 0xd0, 0x34, 0xd9, 0xf7, 0x93}},
}
-func newCipher(key []byte) *desCipher {
- c, err := NewCipher(key)
+func newCipher(key []byte) cipher.Block {
+ c, err := des.NewCipher(key)
if err != nil {
panic("NewCipher failed: " + err.Error())
}
- return c.(*desCipher)
+ return c
}
// Use the known weak keys to test DES implementation
var encrypt = func(in []byte) (out []byte) {
c := newCipher(tt.key)
out = make([]byte, len(in))
- encryptBlock(c.subkeys[:], out, in)
+ c.Encrypt(out, in)
return
}
var encrypt = func(key, in []byte) (out []byte) {
c := newCipher(key)
out = make([]byte, len(in))
- encryptBlock(c.subkeys[:], out, in)
+ c.Encrypt(out, in)
return
}
for i, tt := range encryptDESTests {
c := newCipher(tt.key)
out := make([]byte, len(tt.in))
- encryptBlock(c.subkeys[:], out, tt.in)
+ c.Encrypt(out, tt.in)
if !bytes.Equal(out, tt.out) {
t.Errorf("#%d: result: %x want: %x", i, out, tt.out)
for i, tt := range encryptDESTests {
c := newCipher(tt.key)
plain := make([]byte, len(tt.in))
- decryptBlock(c.subkeys[:], plain, tt.out)
+ c.Decrypt(plain, tt.out)
if !bytes.Equal(plain, tt.in) {
t.Errorf("#%d: result: %x want: %x", i, plain, tt.in)
func TestEncryptTripleDES(t *testing.T) {
for i, tt := range encryptTripleDESTests {
- c, _ := NewTripleDESCipher(tt.key)
+ c, _ := des.NewTripleDESCipher(tt.key)
out := make([]byte, len(tt.in))
c.Encrypt(out, tt.in)
func TestDecryptTripleDES(t *testing.T) {
for i, tt := range encryptTripleDESTests {
- c, _ := NewTripleDESCipher(tt.key)
+ c, _ := des.NewTripleDESCipher(tt.key)
plain := make([]byte, len(tt.in))
c.Decrypt(plain, tt.out)
// Defined in Pub 800-20
func TestVariablePlaintextKnownAnswer(t *testing.T) {
for i, tt := range tableA1Tests {
- c, _ := NewTripleDESCipher(tableA1Key)
+ c, _ := des.NewTripleDESCipher(tableA1Key)
out := make([]byte, len(tt.in))
c.Encrypt(out, tt.in)
// Defined in Pub 800-20
func TestVariableCiphertextKnownAnswer(t *testing.T) {
for i, tt := range tableA1Tests {
- c, _ := NewTripleDESCipher(tableA1Key)
+ c, _ := des.NewTripleDESCipher(tableA1Key)
plain := make([]byte, len(tt.out))
c.Decrypt(plain, tt.out)
// 0x01... key produces the original plaintext
func TestInversePermutationKnownAnswer(t *testing.T) {
for i, tt := range tableA1Tests {
- c, _ := NewTripleDESCipher(tableA1Key)
+ c, _ := des.NewTripleDESCipher(tableA1Key)
plain := make([]byte, len(tt.in))
c.Encrypt(plain, tt.out)
// 0x01... key produces the corresponding ciphertext
func TestInitialPermutationKnownAnswer(t *testing.T) {
for i, tt := range tableA1Tests {
- c, _ := NewTripleDESCipher(tableA1Key)
+ c, _ := des.NewTripleDESCipher(tableA1Key)
out := make([]byte, len(tt.in))
c.Decrypt(out, tt.in)
// Defined in Pub 800-20
func TestVariableKeyKnownAnswerEncrypt(t *testing.T) {
for i, tt := range tableA2Tests {
- c, _ := NewTripleDESCipher(tt.key)
+ c, _ := des.NewTripleDESCipher(tt.key)
out := make([]byte, len(tableA2Plaintext))
c.Encrypt(out, tableA2Plaintext)
// Defined in Pub 800-20
func TestVariableKeyKnownAnswerDecrypt(t *testing.T) {
for i, tt := range tableA2Tests {
- c, _ := NewTripleDESCipher(tt.key)
+ c, _ := des.NewTripleDESCipher(tt.key)
out := make([]byte, len(tt.out))
c.Decrypt(out, tt.out)
// Defined in Pub 800-20
func TestPermutationOperationKnownAnswerEncrypt(t *testing.T) {
for i, tt := range tableA3Tests {
- c, _ := NewTripleDESCipher(tt.key)
+ c, _ := des.NewTripleDESCipher(tt.key)
out := make([]byte, len(tableA3Plaintext))
c.Encrypt(out, tableA3Plaintext)
// Defined in Pub 800-20
func TestPermutationOperationKnownAnswerDecrypt(t *testing.T) {
for i, tt := range tableA3Tests {
- c, _ := NewTripleDESCipher(tt.key)
+ c, _ := des.NewTripleDESCipher(tt.key)
out := make([]byte, len(tt.out))
c.Decrypt(out, tt.out)
// Defined in Pub 800-20
func TestSubstitutionTableKnownAnswerEncrypt(t *testing.T) {
for i, tt := range tableA4Tests {
- c, _ := NewTripleDESCipher(tt.key)
+ c, _ := des.NewTripleDESCipher(tt.key)
out := make([]byte, len(tt.in))
c.Encrypt(out, tt.in)
// Defined in Pub 800-20
func TestSubstitutionTableKnownAnswerDecrypt(t *testing.T) {
for i, tt := range tableA4Tests {
- c, _ := NewTripleDESCipher(tt.key)
+ c, _ := des.NewTripleDESCipher(tt.key)
out := make([]byte, len(tt.out))
c.Decrypt(out, tt.out)
}
}
-func TestInitialPermute(t *testing.T) {
- for i := uint(0); i < 64; i++ {
- bit := uint64(1) << i
- got := permuteInitialBlock(bit)
- want := uint64(1) << finalPermutation[63-i]
- if got != want {
- t.Errorf("permute(%x) = %x, want %x", bit, got, want)
- }
- }
-}
-
-func TestFinalPermute(t *testing.T) {
- for i := uint(0); i < 64; i++ {
- bit := uint64(1) << i
- got := permuteFinalBlock(bit)
- want := uint64(1) << initialPermutation[63-i]
- if got != want {
- t.Errorf("permute(%x) = %x, want %x", bit, got, want)
- }
- }
-}
-
func BenchmarkEncrypt(b *testing.B) {
tt := encryptDESTests[0]
- c, err := NewCipher(tt.key)
+ c, err := des.NewCipher(tt.key)
if err != nil {
b.Fatal("NewCipher:", err)
}
func BenchmarkDecrypt(b *testing.B) {
tt := encryptDESTests[0]
- c, err := NewCipher(tt.key)
+ c, err := des.NewCipher(tt.key)
if err != nil {
b.Fatal("NewCipher:", err)
}
func BenchmarkTDESEncrypt(b *testing.B) {
tt := encryptTripleDESTests[0]
- c, err := NewTripleDESCipher(tt.key)
+ c, err := des.NewTripleDESCipher(tt.key)
if err != nil {
b.Fatal("NewCipher:", err)
}
func BenchmarkTDESDecrypt(b *testing.B) {
tt := encryptTripleDESTests[0]
- c, err := NewTripleDESCipher(tt.key)
+ c, err := des.NewTripleDESCipher(tt.key)
if err != nil {
b.Fatal("NewCipher:", err)
}