| 1 |
// Copyright 2010 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 packet |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"crypto/aes" |
| 10 |
"crypto/rand" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
var commonKey128 = []byte{0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c} |
| 15 |
|
| 16 |
func testOCFB(t *testing.T, resync OCFBResyncOption) { |
| 17 |
block, err := aes.NewCipher(commonKey128) |
| 18 |
if err != nil { |
| 19 |
t.Error(err) |
| 20 |
return |
| 21 |
} |
| 22 |
|
| 23 |
plaintext := []byte("this is the plaintext, which is long enough to span several blocks.") |
| 24 |
randData := make([]byte, block.BlockSize()) |
| 25 |
rand.Reader.Read(randData) |
| 26 |
ocfb, prefix := NewOCFBEncrypter(block, randData, resync) |
| 27 |
ciphertext := make([]byte, len(plaintext)) |
| 28 |
ocfb.XORKeyStream(ciphertext, plaintext) |
| 29 |
|
| 30 |
ocfbdec := NewOCFBDecrypter(block, prefix, resync) |
| 31 |
if ocfbdec == nil { |
| 32 |
t.Errorf("NewOCFBDecrypter failed (resync: %t)", resync) |
| 33 |
return |
| 34 |
} |
| 35 |
plaintextCopy := make([]byte, len(plaintext)) |
| 36 |
ocfbdec.XORKeyStream(plaintextCopy, ciphertext) |
| 37 |
|
| 38 |
if !bytes.Equal(plaintextCopy, plaintext) { |
| 39 |
t.Errorf("got: %x, want: %x (resync: %t)", plaintextCopy, plaintext, resync) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func TestOCFB(t *testing.T) { |
| 44 |
testOCFB(t, OCFBNoResync) |
| 45 |
testOCFB(t, OCFBResync) |
| 46 |
} |