@@ -127,8 +127,20 @@ func TestEncryptDecryptRFC7253TestVectors(t *testing.T) {
127
127
adata , _ := hex .DecodeString (test .header )
128
128
targetPt , _ := hex .DecodeString (test .plaintext )
129
129
targetCt , _ := hex .DecodeString (test .ciphertext )
130
- ct := ocbInstance .Seal (nil , nonce , targetPt , adata )
131
130
// Encrypt
131
+ ct := ocbInstance .Seal (nil , nonce , targetPt , adata )
132
+ if ! bytes .Equal (ct , targetCt ) {
133
+ t .Errorf (
134
+ `RFC7253 Test vectors Encrypt error (ciphertexts don't match):
135
+ Got:
136
+ %X
137
+ Want:
138
+ %X` , ct , targetCt )
139
+ }
140
+ // Encrypt reusing buffer
141
+ pt := make ([]byte , len (targetPt ) + ocbInstance .Overhead ())
142
+ copy (pt , targetPt )
143
+ ct = ocbInstance .Seal (pt [:0 ], nonce , pt [:len (targetPt )], adata )
132
144
if ! bytes .Equal (ct , targetCt ) {
133
145
t .Errorf (
134
146
`RFC7253 Test vectors Encrypt error (ciphertexts don't match):
@@ -138,14 +150,14 @@ func TestEncryptDecryptRFC7253TestVectors(t *testing.T) {
138
150
%X` , ct , targetCt )
139
151
}
140
152
// Decrypt
141
- pt , err := ocbInstance .Open (nil , nonce , targetCt , adata )
153
+ pt , err := ocbInstance .Open (nil , nonce , ct , adata )
142
154
if err != nil {
143
155
t .Errorf (
144
156
`RFC7253 Valid ciphertext was refused decryption:
145
157
plaintext %X
146
158
nonce %X
147
159
header %X
148
- ciphertext %X` , targetPt , nonce , adata , targetCt )
160
+ ciphertext %X` , targetPt , nonce , adata , ct )
149
161
}
150
162
if ! bytes .Equal (pt , targetPt ) {
151
163
t .Errorf (
@@ -155,6 +167,24 @@ func TestEncryptDecryptRFC7253TestVectors(t *testing.T) {
155
167
Want:
156
168
%X` , pt , targetPt )
157
169
}
170
+ // Decrypt reusing buffer
171
+ pt , err = ocbInstance .Open (ct [:0 ], nonce , ct , adata )
172
+ if err != nil {
173
+ t .Errorf (
174
+ `RFC7253 Valid ciphertext was refused decryption:
175
+ plaintext %X
176
+ nonce %X
177
+ header %X
178
+ ciphertext %X` , targetPt , nonce , adata , ct )
179
+ }
180
+ if ! bytes .Equal (pt , targetPt ) {
181
+ t .Errorf (
182
+ `RFC7253 test vectors Decrypt error (plaintexts don't match):
183
+ Got:
184
+ %X
185
+ Want:
186
+ %X` , targetPt , pt )
187
+ }
158
188
}
159
189
}
160
190
@@ -182,7 +212,30 @@ func TestEncryptDecryptRFC7253TagLen96(t *testing.T) {
182
212
Want:
183
213
%X` , ct , targetCt )
184
214
}
185
- pt , err := ocbInstance .Open (nil , nonce , targetCt , adata )
215
+ pt := make ([]byte , len (targetPt ) + ocbInstance .Overhead ())
216
+ copy (pt , targetPt )
217
+ ct = ocbInstance .Seal (pt [:0 ], nonce , pt [:len (targetPt )], adata )
218
+ if ! bytes .Equal (ct , targetCt ) {
219
+ t .Errorf (
220
+ `RFC7253 test tagLen96 error (ciphertexts don't match):
221
+ Got:
222
+ %X
223
+ Want:
224
+ %X` , ct , targetCt )
225
+ }
226
+ pt , err = ocbInstance .Open (nil , nonce , ct , adata )
227
+ if err != nil {
228
+ t .Errorf (`RFC7253 test tagLen96 was refused decryption` )
229
+ }
230
+ if ! bytes .Equal (pt , targetPt ) {
231
+ t .Errorf (
232
+ `RFC7253 test tagLen96 error (plaintexts don't match):
233
+ Got:
234
+ %X
235
+ Want:
236
+ %X` , pt , targetPt )
237
+ }
238
+ pt , err = ocbInstance .Open (ct [:0 ], nonce , ct , adata )
186
239
if err != nil {
187
240
t .Errorf (`RFC7253 test tagLen96 was refused decryption` )
188
241
}
@@ -274,15 +327,47 @@ func TestEncryptDecryptGoTestVectors(t *testing.T) {
274
327
%X` , ct , targetCt )
275
328
}
276
329
330
+ // Encrypt reusing buffer
331
+ pt := make ([]byte , len (targetPt ) + ocbInstance .Overhead ())
332
+ copy (pt , targetPt )
333
+ ct = ocbInstance .Seal (pt [:0 ], nonce , pt [:len (targetPt )], adata )
334
+ if ! bytes .Equal (ct , targetCt ) {
335
+ t .Errorf (
336
+ `Go Test vectors Encrypt error (ciphertexts don't match):
337
+ Got:
338
+ %X
339
+ Want:
340
+ %X` , ct , targetCt )
341
+ }
342
+
277
343
// Decrypt
278
- pt , err : = ocbInstance .Open (nil , nonce , targetCt , adata )
344
+ pt , err = ocbInstance .Open (nil , nonce , ct , adata )
279
345
if err != nil {
280
346
t .Errorf (
281
347
`Valid Go ciphertext was refused decryption:
282
348
plaintext %X
283
349
nonce %X
284
350
header %X
285
- ciphertext %X` , targetPt , nonce , adata , targetCt )
351
+ ciphertext %X` , targetPt , nonce , adata , ct )
352
+ }
353
+ if ! bytes .Equal (pt , targetPt ) {
354
+ t .Errorf (
355
+ `Go Test vectors Decrypt error (plaintexts don't match):
356
+ Got:
357
+ %X
358
+ Want:
359
+ %X` , pt , targetPt )
360
+ }
361
+
362
+ // Decrypt reusing buffer
363
+ pt , err = ocbInstance .Open (ct [:0 ], nonce , ct , adata )
364
+ if err != nil {
365
+ t .Errorf (
366
+ `Valid Go ciphertext was refused decryption:
367
+ plaintext %X
368
+ nonce %X
369
+ header %X
370
+ ciphertext %X` , targetPt , nonce , adata , ct )
286
371
}
287
372
if ! bytes .Equal (pt , targetPt ) {
288
373
t .Errorf (
@@ -333,6 +418,17 @@ func TestEncryptDecryptVectorsWithPreviousDataRandomizeSlow(t *testing.T) {
333
418
`Random Encrypt/Decrypt error (plaintexts don't match)` )
334
419
break
335
420
}
421
+ decrypted , err = ocb .Open (ct [:0 ], nonce , ct , header )
422
+ if err != nil {
423
+ t .Errorf (
424
+ `Decrypt refused valid tag (not displaying long output)` )
425
+ break
426
+ }
427
+ if ! bytes .Equal (pt , decrypted ) {
428
+ t .Errorf (
429
+ `Random Encrypt/Decrypt error (plaintexts don't match)` )
430
+ break
431
+ }
336
432
}
337
433
}
338
434
@@ -369,6 +465,12 @@ func TestRejectTamperedCiphertextRandomizeSlow(t *testing.T) {
369
465
"Tampered ciphertext was not refused decryption (OCB did not return an error)" )
370
466
return
371
467
}
468
+ _ , err = ocb .Open (tampered [:0 ], nonce , tampered , header )
469
+ if err == nil {
470
+ t .Errorf (
471
+ "Tampered ciphertext was not refused decryption (OCB did not return an error)" )
472
+ return
473
+ }
372
474
}
373
475
374
476
func TestParameters (t * testing.T ) {
0 commit comments