1 /*############################################################################
2 # Copyright 2016-2017 Intel Corporation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 ############################################################################*/
16
17 /*!
18 * \file
19 *
20 * \brief Conversion utilities implementation.
21 *
22 */
23
24 #include "util/convutil.h"
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "util/envutil.h"
29
30 const char* hash_alg_to_string[] = {"SHA-256", "SHA-384", "SHA-512",
31 "SHA-512/256", "SHA3/256", "SHA3/384",
32 "SHA3/512"};
33
34 #define COUNT_OF(A) (sizeof(A) / sizeof((A)[0]))
35
HashAlgToString(HashAlg alg)36 char const* HashAlgToString(HashAlg alg) {
37 if ((int)alg < 0 || (size_t)alg >= COUNT_OF(hash_alg_to_string))
38 return "unknown";
39 return hash_alg_to_string[alg];
40 }
41
StringToHashAlg(char const * str,HashAlg * alg)42 bool StringToHashAlg(char const* str, HashAlg* alg) {
43 size_t i;
44 if (!alg || !str) return false;
45 for (i = 0; i < COUNT_OF(hash_alg_to_string); i++) {
46 if (0 == strcmp(str, hash_alg_to_string[i])) {
47 *alg = (HashAlg)i;
48 return true;
49 }
50 }
51 return false;
52 }
53
54 const char* epid_version_to_string[kNumEpidVersions] = {"1", "2"};
55
EpidVersionToString(EpidVersion version)56 char const* EpidVersionToString(EpidVersion version) {
57 if ((int)version < 0 || (size_t)version >= COUNT_OF(epid_version_to_string))
58 return "unknown";
59 return epid_version_to_string[version];
60 }
61
StringToEpidVersion(char const * str,EpidVersion * version)62 bool StringToEpidVersion(char const* str, EpidVersion* version) {
63 size_t i;
64 if (!version || !str) return false;
65 for (i = 0; i < COUNT_OF(epid_version_to_string); i++) {
66 if (0 == strcmp(str, epid_version_to_string[i])) {
67 *version = (EpidVersion)i;
68 return true;
69 }
70 }
71 log_error("epid version \"%s\" is unknown", str);
72 return false;
73 }
74
75 const char* epid_file_type_to_string[kNumFileTypes] = {
76 "IssuingCaPubKey", "GroupPubKey", "PrivRl", "SigRl", "GroupRl"};
77
EpidFileTypeToString(EpidFileType type)78 char const* EpidFileTypeToString(EpidFileType type) {
79 if ((int)type < 0 || (size_t)type >= COUNT_OF(epid_file_type_to_string))
80 return "unknown";
81 return epid_file_type_to_string[type];
82 }
83
StringToEpidFileType(char const * str,EpidFileType * type)84 bool StringToEpidFileType(char const* str, EpidFileType* type) {
85 size_t i;
86 if (!type || !str) return false;
87 for (i = 0; i < COUNT_OF(epid_file_type_to_string); i++) {
88 if (0 == strcmp(str, epid_file_type_to_string[i])) {
89 *type = (EpidFileType)i;
90 return true;
91 }
92 }
93 log_error("epid file type \"%s\" is unknown", str);
94 return false;
95 }
96
SetMemberParams(BitSupplier rnd_func,void * rnd_param,FpElemStr * f,MemberParams * params)97 void SetMemberParams(BitSupplier rnd_func, void* rnd_param, FpElemStr* f,
98 MemberParams* params) {
99 #ifdef TPM_TSS
100 (void)rnd_func;
101 (void)rnd_param;
102 params->f = f;
103 #else
104 params->rnd_func = rnd_func;
105 params->rnd_param = rnd_param;
106 params->f = f;
107 #endif
108 }
109