1 /*
2  * Copyright 2014 The Android Open Source Project
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 #include "android_keymaster_test_utils.h"
18 
19 #include <algorithm>
20 
21 #include <openssl/rand.h>
22 
23 #include <keymaster/android_keymaster_messages.h>
24 #include <keymaster/android_keymaster_utils.h>
25 
26 using std::copy_if;
27 using std::find_if;
28 using std::is_permutation;
29 using std::ostream;
30 using std::string;
31 using std::vector;
32 
33 #ifndef KEYMASTER_NAME_TAGS
34 #error Keymaster test code requires that KEYMASTER_NAME_TAGS is defined
35 #endif
36 
operator <<(std::ostream & os,const keymaster_key_param_t & param)37 std::ostream& operator<<(std::ostream& os, const keymaster_key_param_t& param) {
38     os << "Tag: " << keymaster::StringifyTag(param.tag);
39     switch (keymaster_tag_get_type(param.tag)) {
40     case KM_INVALID:
41         os << " Invalid";
42         break;
43     case KM_UINT_REP:
44         os << " (Rep)";
45         /* Falls through */
46         [[fallthrough]];
47     case KM_UINT:
48         os << " Int: " << param.integer;
49         break;
50     case KM_ENUM_REP:
51         os << " (Rep)";
52         /* Falls through */
53         [[fallthrough]];
54     case KM_ENUM:
55         os << " Enum: " << param.enumerated;
56         break;
57     case KM_ULONG_REP:
58         os << " (Rep)";
59         /* Falls through */
60         [[fallthrough]];
61     case KM_ULONG:
62         os << " Long: " << param.long_integer;
63         break;
64     case KM_DATE:
65         os << " Date: " << param.date_time;
66         break;
67     case KM_BOOL:
68         os << " Bool: " << param.boolean;
69         break;
70     case KM_BIGNUM:
71         os << " Bignum: ";
72         if (!param.blob.data)
73             os << "(null)";
74         else
75             for (size_t i = 0; i < param.blob.data_length; ++i)
76                 os << std::hex << std::setw(2) << static_cast<int>(param.blob.data[i]) << std::dec;
77         break;
78     case KM_BYTES:
79         os << " Bytes: ";
80         if (!param.blob.data)
81             os << "(null)";
82         else
83             for (size_t i = 0; i < param.blob.data_length; ++i)
84                 os << std::hex << std::setw(2) << static_cast<int>(param.blob.data[i]) << std::dec;
85         break;
86     }
87     return os;
88 }
89 
operator ==(const keymaster_key_param_t & a,const keymaster_key_param_t & b)90 bool operator==(const keymaster_key_param_t& a, const keymaster_key_param_t& b) {
91     if (a.tag != b.tag) {
92         return false;
93     }
94 
95     switch (keymaster_tag_get_type(a.tag)) {
96     case KM_INVALID:
97         return true;
98     case KM_UINT_REP:
99     case KM_UINT:
100         return a.integer == b.integer;
101     case KM_ENUM_REP:
102     case KM_ENUM:
103         return a.enumerated == b.enumerated;
104     case KM_ULONG:
105     case KM_ULONG_REP:
106         return a.long_integer == b.long_integer;
107     case KM_DATE:
108         return a.date_time == b.date_time;
109     case KM_BOOL:
110         return a.boolean == b.boolean;
111     case KM_BIGNUM:
112     case KM_BYTES:
113         if ((a.blob.data == nullptr || b.blob.data == nullptr) && a.blob.data != b.blob.data)
114             return false;
115         return a.blob.data_length == b.blob.data_length &&
116                (memcmp(a.blob.data, b.blob.data, a.blob.data_length) == 0);
117     }
118 
119     return false;
120 }
121 
122 static char hex_value[256] = {
123     0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,
124     0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,
125     0, 1,  2,  3,  4,  5,  6,  7, 8, 9, 0, 0, 0, 0, 0, 0,  // '0'..'9'
126     0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0,  // 'A'..'F'
127     0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15, 0,
128     0, 0,  0,  0,  0,  0,  0,  0,  // 'a'..'f'
129     0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,
130     0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,
131     0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,
132     0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,
133     0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0,
134     0, 0,  0,  0,  0,  0,  0,  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0,  0,  0,  0};
135 
hex2str(string a)136 string hex2str(string a) {
137     string b;
138     size_t num = a.size() / 2;
139     b.resize(num);
140     for (size_t i = 0; i < num; i++) {
141         b[i] = (hex_value[a[i * 2] & 0xFF] << 4) + (hex_value[a[i * 2 + 1] & 0xFF]);
142     }
143     return b;
144 }
145 
146 namespace keymaster {
147 
operator ==(const AuthorizationSet & a,const AuthorizationSet & b)148 bool operator==(const AuthorizationSet& a, const AuthorizationSet& b) {
149     if (a.size() != b.size()) return false;
150 
151     for (size_t i = 0; i < a.size(); ++i)
152         if (!(a[i] == b[i])) return false;
153     return true;
154 }
155 
operator !=(const AuthorizationSet & a,const AuthorizationSet & b)156 bool operator!=(const AuthorizationSet& a, const AuthorizationSet& b) {
157     return !(a == b);
158 }
159 
operator <<(std::ostream & os,const AuthorizationSet & set)160 std::ostream& operator<<(std::ostream& os, const AuthorizationSet& set) {
161     if (set.size() == 0)
162         os << "(Empty)" << std::endl;
163     else {
164         os << "\n";
165         for (size_t i = 0; i < set.size(); ++i)
166             os << set[i] << std::endl;
167     }
168     return os;
169 }
170 
171 }  // namespace keymaster
172