|
| 1 | +// Copyright 2024 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 | +// ////////////////////////////////////////////////////////////////////////////// |
| 16 | + |
| 17 | +package daead_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/google/tink/go/daead" |
| 23 | + "github.com/google/tink/go/keyset" |
| 24 | + "github.com/google/tink/go/subtle/random" |
| 25 | + tinkpb "github.com/google/tink/go/proto/tink_go_proto" |
| 26 | +) |
| 27 | + |
| 28 | +// Benchmarks for Deterministic AEAD algorithms. |
| 29 | + |
| 30 | +func BenchmarkAESSIV(b *testing.B) { |
| 31 | + const ( |
| 32 | + plaintextSize = 16 * 1024 |
| 33 | + associatedDataSize = 256 |
| 34 | + ) |
| 35 | + testCases := []struct { |
| 36 | + name string |
| 37 | + template *tinkpb.KeyTemplate |
| 38 | + }{ |
| 39 | + { |
| 40 | + name: "AES256_SIV", |
| 41 | + template: daead.AESSIVKeyTemplate(), |
| 42 | + }, |
| 43 | + } |
| 44 | + for _, tc := range testCases { |
| 45 | + b.Run(tc.name, func(b *testing.B) { |
| 46 | + b.ReportAllocs() |
| 47 | + |
| 48 | + handle, err := keyset.NewHandle(tc.template) |
| 49 | + if err != nil { |
| 50 | + b.Fatal(err) |
| 51 | + } |
| 52 | + primitive, err := daead.New(handle) |
| 53 | + if err != nil { |
| 54 | + b.Fatal(err) |
| 55 | + } |
| 56 | + plaintext := random.GetRandomBytes(plaintextSize) |
| 57 | + associatedData := random.GetRandomBytes(associatedDataSize) |
| 58 | + b.ResetTimer() |
| 59 | + for i := 0; i < b.N; i++ { |
| 60 | + ciphertext, err := primitive.EncryptDeterministically(plaintext, associatedData) |
| 61 | + if err != nil { |
| 62 | + b.Fatal(err) |
| 63 | + } |
| 64 | + _, err = primitive.DecryptDeterministically(ciphertext, associatedData) |
| 65 | + if err != nil { |
| 66 | + b.Error(err) |
| 67 | + } |
| 68 | + } |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
0 commit comments