Synchronize legacy UDP crypto setup

This commit is contained in:
Brandon McGinty (chatgpt)
2026-08-10 04:06:12 -04:00
committed by Brandon McGinty
parent 42861dfcd5
commit fa84373a81
2 changed files with 31 additions and 0 deletions
+6
View File
@@ -233,6 +233,8 @@ type cryptState struct {
}
func (cs *cryptState) setup(key, iv []byte) error {
cs.mu.Lock()
defer cs.mu.Unlock()
if len(key) != 16 {
return errors.New("gumble: crypt key must be 16 bytes")
}
@@ -269,6 +271,8 @@ func (cs *cryptState) nonceForPacket(counter uint32) [12]byte {
}
func (cs *cryptState) encrypt(counter uint32, plaintext []byte) ([]byte, error) {
cs.mu.Lock()
defer cs.mu.Unlock()
if !cs.initialized {
return plaintext, nil
}
@@ -277,6 +281,8 @@ func (cs *cryptState) encrypt(counter uint32, plaintext []byte) ([]byte, error)
}
func (cs *cryptState) decrypt(counter uint32, ciphertext []byte) ([]byte, error) {
cs.mu.Lock()
defer cs.mu.Unlock()
if !cs.initialized {
return ciphertext, nil
}
+25
View File
@@ -7,9 +7,34 @@ import (
"encoding/hex"
"fmt"
"strings"
"sync"
"testing"
)
func TestCryptStateSetupAndEncryptAreConcurrentSafe(t *testing.T) {
key := make([]byte, 16)
iv := make([]byte, 16)
var cs cryptState
if err := cs.setup(key, iv); err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go func(counter uint32) {
defer wg.Done()
if counter%2 == 0 {
if err := cs.setup(key, iv); err != nil {
t.Error(err)
}
} else if _, err := cs.encrypt(counter, []byte("audio")); err != nil {
t.Error(err)
}
}(uint32(i))
}
wg.Wait()
}
// TestOCBRoundTrip verifies encrypt-then-decrypt returns the original.
func TestOCBRoundTrip(t *testing.T) {
key := make([]byte, 16)