File: commit_test.go

package info (click to toggle)
golang-github-google-go-tpm 0.9.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,932 kB
  • sloc: makefile: 13
file content (112 lines) | stat: -rw-r--r-- 2,232 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package tpm2test

import (
	"testing"

	. "github.com/google/go-tpm/tpm2"
	"github.com/google/go-tpm/tpm2/transport/simulator"
)

func TestCommit(t *testing.T) {
	thetpm, err := simulator.OpenSimulator()
	if err != nil {
		t.Fatalf("could not connect to TPM simulator: %v", err)
	}

	defer thetpm.Close()

	password := []byte("hello")

	create := CreateLoaded{
		ParentHandle: TPMRHOwner,
		InSensitive: TPM2BSensitiveCreate{
			Sensitive: &TPMSSensitiveCreate{
				UserAuth: TPM2BAuth{
					Buffer: password,
				},
			},
		},
		InPublic: New2BTemplate(
			&TPMTPublic{
				Type:    TPMAlgECC,
				NameAlg: TPMAlgSHA256,
				ObjectAttributes: TPMAObject{
					FixedTPM:            true,
					FixedParent:         true,
					UserWithAuth:        true,
					SensitiveDataOrigin: true,
					SignEncrypt:         true,
				},
				Parameters: NewTPMUPublicParms(
					TPMAlgECC,
					&TPMSECCParms{
						Symmetric: TPMTSymDefObject{
							Algorithm: TPMAlgNull,
						},
						Scheme: TPMTECCScheme{
							Scheme: TPMAlgECDAA,
							Details: NewTPMUAsymScheme(
								TPMAlgECDAA,
								&TPMSSchemeECDAA{
									HashAlg: TPMAlgSHA256,
								},
							),
						},
						CurveID: TPMECCBNP256,
						KDF: TPMTKDFScheme{
							Scheme: TPMAlgNull,
						},
					},
				),
			}),
	}

	rspCP, err := create.Execute(thetpm)
	if err != nil {
		t.Fatalf("could not create key: %v", err)
	}

	flushContextCP := FlushContext{FlushHandle: rspCP.ObjectHandle}
	defer flushContextCP.Execute(thetpm)

	commit := Commit{
		SignHandle: AuthHandle{
			Handle: rspCP.ObjectHandle,
			Name:   rspCP.Name,
			Auth:   PasswordAuth(password),
		},
		P1: New2B(
			TPMSECCPoint{
				X: TPM2BECCParameter{
					Buffer: []byte{1},
				},
				Y: TPM2BECCParameter{
					Buffer: []byte{2},
				},
			}),
		S2: TPM2BSensitiveData{
			Buffer: []byte{},
		},
		Y2: TPM2BECCParameter{
			Buffer: []byte{},
		},
	}

	resp, err := commit.Execute(thetpm)
	if err != nil {
		t.Fatalf("could not commit: %v", err)
	}

	firstCounter := resp.Counter

	resp, err = commit.Execute(thetpm)
	if err != nil {
		t.Fatalf("could not commit: %v", err)
	}

	secondCounter := resp.Counter

	if firstCounter+1 != secondCounter {
		t.Fatalf("counter did not increment")
	}
}