]> Cypherpunks repositories - gostls13.git/commitdiff
crypto/des: test using the public API
authorqmuntal <quimmuntal@gmail.com>
Thu, 17 Aug 2023 13:30:58 +0000 (15:30 +0200)
committerQuim Muntal <quimmuntal@gmail.com>
Tue, 29 Aug 2023 16:49:56 +0000 (16:49 +0000)
Several of the tests in crypto/des were using the unexported
desCipher type and other unexported functions to test the package,
leaving desCipher.Encrypt and desCipher.Decrypt only partially tested.

This CL changes the tests to use the public API, except for
TestInitialPermute and TestFinalPermute, which are testing
implementation details on purpose.

Change-Id: I0bc13cea06b79b29425412b9bf36b997871518ac
Reviewed-on: https://go-review.googlesource.com/c/go/+/520495
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Filippo Valsorda <filippo@golang.org>
Run-TryBot: Quim Muntal <quimmuntal@gmail.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
src/crypto/des/block.go
src/crypto/des/cipher.go
src/crypto/des/des_test.go
src/crypto/des/internal_test.go [new file with mode: 0644]

index e0299760d9500f8f13850a1fd7f6e47645522179..c525ab0e5ca2eba4cf2b0c658ce2e27ea6796d27 100644 (file)
@@ -35,16 +35,6 @@ func cryptBlock(subkeys []uint64, dst, src []byte, decrypt bool) {
        binary.BigEndian.PutUint64(dst, permuteFinalBlock(preOutput))
 }
 
-// Encrypt one block from src into dst, using the subkeys.
-func encryptBlock(subkeys []uint64, dst, src []byte) {
-       cryptBlock(subkeys, dst, src, false)
-}
-
-// Decrypt one block from src into dst, using the subkeys.
-func decryptBlock(subkeys []uint64, dst, src []byte) {
-       cryptBlock(subkeys, dst, src, true)
-}
-
 // DES Feistel function. feistelBox must be initialized via
 // feistelBoxOnce.Do(initFeistelBox) first.
 func feistel(l, r uint32, k0, k1 uint64) (lout, rout uint32) {
index ece764f171ad7b71771d73884f4ba341fd2f38f2..699e5177aef5d5887f304dd949514683b75d4bf8 100644 (file)
@@ -48,7 +48,7 @@ func (c *desCipher) Encrypt(dst, src []byte) {
        if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
                panic("crypto/des: invalid buffer overlap")
        }
-       encryptBlock(c.subkeys[:], dst, src)
+       cryptBlock(c.subkeys[:], dst, src, false)
 }
 
 func (c *desCipher) Decrypt(dst, src []byte) {
@@ -61,7 +61,7 @@ func (c *desCipher) Decrypt(dst, src []byte) {
        if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
                panic("crypto/des: invalid buffer overlap")
        }
-       decryptBlock(c.subkeys[:], dst, src)
+       cryptBlock(c.subkeys[:], dst, src, true)
 }
 
 // A tripleDESCipher is an instance of TripleDES encryption.
index 690a49f5efb05a6e8aa065369e8958b2bf0d92a0..7bebcd93d4137be165376aa7f3aad343d167069c 100644 (file)
@@ -2,10 +2,12 @@
 // 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"
 )
 
@@ -1260,12 +1262,12 @@ var tableA4Tests = []CryptTest{
                []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
@@ -1274,7 +1276,7 @@ func TestWeakKeys(t *testing.T) {
                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
                }
 
@@ -1295,7 +1297,7 @@ func TestSemiWeakKeyPairs(t *testing.T) {
                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
                }
 
@@ -1315,7 +1317,7 @@ func TestDESEncryptBlock(t *testing.T) {
        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)
@@ -1327,7 +1329,7 @@ func TestDESDecryptBlock(t *testing.T) {
        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)
@@ -1337,7 +1339,7 @@ func TestDESDecryptBlock(t *testing.T) {
 
 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)
 
@@ -1349,7 +1351,7 @@ func TestEncryptTripleDES(t *testing.T) {
 
 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)
@@ -1363,7 +1365,7 @@ func TestDecryptTripleDES(t *testing.T) {
 // 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)
@@ -1377,7 +1379,7 @@ func TestVariablePlaintextKnownAnswer(t *testing.T) {
 // 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)
@@ -1393,7 +1395,7 @@ func TestVariableCiphertextKnownAnswer(t *testing.T) {
 // 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)
@@ -1409,7 +1411,7 @@ func TestInversePermutationKnownAnswer(t *testing.T) {
 // 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)
@@ -1423,7 +1425,7 @@ func TestInitialPermutationKnownAnswer(t *testing.T) {
 // 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)
@@ -1437,7 +1439,7 @@ func TestVariableKeyKnownAnswerEncrypt(t *testing.T) {
 // 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)
@@ -1451,7 +1453,7 @@ func TestVariableKeyKnownAnswerDecrypt(t *testing.T) {
 // 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)
@@ -1465,7 +1467,7 @@ func TestPermutationOperationKnownAnswerEncrypt(t *testing.T) {
 // 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)
@@ -1479,7 +1481,7 @@ func TestPermutationOperationKnownAnswerDecrypt(t *testing.T) {
 // 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)
@@ -1493,7 +1495,7 @@ func TestSubstitutionTableKnownAnswerEncrypt(t *testing.T) {
 // 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)
@@ -1504,31 +1506,9 @@ func TestSubstitutionTableKnownAnswerDecrypt(t *testing.T) {
        }
 }
 
-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)
        }
@@ -1542,7 +1522,7 @@ func BenchmarkEncrypt(b *testing.B) {
 
 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)
        }
@@ -1556,7 +1536,7 @@ func BenchmarkDecrypt(b *testing.B) {
 
 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)
        }
@@ -1570,7 +1550,7 @@ func BenchmarkTDESEncrypt(b *testing.B) {
 
 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)
        }
diff --git a/src/crypto/des/internal_test.go b/src/crypto/des/internal_test.go
new file mode 100644 (file)
index 0000000..f309b01
--- /dev/null
@@ -0,0 +1,29 @@
+// Copyright 2023 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 des
+
+import "testing"
+
+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)
+               }
+       }
+}