-
Notifications
You must be signed in to change notification settings - Fork 7
/
flags.go
82 lines (70 loc) · 1.7 KB
/
flags.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
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
// Copyright 2021 Canonical Ltd.
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.
package flags
import (
"fmt"
"strconv"
"strings"
"github.com/bsiegert/ranges"
"github.com/canonical/go-tpm2"
"github.com/canonical/tcglog-parser"
)
type HashAlgorithmId tpm2.HashAlgorithmId
func (h HashAlgorithmId) MarshalFlag() (string, error) {
switch tpm2.HashAlgorithmId(h) {
case tpm2.HashAlgorithmSHA1:
return "sha1", nil
case tpm2.HashAlgorithmSHA256:
return "sha256", nil
case tpm2.HashAlgorithmSHA384:
return "sha384", nil
case tpm2.HashAlgorithmSHA512:
return "sha512", nil
default:
return "", fmt.Errorf("unrecognized algorithm %v", h)
}
}
func (h *HashAlgorithmId) UnmarshalFlag(value string) error {
switch value {
case "sha1":
*h = HashAlgorithmId(tpm2.HashAlgorithmSHA1)
case "sha256":
*h = HashAlgorithmId(tpm2.HashAlgorithmSHA256)
case "sha384":
*h = HashAlgorithmId(tpm2.HashAlgorithmSHA384)
case "sha512":
*h = HashAlgorithmId(tpm2.HashAlgorithmSHA512)
case "auto":
*h = HashAlgorithmId(tpm2.HashAlgorithmNull)
default:
return fmt.Errorf("unrecognized algorithm \"%s\"", value)
}
return nil
}
type PCRRange []tcglog.PCRIndex
func (r PCRRange) MarshalFlag() (string, error) {
var s []string
for _, p := range r {
s = append(s, strconv.FormatUint(uint64(p), 10))
}
return strings.Join(s, ","), nil
}
func (r *PCRRange) UnmarshalFlag(value string) error {
i, err := ranges.Parse(value)
if err != nil {
return err
}
for _, p := range i {
*r = append(*r, tcglog.PCRIndex(p))
}
return nil
}
func (r *PCRRange) Contains(index tcglog.PCRIndex) bool {
for _, p := range *r {
if p == index {
return true
}
}
return false
}