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

     1  package chacha20_test
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"testing"
     7  
     8  	"git.sr.ht/~pingoo/stdx/crypto/chacha20"
     9  )
    10  
    11  func toHex(bits []byte) string {
    12  	return hex.EncodeToString(bits)
    13  }
    14  
    15  func fromHex(bits string) []byte {
    16  	b, err := hex.DecodeString(bits)
    17  	if err != nil {
    18  		panic(err)
    19  	}
    20  	return b
    21  }
    22  
    23  func TestChaCha20(t *testing.T) {
    24  	for i, v := range chacha20Vectors {
    25  		if len(v.plaintext) == 0 {
    26  			v.plaintext = make([]byte, len(v.ciphertext))
    27  		}
    28  
    29  		dst := make([]byte, len(v.ciphertext))
    30  
    31  		// XORKeyStream(dst, v.plaintext, v.nonce, v.key)
    32  		// if !bytes.Equal(dst, v.ciphertext) {
    33  		// 	t.Errorf("Test %d: ciphertext mismatch:\n \t got:  %s\n \t want: %s", i, toHex(dst), toHex(v.ciphertext))
    34  		// }
    35  
    36  		c, err := chacha20.New(v.key, v.nonce)
    37  		if err != nil {
    38  			t.Errorf("plaintext: %s", toHex(v.plaintext))
    39  			t.Errorf("nonce: %s", toHex(v.nonce))
    40  			t.Fatal(err)
    41  		}
    42  
    43  		c.XORKeyStream(dst[:1], v.plaintext[:1])
    44  		c.XORKeyStream(dst[1:], v.plaintext[1:])
    45  		if !bytes.Equal(dst, v.ciphertext) {
    46  			t.Errorf("Test %d: ciphertext mismatch:\n \t got:  %s\n \t want: %s", i, toHex(dst), toHex(v.ciphertext))
    47  		}
    48  	}
    49  }
    50  
    51  func TestXChaCha20(t *testing.T) {
    52  	for i, v := range xchacha20Vectors {
    53  		if len(v.plaintext) == 0 {
    54  			v.plaintext = make([]byte, len(v.ciphertext))
    55  		}
    56  
    57  		dst := make([]byte, len(v.ciphertext))
    58  
    59  		// XORKeyStream(dst, v.plaintext, v.nonce, v.key)
    60  		// if !bytes.Equal(dst, v.ciphertext) {
    61  		// 	t.Errorf("Test %d: ciphertext mismatch:\n \t got:  %s\n \t want: %s", i, toHex(dst), toHex(v.ciphertext))
    62  		// }
    63  
    64  		c, err := chacha20.NewX(v.key, v.nonce)
    65  		if err != nil {
    66  			t.Errorf("plaintext: %s", toHex(v.plaintext))
    67  			t.Errorf("nonce: %s", toHex(v.nonce))
    68  			t.Fatal(err)
    69  		}
    70  
    71  		c.XORKeyStream(dst[:1], v.plaintext[:1])
    72  		c.XORKeyStream(dst[1:], v.plaintext[1:])
    73  		if !bytes.Equal(dst, v.ciphertext) {
    74  			t.Errorf("Test %d: ciphertext mismatch:\n \t got:  %s\n \t want: %s", i, toHex(dst), toHex(v.ciphertext))
    75  		}
    76  	}
    77  }
    78  
    79  var chacha20Vectors = []struct {
    80  	key, nonce, plaintext, ciphertext []byte
    81  }{
    82  	{
    83  		fromHex("0000000000000000000000000000000000000000000000000000000000000000"),
    84  		fromHex("0000000000000000"),
    85  		nil,
    86  		fromHex("76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7724e03fb8d84a376a43b8f41518a11cc387b669b2ee6586"),
    87  	},
    88  	// we only support 64-bit nonces
    89  	// {
    90  	// 	fromHex("0000000000000000000000000000000000000000000000000000000000000000"),
    91  	// 	fromHex("000000000000000000000000"),
    92  	// 	nil,
    93  	// 	fromHex("76b8e0ada0f13d90405d6ae55386bd28bdd219b8a08ded1aa836efcc8b770dc7da41597c5157488d7724e03fb8d84a376a43b8f41518a11cc387b669b2ee6586"),
    94  	// },
    95  	// {
    96  	// 	fromHex("0000000000000000000000000000000000000000000000000000000000000000"),
    97  	// 	fromHex("000000000000000000000000000000000000000000000000"),
    98  	// 	nil,
    99  	// 	fromHex("bcd02a18bf3f01d19292de30a7a8fdaca4b65e50a6002cc72cd6d2f7c91ac3d5728f83e0aad2bfcf9abd2d2db58faedd65015dd83fc09b131e271043019e8e0f"),
   100  	// },
   101  }
   102  
   103  var xchacha20Vectors = []struct {
   104  	key, nonce, plaintext, ciphertext []byte
   105  }{
   106  	{
   107  		fromHex("0000000000000000000000000000000000000000000000000000000000000000"),
   108  		fromHex("000000000000000000000000000000000000000000000000"),
   109  		nil,
   110  		fromHex("bcd02a18bf3f01d19292de30a7a8fdaca4b65e50a6002cc72cd6d2f7c91ac3d5728f83e0aad2bfcf9abd2d2db58faedd65015dd83fc09b131e271043019e8e0f"),
   111  	},
   112  }