summaryrefslogtreecommitdiff
path: root/libgo/go/crypto/cast5/cast5_test.go
blob: 5f7025ff24cb7aba2d598a8b27a643ab6492081a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2010 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 cast5

import (
	"bytes"
	"encoding/hex"
	"testing"
)

// This test vector is taken from RFC 2144, App B.1.
// Since the other two test vectors are for reduced-round variants, we can't
// use them.
var basicTests = []struct {
	key, plainText, cipherText string
}{
	{
		"0123456712345678234567893456789a",
		"0123456789abcdef",
		"238b4fe5847e44b2",
	},
}

func TestBasic(t *testing.T) {
	for i, test := range basicTests {
		key, _ := hex.DecodeString(test.key)
		plainText, _ := hex.DecodeString(test.plainText)
		expected, _ := hex.DecodeString(test.cipherText)

		c, err := NewCipher(key)
		if err != nil {
			t.Errorf("#%d: failed to create Cipher: %s", i, err)
			continue
		}
		var cipherText [BlockSize]byte
		c.Encrypt(cipherText[:], plainText)
		if !bytes.Equal(cipherText[:], expected) {
			t.Errorf("#%d: got:%x want:%x", i, cipherText, expected)
		}

		var plainTextAgain [BlockSize]byte
		c.Decrypt(plainTextAgain[:], cipherText[:])
		if !bytes.Equal(plainTextAgain[:], plainText) {
			t.Errorf("#%d: got:%x want:%x", i, plainTextAgain, plainText)
		}
	}
}

// TestFull performs the test specified in RFC 2144, App B.2.
// However, due to the length of time taken, it's disabled here and a more
// limited version is included, below.
func TestFull(t *testing.T) {
	// This is too slow for normal testing
	return

	a, b := iterate(1000000)

	const expectedA = "eea9d0a249fd3ba6b3436fb89d6dca92"
	const expectedB = "b2c95eb00c31ad7180ac05b8e83d696e"

	if hex.EncodeToString(a) != expectedA {
		t.Errorf("a: got:%x want:%s", a, expectedA)
	}
	if hex.EncodeToString(b) != expectedB {
		t.Errorf("b: got:%x want:%s", b, expectedB)
	}
}

func iterate(iterations int) ([]byte, []byte) {
	const initValueHex = "0123456712345678234567893456789a"

	initValue, _ := hex.DecodeString(initValueHex)

	var a, b [16]byte
	copy(a[:], initValue)
	copy(b[:], initValue)

	for i := 0; i < iterations; i++ {
		c, _ := NewCipher(b[:])
		c.Encrypt(a[:8], a[:8])
		c.Encrypt(a[8:], a[8:])
		c, _ = NewCipher(a[:])
		c.Encrypt(b[:8], b[:8])
		c.Encrypt(b[8:], b[8:])
	}

	return a[:], b[:]
}

func TestLimited(t *testing.T) {
	a, b := iterate(1000)

	const expectedA = "23f73b14b02a2ad7dfb9f2c35644798d"
	const expectedB = "e5bf37eff14c456a40b21ce369370a9f"

	if hex.EncodeToString(a) != expectedA {
		t.Errorf("a: got:%x want:%s", a, expectedA)
	}
	if hex.EncodeToString(b) != expectedB {
		t.Errorf("b: got:%x want:%s", b, expectedB)
	}
}