| 1 |
// Copyright (c) 2018, Google LLC All rights reserved. |
| 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 tpm2 |
| 16 |
|
| 17 |
import ( |
| 18 |
"crypto/sha256" |
| 19 |
"testing" |
| 20 |
|
| 21 |
. "github.com/google/go-tpm/legacy/tpm2" |
| 22 |
) |
| 23 |
|
| 24 |
func BenchmarkRSA2048Signing(b *testing.B) { |
| 25 |
b.StopTimer() |
| 26 |
rw := openTPM(b) |
| 27 |
defer rw.Close() |
| 28 |
|
| 29 |
pub := Public{ |
| 30 |
Type: AlgRSA, |
| 31 |
NameAlg: AlgSHA256, |
| 32 |
Attributes: FlagSign | FlagSensitiveDataOrigin | FlagUserWithAuth, |
| 33 |
RSAParameters: &RSAParams{ |
| 34 |
Sign: &SigScheme{ |
| 35 |
Alg: AlgRSASSA, |
| 36 |
Hash: AlgSHA256, |
| 37 |
}, |
| 38 |
KeyBits: 2048, |
| 39 |
}, |
| 40 |
} |
| 41 |
|
| 42 |
signerHandle, _, err := CreatePrimary(rw, HandleOwner, pcrSelection7, emptyPassword, defaultPassword, pub) |
| 43 |
if err != nil { |
| 44 |
b.Fatalf("CreatePrimary failed: %v", err) |
| 45 |
} |
| 46 |
defer FlushContext(rw, signerHandle) |
| 47 |
|
| 48 |
digest := sha256.Sum256([]byte("randomString")) |
| 49 |
|
| 50 |
b.StartTimer() |
| 51 |
for i := 0; i < b.N; i++ { |
| 52 |
if _, err := Sign(rw, signerHandle, defaultPassword, digest[:], nil, pub.RSAParameters.Sign); err != nil { |
| 53 |
b.Fatalf("Signing failed: %v", err) |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
func BenchmarkECCNISTP256Signing(b *testing.B) { |
| 59 |
b.StopTimer() |
| 60 |
rw := openTPM(b) |
| 61 |
defer rw.Close() |
| 62 |
skipOnUnsupportedAlg(b, rw, AlgECC) |
| 63 |
|
| 64 |
pub := Public{ |
| 65 |
Type: AlgECC, |
| 66 |
NameAlg: AlgSHA256, |
| 67 |
Attributes: FlagSign | FlagSensitiveDataOrigin | FlagUserWithAuth, |
| 68 |
ECCParameters: &ECCParams{ |
| 69 |
Sign: &SigScheme{ |
| 70 |
Alg: AlgECDSA, |
| 71 |
Hash: AlgSHA256, |
| 72 |
}, |
| 73 |
CurveID: CurveNISTP256, |
| 74 |
}, |
| 75 |
} |
| 76 |
|
| 77 |
signerHandle, _, err := CreatePrimary(rw, HandleOwner, pcrSelection7, emptyPassword, defaultPassword, pub) |
| 78 |
if err != nil { |
| 79 |
b.Fatalf("CreatePrimary failed: %v", err) |
| 80 |
} |
| 81 |
defer FlushContext(rw, signerHandle) |
| 82 |
|
| 83 |
digest := sha256.Sum256([]byte("randomString")) |
| 84 |
|
| 85 |
b.StartTimer() |
| 86 |
for i := 0; i < b.N; i++ { |
| 87 |
if _, err := Sign(rw, signerHandle, defaultPassword, digest[:], nil, pub.ECCParameters.Sign); err != nil { |
| 88 |
b.Fatalf("Signing failed: %v", err) |
| 89 |
} |
| 90 |
} |
| 91 |
} |