]> Cypherpunks.ru repositories - gogost.git/blobdiff - src/cypherpunks.ru/gogost/gost28147/ctr_test.go
Forbid any later GNU GPL versions autousage
[gogost.git] / src / cypherpunks.ru / gogost / gost28147 / ctr_test.go
index 1881e5ec6f2528cfeccb2df89c7d87ae83adfeed..27806427bcba65888ee92780936936a5d9dd23d7 100644 (file)
@@ -1,10 +1,9 @@
 // GoGOST -- Pure Go GOST cryptographic functions library
-// Copyright (C) 2015-2018 Sergey Matveev <stargrave@stargrave.org>
+// Copyright (C) 2015-2019 Sergey Matveev <stargrave@stargrave.org>
 //
 // 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
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
+// the Free Software Foundation, version 3 of the License.
 //
 // This program is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -25,8 +24,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 +99,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 +142,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 +173,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)