Source file src/crypto/chacha20poly1305_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package crypto_test
     6  
     7  import (
     8  	"bytes"
     9  	"crypto/cipher"
    10  	"crypto/internal/cryptotest"
    11  	"crypto/internal/cryptotest/wycheproof"
    12  	"testing"
    13  
    14  	"golang.org/x/crypto/chacha20poly1305"
    15  )
    16  
    17  func TestChaCha20Poly1305(t *testing.T) {
    18  	key := make([]byte, chacha20poly1305.KeySize)
    19  	for i := range key {
    20  		key[i] = byte(i)
    21  	}
    22  
    23  	t.Run("ChaCha20Poly1305", func(t *testing.T) {
    24  		cryptotest.TestAEAD(t, func() (cipher.AEAD, error) {
    25  			return chacha20poly1305.New(key)
    26  		})
    27  	})
    28  
    29  	t.Run("XChaCha20Poly1305", func(t *testing.T) {
    30  		cryptotest.TestAEAD(t, func() (cipher.AEAD, error) {
    31  			return chacha20poly1305.NewX(key)
    32  		})
    33  	})
    34  }
    35  
    36  func TestChaCha20Poly1305Wycheproof(t *testing.T) {
    37  	for _, tc := range []struct {
    38  		file      string
    39  		newCipher func([]byte) (cipher.AEAD, error)
    40  	}{
    41  		{"chacha20_poly1305_test.json", chacha20poly1305.New},
    42  		{"xchacha20_poly1305_test.json", chacha20poly1305.NewX},
    43  	} {
    44  		var testdata wycheproof.AeadTestSchemaV1Json
    45  		wycheproof.LoadVectorFile(t, tc.file, &testdata)
    46  
    47  		for _, tg := range testdata.TestGroups {
    48  			for _, tv := range tg.Tests {
    49  				t.Run(wycheproof.TestName(tc.file, tv), func(t *testing.T) {
    50  					t.Parallel()
    51  
    52  					aead, err := tc.newCipher(wycheproof.MustDecodeHex(tv.Key))
    53  					if err != nil {
    54  						t.Fatalf("failed to construct cipher: %s", err)
    55  					}
    56  
    57  					iv := wycheproof.MustDecodeHex(tv.Iv)
    58  					tag := wycheproof.MustDecodeHex(tv.Tag)
    59  					ct := wycheproof.MustDecodeHex(tv.Ct)
    60  					msg := wycheproof.MustDecodeHex(tv.Msg)
    61  					aad := wycheproof.MustDecodeHex(tv.Aad)
    62  
    63  					// The Go implementation panics on invalid nonce sizes rather
    64  					// than returning an error. Verify this behavior.
    65  					if len(iv) != aead.NonceSize() {
    66  						ctWithTag := append(ct, tag...)
    67  						wycheproof.MustPanic(t, "Seal", func() { aead.Seal(nil, iv, msg, aad) })
    68  						wycheproof.MustPanic(t, "Open", func() { aead.Open(nil, iv, ctWithTag, aad) })
    69  						return
    70  					}
    71  
    72  					genCT := aead.Seal(nil, iv, msg, aad)
    73  					genMsg, err := aead.Open(nil, iv, genCT, aad)
    74  					if err != nil {
    75  						t.Errorf("failed to decrypt generated ciphertext: %s", err)
    76  					}
    77  					if !bytes.Equal(genMsg, msg) {
    78  						t.Errorf("unexpected roundtripped plaintext: got %x, want %x", genMsg, msg)
    79  					}
    80  
    81  					ctWithTag := append(ct, tag...)
    82  					msg2, err := aead.Open(nil, iv, ctWithTag, aad)
    83  					wantPass := wycheproof.ShouldPass(t, tv.Result, tv.Flags, nil)
    84  					if !wantPass && err == nil {
    85  						t.Error("decryption succeeded when it should've failed")
    86  					} else if wantPass {
    87  						if err != nil {
    88  							t.Fatalf("decryption failed: %s", err)
    89  						}
    90  						if !bytes.Equal(genCT, ctWithTag) {
    91  							t.Errorf("generated ciphertext doesn't match expected: got %x, want %x", genCT, ctWithTag)
    92  						}
    93  						if !bytes.Equal(msg, msg2) {
    94  							t.Errorf("decrypted ciphertext doesn't match expected: got %x, want %x", msg2, msg)
    95  						}
    96  					}
    97  				})
    98  			}
    99  		}
   100  	}
   101  }
   102  

View as plain text