|
5 | 5 | package cryptstate |
6 | 6 |
|
7 | 7 | import ( |
8 | | - "bytes" |
9 | | - "crypto/aes" |
10 | | - "encoding/hex" |
11 | | - "testing" |
| 8 | + "bytes" |
| 9 | + "crypto/aes" |
| 10 | + "encoding/hex" |
| 11 | + "testing" |
12 | 12 | ) |
13 | 13 |
|
14 | 14 | func TestOCB2AES128Encrypt(t *testing.T) { |
15 | | - msg := [15]byte{ |
16 | | - 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
17 | | - } |
18 | | - key := [aes.BlockSize]byte{ |
19 | | - 0x96, 0x8b, 0x1b, 0x0c, 0x53, 0x1e, 0x1f, 0x80, 0xa6, 0x1d, 0xcb, 0x27, 0x94, 0x09, 0x6f, 0x32, |
20 | | - } |
21 | | - eiv := [aes.BlockSize]byte{ |
22 | | - 0x1e, 0x2a, 0x9b, 0xd0, 0x2d, 0xa6, 0x8e, 0x46, 0x26, 0x85, 0x83, 0xe9, 0x14, 0x2a, 0xff, 0x2a, |
23 | | - } |
24 | | - div := [aes.BlockSize]byte{ |
25 | | - 0x73, 0x99, 0x9d, 0xa2, 0x03, 0x70, 0x00, 0x96, 0xef, 0x55, 0x06, 0x7a, 0x8b, 0xbe, 0x00, 0x07, |
26 | | - } |
27 | | - expected := [19]byte{ |
28 | | - 0x1f, 0xfc, 0xdd, 0xb4, 0x68, 0x13, 0x68, 0xb7, 0x92, 0x67, 0xca, 0x2d, 0xba, 0xb7, 0x0d, 0x44, 0xdf, 0x32, 0xd4, |
29 | | - } |
30 | | - expected_eiv := [aes.BlockSize]byte{ |
31 | | - 0x1f, 0x2a, 0x9b, 0xd0, 0x2d, 0xa6, 0x8e, 0x46, 0x26, 0x85, 0x83, 0xe9, 0x14, 0x2a, 0xff, 0x2a, |
32 | | - } |
33 | | - |
34 | | - cs := CryptState{} |
35 | | - out := make([]byte, 19) |
36 | | - cs.SetKey("OCB2-AES128", key[:], eiv[:], div[:]) |
37 | | - cs.Encrypt(out, msg[:]) |
38 | | - |
39 | | - if !bytes.Equal(out[:], expected[:]) { |
40 | | - t.Errorf("Mismatch in output") |
41 | | - } |
42 | | - |
43 | | - if !bytes.Equal(cs.EncryptIV[:], expected_eiv[:]) { |
44 | | - t.Errorf("EIV mismatch") |
45 | | - } |
| 15 | + msg := [15]byte{ |
| 16 | + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 17 | + } |
| 18 | + key := [aes.BlockSize]byte{ |
| 19 | + 0x96, 0x8b, 0x1b, 0x0c, 0x53, 0x1e, 0x1f, 0x80, 0xa6, 0x1d, 0xcb, 0x27, 0x94, 0x09, 0x6f, 0x32, |
| 20 | + } |
| 21 | + eiv := [aes.BlockSize]byte{ |
| 22 | + 0x1e, 0x2a, 0x9b, 0xd0, 0x2d, 0xa6, 0x8e, 0x46, 0x26, 0x85, 0x83, 0xe9, 0x14, 0x2a, 0xff, 0x2a, |
| 23 | + } |
| 24 | + div := [aes.BlockSize]byte{ |
| 25 | + 0x73, 0x99, 0x9d, 0xa2, 0x03, 0x70, 0x00, 0x96, 0xef, 0x55, 0x06, 0x7a, 0x8b, 0xbe, 0x00, 0x07, |
| 26 | + } |
| 27 | + expected := [19]byte{ |
| 28 | + 0x1f, 0xfc, 0xdd, 0xb4, 0x68, 0x13, 0x68, 0xb7, 0x92, 0x67, 0xca, 0x2d, 0xba, 0xb7, 0x0d, 0x44, 0xdf, 0x32, 0xd4, |
| 29 | + } |
| 30 | + expected_eiv := [aes.BlockSize]byte{ |
| 31 | + 0x1f, 0x2a, 0x9b, 0xd0, 0x2d, 0xa6, 0x8e, 0x46, 0x26, 0x85, 0x83, 0xe9, 0x14, 0x2a, 0xff, 0x2a, |
| 32 | + } |
| 33 | + |
| 34 | + cs := CryptState{} |
| 35 | + out := make([]byte, 19) |
| 36 | + cs.SetKey("OCB2-AES128", key[:], eiv[:], div[:]) |
| 37 | + cs.Encrypt(out, msg[:]) |
| 38 | + |
| 39 | + if !bytes.Equal(out[:], expected[:]) { |
| 40 | + t.Errorf("Mismatch in output") |
| 41 | + } |
| 42 | + |
| 43 | + if !bytes.Equal(cs.EncryptIV[:], expected_eiv[:]) { |
| 44 | + t.Errorf("EIV mismatch") |
| 45 | + } |
46 | 46 | } |
47 | 47 |
|
48 | 48 | func TestOCB2AES128Decrypt(t *testing.T) { |
49 | | - key := [aes.BlockSize]byte{ |
50 | | - 0x96, 0x8b, 0x1b, 0x0c, 0x53, 0x1e, 0x1f, 0x80, 0xa6, 0x1d, 0xcb, 0x27, 0x94, 0x09, 0x6f, 0x32, |
51 | | - } |
52 | | - eiv := [aes.BlockSize]byte{ |
53 | | - 0x1e, 0x2a, 0x9b, 0xd0, 0x2d, 0xa6, 0x8e, 0x46, 0x26, 0x85, 0x83, 0xe9, 0x14, 0x2a, 0xff, 0x2a, |
54 | | - } |
55 | | - div := [aes.BlockSize]byte{ |
56 | | - 0x73, 0x99, 0x9d, 0xa2, 0x03, 0x70, 0x00, 0x96, 0xef, 0x55, 0x06, 0x7a, 0x8b, 0xbe, 0x00, 0x07, |
57 | | - } |
58 | | - crypted := [19]byte{ |
59 | | - 0x1f, 0xfc, 0xdd, 0xb4, 0x68, 0x13, 0x68, 0xb7, 0x92, 0x67, 0xca, 0x2d, 0xba, 0xb7, 0x0d, 0x44, 0xdf, 0x32, 0xd4, |
60 | | - } |
61 | | - expected := [15]byte{ |
62 | | - 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
63 | | - } |
64 | | - post_div := [aes.BlockSize]byte{ |
65 | | - 0x1f, 0x2a, 0x9b, 0xd0, 0x2d, 0xa6, 0x8e, 0x46, 0x26, 0x85, 0x83, 0xe9, 0x14, 0x2a, 0xff, 0x2a, |
66 | | - } |
67 | | - |
68 | | - cs := CryptState{} |
69 | | - out := make([]byte, 15) |
70 | | - cs.SetKey("OCB2-AES128", key[:], div[:], eiv[:]) |
71 | | - err := cs.Decrypt(out, crypted[:]) |
72 | | - if err != nil { |
73 | | - t.Fatalf("%v", err) |
74 | | - } |
75 | | - |
76 | | - if !bytes.Equal(out, expected[:]) { |
77 | | - t.Errorf("Mismatch in output") |
78 | | - } |
79 | | - |
80 | | - if !bytes.Equal(cs.DecryptIV, post_div[:]) { |
81 | | - t.Errorf("Mismatch in DIV") |
82 | | - } |
| 49 | + key := [aes.BlockSize]byte{ |
| 50 | + 0x96, 0x8b, 0x1b, 0x0c, 0x53, 0x1e, 0x1f, 0x80, 0xa6, 0x1d, 0xcb, 0x27, 0x94, 0x09, 0x6f, 0x32, |
| 51 | + } |
| 52 | + eiv := [aes.BlockSize]byte{ |
| 53 | + 0x1e, 0x2a, 0x9b, 0xd0, 0x2d, 0xa6, 0x8e, 0x46, 0x26, 0x85, 0x83, 0xe9, 0x14, 0x2a, 0xff, 0x2a, |
| 54 | + } |
| 55 | + div := [aes.BlockSize]byte{ |
| 56 | + 0x73, 0x99, 0x9d, 0xa2, 0x03, 0x70, 0x00, 0x96, 0xef, 0x55, 0x06, 0x7a, 0x8b, 0xbe, 0x00, 0x07, |
| 57 | + } |
| 58 | + crypted := [19]byte{ |
| 59 | + 0x1f, 0xfc, 0xdd, 0xb4, 0x68, 0x13, 0x68, 0xb7, 0x92, 0x67, 0xca, 0x2d, 0xba, 0xb7, 0x0d, 0x44, 0xdf, 0x32, 0xd4, |
| 60 | + } |
| 61 | + expected := [15]byte{ |
| 62 | + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 63 | + } |
| 64 | + post_div := [aes.BlockSize]byte{ |
| 65 | + 0x1f, 0x2a, 0x9b, 0xd0, 0x2d, 0xa6, 0x8e, 0x46, 0x26, 0x85, 0x83, 0xe9, 0x14, 0x2a, 0xff, 0x2a, |
| 66 | + } |
| 67 | + |
| 68 | + cs := CryptState{} |
| 69 | + out := make([]byte, 15) |
| 70 | + cs.SetKey("OCB2-AES128", key[:], div[:], eiv[:]) |
| 71 | + err := cs.Decrypt(out, crypted[:]) |
| 72 | + if err != nil { |
| 73 | + t.Fatalf("%v", err) |
| 74 | + } |
| 75 | + |
| 76 | + if !bytes.Equal(out, expected[:]) { |
| 77 | + t.Errorf("Mismatch in output") |
| 78 | + } |
| 79 | + |
| 80 | + if !bytes.Equal(cs.DecryptIV, post_div[:]) { |
| 81 | + t.Errorf("Mismatch in DIV") |
| 82 | + } |
83 | 83 | } |
84 | 84 |
|
85 | 85 | // Test that our wrapped NaCl secretbox cipher |
86 | 86 | // works. The test data for this test was lifted |
87 | 87 | // from the secretbox_test.go file. |
88 | 88 | func TestXSalsa20Poly1305Encrypt(t *testing.T) { |
89 | | - cs := CryptState{} |
90 | | - |
91 | | - var key [32]byte |
92 | | - var eiv [24]byte |
93 | | - var div [24]byte |
94 | | - var message [64]byte |
95 | | - |
96 | | - for i := range key[:] { |
97 | | - key[i] = 1 |
98 | | - } |
99 | | - |
100 | | - // Since we pre-increment our EIV, |
101 | | - // this look a bit off compared to |
102 | | - // the secretbox_test.go test case. |
103 | | - for i := range eiv[:] { |
104 | | - eiv[i] = 2 |
105 | | - div[i] = 2 |
106 | | - } |
107 | | - eiv[0] = 1 |
108 | | - div[0] = 1 |
109 | | - |
110 | | - for i := range message[:] { |
111 | | - message[i] = 3 |
112 | | - } |
113 | | - |
114 | | - cs.SetKey("XSalsa20-Poly1305", key[:], div[:], eiv[:]) |
115 | | - dst := make([]byte, len(message)+cs.Overhead()) |
116 | | - cs.Encrypt(dst, message[:]) |
117 | | - |
118 | | - expected, _ := hex.DecodeString("8442bc313f4626f1359e3b50122b6ce6fe66ddfe7d39d14e637eb4fd5b45beadab55198df6ab5368439792a23c87db70acb6156dc5ef957ac04f6276cf6093b84be77ff0849cc33e34b7254d5a8f65ad") |
119 | | - if !bytes.Equal(dst[1:], expected) { |
120 | | - t.Fatalf("mismatch! got\n%x\n, expected\n%x", dst, expected) |
121 | | - } |
| 89 | + cs := CryptState{} |
| 90 | + |
| 91 | + var key [32]byte |
| 92 | + var eiv [24]byte |
| 93 | + var div [24]byte |
| 94 | + var message [64]byte |
| 95 | + |
| 96 | + for i := range key[:] { |
| 97 | + key[i] = 1 |
| 98 | + } |
| 99 | + |
| 100 | + // Since we pre-increment our EIV, |
| 101 | + // this look a bit off compared to |
| 102 | + // the secretbox_test.go test case. |
| 103 | + for i := range eiv[:] { |
| 104 | + eiv[i] = 2 |
| 105 | + div[i] = 2 |
| 106 | + } |
| 107 | + eiv[0] = 1 |
| 108 | + div[0] = 1 |
| 109 | + |
| 110 | + for i := range message[:] { |
| 111 | + message[i] = 3 |
| 112 | + } |
| 113 | + |
| 114 | + cs.SetKey("XSalsa20-Poly1305", key[:], div[:], eiv[:]) |
| 115 | + dst := make([]byte, len(message)+cs.Overhead()) |
| 116 | + cs.Encrypt(dst, message[:]) |
| 117 | + |
| 118 | + expected, _ := hex.DecodeString("8442bc313f4626f1359e3b50122b6ce6fe66ddfe7d39d14e637eb4fd5b45beadab55198df6ab5368439792a23c87db70acb6156dc5ef957ac04f6276cf6093b84be77ff0849cc33e34b7254d5a8f65ad") |
| 119 | + if !bytes.Equal(dst[1:], expected) { |
| 120 | + t.Fatalf("mismatch! got\n%x\n, expected\n%x", dst, expected) |
| 121 | + } |
122 | 122 | } |
123 | 123 |
|
124 | 124 | // Test that we can reverse the result of the Encrypt test. |
125 | 125 | func TestXSalsa20Poly1305Decrypt(t *testing.T) { |
126 | | - cs := CryptState{} |
127 | | - |
128 | | - var key [32]byte |
129 | | - var eiv [24]byte |
130 | | - var div [24]byte |
131 | | - var expected [64]byte |
132 | | - |
133 | | - for i := range key[:] { |
134 | | - key[i] = 1 |
135 | | - } |
136 | | - |
137 | | - // Since we pre-increment our EIV, |
138 | | - // this look a bit off compared to |
139 | | - // the secretbox_test.go test case. |
140 | | - for i := range eiv[:] { |
141 | | - eiv[i] = 2 |
142 | | - div[i] = 2 |
143 | | - } |
144 | | - eiv[0] = 1 |
145 | | - div[0] = 1 |
146 | | - |
147 | | - for i := range expected[:] { |
148 | | - expected[i] = 3 |
149 | | - } |
150 | | - |
151 | | - message, _ := hex.DecodeString("028442bc313f4626f1359e3b50122b6ce6fe66ddfe7d39d14e637eb4fd5b45beadab55198df6ab5368439792a23c87db70acb6156dc5ef957ac04f6276cf6093b84be77ff0849cc33e34b7254d5a8f65ad") |
152 | | - cs.SetKey("XSalsa20-Poly1305", key[:], eiv[:], div[:]) |
153 | | - dst := make([]byte, len(message)-cs.Overhead()) |
154 | | - err := cs.Decrypt(dst, message[:]) |
155 | | - if err != nil { |
156 | | - t.Fatalf("%v", err) |
157 | | - } |
158 | | - |
159 | | - if !bytes.Equal(dst, expected[:]) { |
160 | | - t.Fatalf("mismatch! got\n%x\n, expected\n%x", dst, expected) |
161 | | - } |
| 126 | + cs := CryptState{} |
| 127 | + |
| 128 | + var key [32]byte |
| 129 | + var eiv [24]byte |
| 130 | + var div [24]byte |
| 131 | + var expected [64]byte |
| 132 | + |
| 133 | + for i := range key[:] { |
| 134 | + key[i] = 1 |
| 135 | + } |
| 136 | + |
| 137 | + // Since we pre-increment our EIV, |
| 138 | + // this look a bit off compared to |
| 139 | + // the secretbox_test.go test case. |
| 140 | + for i := range eiv[:] { |
| 141 | + eiv[i] = 2 |
| 142 | + div[i] = 2 |
| 143 | + } |
| 144 | + eiv[0] = 1 |
| 145 | + div[0] = 1 |
| 146 | + |
| 147 | + for i := range expected[:] { |
| 148 | + expected[i] = 3 |
| 149 | + } |
| 150 | + |
| 151 | + message, _ := hex.DecodeString("028442bc313f4626f1359e3b50122b6ce6fe66ddfe7d39d14e637eb4fd5b45beadab55198df6ab5368439792a23c87db70acb6156dc5ef957ac04f6276cf6093b84be77ff0849cc33e34b7254d5a8f65ad") |
| 152 | + cs.SetKey("XSalsa20-Poly1305", key[:], eiv[:], div[:]) |
| 153 | + dst := make([]byte, len(message)-cs.Overhead()) |
| 154 | + err := cs.Decrypt(dst, message[:]) |
| 155 | + if err != nil { |
| 156 | + t.Fatalf("%v", err) |
| 157 | + } |
| 158 | + |
| 159 | + if !bytes.Equal(dst, expected[:]) { |
| 160 | + t.Fatalf("mismatch! got\n%x\n, expected\n%x", dst, expected) |
| 161 | + } |
162 | 162 | } |
163 | 163 |
|
164 | 164 | func TestNullEncrypt(t *testing.T) { |
165 | | - cs := CryptState{} |
166 | | - cs.SetKey("NULL", []byte{}, []byte{1}, []byte{1}) |
167 | | - msg := []byte("HelloWorld") |
168 | | - dst := make([]byte, len(msg)+cs.Overhead()) |
169 | | - cs.Encrypt(dst, msg) |
170 | | - if !bytes.Equal(dst[1:], msg) { |
171 | | - t.Fatalf("mismatch! got\n%x\n, expected\n%x", dst, msg) |
172 | | - } |
| 165 | + cs := CryptState{} |
| 166 | + cs.SetKey("NULL", []byte{}, []byte{1}, []byte{1}) |
| 167 | + msg := []byte("HelloWorld") |
| 168 | + dst := make([]byte, len(msg)+cs.Overhead()) |
| 169 | + cs.Encrypt(dst, msg) |
| 170 | + if !bytes.Equal(dst[1:], msg) { |
| 171 | + t.Fatalf("mismatch! got\n%x\n, expected\n%x", dst, msg) |
| 172 | + } |
173 | 173 | } |
174 | 174 |
|
175 | 175 | func TestNullDecrypt(t *testing.T) { |
176 | | - cs := CryptState{} |
177 | | - cs.SetKey("NULL", []byte{}, []byte{1}, []byte{1}) |
178 | | - msg := []byte{2} |
179 | | - msg = append(msg, []byte("HelloWorld")...) |
180 | | - dst := make([]byte, len(msg)-cs.Overhead()) |
181 | | - err := cs.Decrypt(dst, msg) |
182 | | - if err != nil { |
183 | | - t.Fatalf("%v", err) |
184 | | - } |
185 | | - if !bytes.Equal(dst, msg[1:]) { |
186 | | - t.Fatalf("mismatch! got\n%x\n, expected\n%x", dst, msg) |
187 | | - } |
| 176 | + cs := CryptState{} |
| 177 | + cs.SetKey("NULL", []byte{}, []byte{1}, []byte{1}) |
| 178 | + msg := []byte{2} |
| 179 | + msg = append(msg, []byte("HelloWorld")...) |
| 180 | + dst := make([]byte, len(msg)-cs.Overhead()) |
| 181 | + err := cs.Decrypt(dst, msg) |
| 182 | + if err != nil { |
| 183 | + t.Fatalf("%v", err) |
| 184 | + } |
| 185 | + if !bytes.Equal(dst, msg[1:]) { |
| 186 | + t.Fatalf("mismatch! got\n%x\n, expected\n%x", dst, msg) |
| 187 | + } |
188 | 188 | } |
0 commit comments