github.com/emmansun/gmsm@v0.29.1/pkcs/internal/rc2/rc2_test.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package rc2
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/hex"
    10  	"testing"
    11  
    12  	"github.com/emmansun/gmsm/internal/cryptotest"
    13  )
    14  
    15  func TestEncryptDecrypt(t *testing.T) {
    16  	// TODO(dgryski): add the rest of the test vectors from the RFC
    17  	var tests = []struct {
    18  		key    string
    19  		plain  string
    20  		cipher string
    21  		t1     int
    22  	}{
    23  		{
    24  			"0000000000000000",
    25  			"0000000000000000",
    26  			"ebb773f993278eff",
    27  			63,
    28  		},
    29  		{
    30  			"ffffffffffffffff",
    31  			"ffffffffffffffff",
    32  			"278b27e42e2f0d49",
    33  			64,
    34  		},
    35  		{
    36  			"3000000000000000",
    37  			"1000000000000001",
    38  			"30649edf9be7d2c2",
    39  			64,
    40  		},
    41  		{
    42  			"88",
    43  			"0000000000000000",
    44  			"61a8a244adacccf0",
    45  			64,
    46  		},
    47  		{
    48  			"88bca90e90875a",
    49  			"0000000000000000",
    50  			"6ccf4308974c267f",
    51  			64,
    52  		},
    53  		{
    54  			"88bca90e90875a7f0f79c384627bafb2",
    55  			"0000000000000000",
    56  			"1a807d272bbe5db1",
    57  			64,
    58  		},
    59  		{
    60  			"88bca90e90875a7f0f79c384627bafb2",
    61  			"0000000000000000",
    62  			"2269552ab0f85ca6",
    63  			128,
    64  		},
    65  		{
    66  			"88bca90e90875a7f0f79c384627bafb216f80a6f85920584c42fceb0be255daf1e",
    67  			"0000000000000000",
    68  			"5b78d3a43dfff1f1",
    69  			129,
    70  		},
    71  	}
    72  
    73  	for _, tt := range tests {
    74  		k, _ := hex.DecodeString(tt.key)
    75  		p, _ := hex.DecodeString(tt.plain)
    76  		c, _ := hex.DecodeString(tt.cipher)
    77  
    78  		b, err := NewCipherWithEffectiveKeyBits(k, tt.t1)
    79  		if err != nil {
    80  			t.Errorf("New(%q, %d) failed: %v", tt.key, tt.t1, err)
    81  			continue
    82  		}
    83  
    84  		var dst [8]byte
    85  
    86  		b.Encrypt(dst[:], p)
    87  
    88  		if !bytes.Equal(dst[:], c) {
    89  			t.Errorf("encrypt failed: got % 2x wanted % 2x\n", dst, c)
    90  		}
    91  
    92  		b.Decrypt(dst[:], c)
    93  
    94  		if !bytes.Equal(dst[:], p) {
    95  			t.Errorf("decrypt failed: got % 2x wanted % 2x\n", dst, p)
    96  		}
    97  	}
    98  }
    99  
   100  func TestRC2Block(t *testing.T) {
   101  	t.Run("RC2", func(t *testing.T) {
   102  		cryptotest.TestBlock(t, 8, NewCipher)
   103  	})
   104  }