Debian Codegraph @

keymanager/workload_service/proto_enums.go

raw
1 package workloadservice
2
3 import (
4 "encoding/json"
5 "fmt"
6 "strings"
7
8 keymanager "github.com/google/go-tpm-tools/keymanager/km_common/proto"
9 )
10
11 // KemAlgorithm represents the requested KEM algorithm.
12 type KemAlgorithm int32
13
14 const (
15 // KemAlgorithmUnspecified indicates an unspecified or invalid KEM algorithm.
16 KemAlgorithmUnspecified KemAlgorithm = 0
17 // KemAlgorithmDHKEMX25519HKDFSHA256 specifies the DHKEM(X25519, HKDF-SHA256) algorithm.
18 KemAlgorithmDHKEMX25519HKDFSHA256 KemAlgorithm = 1
19 )
20
21 var (
22 kemAlgorithmToString = map[KemAlgorithm]string{
23 KemAlgorithmUnspecified: "KEM_ALGORITHM_UNSPECIFIED",
24 KemAlgorithmDHKEMX25519HKDFSHA256: "DHKEM_X25519_HKDF_SHA256",
25 }
26 stringToKemAlgorithm = map[string]KemAlgorithm{
27 "KEM_ALGORITHM_UNSPECIFIED": KemAlgorithmUnspecified,
28 "DHKEM_X25519_HKDF_SHA256": KemAlgorithmDHKEMX25519HKDFSHA256,
29 }
30 )
31
32 func (k KemAlgorithm) String() string {
33 if s, ok := kemAlgorithmToString[k]; ok {
34 return s
35 }
36 return fmt.Sprintf("KEM_ALGORITHM_UNKNOWN(%d)", k)
37 }
38
39 // MarshalJSON converts a KemAlgorithm enum value to its JSON string representation.
40 func (k KemAlgorithm) MarshalJSON() ([]byte, error) {
41 return json.Marshal(k.String())
42 }
43
44 // UnmarshalJSON converts a JSON string back into a KemAlgorithm enum value.
45 func (k *KemAlgorithm) UnmarshalJSON(data []byte) error {
46 var s string
47 if err := json.Unmarshal(data, &s); err != nil {
48 return fmt.Errorf("KemAlgorithm must be a string")
49 }
50 if v, ok := stringToKemAlgorithm[s]; ok {
51 *k = v
52 return nil
53 }
54 return fmt.Errorf("unknown KemAlgorithm: %q", s)
55 }
56
57 // Supported algorithms and mechanisms.
58 var (
59 // SupportedKemAlgorithms is the source of truth for supported algorithms.
60 SupportedKemAlgorithms = []KemAlgorithm{
61 KemAlgorithmDHKEMX25519HKDFSHA256,
62 }
63 )
64
65 // IsSupported returns true if the KEM algorithm is supported.
66 func (k KemAlgorithm) IsSupported() bool {
67 for _, supported := range SupportedKemAlgorithms {
68 if k == supported {
69 return true
70 }
71 }
72 return false
73 }
74
75 // SupportedKemAlgorithmsString returns a comma-separated list of supported KEM algorithms.
76 func SupportedKemAlgorithmsString() string {
77 var names []string
78 for _, k := range SupportedKemAlgorithms {
79 names = append(names, k.String())
80 }
81 return strings.Join(names, ", ")
82 }
83
84 // ToHpkeAlgorithm returns the full HPKE suite configuration for this algorithm.
85 func (k KemAlgorithm) ToHpkeAlgorithm() (*keymanager.HpkeAlgorithm, error) {
86 switch k {
87 case KemAlgorithmDHKEMX25519HKDFSHA256:
88 return &keymanager.HpkeAlgorithm{
89 Kem: keymanager.KemAlgorithm_KEM_ALGORITHM_DHKEM_X25519_HKDF_SHA256,
90 Kdf: keymanager.KdfAlgorithm_KDF_ALGORITHM_HKDF_SHA256,
91 Aead: keymanager.AeadAlgorithm_AEAD_ALGORITHM_AES_256_GCM,
92 }, nil
93 default:
94 return nil, fmt.Errorf("unsupported algorithm: %s", k)
95 }
96 }