github.com/qv2ray/smaead@v0.0.0-20211021072225-a01f7e01d185/smaead_test.go (about)

     1  package smaead_test
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/aes"
     6  	"crypto/cipher"
     7  	"github.com/qv2ray/smaead"
     8  	"golang.org/x/crypto/chacha20poly1305"
     9  	"math/rand"
    10  	"testing"
    11  )
    12  
    13  var key = []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}
    14  var bsseq = []int{1, 2, 3, 4, 7, 8, 8, 16, 17, 17, 64, 128, 256, 4096, 0x3fff}
    15  
    16  func partialAeadRun(aead cipher.AEAD, paead smaead.PartialAEAD, bs, count int) bool {
    17  	for n := 0; n < count; n++ {
    18  		pt := make([]byte, bs)
    19  		for p := 0; p < bs; p++ {
    20  			pt[p] = byte(rand.Int() % 256)
    21  		}
    22  		nonce := make([]byte, 12)
    23  		for p := 0; p < 12; p++ {
    24  			nonce[p] = byte(rand.Int() % 256)
    25  		}
    26  
    27  		ct := aead.Seal(nil, nonce, pt, nil)
    28  		ct = ct[:len(pt)]
    29  		pt2 := paead.OpenWithoutCheck(nil, nonce, ct)
    30  		if bytes.Compare(pt, pt2) != 0 {
    31  			return false
    32  		}
    33  	}
    34  	return true
    35  }
    36  func partialAeadBench(aead cipher.AEAD, paead smaead.PartialAEAD, bs, count int) bool {
    37  	pt := make([]byte, bs)
    38  	for p := 0; p < bs; p++ {
    39  		pt[p] = byte(rand.Int() % 256)
    40  	}
    41  	nonce := make([]byte, 12)
    42  	for p := 0; p < 12; p++ {
    43  		nonce[p] = byte(rand.Int() % 256)
    44  	}
    45  
    46  	ct := aead.Seal(nil, nonce, pt, nil)
    47  	ct = ct[:len(pt)]
    48  	for n := 0; n < count; n++ {
    49  		pt2 := paead.OpenWithoutCheck(nil, nonce, ct)
    50  		if bytes.Compare(pt, pt2) != 0 {
    51  			return false
    52  		}
    53  	}
    54  	return true
    55  }
    56  
    57  func TestPartialGCM(t *testing.T) {
    58  	for _, i := range bsseq {
    59  		a1, e := aes.NewCipher(key)
    60  		if e != nil {
    61  			t.Fatal(e)
    62  		}
    63  		a2, e := aes.NewCipher(key)
    64  		if e != nil {
    65  			t.Fatal(e)
    66  		}
    67  		stdgcm, e := cipher.NewGCM(a1)
    68  		if e != nil {
    69  			t.Fatal(e)
    70  		}
    71  		mygcm, e := smaead.NewPartialGCM(a2)
    72  		if e != nil {
    73  			t.Fatal(e)
    74  		}
    75  
    76  		if !partialAeadRun(stdgcm, mygcm, i, 100) {
    77  			t.Fail()
    78  		}
    79  	}
    80  }
    81  
    82  func TestPartialChacha20Poly1305(t *testing.T) {
    83  	for _, i := range bsseq {
    84  		stdc20p1305, e := chacha20poly1305.New(key)
    85  		if e != nil {
    86  			t.Fatal(e)
    87  		}
    88  		myc20p1305, e := smaead.NewPartialChacha20Poly1305(key)
    89  		if e != nil {
    90  			t.Fatal(e)
    91  		}
    92  
    93  		if !partialAeadBench(stdc20p1305, myc20p1305, i, 100) {
    94  			t.Fail()
    95  		}
    96  	}
    97  }
    98  
    99  func BenchmarkPartialGCM(b *testing.B) {
   100  	a1, e := aes.NewCipher(key)
   101  	if e != nil {
   102  		b.Fatal(e)
   103  	}
   104  	a2, e := aes.NewCipher(key)
   105  	if e != nil {
   106  		b.Fatal(e)
   107  	}
   108  	stdgcm, e := cipher.NewGCM(a1)
   109  	if e != nil {
   110  		b.Fatal(e)
   111  	}
   112  	mygcm, e := smaead.NewPartialGCM(a2)
   113  	if e != nil {
   114  		b.Fatal(e)
   115  	}
   116  	b.Log(b.N)
   117  	if !partialAeadBench(stdgcm, mygcm, 16, b.N) {
   118  		b.Fail()
   119  	}
   120  }
   121  
   122  func BenchmarkPartialChacha20Poly1305(b *testing.B) {
   123  	stdc20p1305, e := chacha20poly1305.New(key)
   124  	if e != nil {
   125  		b.Fatal(e)
   126  	}
   127  	myc20p1305, e := smaead.NewPartialChacha20Poly1305(key)
   128  	if e != nil {
   129  		b.Fatal(e)
   130  	}
   131  
   132  	b.Log(b.N)
   133  	if !partialAeadBench(stdc20p1305, myc20p1305, 16, b.N) {
   134  		b.Fail()
   135  	}
   136  }