1
2
3
4
5
6
7
8
9
10
11 package cipher_test
12
13 import (
14 "bytes"
15 "crypto/aes"
16 "crypto/cipher"
17 "crypto/des"
18 "crypto/internal/cryptotest"
19 "fmt"
20 "testing"
21 )
22
23 type ofbTest struct {
24 name string
25 key []byte
26 iv []byte
27 in []byte
28 out []byte
29 }
30
31 var ofbTests = []ofbTest{
32
33 {
34 "OFB-AES128",
35 commonKey128,
36 commonIV,
37 commonInput,
38 []byte{
39 0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20, 0x33, 0x34, 0x49, 0xf8, 0xe8, 0x3c, 0xfb, 0x4a,
40 0x77, 0x89, 0x50, 0x8d, 0x16, 0x91, 0x8f, 0x03, 0xf5, 0x3c, 0x52, 0xda, 0xc5, 0x4e, 0xd8, 0x25,
41 0x97, 0x40, 0x05, 0x1e, 0x9c, 0x5f, 0xec, 0xf6, 0x43, 0x44, 0xf7, 0xa8, 0x22, 0x60, 0xed, 0xcc,
42 0x30, 0x4c, 0x65, 0x28, 0xf6, 0x59, 0xc7, 0x78, 0x66, 0xa5, 0x10, 0xd9, 0xc1, 0xd6, 0xae, 0x5e,
43 },
44 },
45 {
46 "OFB-AES192",
47 commonKey192,
48 commonIV,
49 commonInput,
50 []byte{
51 0xcd, 0xc8, 0x0d, 0x6f, 0xdd, 0xf1, 0x8c, 0xab, 0x34, 0xc2, 0x59, 0x09, 0xc9, 0x9a, 0x41, 0x74,
52 0xfc, 0xc2, 0x8b, 0x8d, 0x4c, 0x63, 0x83, 0x7c, 0x09, 0xe8, 0x17, 0x00, 0xc1, 0x10, 0x04, 0x01,
53 0x8d, 0x9a, 0x9a, 0xea, 0xc0, 0xf6, 0x59, 0x6f, 0x55, 0x9c, 0x6d, 0x4d, 0xaf, 0x59, 0xa5, 0xf2,
54 0x6d, 0x9f, 0x20, 0x08, 0x57, 0xca, 0x6c, 0x3e, 0x9c, 0xac, 0x52, 0x4b, 0xd9, 0xac, 0xc9, 0x2a,
55 },
56 },
57 {
58 "OFB-AES256",
59 commonKey256,
60 commonIV,
61 commonInput,
62 []byte{
63 0xdc, 0x7e, 0x84, 0xbf, 0xda, 0x79, 0x16, 0x4b, 0x7e, 0xcd, 0x84, 0x86, 0x98, 0x5d, 0x38, 0x60,
64 0x4f, 0xeb, 0xdc, 0x67, 0x40, 0xd2, 0x0b, 0x3a, 0xc8, 0x8f, 0x6a, 0xd8, 0x2a, 0x4f, 0xb0, 0x8d,
65 0x71, 0xab, 0x47, 0xa0, 0x86, 0xe8, 0x6e, 0xed, 0xf3, 0x9d, 0x1c, 0x5b, 0xba, 0x97, 0xc4, 0x08,
66 0x01, 0x26, 0x14, 0x1d, 0x67, 0xf3, 0x7b, 0xe8, 0x53, 0x8f, 0x5a, 0x8b, 0xe7, 0x40, 0xe4, 0x84,
67 },
68 },
69 }
70
71 func TestOFB(t *testing.T) {
72 for _, tt := range ofbTests {
73 test := tt.name
74
75 c, err := aes.NewCipher(tt.key)
76 if err != nil {
77 t.Errorf("%s: NewCipher(%d bytes) = %s", test, len(tt.key), err)
78 continue
79 }
80
81 for j := 0; j <= 5; j += 5 {
82 plaintext := tt.in[0 : len(tt.in)-j]
83 ofb := cipher.NewOFB(c, tt.iv)
84 ciphertext := make([]byte, len(plaintext))
85 ofb.XORKeyStream(ciphertext, plaintext)
86 if !bytes.Equal(ciphertext, tt.out[:len(plaintext)]) {
87 t.Errorf("%s/%d: encrypting\ninput % x\nhave % x\nwant % x", test, len(plaintext), plaintext, ciphertext, tt.out)
88 }
89 }
90
91 for j := 0; j <= 5; j += 5 {
92 ciphertext := tt.out[0 : len(tt.in)-j]
93 ofb := cipher.NewOFB(c, tt.iv)
94 plaintext := make([]byte, len(ciphertext))
95 ofb.XORKeyStream(plaintext, ciphertext)
96 if !bytes.Equal(plaintext, tt.in[:len(ciphertext)]) {
97 t.Errorf("%s/%d: decrypting\nhave % x\nwant % x", test, len(ciphertext), plaintext, tt.in)
98 }
99 }
100
101 if t.Failed() {
102 break
103 }
104 }
105 }
106
107 func TestOFBStream(t *testing.T) {
108
109 for _, keylen := range []int{128, 192, 256} {
110
111 t.Run(fmt.Sprintf("AES-%d", keylen), func(t *testing.T) {
112 rng := newRandReader(t)
113
114 key := make([]byte, keylen/8)
115 rng.Read(key)
116
117 block, err := aes.NewCipher(key)
118 if err != nil {
119 panic(err)
120 }
121
122 cryptotest.TestStreamFromBlock(t, block, cipher.NewOFB)
123 })
124 }
125
126 t.Run("DES", func(t *testing.T) {
127 rng := newRandReader(t)
128
129 key := make([]byte, 8)
130 rng.Read(key)
131
132 block, err := des.NewCipher(key)
133 if err != nil {
134 panic(err)
135 }
136
137 cryptotest.TestStreamFromBlock(t, block, cipher.NewOFB)
138 })
139 }
140
View as plain text