| 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 streamingaead_testscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/streamingaead_test`/ |
| 16 | |
| 17 | import ( |
| 18 | "bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/" |
| 19 | "ioscip-go gomod github.com/golang/go/src go1.24.0 io/" |
| 20 | "testingscip-go gomod github.com/golang/go/src go1.24.0 testing/" |
| 21 | |
| 22 | "github.com/tink-crypto/tink-go/v2/keysetscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyset`/" |
| 23 | "github.com/tink-crypto/tink-go/v2/streamingaeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/streamingaead`/" |
| 24 | tinkpbscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/ "github.com/tink-crypto/tink-go/v2/proto/tink_go_protoscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/" |
| 25 | ) |
| 26 | |
| 27 | func TestKeyTemplatesscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/streamingaead_test`/TestKeyTemplates().(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#) { |
| 28 | var testCases = []struct { |
| 29 | name string |
| 30 | template *tinkpbscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/.KeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/proto/tink_go_proto`/KeyTemplate# |
| 31 | }{ |
| 32 | { |
| 33 | name: "AES128_GCM_HKDF_4KB", |
| 34 | template: streamingaeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/streamingaead`/.AES128GCMHKDF4KBKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/streamingaead`/AES128GCMHKDF4KBKeyTemplate().(), |
| 35 | }, |
| 36 | { |
| 37 | name: "AES128_GCM_HKDF_1MB", |
| 38 | template: streamingaeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/streamingaead`/.AES128GCMHKDF1MBKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/streamingaead`/AES128GCMHKDF1MBKeyTemplate().(), |
| 39 | }, |
| 40 | { |
| 41 | name: "AES256_GCM_HKDF_4KB", |
| 42 | template: streamingaeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/streamingaead`/.AES256GCMHKDF4KBKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/streamingaead`/AES256GCMHKDF4KBKeyTemplate().(), |
| 43 | }, { |
| 44 | name: "AES256_GCM_HKDF_1MB", |
| 45 | template: streamingaeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/streamingaead`/.AES256GCMHKDF1MBKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/streamingaead`/AES256GCMHKDF1MBKeyTemplate().(), |
| 46 | }, { |
| 47 | name: "AES128_CTR_HMAC_SHA256_4KB", |
| 48 | template: streamingaeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/streamingaead`/.AES128CTRHMACSHA256Segment4KBKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/streamingaead`/AES128CTRHMACSHA256Segment4KBKeyTemplate().(), |
| 49 | }, |
| 50 | { |
| 51 | name: "AES128_CTR_HMAC_SHA256_1MB", |
| 52 | template: streamingaeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/streamingaead`/.AES128CTRHMACSHA256Segment1MBKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/streamingaead`/AES128CTRHMACSHA256Segment1MBKeyTemplate().(), |
| 53 | }, |
| 54 | { |
| 55 | name: "AES256_CTR_HMAC_SHA256_4KB", |
| 56 | template: streamingaeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/streamingaead`/.AES256CTRHMACSHA256Segment4KBKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/streamingaead`/AES256CTRHMACSHA256Segment4KBKeyTemplate().(), |
| 57 | }, |
| 58 | { |
| 59 | name: "AES256_CTR_HMAC_SHA256_1MB", |
| 60 | template: streamingaeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/streamingaead`/.AES256CTRHMACSHA256Segment1MBKeyTemplatescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/streamingaead`/AES256CTRHMACSHA256Segment1MBKeyTemplate().(), |
| 61 | }, |
| 62 | } |
| 63 | for _, tc := range testCases { |
| 64 | t.Runscip-go gomod github.com/golang/go/src go1.24.0 testing/T#Run().(tc.name, 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#) { |
| 65 | handle, err := keysetscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyset`/.NewHandlescip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/keyset`/NewHandle().(tc.template) |
| 66 | if err != nil { |
| 67 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("keyset.NewHandle(template) failed: %v", err) |
| 68 | } |
| 69 | primitive, err := streamingaeadscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/streamingaead`/.Newscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/streamingaead`/New().(handle) |
| 70 | if err != nil { |
| 71 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("aead.New(handle) failed: %v", err) |
| 72 | } |
| 73 | |
| 74 | plaintext := []byte("some data to encrypt") |
| 75 | aad := []byte("extra data to authenticate") |
| 76 | buf := &bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.24.0 bytes/Buffer#{} |
| 77 | w, err := primitive.NewEncryptingWriterscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/StreamingAEAD#NewEncryptingWriter.(buf, aad) |
| 78 | if err != nil { |
| 79 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("primitive.NewEncryptingWriter(buf, aad) failed: %v", err) |
| 80 | } |
| 81 | if _, err := w.Writescip-go gomod github.com/golang/go/src go1.24.0 io/Writer#Write().(plaintext); err != nil { |
| 82 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("w.Write(plaintext) failed: %v", err) |
| 83 | } |
| 84 | if err := w.Closescip-go gomod github.com/golang/go/src go1.24.0 io/Closer#Close().(); err != nil { |
| 85 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("w.Close() failed: %v", err) |
| 86 | } |
| 87 | |
| 88 | r, err := primitive.NewDecryptingReaderscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/StreamingAEAD#NewDecryptingReader.(buf, aad) |
| 89 | if err != nil { |
| 90 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("primitive.NewDecryptingReader(buf, aad) failed: %v", err) |
| 91 | } |
| 92 | decrypted, err := ioscip-go gomod github.com/golang/go/src go1.24.0 io/.ReadAllscip-go gomod github.com/golang/go/src go1.24.0 io/ReadAll().(r) |
| 93 | if err != nil { |
| 94 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("io.ReadAll(r) failed: %v", err) |
| 95 | } |
| 96 | 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, plaintext) { |
| 97 | t.Errorfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Errorf().("decrypted data doesn't match plaintext, got: %q, want: ''", decrypted) |
| 98 | } |
| 99 | }) |
| 100 | } |
| 101 | } |