xref: /aosp_15_r20/external/tink/cc/util/enums.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2017 Google Inc.
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 ///////////////////////////////////////////////////////////////////////////////
16 
17 #ifndef TINK_UTIL_ENUMS_H_
18 #define TINK_UTIL_ENUMS_H_
19 
20 #include "absl/strings/string_view.h"
21 #include "tink/subtle/common_enums.h"
22 #include "tink/util/statusor.h"
23 #include "proto/common.pb.h"
24 #include "proto/ecdsa.pb.h"
25 #include "proto/tink.pb.h"
26 
27 namespace crypto {
28 namespace tink {
29 namespace util {
30 
31 // Helpers for translation of common enums between protocol buffer enums,
32 // their string representation, and common enums used in subtle.
33 class Enums {
34  public:
35   // EllipticCurveType.
36   static google::crypto::tink::EllipticCurveType SubtleToProto(
37       crypto::tink::subtle::EllipticCurveType type);
38 
39   static crypto::tink::subtle::EllipticCurveType ProtoToSubtle(
40       google::crypto::tink::EllipticCurveType type);
41 
42   // EcPointFormat.
43   static google::crypto::tink::EcPointFormat SubtleToProto(
44       crypto::tink::subtle::EcPointFormat format);
45 
46   static crypto::tink::subtle::EcPointFormat ProtoToSubtle(
47       google::crypto::tink::EcPointFormat format);
48 
49   // HashType.
50   static google::crypto::tink::HashType SubtleToProto(
51       crypto::tink::subtle::HashType type);
52 
53   static crypto::tink::subtle::HashType ProtoToSubtle(
54       google::crypto::tink::HashType type);
55 
56   // Returns the length in bytes of the given hash type `hash_type`. Returns
57   // INVALID_ARGUMENT if the algorithm is unsupported.
58   static util::StatusOr<int> HashLength(
59       google::crypto::tink::HashType hash_type);
60 
61   // EcdsaSignatureEncoding.
62   static google::crypto::tink::EcdsaSignatureEncoding SubtleToProto(
63       crypto::tink::subtle::EcdsaSignatureEncoding encoding);
64 
65   static crypto::tink::subtle::EcdsaSignatureEncoding ProtoToSubtle(
66       google::crypto::tink::EcdsaSignatureEncoding encoding);
67 
68   // Printable names for common enums.
69   static const char* KeyStatusName(
70       google::crypto::tink::KeyStatusType key_status_type);
71   static const char* HashName(google::crypto::tink::HashType hash_type);
72   static const char* KeyMaterialName(
73       google::crypto::tink::KeyData::KeyMaterialType key_material_type);
74   static const char* OutputPrefixName(
75       google::crypto::tink::OutputPrefixType output_prefix_type);
76 
77   static google::crypto::tink::KeyStatusType KeyStatus(absl::string_view name);
78   static google::crypto::tink::HashType Hash(absl::string_view name);
79   static google::crypto::tink::KeyData::KeyMaterialType KeyMaterial(
80       absl::string_view name);
81   static google::crypto::tink::OutputPrefixType OutputPrefix(
82       absl::string_view name);
83 };
84 
85 }  // namespace util
86 }  // namespace tink
87 }  // namespace crypto
88 
89 #endif  // TINK_UTIL_ENUMS_H_
90