git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/crypto/chacha/hchacha20_test.go (about)

     1  package chacha
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/rand"
     6  	"encoding/hex"
     7  	"fmt"
     8  	"testing"
     9  )
    10  
    11  // Test that the out buffer is actually filled with some data to make sure that we do not pass the array
    12  // by value instead of a reference to mutate it
    13  func TestHChaCha20NotEmpty(t *testing.T) {
    14  	// var out [32]byte
    15  	var emptyOut [32]byte
    16  	var key [32]byte
    17  	var nonce [16]byte
    18  
    19  	rand.Read(key[:])
    20  	rand.Read(nonce[:])
    21  
    22  	out, _ := HChaCha20(key[:], nonce[:])
    23  
    24  	if bytes.Equal(out[:], emptyOut[:]) {
    25  		fmt.Println("out", hex.Dump(out[:]))
    26  		fmt.Println("emptyOut", hex.Dump(emptyOut[:]))
    27  		t.Fatalf("out is empty")
    28  	}
    29  }
    30  
    31  func TestHChaCha20(t *testing.T) {
    32  	defer func(sse2, ssse3, avx, avx2 bool) {
    33  		useSSE2, useSSSE3, useAVX, useAVX2 = sse2, ssse3, avx, avx2
    34  	}(useSSE2, useSSSE3, useAVX, useAVX2)
    35  
    36  	if useAVX2 {
    37  		t.Log("AVX2 version")
    38  		testHChaCha20(t)
    39  		useAVX2 = false
    40  	}
    41  	if useAVX {
    42  		t.Log("AVX version")
    43  		testHChaCha20(t)
    44  		useAVX = false
    45  	}
    46  	if useSSSE3 {
    47  		t.Log("SSSE3 version")
    48  		testHChaCha20(t)
    49  		useSSSE3 = false
    50  	}
    51  	if useSSE2 {
    52  		t.Log("SSE2 version")
    53  		testHChaCha20(t)
    54  		useSSE2 = false
    55  	}
    56  	t.Log("generic version")
    57  	testHChaCha20(t)
    58  }
    59  
    60  func testHChaCha20(t *testing.T) {
    61  	for i, v := range hChaCha20Vectors {
    62  		var key [32]byte
    63  		var nonce [16]byte
    64  		copy(key[:], v.key)
    65  		copy(nonce[:], v.nonce)
    66  
    67  		out, _ := HChaCha20(key[:], nonce[:])
    68  		if !bytes.Equal(out, v.keystream) {
    69  			t.Errorf("Test %d: keystream mismatch:\n \t got:  %s\n \t want: %s", i, toHex(key[:]), toHex(v.keystream))
    70  		}
    71  	}
    72  }
    73  
    74  var hChaCha20Vectors = []struct {
    75  	key, nonce, keystream []byte
    76  }{
    77  	{
    78  		fromHex("0000000000000000000000000000000000000000000000000000000000000000"),
    79  		fromHex("000000000000000000000000000000000000000000000000"),
    80  		fromHex("1140704c328d1d5d0e30086cdf209dbd6a43b8f41518a11cc387b669b2ee6586"),
    81  	},
    82  	{
    83  		fromHex("8000000000000000000000000000000000000000000000000000000000000000"),
    84  		fromHex("000000000000000000000000000000000000000000000000"),
    85  		fromHex("7d266a7fd808cae4c02a0a70dcbfbcc250dae65ce3eae7fc210f54cc8f77df86"),
    86  	},
    87  	{
    88  		fromHex("0000000000000000000000000000000000000000000000000000000000000001"),
    89  		fromHex("000000000000000000000000000000000000000000000002"),
    90  		fromHex("e0c77ff931bb9163a5460c02ac281c2b53d792b1c43fea817e9ad275ae546963"),
    91  	},
    92  	{
    93  		fromHex("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"),
    94  		fromHex("000102030405060708090a0b0c0d0e0f1011121314151617"),
    95  		fromHex("51e3ff45a895675c4b33b46c64f4a9ace110d34df6a2ceab486372bacbd3eff6"),
    96  	},
    97  	{
    98  		// from draft-irtf-cfrg-xchacha-03
    99  		fromHex("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"),
   100  		fromHex("000000090000004a0000000031415927"),
   101  		fromHex("82413b4227b27bfed30e42508a877d73a0f9e4d58a74a853c12ec41326d3ecdc"),
   102  	},
   103  }