X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=src%2Fcypherpunks.ru%2Fgogost%2Fgost28147%2Fctr_test.go;h=139bbb984e762e235e32a9c7d237de205841d0cc;hb=1f35967fa918c1653dfa787818ce4f3559283ff5;hp=1881e5ec6f2528cfeccb2df89c7d87ae83adfeed;hpb=d1cbfa307ebcb72b61f926dfdbc21175a3eec1a8;p=gogost.git diff --git a/src/cypherpunks.ru/gogost/gost28147/ctr_test.go b/src/cypherpunks.ru/gogost/gost28147/ctr_test.go index 1881e5e..139bbb9 100644 --- a/src/cypherpunks.ru/gogost/gost28147/ctr_test.go +++ b/src/cypherpunks.ru/gogost/gost28147/ctr_test.go @@ -1,5 +1,5 @@ // GoGOST -- Pure Go GOST cryptographic functions library -// Copyright (C) 2015-2018 Sergey Matveev +// Copyright (C) 2015-2019 Sergey Matveev // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -25,8 +25,8 @@ import ( ) func TestCTRGCL3Vector(t *testing.T) { - sbox := &Gost2814789_TestParamSet - key := [KeySize]byte{ + sbox := &SboxIdGost2814789TestParamSet + key := []byte{ 0x04, 0x75, 0xf6, 0xe0, 0x50, 0x38, 0xfb, 0xfa, 0xd2, 0xc7, 0xc3, 0x90, 0xed, 0xb3, 0xca, 0x3d, 0x15, 0x47, 0x12, 0x42, 0x91, 0xae, 0x1e, 0x8a, @@ -100,24 +100,24 @@ func TestCTRGCL3Vector(t *testing.T) { 0x13, 0xc3, 0xfe, 0x1f, 0x8c, 0x55, 0x63, 0x09, 0x1f, 0xcd, 0xd4, 0x28, 0xca, } - iv := [8]byte{0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01} + iv := []byte{0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01} c := NewCipher(key, sbox) ctr := c.NewCTR(iv) tmp := make([]byte, len(plaintext)) ctr.XORKeyStream(tmp, plaintext) if bytes.Compare(tmp, ciphertext) != 0 { - t.Fail() + t.Fatal("encryption failed") } ctr = c.NewCTR(iv) ctr.XORKeyStream(tmp, tmp) if bytes.Compare(tmp, plaintext) != 0 { - t.Fail() + t.Fatal("decryption failed") } } func TestCTRGCL2Vector(t *testing.T) { - sbox := &Gost2814789_TestParamSet - key := [KeySize]byte{ + sbox := &SboxIdGost2814789TestParamSet + key := []byte{ 0xfc, 0x7a, 0xd2, 0x88, 0x6f, 0x45, 0x5b, 0x50, 0xd2, 0x90, 0x08, 0xfa, 0x62, 0x2b, 0x57, 0xd5, 0xc6, 0x5b, 0x3c, 0x63, 0x72, 0x02, 0x02, 0x57, @@ -143,33 +143,29 @@ func TestCTRGCL2Vector(t *testing.T) { } var iv [8]byte c := NewCipher(key, sbox) - ctr := c.NewCTR(iv) + ctr := c.NewCTR(iv[:]) tmp := make([]byte, len(plaintext)) ctr.XORKeyStream(tmp, plaintext) if bytes.Compare(tmp, ciphertext) != 0 { - t.Fail() + t.Fatal("encryption failed") } - ctr = c.NewCTR(iv) + ctr = c.NewCTR(iv[:]) ctr.XORKeyStream(tmp, tmp) if bytes.Compare(tmp, plaintext) != 0 { - t.Fail() + t.Fatal("decryption failed") } } func TestCTRRandom(t *testing.T) { - var key [KeySize]byte - rand.Read(key[:]) - c := NewCipher(key, SboxDefault) - f := func(ivRaw []byte, pt []byte) bool { - if len(pt) == 0 || len(ivRaw) < 8 { + f := func(key [KeySize]byte, iv [BlockSize]byte, pt []byte) bool { + if len(pt) == 0 { return true } - var iv [8]byte - copy(iv[:], ivRaw[:8]) + c := NewCipher(key[:], SboxDefault) tmp := make([]byte, len(pt)) - ctr := c.NewCTR(iv) - ctr.XORKeyStream(tmp, pt) - ctr = c.NewCTR(iv) + ctr := c.NewCTR(iv[:]) + ctr.XORKeyStream(tmp, pt[:]) + ctr = c.NewCTR(iv[:]) ctr.XORKeyStream(tmp, tmp) return bytes.Compare(tmp, pt) == 0 } @@ -178,17 +174,17 @@ func TestCTRRandom(t *testing.T) { } } func TestCTRInterface(t *testing.T) { - var key [32]byte + var key [KeySize]byte var iv [8]byte - c := NewCipher(key, SboxDefault) - var _ cipher.Stream = c.NewCTR(iv) + c := NewCipher(key[:], SboxDefault) + var _ cipher.Stream = c.NewCTR(iv[:]) } func BenchmarkCTR(b *testing.B) { - var key [KeySize]byte - var iv [BlockSize]byte - rand.Read(key[:]) - rand.Read(iv[:]) + key := make([]byte, KeySize) + iv := make([]byte, BlockSize) + rand.Read(key) + rand.Read(iv) dst := make([]byte, BlockSize) src := make([]byte, BlockSize) rand.Read(src)