1 /*
2 * Copyright (C) 2021 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 #define TLOG_TAG "hwaes_unittest"
18
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include <lib/hwaes/hwaes.h>
23 #include <lib/hwkey/hwkey.h>
24 #include <sys/auxv.h>
25 #include <sys/mman.h>
26 #include <trusty/memref.h>
27 #include <trusty_unittest.h>
28 #include <uapi/err.h>
29
30 #define AUX_PAGE_SIZE() getauxval(AT_PAGESZ)
31
32 #if SIMULATION
33 #define MAX_TRY_TIMES 3
34 #else
35 #define MAX_TRY_TIMES 1000
36 #endif
37
38 #define UNUSED_HWAES_ERROR_CODE HWAES_NO_ERROR
39
40 #define HWAES_GCM_IV_SIZE 12
41
42 #if WITH_HWCRYPTO_UNITTEST
43 #define DISABLED_WITHOUT_HWCRYPTO_UNITTEST(name) name
44 #else
45 #define DISABLED_WITHOUT_HWCRYPTO_UNITTEST(name) DISABLED_##name
46 #endif
47
48 /**
49 * struct hwaes_iov - an wrapper of an array of iovec.
50 * @iovs: array of iovec.
51 * @num_iov: number of iovec.
52 * @total_len: total length of the tipc message.
53 */
54 struct hwaes_iov {
55 struct iovec iov[TIPC_MAX_MSG_PARTS];
56 size_t num_iov;
57 size_t total_len;
58 };
59
60 /**
61 * struct hwaes_shm - an wrapper of an array of shared memory handles.
62 * @handles: array of shared memory handles.
63 * @num_handles: number of shared memory handles.
64 */
65 struct hwaes_shm {
66 handle_t handles[HWAES_MAX_NUM_HANDLES];
67 size_t num_handles;
68 };
69
70 struct test_vector {
71 uint32_t mode;
72 struct hwcrypt_arg_in key;
73 struct hwcrypt_arg_in iv;
74 struct hwcrypt_arg_in aad;
75 struct hwcrypt_arg_in tag;
76 struct hwcrypt_arg_in plaintext;
77 struct hwcrypt_arg_in ciphertext;
78 };
79
80 /*
81 * Test vectors are from boringssl cipher_tests.txt which are in turn taken from
82 * the GCM spec at
83 * http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-spec.pdf
84 *
85 * We are intentionally not testing non-standard IV lengths because we don't
86 * support this (yet).
87 */
88 /* AES 128 GCM */
89 static const uint8_t gcm_test1_key[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
90 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91 0x00, 0x00, 0x00, 0x00};
92 static const uint8_t gcm_test1_iv[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
93 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
94 static const uint8_t gcm_test1_plaintext[] = {
95 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
96 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
97 static const uint8_t gcm_test1_ciphertext[] = {
98 0x03, 0x88, 0xda, 0xce, 0x60, 0xb6, 0xa3, 0x92,
99 0xf3, 0x28, 0xc2, 0xb9, 0x71, 0xb2, 0xfe, 0x78};
100 static const uint8_t gcm_test1_aad[] = {};
101 static const uint8_t gcm_test1_tag[] = {0xab, 0x6e, 0x47, 0xd4, 0x2c, 0xec,
102 0x13, 0xbd, 0xf5, 0x3a, 0x67, 0xb2,
103 0x12, 0x57, 0xbd, 0xdf};
104
105 static const uint8_t gcm_test2_key[] = {0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65,
106 0x73, 0x1c, 0x6d, 0x6a, 0x8f, 0x94,
107 0x67, 0x30, 0x83, 0x08};
108 static const uint8_t gcm_test2_iv[] = {0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce,
109 0xdb, 0xad, 0xde, 0xca, 0xf8, 0x88};
110 static const uint8_t gcm_test2_plaintext[] = {
111 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5, 0xa5, 0x59, 0x09,
112 0xc5, 0xaf, 0xf5, 0x26, 0x9a, 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34,
113 0xf7, 0xda, 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72, 0x1c,
114 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53, 0x2f, 0xcf, 0x0e, 0x24,
115 0x49, 0xa6, 0xb5, 0x25, 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6,
116 0x57, 0xba, 0x63, 0x7b, 0x39, 0x1a, 0xaf, 0xd2, 0x55};
117 static const uint8_t gcm_test2_ciphertext[] = {
118 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24, 0x4b, 0x72, 0x21,
119 0xb7, 0x84, 0xd0, 0xd4, 0x9c, 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02,
120 0xa4, 0xe0, 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e, 0x21,
121 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c, 0x7d, 0x8f, 0x6a, 0x5a,
122 0xac, 0x84, 0xaa, 0x05, 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac,
123 0x97, 0x3d, 0x58, 0xe0, 0x91, 0x47, 0x3f, 0x59, 0x85};
124 static const uint8_t gcm_test2_aad[] = {};
125 static const uint8_t gcm_test2_tag[] = {0x4d, 0x5c, 0x2a, 0xf3, 0x27, 0xcd,
126 0x64, 0xa6, 0x2c, 0xf3, 0x5a, 0xbd,
127 0x2b, 0xa6, 0xfa, 0xb4};
128
129 static const uint8_t gcm_test3_key[] = {0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65,
130 0x73, 0x1c, 0x6d, 0x6a, 0x8f, 0x94,
131 0x67, 0x30, 0x83, 0x08};
132 static const uint8_t gcm_test3_iv[] = {0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce,
133 0xdb, 0xad, 0xde, 0xca, 0xf8, 0x88};
134 static const uint8_t gcm_test3_plaintext[] = {
135 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5, 0xa5, 0x59, 0x09, 0xc5,
136 0xaf, 0xf5, 0x26, 0x9a, 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
137 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72, 0x1c, 0x3c, 0x0c, 0x95,
138 0x95, 0x68, 0x09, 0x53, 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
139 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57, 0xba, 0x63, 0x7b, 0x39};
140 static const uint8_t gcm_test3_ciphertext[] = {
141 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24, 0x4b, 0x72, 0x21, 0xb7,
142 0x84, 0xd0, 0xd4, 0x9c, 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0,
143 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e, 0x21, 0xd5, 0x14, 0xb2,
144 0x54, 0x66, 0x93, 0x1c, 0x7d, 0x8f, 0x6a, 0x5a, 0xac, 0x84, 0xaa, 0x05,
145 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, 0x97, 0x3d, 0x58, 0xe0, 0x91};
146 static const uint8_t gcm_test3_aad[] = {
147 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed,
148 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xab, 0xad, 0xda, 0xd2};
149 static const uint8_t gcm_test3_tag[] = {0x5b, 0xc9, 0x4f, 0xbc, 0x32, 0x21,
150 0xa5, 0xdb, 0x94, 0xfa, 0xe9, 0x5a,
151 0xe7, 0x12, 0x1a, 0x47};
152
153 /* AES 256 GCM */
154 static const uint8_t gcm_test4_key[] = {
155 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
156 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
157 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
158 static const uint8_t gcm_test4_iv[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
159 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
160 static const uint8_t gcm_test4_plaintext[] = {
161 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
162 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
163 static const uint8_t gcm_test4_ciphertext[] = {
164 0xce, 0xa7, 0x40, 0x3d, 0x4d, 0x60, 0x6b, 0x6e,
165 0x07, 0x4e, 0xc5, 0xd3, 0xba, 0xf3, 0x9d, 0x18};
166 static const uint8_t gcm_test4_aad[] = {};
167 static const uint8_t gcm_test4_tag[] = {0xd0, 0xd1, 0xc8, 0xa7, 0x99, 0x99,
168 0x6b, 0xf0, 0x26, 0x5b, 0x98, 0xb5,
169 0xd4, 0x8a, 0xb9, 0x19};
170
171 static const uint8_t gcm_test5_key[] = {
172 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c, 0x6d, 0x6a, 0x8f,
173 0x94, 0x67, 0x30, 0x83, 0x08, 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65,
174 0x73, 0x1c, 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08};
175 static const uint8_t gcm_test5_iv[] = {0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce,
176 0xdb, 0xad, 0xde, 0xca, 0xf8, 0x88};
177 static const uint8_t gcm_test5_plaintext[] = {
178 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5, 0xa5, 0x59, 0x09,
179 0xc5, 0xaf, 0xf5, 0x26, 0x9a, 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34,
180 0xf7, 0xda, 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72, 0x1c,
181 0x3c, 0x0c, 0x95, 0x95, 0x68, 0x09, 0x53, 0x2f, 0xcf, 0x0e, 0x24,
182 0x49, 0xa6, 0xb5, 0x25, 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6,
183 0x57, 0xba, 0x63, 0x7b, 0x39, 0x1a, 0xaf, 0xd2, 0x55};
184 static const uint8_t gcm_test5_ciphertext[] = {
185 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07, 0xf4, 0x7f, 0x37,
186 0xa3, 0x2a, 0x84, 0x42, 0x7d, 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5,
187 0xc0, 0xc9, 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa, 0x8c,
188 0xb0, 0x8e, 0x48, 0x59, 0x0d, 0xbb, 0x3d, 0xa7, 0xb0, 0x8b, 0x10,
189 0x56, 0x82, 0x88, 0x38, 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a,
190 0x0a, 0xbc, 0xc9, 0xf6, 0x62, 0x89, 0x80, 0x15, 0xad};
191 static const uint8_t gcm_test5_aad[] = {};
192 static const uint8_t gcm_test5_tag[] = {0xb0, 0x94, 0xda, 0xc5, 0xd9, 0x34,
193 0x71, 0xbd, 0xec, 0x1a, 0x50, 0x22,
194 0x70, 0xe3, 0xcc, 0x6c};
195
196 static const uint8_t gcm_test6_key[] = {
197 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65, 0x73, 0x1c, 0x6d, 0x6a, 0x8f,
198 0x94, 0x67, 0x30, 0x83, 0x08, 0xfe, 0xff, 0xe9, 0x92, 0x86, 0x65,
199 0x73, 0x1c, 0x6d, 0x6a, 0x8f, 0x94, 0x67, 0x30, 0x83, 0x08};
200 static const uint8_t gcm_test6_iv[] = {0xca, 0xfe, 0xba, 0xbe, 0xfa, 0xce,
201 0xdb, 0xad, 0xde, 0xca, 0xf8, 0x88};
202 static const uint8_t gcm_test6_plaintext[] = {
203 0xd9, 0x31, 0x32, 0x25, 0xf8, 0x84, 0x06, 0xe5, 0xa5, 0x59, 0x09, 0xc5,
204 0xaf, 0xf5, 0x26, 0x9a, 0x86, 0xa7, 0xa9, 0x53, 0x15, 0x34, 0xf7, 0xda,
205 0x2e, 0x4c, 0x30, 0x3d, 0x8a, 0x31, 0x8a, 0x72, 0x1c, 0x3c, 0x0c, 0x95,
206 0x95, 0x68, 0x09, 0x53, 0x2f, 0xcf, 0x0e, 0x24, 0x49, 0xa6, 0xb5, 0x25,
207 0xb1, 0x6a, 0xed, 0xf5, 0xaa, 0x0d, 0xe6, 0x57, 0xba, 0x63, 0x7b, 0x39};
208 static const uint8_t gcm_test6_ciphertext[] = {
209 0x52, 0x2d, 0xc1, 0xf0, 0x99, 0x56, 0x7d, 0x07, 0xf4, 0x7f, 0x37, 0xa3,
210 0x2a, 0x84, 0x42, 0x7d, 0x64, 0x3a, 0x8c, 0xdc, 0xbf, 0xe5, 0xc0, 0xc9,
211 0x75, 0x98, 0xa2, 0xbd, 0x25, 0x55, 0xd1, 0xaa, 0x8c, 0xb0, 0x8e, 0x48,
212 0x59, 0x0d, 0xbb, 0x3d, 0xa7, 0xb0, 0x8b, 0x10, 0x56, 0x82, 0x88, 0x38,
213 0xc5, 0xf6, 0x1e, 0x63, 0x93, 0xba, 0x7a, 0x0a, 0xbc, 0xc9, 0xf6, 0x62};
214 static const uint8_t gcm_test6_aad[] = {
215 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed,
216 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xab, 0xad, 0xda, 0xd2};
217 static const uint8_t gcm_test6_tag[] = {0x76, 0xfc, 0x6e, 0xce, 0x0f, 0x4e,
218 0x17, 0x68, 0xcd, 0xdf, 0x88, 0x53,
219 0xbb, 0x2d, 0x55, 0x1b};
220
221 /* clang-format off */
222 #define TV_DATA(name_, field_) \
223 .field_ = {.data_ptr = &name_##_##field_, .len = sizeof(name_##_##field_)}
224
225 #define TV(mode_, name_) \
226 { \
227 .mode = mode_, \
228 TV_DATA(name_, key), \
229 TV_DATA(name_, iv), \
230 TV_DATA(name_, aad), \
231 TV_DATA(name_, tag), \
232 TV_DATA(name_, ciphertext), \
233 TV_DATA(name_, plaintext) \
234 }
235 /* clang-format on */
236
237 static const struct test_vector vectors[] = {
238 TV(HWAES_GCM_MODE, gcm_test1), TV(HWAES_GCM_MODE, gcm_test2),
239 TV(HWAES_GCM_MODE, gcm_test3), TV(HWAES_GCM_MODE, gcm_test4),
240 TV(HWAES_GCM_MODE, gcm_test5), TV(HWAES_GCM_MODE, gcm_test6),
241 };
242
parse_vector(const struct test_vector * vector,struct hwcrypt_shm_hd * shm_handle,struct hwcrypt_args * args,int encrypt)243 static void parse_vector(const struct test_vector* vector,
244 struct hwcrypt_shm_hd* shm_handle,
245 struct hwcrypt_args* args,
246 int encrypt) {
247 args->key_type = HWAES_PLAINTEXT_KEY;
248 args->padding = HWAES_NO_PADDING;
249 args->mode = vector->mode;
250
251 args->key = vector->key;
252 args->iv = vector->iv;
253 args->aad = vector->aad;
254 if (encrypt) {
255 args->text_in = vector->plaintext;
256 } else {
257 args->text_in = vector->ciphertext;
258 args->tag_in = vector->tag;
259 }
260
261 uint8_t* base = (uint8_t*)shm_handle->base;
262
263 args->text_out.data_ptr = base;
264 args->text_out.len = args->text_in.len;
265 args->text_out.shm_hd_ptr = shm_handle;
266
267 if (encrypt && vector->tag.len > 0) {
268 args->tag_out.data_ptr = base + args->text_out.len;
269 args->tag_out.len = vector->tag.len;
270 args->tag_out.shm_hd_ptr = shm_handle;
271 }
272 }
273
274 static const uint8_t hwaes_key[32];
275 static const uint8_t hwaes_iv[16];
276 static const uint8_t hwaes_cbc_plaintext[] = {
277 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
278 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
279 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
280 static const uint8_t hwaes_cbc_ciphertext[] = {
281 0xdc, 0x95, 0xc0, 0x78, 0xa2, 0x40, 0x89, 0x89, 0xad, 0x48, 0xa2,
282 0x14, 0x92, 0x84, 0x20, 0x87, 0x08, 0xc3, 0x74, 0x84, 0x8c, 0x22,
283 0x82, 0x33, 0xc2, 0xb3, 0x4f, 0x33, 0x2b, 0xd2, 0xe9, 0xd3};
284
285 typedef struct hwaes {
286 hwaes_session_t hwaes_session;
287 handle_t memref;
288 void* shm_base;
289 size_t shm_len;
290 struct hwcrypt_shm_hd shm_hd;
291 struct hwcrypt_args args_encrypt;
292 struct hwcrypt_args args_decrypt;
293 struct hwaes_req req_hdr;
294 struct hwaes_aes_req cmd_hdr;
295 struct hwaes_shm_desc shm_descs[HWAES_MAX_NUM_HANDLES];
296 struct hwaes_iov req_iov;
297 struct hwaes_shm req_shm;
298 } hwaes_t;
299
make_bad_request(handle_t channel,struct hwaes_iov * req_iov,struct hwaes_shm * req_shm,bool expect_reply,uint32_t expect_error)300 static void make_bad_request(handle_t channel,
301 struct hwaes_iov* req_iov,
302 struct hwaes_shm* req_shm,
303 bool expect_reply,
304 uint32_t expect_error) {
305 struct uevent event;
306 ipc_msg_info_t msg_inf;
307 bool got_msg = false;
308
309 ipc_msg_t req_msg = {
310 .iov = req_iov->iov,
311 .num_iov = req_iov->num_iov,
312 .handles = req_shm->handles,
313 .num_handles = req_shm->num_handles,
314 };
315
316 int rc;
317 rc = send_msg(channel, &req_msg);
318 ASSERT_EQ((size_t)rc, req_iov->total_len);
319
320 rc = wait(channel, &event, INFINITE_TIME);
321 ASSERT_EQ(rc, NO_ERROR);
322
323 if (expect_reply) {
324 ASSERT_NE(event.event & IPC_HANDLE_POLL_MSG, 0);
325 } else {
326 ASSERT_EQ(event.event, IPC_HANDLE_POLL_HUP);
327 return;
328 }
329
330 rc = get_msg(channel, &msg_inf);
331 ASSERT_EQ(rc, NO_ERROR);
332
333 got_msg = true;
334 ASSERT_EQ(msg_inf.len, sizeof(struct hwaes_resp));
335
336 struct hwaes_resp resp_hdr = {0};
337 struct iovec resp_iov = {
338 .iov_base = (void*)&resp_hdr,
339 .iov_len = sizeof(resp_hdr),
340 };
341 ipc_msg_t resp_msg = {
342 .iov = &resp_iov,
343 .num_iov = 1,
344 .handles = NULL,
345 .num_handles = 0,
346 };
347 rc = read_msg(channel, msg_inf.id, 0, &resp_msg);
348 ASSERT_EQ((size_t)rc, msg_inf.len);
349
350 struct hwaes_req* req_hdr = (struct hwaes_req*)req_iov->iov[0].iov_base;
351 ASSERT_EQ(resp_hdr.cmd, req_hdr->cmd | HWAES_RESP_BIT);
352
353 put_msg(channel, msg_inf.id);
354 EXPECT_EQ(expect_error, resp_hdr.result);
355 return;
356
357 test_abort:
358 if (got_msg) {
359 put_msg(channel, msg_inf.id);
360 }
361 return;
362 }
363
TEST_F_SETUP(hwaes)364 TEST_F_SETUP(hwaes) {
365 int rc;
366 void* shm_base;
367 size_t shm_len = AUX_PAGE_SIZE();
368 _state->hwaes_session = INVALID_IPC_HANDLE;
369 _state->memref = INVALID_IPC_HANDLE;
370 _state->shm_base = NULL;
371
372 rc = hwaes_open(&_state->hwaes_session);
373 ASSERT_EQ(rc, 0);
374
375 shm_base = memalign(AUX_PAGE_SIZE(), shm_len);
376 ASSERT_NE(NULL, shm_base, "fail to allocate shared memory");
377
378 rc = memref_create(shm_base, shm_len, PROT_READ | PROT_WRITE);
379 ASSERT_GE(rc, 0);
380 _state->memref = (handle_t)rc;
381 _state->shm_base = shm_base;
382 _state->shm_len = shm_len;
383 memset(_state->shm_base, 0, _state->shm_len);
384 memcpy(_state->shm_base, hwaes_cbc_plaintext, sizeof(hwaes_cbc_plaintext));
385
386 _state->shm_hd = (struct hwcrypt_shm_hd){
387 .handle = _state->memref,
388 .base = _state->shm_base,
389 .size = _state->shm_len,
390 };
391
392 _state->args_encrypt = (struct hwcrypt_args){
393 .key =
394 {
395 .data_ptr = hwaes_key,
396 .len = sizeof(hwaes_key),
397 },
398 .iv =
399 {
400 .data_ptr = hwaes_iv,
401 .len = sizeof(hwaes_iv),
402 },
403 .text_in =
404 {
405 .data_ptr = _state->shm_base,
406 .len = sizeof(hwaes_cbc_plaintext),
407 .shm_hd_ptr = &_state->shm_hd,
408 },
409 .text_out =
410 {
411 .data_ptr = _state->shm_base,
412 .len = sizeof(hwaes_cbc_ciphertext),
413 .shm_hd_ptr = &_state->shm_hd,
414 },
415 .key_type = HWAES_PLAINTEXT_KEY,
416 .padding = HWAES_NO_PADDING,
417 .mode = HWAES_CBC_MODE,
418 };
419
420 _state->args_decrypt = (struct hwcrypt_args){
421 .key =
422 {
423 .data_ptr = hwaes_key,
424 .len = sizeof(hwaes_key),
425 },
426 .iv =
427 {
428 .data_ptr = hwaes_iv,
429 .len = sizeof(hwaes_iv),
430 },
431 .text_in =
432 {
433 .data_ptr = _state->shm_base,
434 .len = sizeof(hwaes_cbc_ciphertext),
435 .shm_hd_ptr = &_state->shm_hd,
436 },
437 .text_out =
438 {
439 .data_ptr = _state->shm_base,
440 .len = sizeof(hwaes_cbc_plaintext),
441 .shm_hd_ptr = &_state->shm_hd,
442 },
443 .key_type = HWAES_PLAINTEXT_KEY,
444 .padding = HWAES_NO_PADDING,
445 .mode = HWAES_CBC_MODE,
446 };
447
448 _state->req_hdr = (struct hwaes_req){
449 .cmd = HWAES_AES,
450 };
451 _state->cmd_hdr = (struct hwaes_aes_req){
452 .key =
453 (struct hwaes_data_desc){
454 .len = sizeof(hwaes_key),
455 .shm_idx = 0,
456 },
457 .num_handles = 1,
458 };
459 _state->shm_descs[0] = (struct hwaes_shm_desc){.size = _state->shm_len};
460 _state->req_iov = (struct hwaes_iov){
461 .iov =
462 {
463 {&_state->req_hdr, sizeof(_state->req_hdr)},
464 {&_state->cmd_hdr, sizeof(_state->cmd_hdr)},
465 {&_state->shm_descs, sizeof(struct hwaes_shm_desc)},
466 },
467 .num_iov = 3,
468 .total_len = sizeof(_state->req_hdr) + sizeof(_state->cmd_hdr) +
469 sizeof(struct hwaes_shm_desc),
470 };
471 _state->req_shm = (struct hwaes_shm){
472 .handles = {_state->memref},
473 .num_handles = 1,
474 };
475
476 test_abort:;
477 }
478
TEST_F_TEARDOWN(hwaes)479 TEST_F_TEARDOWN(hwaes) {
480 close(_state->hwaes_session);
481 close(_state->memref);
482 free(_state->shm_base);
483 }
484
TEST_F(hwaes,GenericInvalidSession)485 TEST_F(hwaes, GenericInvalidSession) {
486 hwaes_session_t invalid = INVALID_IPC_HANDLE;
487 struct hwcrypt_args args = {};
488
489 // should fail immediately
490 int rc = hwaes_encrypt(invalid, &args);
491
492 EXPECT_EQ(ERR_BAD_HANDLE, rc, "generic - bad handle");
493 }
494
TEST_F(hwaes,RequestHeaderReservedNotZero)495 TEST_F(hwaes, RequestHeaderReservedNotZero) {
496 _state->req_hdr.reserved = 1U;
497 make_bad_request(_state->hwaes_session, &_state->req_iov, &_state->req_shm,
498 false, UNUSED_HWAES_ERROR_CODE);
499 }
500
TEST_F(hwaes,CommandUnsupported)501 TEST_F(hwaes, CommandUnsupported) {
502 _state->req_hdr.cmd = 0U;
503 make_bad_request(_state->hwaes_session, &_state->req_iov, &_state->req_shm,
504 true, HWAES_ERR_NOT_IMPLEMENTED);
505 }
506
TEST_F(hwaes,CommandHeaderReservedNotZero)507 TEST_F(hwaes, CommandHeaderReservedNotZero) {
508 _state->cmd_hdr.reserved = 1U;
509 make_bad_request(_state->hwaes_session, &_state->req_iov, &_state->req_shm,
510 false, UNUSED_HWAES_ERROR_CODE);
511 }
512
TEST_F(hwaes,SharedMemoryHandlesNumberConflict)513 TEST_F(hwaes, SharedMemoryHandlesNumberConflict) {
514 _state->cmd_hdr.num_handles += 1;
515 make_bad_request(_state->hwaes_session, &_state->req_iov, &_state->req_shm,
516 false, UNUSED_HWAES_ERROR_CODE);
517 }
518
TEST_F(hwaes,SharedMemoryDescriptorReservedNotZero)519 TEST_F(hwaes, SharedMemoryDescriptorReservedNotZero) {
520 _state->shm_descs[0].reserved = 1U;
521 make_bad_request(_state->hwaes_session, &_state->req_iov, &_state->req_shm,
522 true, HWAES_ERR_IO);
523 }
524
TEST_F(hwaes,SharedMemoryDescriptorWrongWriteFlag)525 TEST_F(hwaes, SharedMemoryDescriptorWrongWriteFlag) {
526 _state->shm_descs[0].write = 2U;
527 make_bad_request(_state->hwaes_session, &_state->req_iov, &_state->req_shm,
528 true, HWAES_ERR_IO);
529 }
530
TEST_F(hwaes,SharedMemoryDescriptorBadSize)531 TEST_F(hwaes, SharedMemoryDescriptorBadSize) {
532 /* size is not page aligned */
533 _state->shm_descs[0].size = 4;
534 make_bad_request(_state->hwaes_session, &_state->req_iov, &_state->req_shm,
535 true, HWAES_ERR_INVALID_ARGS);
536 }
537
TEST_F(hwaes,DataDescriptorReservedNotZero)538 TEST_F(hwaes, DataDescriptorReservedNotZero) {
539 _state->cmd_hdr.key.reserved = 1U;
540 make_bad_request(_state->hwaes_session, &_state->req_iov, &_state->req_shm,
541 true, HWAES_ERR_IO);
542 }
543
TEST_F(hwaes,DataDescriptorBadLength)544 TEST_F(hwaes, DataDescriptorBadLength) {
545 _state->cmd_hdr.key.len = _state->shm_len + 1ULL;
546 make_bad_request(_state->hwaes_session, &_state->req_iov, &_state->req_shm,
547 true, HWAES_ERR_INVALID_ARGS);
548 }
549
TEST_F(hwaes,DataDescriptorBadSharedMemoryHandleIndex)550 TEST_F(hwaes, DataDescriptorBadSharedMemoryHandleIndex) {
551 _state->cmd_hdr.key.shm_idx = 4;
552 make_bad_request(_state->hwaes_session, &_state->req_iov, &_state->req_shm,
553 true, HWAES_ERR_IO);
554 }
TEST_F(hwaes,InvalidSharedMemoryHandle)555 TEST_F(hwaes, InvalidSharedMemoryHandle) {
556 struct hwcrypt_shm_hd bad_shm_hd = {
557 .handle = INVALID_IPC_HANDLE,
558 .base = _state->shm_base,
559 .size = _state->shm_len,
560 };
561
562 _state->args_encrypt.text_in.shm_hd_ptr = &bad_shm_hd;
563
564 int rc = hwaes_encrypt(_state->hwaes_session, &_state->args_encrypt);
565 EXPECT_EQ(ERR_BAD_HANDLE, rc, "expect bad handle error");
566 }
567
TEST_F(hwaes,BadSharedMemorySize)568 TEST_F(hwaes, BadSharedMemorySize) {
569 struct hwcrypt_shm_hd bad_shm_hd = {
570 .handle = _state->memref,
571 .base = _state->shm_base,
572 .size = 0,
573 };
574
575 _state->args_encrypt.text_in.shm_hd_ptr = &bad_shm_hd;
576
577 int rc = hwaes_encrypt(_state->hwaes_session, &_state->args_encrypt);
578 EXPECT_EQ(ERR_INVALID_ARGS, rc, "expect bad length error");
579 }
580
TEST_F(hwaes,KeyArgumentNotSetEncrypt)581 TEST_F(hwaes, KeyArgumentNotSetEncrypt) {
582 _state->args_encrypt.key.len = 0;
583
584 int rc = hwaes_encrypt(_state->hwaes_session, &_state->args_encrypt);
585 EXPECT_EQ(ERR_INVALID_ARGS, rc, "expect invalid_args error");
586 }
587
TEST_F(hwaes,IVArgumentNotSetEncrypt)588 TEST_F(hwaes, IVArgumentNotSetEncrypt) {
589 _state->args_encrypt.iv.len = 0;
590
591 int rc = hwaes_encrypt(_state->hwaes_session, &_state->args_encrypt);
592 EXPECT_EQ(ERR_INVALID_ARGS, rc, "expect invalid_args error");
593 }
594
TEST_F(hwaes,TextInArgumentNotSetEncrypt)595 TEST_F(hwaes, TextInArgumentNotSetEncrypt) {
596 _state->args_encrypt.text_in.len = 0;
597
598 int rc = hwaes_encrypt(_state->hwaes_session, &_state->args_encrypt);
599 EXPECT_EQ(ERR_INVALID_ARGS, rc, "expect invalid_args error");
600 }
601
TEST_F(hwaes,TextOutArgumentNotSetEncrypt)602 TEST_F(hwaes, TextOutArgumentNotSetEncrypt) {
603 _state->args_encrypt.text_out.len = 0;
604
605 int rc = hwaes_encrypt(_state->hwaes_session, &_state->args_encrypt);
606 EXPECT_EQ(ERR_INVALID_ARGS, rc, "expect invalid_args error");
607 }
608
TEST_F(hwaes,KeyArgumentNotSetDecrypt)609 TEST_F(hwaes, KeyArgumentNotSetDecrypt) {
610 _state->args_decrypt.key.len = 0;
611
612 int rc = hwaes_decrypt(_state->hwaes_session, &_state->args_decrypt);
613 EXPECT_EQ(ERR_INVALID_ARGS, rc, "expect invalid_args error");
614 }
615
TEST_F(hwaes,IVArgumentNotSetDecrypt)616 TEST_F(hwaes, IVArgumentNotSetDecrypt) {
617 _state->args_decrypt.iv.len = 0;
618
619 int rc = hwaes_decrypt(_state->hwaes_session, &_state->args_decrypt);
620 EXPECT_EQ(ERR_INVALID_ARGS, rc, "expect invalid_args error");
621 }
622
TEST_F(hwaes,TextInArgumentNotSetDecrypt)623 TEST_F(hwaes, TextInArgumentNotSetDecrypt) {
624 _state->args_decrypt.text_in.len = 0;
625
626 int rc = hwaes_decrypt(_state->hwaes_session, &_state->args_decrypt);
627 EXPECT_EQ(ERR_INVALID_ARGS, rc, "expect invalid_args error");
628 }
629
TEST_F(hwaes,TextOutArgumentNotSetDecrypt)630 TEST_F(hwaes, TextOutArgumentNotSetDecrypt) {
631 _state->args_decrypt.text_out.len = 0;
632
633 int rc = hwaes_decrypt(_state->hwaes_session, &_state->args_decrypt);
634 EXPECT_EQ(ERR_INVALID_ARGS, rc, "expect invalid_args error");
635 }
636
TEST_F(hwaes,EncryptionDecryptionCBC)637 TEST_F(hwaes, EncryptionDecryptionCBC) {
638 int rc = hwaes_encrypt(_state->hwaes_session, &_state->args_encrypt);
639
640 EXPECT_EQ(NO_ERROR, rc, "encryption - cbc mode");
641 rc = memcmp(_state->shm_base, hwaes_cbc_ciphertext,
642 sizeof(hwaes_cbc_ciphertext));
643 EXPECT_EQ(0, rc, "wrong encryption result");
644
645 rc = hwaes_decrypt(_state->hwaes_session, &_state->args_decrypt);
646
647 EXPECT_EQ(NO_ERROR, rc, "decryption - cbc mode");
648 rc = memcmp(_state->shm_base, hwaes_cbc_plaintext,
649 sizeof(hwaes_cbc_plaintext));
650 EXPECT_EQ(0, rc, "wrong decryption result");
651 }
652
TEST_F(hwaes,EncryptionDecryptionCBCNoSHM)653 TEST_F(hwaes, EncryptionDecryptionCBCNoSHM) {
654 uint8_t buf[sizeof(hwaes_cbc_plaintext)] = {0};
655 memcpy(buf, hwaes_cbc_plaintext, sizeof(hwaes_cbc_plaintext));
656
657 _state->args_encrypt.text_in = (struct hwcrypt_arg_in){
658 .data_ptr = buf,
659 .len = sizeof(buf),
660 };
661 _state->args_encrypt.text_out = (struct hwcrypt_arg_out){
662 .data_ptr = buf,
663 .len = sizeof(buf),
664 };
665
666 _state->args_decrypt.text_in = (struct hwcrypt_arg_in){
667 .data_ptr = buf,
668 .len = sizeof(buf),
669 };
670 _state->args_decrypt.text_out = (struct hwcrypt_arg_out){
671 .data_ptr = buf,
672 .len = sizeof(buf),
673 };
674
675 int rc = hwaes_encrypt(_state->hwaes_session, &_state->args_encrypt);
676
677 EXPECT_EQ(NO_ERROR, rc, "encryption - cbc mode");
678 rc = memcmp(buf, hwaes_cbc_ciphertext, sizeof(hwaes_cbc_ciphertext));
679 EXPECT_EQ(0, rc, "wrong encryption result");
680
681 rc = hwaes_decrypt(_state->hwaes_session, &_state->args_decrypt);
682
683 EXPECT_EQ(NO_ERROR, rc, "decryption - cbc mode");
684 rc = memcmp(buf, hwaes_cbc_plaintext, sizeof(hwaes_cbc_plaintext));
685 EXPECT_EQ(0, rc, "wrong decryption result");
686 }
687
TEST_F(hwaes,RunEncryptMany)688 TEST_F(hwaes, RunEncryptMany) {
689 int rc;
690 for (size_t i = 0; i < MAX_TRY_TIMES; i++) {
691 rc = hwaes_encrypt(_state->hwaes_session, &_state->args_encrypt);
692 ASSERT_EQ(NO_ERROR, rc, "encryption - in loop");
693 }
694
695 memcpy(_state->shm_base, hwaes_cbc_plaintext, sizeof(hwaes_cbc_plaintext));
696 rc = hwaes_encrypt(_state->hwaes_session, &_state->args_encrypt);
697 EXPECT_EQ(NO_ERROR, rc, "encryption - final round");
698 rc = memcmp(_state->shm_base, hwaes_cbc_ciphertext,
699 sizeof(hwaes_cbc_ciphertext));
700 EXPECT_EQ(0, rc, "wrong encryption result");
701
702 test_abort:;
703 }
704
TEST_F(hwaes,EncryptVectors)705 TEST_F(hwaes, EncryptVectors) {
706 const struct test_vector* vector = vectors;
707 for (unsigned long i = 0; i < countof(vectors); ++i, ++vector) {
708 memset(_state->shm_base, 0, _state->shm_len);
709 struct hwcrypt_args args = {};
710
711 struct hwcrypt_shm_hd shm_hd = {
712 .handle = _state->memref,
713 .base = _state->shm_base,
714 .size = _state->shm_len,
715 };
716 parse_vector(vector, &shm_hd, &args, 1 /* encrypt */);
717
718 int rc = hwaes_encrypt(_state->hwaes_session, &args);
719 EXPECT_EQ(NO_ERROR, rc, "Encryption failed for test vector %lu\n", i);
720
721 rc = memcmp(_state->shm_base, vector->ciphertext.data_ptr,
722 vector->ciphertext.len);
723 EXPECT_EQ(0, rc, "wrong ciphertext result");
724
725 if (vector->tag.len > 0) {
726 rc = memcmp((uint8_t*)_state->shm_base + vector->ciphertext.len,
727 vector->tag.data_ptr, vector->tag.len);
728 EXPECT_EQ(0, rc, "wrong encryption tag result");
729 }
730 }
731
732 test_abort:;
733 }
734
TEST_F(hwaes,DecryptVectors)735 TEST_F(hwaes, DecryptVectors) {
736 const struct test_vector* vector = vectors;
737 for (unsigned long i = 0; i < countof(vectors); ++i, ++vector) {
738 memset(_state->shm_base, 0, _state->shm_len);
739 struct hwcrypt_args args = {};
740
741 struct hwcrypt_shm_hd shm_hd = {
742 .handle = _state->memref,
743 .base = _state->shm_base,
744 .size = _state->shm_len,
745 };
746 parse_vector(vector, &shm_hd, &args, 0 /* decrypt */);
747
748 int rc = hwaes_decrypt(_state->hwaes_session, &args);
749 EXPECT_EQ(NO_ERROR, rc, "Decryption failed for test vector %lu\n", i);
750
751 rc = memcmp(_state->shm_base, vector->plaintext.data_ptr,
752 vector->plaintext.len);
753 EXPECT_EQ(0, rc, "wrong decryption result");
754 }
755
756 test_abort:;
757 }
758
TEST_F(hwaes,InvalidAEADArgsForCBC)759 TEST_F(hwaes, InvalidAEADArgsForCBC) {
760 int rc;
761 uint8_t buf[sizeof(hwaes_cbc_plaintext)] = {0};
762
763 _state->args_encrypt.aad = (struct hwcrypt_arg_in){
764 .data_ptr = buf,
765 .len = sizeof(buf),
766 };
767 rc = hwaes_encrypt(_state->hwaes_session, &_state->args_encrypt);
768 EXPECT_EQ(ERR_INVALID_ARGS, rc, "unsupported AAD");
769
770 _state->args_encrypt.aad = (struct hwcrypt_arg_in){};
771 _state->args_encrypt.tag_in = (struct hwcrypt_arg_in){
772 .data_ptr = buf,
773 .len = sizeof(buf),
774 };
775 rc = hwaes_encrypt(_state->hwaes_session, &_state->args_encrypt);
776 EXPECT_EQ(ERR_INVALID_ARGS, rc, "unsupported tag");
777
778 _state->args_encrypt.tag_in = (struct hwcrypt_arg_in){};
779 _state->args_encrypt.tag_out = (struct hwcrypt_arg_out){
780 .data_ptr = buf,
781 .len = sizeof(buf),
782 };
783 rc = hwaes_encrypt(_state->hwaes_session, &_state->args_encrypt);
784 EXPECT_EQ(ERR_INVALID_ARGS, rc, "unsupported tag");
785
786 _state->args_decrypt.aad = (struct hwcrypt_arg_in){
787 .data_ptr = buf,
788 .len = sizeof(buf),
789 };
790 rc = hwaes_decrypt(_state->hwaes_session, &_state->args_decrypt);
791 EXPECT_EQ(ERR_INVALID_ARGS, rc, "unsupported AAD");
792
793 _state->args_decrypt.aad = (struct hwcrypt_arg_in){};
794 _state->args_decrypt.tag_in = (struct hwcrypt_arg_in){
795 .data_ptr = buf,
796 .len = sizeof(buf),
797 };
798 rc = hwaes_decrypt(_state->hwaes_session, &_state->args_decrypt);
799 EXPECT_EQ(ERR_INVALID_ARGS, rc, "unsupported tag");
800
801 _state->args_decrypt.tag_in = (struct hwcrypt_arg_in){};
802 _state->args_decrypt.tag_out = (struct hwcrypt_arg_out){
803 .data_ptr = buf,
804 .len = sizeof(buf),
805 };
806 rc = hwaes_decrypt(_state->hwaes_session, &_state->args_decrypt);
807 EXPECT_EQ(ERR_INVALID_ARGS, rc, "unsupported tag");
808 }
809
TEST_F(hwaes,InvalidGCMTagArgs)810 TEST_F(hwaes, InvalidGCMTagArgs) {
811 int rc;
812 uint8_t buf[16] = {0};
813
814 _state->args_encrypt.mode = HWAES_GCM_MODE;
815 ASSERT_LE(HWAES_GCM_IV_SIZE, _state->args_encrypt.iv.len,
816 "IV length too short");
817 _state->args_encrypt.iv.len = HWAES_GCM_IV_SIZE;
818
819 rc = hwaes_encrypt(_state->hwaes_session, &_state->args_encrypt);
820 EXPECT_EQ(ERR_INVALID_ARGS, rc, "missing tag output for GCM");
821
822 _state->args_encrypt.tag_in = (struct hwcrypt_arg_in){
823 .data_ptr = buf,
824 .len = sizeof(buf),
825 };
826 rc = hwaes_encrypt(_state->hwaes_session, &_state->args_encrypt);
827 EXPECT_EQ(ERR_INVALID_ARGS, rc, "wrong tag direction for encryption");
828
829 _state->args_decrypt.mode = HWAES_GCM_MODE;
830 ASSERT_LE(HWAES_GCM_IV_SIZE, _state->args_decrypt.iv.len,
831 "IV length too short");
832 _state->args_decrypt.iv.len = HWAES_GCM_IV_SIZE;
833
834 rc = hwaes_decrypt(_state->hwaes_session, &_state->args_decrypt);
835 EXPECT_EQ(ERR_INVALID_ARGS, rc, "missing tag input for GCM");
836
837 _state->args_decrypt.tag_in = (struct hwcrypt_arg_in){
838 .data_ptr = buf,
839 .len = sizeof(buf),
840 };
841 _state->args_decrypt.tag_out = (struct hwcrypt_arg_out){
842 .data_ptr = buf,
843 .len = sizeof(buf),
844 };
845 rc = hwaes_decrypt(_state->hwaes_session, &_state->args_decrypt);
846 EXPECT_EQ(ERR_INVALID_ARGS, rc, "wrong tag direction for decryption");
847
848 test_abort:;
849 }
850
851 static const uint8_t opaque_key_ciphertext[] = {
852 0x8c, 0xc8, 0xc0, 0xc7, 0x76, 0x57, 0xb4, 0x4f, 0x22, 0xd3, 0x33,
853 0x2e, 0x6f, 0x23, 0x92, 0x51, 0x21, 0x69, 0x30, 0xff, 0x84, 0x88,
854 0x4c, 0x38, 0x04, 0xe5, 0x17, 0xad, 0x5c, 0x08, 0x87, 0xfc,
855 };
856
857 #define HWKEY_OPAQUE_KEY_ID "com.android.trusty.hwaes.unittest.opaque_handle"
858
TEST_F(hwaes,DISABLED_WITHOUT_HWCRYPTO_UNITTEST (EncryptWithOpaqueKey))859 TEST_F(hwaes, DISABLED_WITHOUT_HWCRYPTO_UNITTEST(EncryptWithOpaqueKey)) {
860 long rc = hwkey_open();
861 ASSERT_GE(rc, 0, "Could not connect to hwkey");
862 hwkey_session_t hwkey_session = (hwkey_session_t)rc;
863
864 uint8_t key_handle[HWKEY_OPAQUE_HANDLE_MAX_SIZE] = {0};
865 uint32_t key_handle_size = HWKEY_OPAQUE_HANDLE_MAX_SIZE;
866 rc = hwkey_get_keyslot_data(hwkey_session, HWKEY_OPAQUE_KEY_ID, key_handle,
867 &key_handle_size);
868 EXPECT_EQ(NO_ERROR, rc, "Could not get opaque key handle");
869 EXPECT_LE(key_handle_size, HWKEY_OPAQUE_HANDLE_MAX_SIZE,
870 "Wrong handle size");
871
872 _state->args_encrypt.key_type = HWAES_OPAQUE_HANDLE;
873 _state->args_encrypt.key.data_ptr = key_handle;
874 _state->args_encrypt.key.len = key_handle_size;
875 _state->args_encrypt.key.shm_hd_ptr = NULL;
876
877 rc = hwaes_encrypt(_state->hwaes_session, &_state->args_encrypt);
878
879 EXPECT_EQ(NO_ERROR, rc, "opaque encryption - cbc mode");
880 rc = memcmp(_state->shm_base, opaque_key_ciphertext,
881 sizeof(opaque_key_ciphertext));
882 EXPECT_EQ(0, rc, "wrong encryption result");
883
884 hwkey_close(hwkey_session);
885 test_abort:;
886 }
887
TEST_F(hwaes,DISABLED_WITHOUT_HWCRYPTO_UNITTEST (DecryptWithOpaqueKey))888 TEST_F(hwaes, DISABLED_WITHOUT_HWCRYPTO_UNITTEST(DecryptWithOpaqueKey)) {
889 long rc = hwkey_open();
890 ASSERT_GE(rc, 0, "Could not connect to hwkey");
891 hwkey_session_t hwkey_session = (hwkey_session_t)rc;
892
893 uint8_t key_handle[HWKEY_OPAQUE_HANDLE_MAX_SIZE] = {0};
894 uint32_t key_handle_size = HWKEY_OPAQUE_HANDLE_MAX_SIZE;
895 rc = hwkey_get_keyslot_data(hwkey_session, HWKEY_OPAQUE_KEY_ID, key_handle,
896 &key_handle_size);
897 EXPECT_EQ(NO_ERROR, rc, "Could not get opaque key handle");
898 EXPECT_LE(key_handle_size, HWKEY_OPAQUE_HANDLE_MAX_SIZE,
899 "Wrong handle size");
900
901 _state->args_decrypt.key_type = HWAES_OPAQUE_HANDLE;
902 _state->args_decrypt.key.data_ptr = key_handle;
903 _state->args_decrypt.key.len = key_handle_size;
904 _state->args_decrypt.key.shm_hd_ptr = NULL;
905
906 memcpy(_state->shm_base, opaque_key_ciphertext,
907 sizeof(opaque_key_ciphertext));
908 _state->args_decrypt.text_in.data_ptr = _state->shm_base;
909 _state->args_decrypt.text_in.len = sizeof(opaque_key_ciphertext);
910 _state->args_decrypt.text_in.shm_hd_ptr = &_state->shm_hd;
911
912 rc = hwaes_decrypt(_state->hwaes_session, &_state->args_decrypt);
913
914 EXPECT_EQ(NO_ERROR, rc, "opaque decryption - cbc mode");
915 rc = memcmp(_state->shm_base, hwaes_cbc_plaintext,
916 sizeof(hwaes_cbc_plaintext));
917 EXPECT_EQ(0, rc, "wrong decryption result");
918
919 hwkey_close(hwkey_session);
920 test_abort:;
921 }
922
TEST_F(hwaes,DISABLED_WITHOUT_HWCRYPTO_UNITTEST (RunOpaqueEncryptMany))923 TEST_F(hwaes, DISABLED_WITHOUT_HWCRYPTO_UNITTEST(RunOpaqueEncryptMany)) {
924 long rc = hwkey_open();
925 ASSERT_GE(rc, 0, "Could not connect to hwkey");
926 hwkey_session_t hwkey_session = (hwkey_session_t)rc;
927
928 uint8_t key_handle[HWKEY_OPAQUE_HANDLE_MAX_SIZE] = {0};
929 uint32_t key_handle_size = HWKEY_OPAQUE_HANDLE_MAX_SIZE;
930 rc = hwkey_get_keyslot_data(hwkey_session, HWKEY_OPAQUE_KEY_ID, key_handle,
931 &key_handle_size);
932 EXPECT_EQ(NO_ERROR, rc, "Could not get opaque key handle");
933 EXPECT_LE(key_handle_size, HWKEY_OPAQUE_HANDLE_MAX_SIZE,
934 "Wrong handle size");
935
936 _state->args_encrypt.key_type = HWAES_OPAQUE_HANDLE;
937 _state->args_encrypt.key.data_ptr = key_handle;
938 _state->args_encrypt.key.len = key_handle_size;
939 _state->args_encrypt.key.shm_hd_ptr = NULL;
940
941 for (size_t i = 0; i < MAX_TRY_TIMES; i++) {
942 rc = hwaes_encrypt(_state->hwaes_session, &_state->args_encrypt);
943 ASSERT_EQ(NO_ERROR, rc, "encryption - in loop");
944 }
945
946 memcpy(_state->shm_base, hwaes_cbc_plaintext, sizeof(hwaes_cbc_plaintext));
947 rc = hwaes_encrypt(_state->hwaes_session, &_state->args_encrypt);
948 EXPECT_EQ(NO_ERROR, rc, "encryption - final round");
949 rc = memcmp(_state->shm_base, opaque_key_ciphertext,
950 sizeof(opaque_key_ciphertext));
951 EXPECT_EQ(0, rc, "wrong encryption result");
952
953 hwkey_close(hwkey_session);
954 test_abort:;
955 }
956
TEST_F(hwaes,DISABLED_WITHOUT_HWCRYPTO_UNITTEST (InvalidOpaqueKeySize))957 TEST_F(hwaes, DISABLED_WITHOUT_HWCRYPTO_UNITTEST(InvalidOpaqueKeySize)) {
958 long rc = hwkey_open();
959 ASSERT_GE(rc, 0, "Could not connect to hwkey");
960 hwkey_session_t hwkey_session = (hwkey_session_t)rc;
961
962 uint8_t key_handle[HWKEY_OPAQUE_HANDLE_MAX_SIZE + 1] = {0};
963
964 _state->args_encrypt.key_type = HWAES_OPAQUE_HANDLE;
965 _state->args_encrypt.key.data_ptr = key_handle;
966 _state->args_encrypt.key.len = sizeof(key_handle);
967 _state->args_encrypt.key.shm_hd_ptr = NULL;
968
969 rc = hwaes_encrypt(_state->hwaes_session, &_state->args_encrypt);
970
971 EXPECT_EQ(ERR_INVALID_ARGS, rc, "Did not error on invalid opaque key size");
972
973 hwkey_close(hwkey_session);
974 test_abort:;
975 }
976
TEST_F(hwaes,DISABLED_WITHOUT_HWCRYPTO_UNITTEST (InvalidOpaqueKeyTerminator))977 TEST_F(hwaes, DISABLED_WITHOUT_HWCRYPTO_UNITTEST(InvalidOpaqueKeyTerminator)) {
978 long rc = hwkey_open();
979 ASSERT_GE(rc, 0, "Could not connect to hwkey");
980 hwkey_session_t hwkey_session = (hwkey_session_t)rc;
981
982 uint8_t key_handle[HWKEY_OPAQUE_HANDLE_MAX_SIZE];
983
984 /* Non-null terminated handle */
985 memset(key_handle, 0xa, HWKEY_OPAQUE_HANDLE_MAX_SIZE);
986
987 _state->args_encrypt.key_type = HWAES_OPAQUE_HANDLE;
988 _state->args_encrypt.key.data_ptr = key_handle;
989 _state->args_encrypt.key.len = sizeof(key_handle);
990 _state->args_encrypt.key.shm_hd_ptr = NULL;
991
992 rc = hwaes_encrypt(_state->hwaes_session, &_state->args_encrypt);
993
994 EXPECT_EQ(ERR_INVALID_ARGS, rc, "Did not error on invalid opaque key");
995
996 hwkey_close(hwkey_session);
997 test_abort:;
998 }
999
TEST_F(hwaes,DISABLED_WITHOUT_HWCRYPTO_UNITTEST (OutdatedOpaqueHandle))1000 TEST_F(hwaes, DISABLED_WITHOUT_HWCRYPTO_UNITTEST(OutdatedOpaqueHandle)) {
1001 long rc = hwkey_open();
1002 ASSERT_GE(rc, 0, "Could not connect to hwkey");
1003 hwkey_session_t hwkey_session = (hwkey_session_t)rc;
1004
1005 uint8_t key_handle[HWKEY_OPAQUE_HANDLE_MAX_SIZE] = {0};
1006 uint32_t key_handle_size = HWKEY_OPAQUE_HANDLE_MAX_SIZE;
1007 rc = hwkey_get_keyslot_data(hwkey_session, HWKEY_OPAQUE_KEY_ID, key_handle,
1008 &key_handle_size);
1009 EXPECT_EQ(NO_ERROR, rc, "Could not get opaque key handle");
1010 EXPECT_LE(key_handle_size, HWKEY_OPAQUE_HANDLE_MAX_SIZE,
1011 "Wrong handle size");
1012
1013 /* Close the session and invalidate the handle */
1014 hwkey_close(hwkey_session);
1015
1016 _state->args_encrypt.key_type = HWAES_OPAQUE_HANDLE;
1017 _state->args_encrypt.key.data_ptr = key_handle;
1018 _state->args_encrypt.key.len = key_handle_size;
1019 _state->args_encrypt.key.shm_hd_ptr = NULL;
1020
1021 rc = hwaes_encrypt(_state->hwaes_session, &_state->args_encrypt);
1022 EXPECT_EQ(ERR_IO, rc, "Should not be able to fetch key for opaque handle");
1023
1024 test_abort:;
1025 }
1026
1027 PORT_TEST(hwaes, "com.android.trusty.hwaes.test")
1028