gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/xcrypto/pkcs12/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  
    13  func TestEncryptDecrypt(t *testing.T) {
    14  	// TODO(dgryski): add the rest of the test vectors from the RFC
    15  	var tests = []struct {
    16  		key    string
    17  		plain  string
    18  		cipher string
    19  		t1     int
    20  	}{
    21  		{
    22  			"0000000000000000",
    23  			"0000000000000000",
    24  			"ebb773f993278eff",
    25  			63,
    26  		},
    27  		{
    28  			"ffffffffffffffff",
    29  			"ffffffffffffffff",
    30  			"278b27e42e2f0d49",
    31  			64,
    32  		},
    33  		{
    34  			"3000000000000000",
    35  			"1000000000000001",
    36  			"30649edf9be7d2c2",
    37  			64,
    38  		},
    39  		{
    40  			"88",
    41  			"0000000000000000",
    42  			"61a8a244adacccf0",
    43  			64,
    44  		},
    45  		{
    46  			"88bca90e90875a",
    47  			"0000000000000000",
    48  			"6ccf4308974c267f",
    49  			64,
    50  		},
    51  		{
    52  			"88bca90e90875a7f0f79c384627bafb2",
    53  			"0000000000000000",
    54  			"1a807d272bbe5db1",
    55  			64,
    56  		},
    57  		{
    58  			"88bca90e90875a7f0f79c384627bafb2",
    59  			"0000000000000000",
    60  			"2269552ab0f85ca6",
    61  			128,
    62  		},
    63  		{
    64  			"88bca90e90875a7f0f79c384627bafb216f80a6f85920584c42fceb0be255daf1e",
    65  			"0000000000000000",
    66  			"5b78d3a43dfff1f1",
    67  			129,
    68  		},
    69  	}
    70  
    71  	for _, tt := range tests {
    72  		k, _ := hex.DecodeString(tt.key)
    73  		p, _ := hex.DecodeString(tt.plain)
    74  		c, _ := hex.DecodeString(tt.cipher)
    75  
    76  		b, _ := New(k, tt.t1)
    77  
    78  		var dst [8]byte
    79  
    80  		b.Encrypt(dst[:], p)
    81  
    82  		if !bytes.Equal(dst[:], c) {
    83  			t.Errorf("encrypt failed: got % 2x wanted % 2x\n", dst, c)
    84  		}
    85  
    86  		b.Decrypt(dst[:], c)
    87  
    88  		if !bytes.Equal(dst[:], p) {
    89  			t.Errorf("decrypt failed: got % 2x wanted % 2x\n", dst, p)
    90  		}
    91  	}
    92  }