]> Cypherpunks.ru repositories - gogost.git/blobdiff - mgm/mode_test.go
Raise copyright years in advance
[gogost.git] / mgm / mode_test.go
index d29120a7e087132abda55c3a92224c8c5fedb3ea..9df177b303103cc005587e7a9bfc2569298acc14 100644 (file)
@@ -1,5 +1,5 @@
 // GoGOST -- Pure Go GOST cryptographic functions library
-// Copyright (C) 2015-2020 Sergey Matveev <stargrave@stargrave.org>
+// Copyright (C) 2015-2024 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
@@ -19,6 +19,7 @@ import (
        "bytes"
        "crypto/cipher"
        "crypto/rand"
+       "io"
        "testing"
        "testing/quick"
 
@@ -56,7 +57,7 @@ func TestVector(t *testing.T) {
        nonce := plaintext[:16]
        aead, _ := NewMGM(c, 16)
        sealed := aead.Seal(nil, nonce, plaintext, additionalData)
-       if bytes.Compare(sealed[:len(plaintext)], []byte{
+       if !bytes.Equal(sealed[:len(plaintext)], []byte{
                0xA9, 0x75, 0x7B, 0x81, 0x47, 0x95, 0x6E, 0x90,
                0x55, 0xB8, 0xA3, 0x3D, 0xE8, 0x9F, 0x42, 0xFC,
                0x80, 0x75, 0xD2, 0x21, 0x2B, 0xF9, 0xFD, 0x5B,
@@ -66,20 +67,20 @@ func TestVector(t *testing.T) {
                0xC6, 0x0C, 0x14, 0xD4, 0xD3, 0xF8, 0x83, 0xD0,
                0xAB, 0x94, 0x42, 0x06, 0x95, 0xC7, 0x6D, 0xEB,
                0x2C, 0x75, 0x52,
-       }) != 0 {
+       }) {
                t.FailNow()
        }
-       if bytes.Compare(sealed[len(plaintext):], []byte{
+       if !bytes.Equal(sealed[len(plaintext):], []byte{
                0xCF, 0x5D, 0x65, 0x6F, 0x40, 0xC3, 0x4F, 0x5C,
                0x46, 0xE8, 0xBB, 0x0E, 0x29, 0xFC, 0xDB, 0x4C,
-       }) != 0 {
+       }) {
                t.FailNow()
        }
        _, err := aead.Open(sealed[:0], nonce, sealed, additionalData)
        if err != nil {
                t.FailNow()
        }
-       if bytes.Compare(sealed[:len(plaintext)], plaintext) != 0 {
+       if !bytes.Equal(sealed[:len(plaintext)], plaintext) {
                t.FailNow()
        }
 }
@@ -101,7 +102,7 @@ func TestSymmetric(t *testing.T) {
                        }
                        for _, initial := range initials {
                                sealed := aead.Seal(initial, nonce, plaintext, additionalData)
-                               if bytes.Compare(sealed[:len(initial)], initial) != 0 {
+                               if !bytes.Equal(sealed[:len(initial)], initial) {
                                        return false
                                }
                                pt, err := aead.Open(
@@ -110,7 +111,7 @@ func TestSymmetric(t *testing.T) {
                                        sealed[len(initial):],
                                        additionalData,
                                )
-                               if err != nil || bytes.Compare(pt, plaintext) != 0 {
+                               if err != nil || !bytes.Equal(pt, plaintext) {
                                        return false
                                }
                        }
@@ -145,3 +146,55 @@ func TestSymmetric(t *testing.T) {
                nonce[:gost341264.BlockSize],
        )
 }
+
+func BenchmarkMGM64(b *testing.B) {
+       key := make([]byte, gost341264.KeySize)
+       if _, err := io.ReadFull(rand.Reader, key); err != nil {
+               panic(err)
+       }
+       nonce := make([]byte, gost341264.BlockSize)
+       if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
+               panic(err)
+       }
+       nonce[0] &= 0x7F
+       pt := make([]byte, 1280+3)
+       if _, err := io.ReadFull(rand.Reader, pt); err != nil {
+               panic(err)
+       }
+       c := gost341264.NewCipher(key)
+       aead, err := NewMGM(c, gost341264.BlockSize)
+       if err != nil {
+               panic(err)
+       }
+       ct := make([]byte, len(pt)+aead.Overhead())
+       b.ResetTimer()
+       for i := 0; i < b.N; i++ {
+               aead.Seal(ct[:0], nonce, pt, nil)
+       }
+}
+
+func BenchmarkMGM128(b *testing.B) {
+       key := make([]byte, gost3412128.KeySize)
+       if _, err := io.ReadFull(rand.Reader, key); err != nil {
+               panic(err)
+       }
+       nonce := make([]byte, gost3412128.BlockSize)
+       if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
+               panic(err)
+       }
+       nonce[0] &= 0x7F
+       pt := make([]byte, 1280+3)
+       if _, err := io.ReadFull(rand.Reader, pt); err != nil {
+               panic(err)
+       }
+       c := gost3412128.NewCipher(key)
+       aead, err := NewMGM(c, gost3412128.BlockSize)
+       if err != nil {
+               panic(err)
+       }
+       ct := make([]byte, len(pt)+aead.Overhead())
+       b.ResetTimer()
+       for i := 0; i < b.N; i++ {
+               aead.Seal(ct[:0], nonce, pt, nil)
+       }
+}