| 1 | // Copyright 2020 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package subtle_testscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle_test`/ |
| 16 | |
| 17 | import ( |
| 18 | "bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/" |
| 19 | "encoding/hexscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/" |
| 20 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 21 | "math/randscip-go gomod github.com/golang/go/src go1.24.0 `math/rand`/" |
| 22 | "testingscip-go gomod github.com/golang/go/src go1.24.0 testing/" |
| 23 | |
| 24 | "golang.org/x/crypto/chacha20poly1305scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/" |
| 25 | "github.com/tink-crypto/tink-go/v2/aead/subtlescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/" |
| 26 | "github.com/tink-crypto/tink-go/v2/internal/testing/aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/aead`/" |
| 27 | "github.com/tink-crypto/tink-go/v2/internal/testing/wycheproofscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/wycheproof`/" |
| 28 | "github.com/tink-crypto/tink-go/v2/subtle/randomscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/" |
| 29 | ) |
| 30 | |
| 31 | func TestXChaCha20Poly1305EncryptDecryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle_test`/TestXChaCha20Poly1305EncryptDecrypt().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 32 | for i, test := range xChaCha20Poly1305Testsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle_test`/xChaCha20Poly1305Tests. { |
| 33 | key, err := hexscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/.DecodeStringscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/DecodeString().(test.keyscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle_test`/xChaCha20Poly1305Tests:key.) |
| 34 | if err != nil { |
| 35 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("hex.DecodeString(test.key) err = %q, want nil", err) |
| 36 | } |
| 37 | pt, err := hexscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/.DecodeStringscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/DecodeString().(test.plaintextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle_test`/xChaCha20Poly1305Tests:plaintext.) |
| 38 | if err != nil { |
| 39 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("hex.DecodeString(test.plaintext) err = %q, want nil", err) |
| 40 | } |
| 41 | aad, err := hexscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/.DecodeStringscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/DecodeString().(test.aadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle_test`/xChaCha20Poly1305Tests:aad.) |
| 42 | if err != nil { |
| 43 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("hex.DecodeString(test.aad) err = %q, want nil", err) |
| 44 | } |
| 45 | nonce, err := hexscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/.DecodeStringscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/DecodeString().(test.noncescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle_test`/xChaCha20Poly1305Tests:nonce.) |
| 46 | if err != nil { |
| 47 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("hex.DecodeString(test.nonce) err = %q, want nil", err) |
| 48 | } |
| 49 | out, err := hexscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/.DecodeStringscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/DecodeString().(test.outscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle_test`/xChaCha20Poly1305Tests:out.) |
| 50 | if err != nil { |
| 51 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("hex.DecodeString(test.out) err = %q, want nil", err) |
| 52 | } |
| 53 | tag, err := hexscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/.DecodeStringscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/DecodeString().(test.tagscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle_test`/xChaCha20Poly1305Tests:tag.) |
| 54 | if err != nil { |
| 55 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("hex.DecodeString(test.tag) err = %q, want nil", err) |
| 56 | } |
| 57 | |
| 58 | x, err := subtlescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/.NewXChaCha20Poly1305scip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/NewXChaCha20Poly1305().(key) |
| 59 | if err != nil { |
| 60 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("#%d, cannot create new instance of XChaCha20Poly1305: %s", i, err) |
| 61 | continue |
| 62 | } |
| 63 | |
| 64 | _, err = x.Encryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#Encrypt().(pt, aad) |
| 65 | if err != nil { |
| 66 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("#%d, unexpected encryption error: %s", i, err) |
| 67 | continue |
| 68 | } |
| 69 | |
| 70 | var combinedCt []byte |
| 71 | combinedCt = append(combinedCt, nonce...) |
| 72 | combinedCt = append(combinedCt, out...) |
| 73 | combinedCt = append(combinedCt, tag...) |
| 74 | if got, err := x.Decryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#Decrypt().(combinedCt, aad); err != nil { |
| 75 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("#%d, unexpected decryption error: %s", i, err) |
| 76 | continue |
| 77 | } else if !bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.24.0 bytes/Equal().(pt, got) { |
| 78 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("#%d, plaintext's don't match: got %x vs %x", i, got, pt) |
| 79 | continue |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestXChaCha20Poly1305EmptyAssociatedDatascip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle_test`/TestXChaCha20Poly1305EmptyAssociatedData().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 85 | key := randomscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/.GetRandomBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/GetRandomBytes().(chacha20poly1305scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/.KeySizescip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/KeySize.) |
| 86 | aad := []byte{} |
| 87 | badAad := []byte{1, 2, 3} |
| 88 | |
| 89 | x, err := subtlescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/.NewXChaCha20Poly1305scip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/NewXChaCha20Poly1305().(key) |
| 90 | if err != nil { |
| 91 | t.Fatalscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatal().(err) |
| 92 | } |
| 93 | |
| 94 | for i := 0; i < 75; i++ { |
| 95 | pt := randomscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/.GetRandomBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/GetRandomBytes().(uint32(i)) |
| 96 | // Encrpting with aad as a 0-length array |
| 97 | { |
| 98 | ct, err := x.Encryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#Encrypt().(pt, aad) |
| 99 | if err != nil { |
| 100 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("Encrypt(%x, %x) failed", pt, aad) |
| 101 | continue |
| 102 | } |
| 103 | |
| 104 | if got, err := x.Decryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#Decrypt().(ct, aad); err != nil || !bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.24.0 bytes/Equal().(pt, got) { |
| 105 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("Decrypt(Encrypt(pt, %x)): plaintext's don't match: got %x vs %x", aad, got, pt) |
| 106 | } |
| 107 | if got, err := x.Decryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#Decrypt().(ct, nil); err != nil || !bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.24.0 bytes/Equal().(pt, got) { |
| 108 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("Decrypt(Encrypt(pt, nil)): plaintext's don't match: got %x vs %x", got, pt) |
| 109 | } |
| 110 | if _, err := x.Decryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#Decrypt().(ct, badAad); err == nil { |
| 111 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("Decrypt(Encrypt(pt, %x)) = _, nil; want: _, err", badAad) |
| 112 | } |
| 113 | } |
| 114 | // Encrpting with aad equal to null |
| 115 | { |
| 116 | ct, err := x.Encryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#Encrypt().(pt, nil) |
| 117 | if err != nil { |
| 118 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("Encrypt(%x, nil) failed", pt) |
| 119 | } |
| 120 | |
| 121 | if got, err := x.Decryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#Decrypt().(ct, aad); err != nil || !bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.24.0 bytes/Equal().(pt, got) { |
| 122 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("Decrypt(Encrypt(pt, %x)): plaintext's don't match: got %x vs %x; error: %v", aad, got, pt, err) |
| 123 | } |
| 124 | if got, err := x.Decryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#Decrypt().(ct, nil); err != nil || !bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.24.0 bytes/Equal().(pt, got) { |
| 125 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("Decrypt(Encrypt(pt, nil)): plaintext's don't match: got %x vs %x; error: %v", got, pt, err) |
| 126 | } |
| 127 | if _, err := x.Decryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#Decrypt().(ct, badAad); err == nil { |
| 128 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("Decrypt(Encrypt(pt, %x)) = _, nil; want: _, err", badAad) |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | func TestXChaCha20Poly1305LongMessagesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle_test`/TestXChaCha20Poly1305LongMessages().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 135 | dataSize := uint32(16) |
| 136 | // Encrypts and decrypts messages of size <= 8192. |
| 137 | for dataSize <= 1<<24 { |
| 138 | pt := randomscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/.GetRandomBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/GetRandomBytes().(dataSize) |
| 139 | aad := randomscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/.GetRandomBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/GetRandomBytes().(dataSize / 3) |
| 140 | key := randomscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/.GetRandomBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/GetRandomBytes().(chacha20poly1305scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/.KeySizescip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/KeySize.) |
| 141 | |
| 142 | x, err := subtlescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/.NewXChaCha20Poly1305scip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/NewXChaCha20Poly1305().(key) |
| 143 | if err != nil { |
| 144 | t.Fatalscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatal().(err) |
| 145 | } |
| 146 | |
| 147 | ct, err := x.Encryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#Encrypt().(pt, aad) |
| 148 | if err != nil { |
| 149 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("Encrypt(%x, %x) failed", pt, aad) |
| 150 | continue |
| 151 | } |
| 152 | |
| 153 | if got, err := x.Decryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#Decrypt().(ct, aad); err != nil || !bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.24.0 bytes/Equal().(pt, got) { |
| 154 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("Decrypt(Encrypt(pt, %x)): plaintext's don't match: got %x vs %x; error: %v", aad, got, pt, err) |
| 155 | } |
| 156 | |
| 157 | dataSize += 5 * dataSize / 11 |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | func TestXChaCha20Poly1305ModifyCiphertextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle_test`/TestXChaCha20Poly1305ModifyCiphertext().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 162 | for i, test := range xChaCha20Poly1305Testsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle_test`/xChaCha20Poly1305Tests. { |
| 163 | key, err := hexscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/.DecodeStringscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/DecodeString().(test.keyscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle_test`/xChaCha20Poly1305Tests:key.) |
| 164 | if err != nil { |
| 165 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("hex.DecodeString(test.key) err = %q, want nil", err) |
| 166 | } |
| 167 | pt, err := hexscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/.DecodeStringscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/DecodeString().(test.plaintextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle_test`/xChaCha20Poly1305Tests:plaintext.) |
| 168 | if err != nil { |
| 169 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("hex.DecodeString(test.plaintext) err = %q, want nil", err) |
| 170 | } |
| 171 | aad, err := hexscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/.DecodeStringscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/DecodeString().(test.aadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle_test`/xChaCha20Poly1305Tests:aad.) |
| 172 | if err != nil { |
| 173 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("hex.DecodeString(test.aad) err = %q, want nil", err) |
| 174 | } |
| 175 | |
| 176 | x, err := subtlescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/.NewXChaCha20Poly1305scip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/NewXChaCha20Poly1305().(key) |
| 177 | if err != nil { |
| 178 | t.Fatalscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatal().(err) |
| 179 | } |
| 180 | |
| 181 | ct, err := x.Encryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#Encrypt().(pt, aad) |
| 182 | if err != nil { |
| 183 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("#%d: Encrypt failed", i) |
| 184 | continue |
| 185 | } |
| 186 | |
| 187 | if len(aad) > 0 { |
| 188 | alterAadIdx := randscip-go gomod github.com/golang/go/src go1.24.0 `math/rand`/.Intnscip-go gomod github.com/golang/go/src go1.24.0 `math/rand`/Intn().(len(aad)) |
| 189 | aad[alterAadIdx] ^= 0x80 |
| 190 | if _, err := x.Decryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#Decrypt().(ct, aad); err == nil { |
| 191 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("#%d: Decrypt was successful after altering additional data", i) |
| 192 | continue |
| 193 | } |
| 194 | aad[alterAadIdx] ^= 0x80 |
| 195 | } |
| 196 | |
| 197 | alterCtIdx := randscip-go gomod github.com/golang/go/src go1.24.0 `math/rand`/.Intnscip-go gomod github.com/golang/go/src go1.24.0 `math/rand`/Intn().(len(ct)) |
| 198 | ct[alterCtIdx] ^= 0x80 |
| 199 | if _, err := x.Decryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#Decrypt().(ct, aad); err == nil { |
| 200 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("#%d: Decrypt was successful after altering ciphertext", i) |
| 201 | continue |
| 202 | } |
| 203 | ct[alterCtIdx] ^= 0x80 |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // This is a very simple test for the randomness of the nonce. |
| 208 | // The test simply checks that the multiple ciphertexts of the same message are distinct. |
| 209 | func TestXChaCha20Poly1305RandomNoncescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle_test`/TestXChaCha20Poly1305RandomNonce().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 210 | key := randomscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/.GetRandomBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/GetRandomBytes().(chacha20poly1305scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/.KeySizescip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/KeySize.) |
| 211 | x, err := subtlescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/.NewXChaCha20Poly1305scip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/NewXChaCha20Poly1305().(key) |
| 212 | if err != nil { |
| 213 | t.Fatalscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatal().(err) |
| 214 | } |
| 215 | |
| 216 | cts := make(map[string]bool) |
| 217 | pt, aad := []byte{}, []byte{} |
| 218 | for i := 0; i < 1<<10; i++ { |
| 219 | ct, err := x.Encryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#Encrypt().(pt, aad) |
| 220 | ctHex := hexscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/.EncodeToStringscip-go gomod github.com/golang/go/src go1.24.0 `encoding/hex`/EncodeToString().(ct) |
| 221 | if err != nil || cts[ctHex] { |
| 222 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("TestRandomNonce failed: %v", err) |
| 223 | } else { |
| 224 | cts[ctHex] = true |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | func TestXChaCha20Poly1305WycheproofCasesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle_test`/TestXChaCha20Poly1305WycheproofCases().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 230 | suite := new(aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/aead`/.WycheproofSuiteV1scip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/aead`/WycheproofSuiteV1#) |
| 231 | wycheproofscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/wycheproof`/.PopulateSuiteV1scip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/wycheproof`/PopulateSuiteV1().(t, suite, "xchacha20_poly1305_test.json") |
| 232 | for _, group := range suite.TestGroupsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/aead`/WycheproofSuiteV1#TestGroups. { |
| 233 | if group.KeySizescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/aead`/WycheproofGroup#KeySize./8 != chacha20poly1305scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/.KeySizescip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/KeySize. { |
| 234 | continue |
| 235 | } |
| 236 | if group.IvSizescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/aead`/WycheproofGroup#IvSize./8 != chacha20poly1305scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/.NonceSizeXscip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/NonceSizeX. { |
| 237 | continue |
| 238 | } |
| 239 | for _, test := range group.Testsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/aead`/WycheproofGroup#Tests. { |
| 240 | caseName := fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("%s-%s:Case-%d", suite.Algorithmscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/wycheproof`/Suite#Algorithm., group.Typescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/aead`/WycheproofGroup#Type., test.CaseIDscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/wycheproof`/Case#CaseID.) |
| 241 | t.Runscip-go gomod github.com/golang/go/src go1.24.0 testing/T#Run().(caseName, func(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { runXChaCha20Poly1305WycheproofCasescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle_test`/runXChaCha20Poly1305WycheproofCase().(t, test) }) |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | func runXChaCha20Poly1305WycheproofCasescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle_test`/runXChaCha20Poly1305WycheproofCase().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#, tc *aeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/aead`/.WycheproofCasescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/aead`/WycheproofCase#) { |
| 247 | var combinedCt []byte |
| 248 | combinedCt = append(combinedCt, tc.Ivscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/aead`/WycheproofCase#Iv....) |
| 249 | combinedCt = append(combinedCt, tc.Ctscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/aead`/WycheproofCase#Ct....) |
| 250 | combinedCt = append(combinedCt, tc.Tagscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/aead`/WycheproofCase#Tag....) |
| 251 | |
| 252 | ca, err := subtlescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/.NewXChaCha20Poly1305scip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/NewXChaCha20Poly1305().(tc.Keyscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/aead`/WycheproofCase#Key.) |
| 253 | if err != nil { |
| 254 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("cannot create new instance of ChaCha20Poly1305: %s", err) |
| 255 | } |
| 256 | |
| 257 | _, err = ca.Encryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#Encrypt().(tc.Msgscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/aead`/WycheproofCase#Msg., tc.Aadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/aead`/WycheproofCase#Aad.) |
| 258 | if err != nil { |
| 259 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("unexpected encryption error: %s", err) |
| 260 | } |
| 261 | |
| 262 | decrypted, err := ca.Decryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#Decrypt().(combinedCt, tc.Aadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/aead`/WycheproofCase#Aad.) |
| 263 | if err != nil { |
| 264 | if tc.Resultscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/wycheproof`/Case#Result. == "valid" { |
| 265 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("unexpected error: %s", err) |
| 266 | } |
| 267 | } else { |
| 268 | if tc.Resultscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/wycheproof`/Case#Result. == "invalid" { |
| 269 | t.Errorscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Error().("decrypted invalid") |
| 270 | } |
| 271 | if !bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.24.0 bytes/Equal().(decrypted, tc.Msgscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/internal/testing/aead`/WycheproofCase#Msg.) { |
| 272 | t.Errorscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Error().("incorrect decryption") |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | func TestPreallocatedCiphertextMemoryInXChaCha20Poly1305IsExactscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle_test`/TestPreallocatedCiphertextMemoryInXChaCha20Poly1305IsExact().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 278 | key := randomscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/.GetRandomBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/GetRandomBytes().(chacha20poly1305scip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/.KeySizescip-go gomod golang.org/x/crypto v0.45.0 `golang.org/x/crypto/chacha20poly1305`/KeySize.) |
| 279 | a, err := subtlescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/.NewXChaCha20Poly1305scip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/NewXChaCha20Poly1305().(key) |
| 280 | if err != nil { |
| 281 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("aead.NewAESGCMInsecureIV() err = %v, want nil", err) |
| 282 | } |
| 283 | plaintext := randomscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/.GetRandomBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/GetRandomBytes().(13) |
| 284 | associatedData := randomscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/.GetRandomBytesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/subtle/random`/GetRandomBytes().(17) |
| 285 | |
| 286 | ciphertext, err := a.Encryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/aead/subtle`/XChaCha20Poly1305#Encrypt().(plaintext, associatedData) |
| 287 | if err != nil { |
| 288 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("a.Encrypt() err = %v, want nil", err) |
| 289 | } |
| 290 | // Encrypt() uses cipher.Overhead() to pre-allocate the memory needed store the ciphertext. |
| 291 | // For ChaCha20Poly1305, the size of the allocated memory should always be exact. If this check |
| 292 | // fails, the pre-allocated memory was too large or too small. If it was too small, the system had |
| 293 | // to re-allocate more memory, which is expensive and should be avoided. |
| 294 | if len(ciphertext) != cap(ciphertext) { |
| 295 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("want len(ciphertext) == cap(ciphertext), got %d != %d", len(ciphertext), cap(ciphertext)) |
| 296 | } |
| 297 | } |