1
0
Fork 0
forked from External/grumble

pkg/cryptstate, pkg/cryptstate/ocb2: split OCB2 implementation out into its own package.

This commit is contained in:
Mikkel Krautz 2012-12-01 11:42:02 +01:00
parent 51eee0abd0
commit 0f058d3b76
4 changed files with 196 additions and 201 deletions

View file

@ -1,76 +1,11 @@
package cryptstate
import (
"bytes"
"crypto/aes"
"testing"
)
func BlockCompare(a []byte, b []byte) (match bool) {
if len(a) != len(b) {
return
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return
}
}
match = true
return
}
func TestTimes2(t *testing.T) {
msg := [aes.BlockSize]byte{
0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
}
expected := [aes.BlockSize]byte{
0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7b,
}
times2(msg[0:])
if BlockCompare(msg[0:], expected[0:]) == false {
t.Errorf("times2 produces invalid output: %v, expected: %v", msg, expected)
}
}
func TestTimes3(t *testing.T) {
msg := [aes.BlockSize]byte{
0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
}
expected := [aes.BlockSize]byte{
0x81, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x85,
}
times3(msg[0:])
if BlockCompare(msg[0:], expected[0:]) == false {
t.Errorf("times3 produces invalid output: %v, expected: %v", msg, expected)
}
}
func TestZeros(t *testing.T) {
var msg [aes.BlockSize]byte
zeros(msg[0:])
for i := 0; i < len(msg); i++ {
if msg[i] != 0 {
t.Errorf("zeros does not zero slice.")
}
}
}
func TestXor(t *testing.T) {
msg := [aes.BlockSize]byte{
0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
}
var out [aes.BlockSize]byte
xor(out[0:], msg[0:], msg[0:])
for i := 0; i < len(out); i++ {
if out[i] != 0 {
t.Errorf("XOR broken")
}
}
}
func TestEncrypt(t *testing.T) {
msg := [15]byte{
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
@ -100,11 +35,11 @@ func TestEncrypt(t *testing.T) {
cs.SetKey(key[0:], eiv[0:], div[0:])
cs.Encrypt(out[0:], msg[0:])
if BlockCompare(out[0:], expected[0:]) == false {
if !bytes.Equal(out[0:], expected[0:]) {
t.Errorf("Mismatch in output")
}
if BlockCompare(cs.EncryptIV[0:], expected_eiv[0:]) == false {
if !bytes.Equal(cs.EncryptIV[0:], expected_eiv[0:]) {
t.Errorf("EIV mismatch")
}
}
@ -138,11 +73,11 @@ func TestDecrypt(t *testing.T) {
cs.SetKey(key[0:], div[0:], eiv[0:])
cs.Decrypt(out[0:], crypted[0:])
if BlockCompare(out[0:], expected[0:]) == false {
if !bytes.Equal(out[0:], expected[0:]) {
t.Errorf("Mismatch in output")
}
if BlockCompare(cs.DecryptIV[0:], post_div[0:]) == false {
if !bytes.Equal(cs.DecryptIV[0:], post_div[0:]) {
t.Errorf("Mismatch in DIV")
}
}