|
| 1 | +package ecies |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + crand "crypto/rand" |
| 6 | + "math/rand/v2" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/btcsuite/btcd/btcec/v2" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "golang.org/x/crypto/chacha20poly1305" |
| 12 | +) |
| 13 | + |
| 14 | +// TestEncryptDecryptSha256ChaCha20Poly1305 tests the |
| 15 | +// EncryptSha256ChaCha20Poly1305 and DecryptSha256ChaCha20Poly1305 functions. It |
| 16 | +// generates a shared secret using ECDH between a sender and receiver key pair, |
| 17 | +// encrypts a message using the shared secret, and then decrypts it to verify |
| 18 | +// that the original message is recovered. |
| 19 | +func TestEncryptDecryptSha256ChaCha20Poly1305(t *testing.T) { |
| 20 | + tests := []struct { |
| 21 | + name string |
| 22 | + message []byte |
| 23 | + additionalData []byte |
| 24 | + }{ |
| 25 | + { |
| 26 | + name: "short message", |
| 27 | + message: []byte("hello"), |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "short message with AD", |
| 31 | + message: []byte("hello"), |
| 32 | + additionalData: []byte("additional data"), |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "empty message", |
| 36 | + message: nil, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "long message", |
| 40 | + message: bytes.Repeat([]byte("a"), 1024), |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + for _, tt := range tests { |
| 45 | + t.Run(tt.name, func(t *testing.T) { |
| 46 | + senderPriv, err := btcec.NewPrivateKey() |
| 47 | + require.NoError(t, err) |
| 48 | + |
| 49 | + receiverPriv, err := btcec.NewPrivateKey() |
| 50 | + require.NoError(t, err) |
| 51 | + receiverPub := receiverPriv.PubKey() |
| 52 | + |
| 53 | + sharedSecret, err := ECDH(senderPriv, receiverPub) |
| 54 | + require.NoError(t, err) |
| 55 | + |
| 56 | + // Encrypt the message. |
| 57 | + ciphertext, err := EncryptSha256ChaCha20Poly1305( |
| 58 | + sharedSecret, tt.message, tt.additionalData, |
| 59 | + ) |
| 60 | + require.NoError(t, err) |
| 61 | + |
| 62 | + require.NotContains(t, ciphertext, tt.message) |
| 63 | + require.GreaterOrEqual( |
| 64 | + t, len(ciphertext), chacha20poly1305.NonceSize, |
| 65 | + ) |
| 66 | + |
| 67 | + // Decrypt the message. |
| 68 | + plaintext, err := DecryptSha256ChaCha20Poly1305( |
| 69 | + sharedSecret, ciphertext, tt.additionalData, |
| 70 | + ) |
| 71 | + require.NoError(t, err) |
| 72 | + |
| 73 | + // Verify the decrypted message matches the original. |
| 74 | + require.Equal(t, tt.message, plaintext) |
| 75 | + }) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// TestEncryptDecryptSha256ChaCha20Poly1305Random tests the |
| 80 | +// EncryptSha256ChaCha20Poly1305 and DecryptSha256ChaCha20Poly1305 functions |
| 81 | +// with random messages. |
| 82 | +func TestEncryptDecryptSha256ChaCha20Poly1305Random(t *testing.T) { |
| 83 | + for i := 0; i < 100; i++ { |
| 84 | + msgLen := rand.Int()%65536 + 1 |
| 85 | + msg := make([]byte, msgLen) |
| 86 | + _, err := crand.Read(msg) |
| 87 | + require.NoError(t, err) |
| 88 | + |
| 89 | + ad := make([]byte, 32) |
| 90 | + _, err = crand.Read(ad) |
| 91 | + require.NoError(t, err) |
| 92 | + |
| 93 | + senderPriv, err := btcec.NewPrivateKey() |
| 94 | + require.NoError(t, err) |
| 95 | + |
| 96 | + receiverPriv, err := btcec.NewPrivateKey() |
| 97 | + require.NoError(t, err) |
| 98 | + receiverPub := receiverPriv.PubKey() |
| 99 | + |
| 100 | + sharedSecret, err := ECDH(senderPriv, receiverPub) |
| 101 | + require.NoError(t, err) |
| 102 | + |
| 103 | + // Encrypt the message. |
| 104 | + ciphertext, err := EncryptSha256ChaCha20Poly1305( |
| 105 | + sharedSecret, msg, ad, |
| 106 | + ) |
| 107 | + require.NoError(t, err) |
| 108 | + |
| 109 | + require.NotContains(t, ciphertext, msg) |
| 110 | + require.GreaterOrEqual(t, len(ciphertext), 32) |
| 111 | + |
| 112 | + // Decrypt the message. |
| 113 | + plaintext, err := DecryptSha256ChaCha20Poly1305( |
| 114 | + sharedSecret, ciphertext, ad, |
| 115 | + ) |
| 116 | + require.NoError(t, err) |
| 117 | + |
| 118 | + // Verify the decrypted message matches the original. |
| 119 | + require.Equal(t, msg, plaintext) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// EncryptSha256ChaCha20Poly1305 tests the performance of the |
| 124 | +// EncryptSha256ChaCha20Poly1305 function. |
| 125 | +func BenchmarkEncryptSha256ChaCha20Poly1305(b *testing.B) { |
| 126 | + senderPriv, err := btcec.NewPrivateKey() |
| 127 | + require.NoError(b, err) |
| 128 | + |
| 129 | + receiverPriv, err := btcec.NewPrivateKey() |
| 130 | + require.NoError(b, err) |
| 131 | + receiverPub := receiverPriv.PubKey() |
| 132 | + |
| 133 | + sharedSecret, err := ECDH(senderPriv, receiverPub) |
| 134 | + require.NoError(b, err) |
| 135 | + |
| 136 | + longMessage := bytes.Repeat([]byte("secret"), 10240) |
| 137 | + ad := bytes.Repeat([]byte("ad"), 1024) |
| 138 | + |
| 139 | + b.ResetTimer() |
| 140 | + for i := 0; i < b.N; i++ { |
| 141 | + _, err := EncryptSha256ChaCha20Poly1305( |
| 142 | + sharedSecret, longMessage, ad, |
| 143 | + ) |
| 144 | + if err != nil { |
| 145 | + b.Fail() |
| 146 | + } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +// BenchmarkDecryptSha256Aes256 tests the performance of the |
| 151 | +// DecryptSha256ChaCha20Poly1305 function. |
| 152 | +func BenchmarkDecryptSha256ChaCha20Poly1305(b *testing.B) { |
| 153 | + senderPriv, err := btcec.NewPrivateKey() |
| 154 | + require.NoError(b, err) |
| 155 | + |
| 156 | + receiverPriv, err := btcec.NewPrivateKey() |
| 157 | + require.NoError(b, err) |
| 158 | + receiverPub := receiverPriv.PubKey() |
| 159 | + |
| 160 | + sharedSecret, err := ECDH(senderPriv, receiverPub) |
| 161 | + require.NoError(b, err) |
| 162 | + |
| 163 | + longMessage := bytes.Repeat([]byte("secret"), 10240) |
| 164 | + ad := bytes.Repeat([]byte("ad"), 1024) |
| 165 | + |
| 166 | + ciphertext, err := EncryptSha256ChaCha20Poly1305( |
| 167 | + sharedSecret, longMessage, ad, |
| 168 | + ) |
| 169 | + require.NoError(b, err) |
| 170 | + |
| 171 | + b.ResetTimer() |
| 172 | + for i := 0; i < b.N; i++ { |
| 173 | + _, err := DecryptSha256ChaCha20Poly1305( |
| 174 | + sharedSecret, ciphertext, ad, |
| 175 | + ) |
| 176 | + if err != nil { |
| 177 | + b.Fail() |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +// FuzzEncryptSha256ChaCha20Poly1305 is a fuzz test for the |
| 183 | +// EncryptSha256ChaCha20Poly1305 function. |
| 184 | +func FuzzEncryptSha256ChaCha20Poly1305(f *testing.F) { |
| 185 | + f.Fuzz(func(t *testing.T, secretBytes, msg, ad []byte) { |
| 186 | + var sharedSecret [32]byte |
| 187 | + copy(sharedSecret[:], secretBytes) |
| 188 | + _, _ = EncryptSha256ChaCha20Poly1305(sharedSecret, msg, ad) |
| 189 | + }) |
| 190 | +} |
| 191 | + |
| 192 | +// FuzzDecryptSha256ChaCha20Poly1305 is a fuzz test for the |
| 193 | +// DecryptSha256ChaCha20Poly1305 function. |
| 194 | +func FuzzDecryptSha256ChaCha20Poly1305(f *testing.F) { |
| 195 | + f.Fuzz(func(t *testing.T, secretBytes, msg, ad []byte) { |
| 196 | + var sharedSecret [32]byte |
| 197 | + copy(sharedSecret[:], secretBytes) |
| 198 | + _, _ = DecryptSha256ChaCha20Poly1305(sharedSecret, msg, ad) |
| 199 | + }) |
| 200 | +} |
0 commit comments