-
Notifications
You must be signed in to change notification settings - Fork 21
/
blobs.go
56 lines (48 loc) · 1.29 KB
/
blobs.go
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
package tpm
import (
"bytes"
"fmt"
"github.com/google/go-tpm/tpmutil"
)
// Blobs is a container for the private and public blobs of data
// that represent a TPM2 object.
type Blobs struct {
private []byte
public []byte
}
// Private returns the private data blob of a TPM2 object including
// a 16-bit header. The blob can be used with tpm2-tools.
func (b *Blobs) Private() (blob []byte, err error) {
if blob, err = toTPM2Tools(b.private); err != nil {
return nil, fmt.Errorf("failed transforming private blob bytes: %w", err)
}
return
}
// Public returns the public data blob of a TPM2 object including
// a 16-bit header. The blob can be used with tpm2-tools.
func (b *Blobs) Public() (blob []byte, err error) {
if blob, err = toTPM2Tools(b.public); err != nil {
return nil, fmt.Errorf("failed transforming public blob bytes: %w", err)
}
return
}
func toTPM2Tools(blob []byte) ([]byte, error) {
var buf bytes.Buffer
bytesWithHeader := tpmutil.U16Bytes(blob)
if err := bytesWithHeader.TPMMarshal(&buf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (ak *AK) setBlobs(private, public []byte) {
ak.blobs = &Blobs{
private: private,
public: public,
}
}
func (k *Key) setBlobs(private, public []byte) {
k.blobs = &Blobs{
private: private,
public: public,
}
}