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

     1  package chacha20blake3_test
     2  
     3  import (
     4  	"bytes"
     5  	"crypto/rand"
     6  	"encoding/hex"
     7  	"errors"
     8  	"testing"
     9  
    10  	"git.sr.ht/~pingoo/stdx/crypto/chacha20blake3"
    11  )
    12  
    13  func toHex(bits []byte) string {
    14  	return hex.EncodeToString(bits)
    15  }
    16  
    17  func fromHex(bits string) []byte {
    18  	b, err := hex.DecodeString(bits)
    19  	if err != nil {
    20  		panic(err)
    21  	}
    22  	return b
    23  }
    24  
    25  func TestVectorsChaCha20Blake3(t *testing.T) {
    26  	for i, v := range chacha20Blake3Vectors {
    27  		dst := make([]byte, len(v.plaintext)+chacha20blake3.TagSize)
    28  
    29  		cipher, err := chacha20blake3.New(v.key)
    30  		if err != nil {
    31  			t.Errorf("plaintext: %s", toHex(v.plaintext))
    32  			t.Errorf("nonce: %s", toHex(v.nonce))
    33  			t.Fatal(err)
    34  		}
    35  
    36  		cipher.Seal(dst[:0], v.nonce, v.plaintext, v.additionalData)
    37  		if !bytes.Equal(dst, v.ciphertext) {
    38  			t.Errorf("Test %d: ciphertext mismatch:\ngot:  %s\nwant: %s", i, toHex(dst), toHex(v.ciphertext))
    39  		}
    40  
    41  		decryptedPlaintext, err := cipher.Open(nil, v.nonce, dst, v.additionalData)
    42  		if err != nil {
    43  			t.Errorf("Test %d: %v", i, err)
    44  		}
    45  		if !bytes.Equal(decryptedPlaintext, v.plaintext) {
    46  			t.Errorf("Test %d: plaintext mismatch:\ngot:  %s\nwant: %s", i, toHex(decryptedPlaintext), toHex(v.plaintext))
    47  		}
    48  	}
    49  }
    50  
    51  func TestBasicX(t *testing.T) {
    52  	var key [chacha20blake3.KeySize]byte
    53  	var nonce [chacha20blake3.NonceSizeX]byte
    54  
    55  	originalPlaintext := []byte("Hello World")
    56  	additionalData := []byte("!")
    57  
    58  	rand.Read(key[:])
    59  	rand.Read(nonce[:])
    60  
    61  	cipher, _ := chacha20blake3.NewX(key[:])
    62  	ciphertext := cipher.Seal(nil, nonce[:], originalPlaintext, additionalData)
    63  
    64  	decryptedPlaintext, err := cipher.Open(nil, nonce[:], ciphertext, additionalData)
    65  	if err != nil {
    66  		t.Errorf("decrypting message: %s", err)
    67  		return
    68  	}
    69  
    70  	if !bytes.Equal(decryptedPlaintext, originalPlaintext) {
    71  		t.Errorf("original message (%s) != decrypted message (%s)", string(originalPlaintext), string(decryptedPlaintext))
    72  		return
    73  	}
    74  }
    75  
    76  func TestAdditionalDataX(t *testing.T) {
    77  	var key [chacha20blake3.KeySize]byte
    78  	var nonce [chacha20blake3.NonceSizeX]byte
    79  
    80  	originalPlaintext := []byte("Hello World")
    81  	additionalData := []byte("!")
    82  
    83  	rand.Read(key[:])
    84  	rand.Read(nonce[:])
    85  
    86  	cipher, _ := chacha20blake3.NewX(key[:])
    87  	ciphertext := cipher.Seal(nil, nonce[:], originalPlaintext, additionalData)
    88  
    89  	_, err := cipher.Open(nil, nonce[:], ciphertext, []byte{})
    90  	if !errors.Is(err, chacha20blake3.ErrOpen) {
    91  		t.Errorf("expected error (%s) | got (%s)", chacha20blake3.ErrOpen, err)
    92  		return
    93  	}
    94  }
    95  
    96  var chacha20Blake3Vectors = []struct {
    97  	key            []byte
    98  	nonce          []byte
    99  	plaintext      []byte
   100  	additionalData []byte
   101  	ciphertext     []byte
   102  }{
   103  	{
   104  		fromHex("0000000000000000000000000000000000000000000000000000000000000000"),
   105  		fromHex("0000000000000000"),
   106  		fromHex("48656C6C6F20576F726C6421"), //  Hello World!
   107  		nil,
   108  		fromHex("d7628bd23a716f15ead6f35d7a6be6fd6ee37de0c569f11705e9c1ba5b576d0886d0f7f1b5fdb82ea07856d7"),
   109  	},
   110  	{
   111  		fromHex("0100000000000000000000000000000000000000000000000000000000000010"),
   112  		fromHex("0100000000000010"),
   113  		fromHex("4368614368613230"), // ChaCha20
   114  		fromHex("424C414B4533"),     // BLAKE3
   115  		fromHex("9f7a1c78e49065c7627ebc71dfe55740dc0ff6164667b3192b928a189da4b6153b579262acda29bb"),
   116  	},
   117  	{
   118  		fromHex("a9541ec64e971c19216360a28aebffdefdbc2f2b4f8d683a2c5c17c12e86059d"),
   119  		fromHex("5722cf5d7efbc3a1"),
   120  		fromHex("112103a99299c403eb92c29ee81f8faa2c4bab00ef4a92ddb3cf7d0c3ec63d19b81ff83defbfa34fb1ac5bf594306a541fb4ba3c18f700d6d38d2eed4f118760"),
   121  		fromHex("bd3c8a9c2c9362c392dd9b9ae7e31552"),
   122  		fromHex("f8742d7ec6862e53715e526f1b91c8c3d60b005d93ea924ca7377e81d8cad3f69d102d604c0688befb8c0fbdfc499bf10ab55021e87d66b2cdee57401a93c4ddcd2501adf933f45bb6309810c744205d0e9d69b9943afa2f01188e2b91be7f11"),
   123  	},
   124  }