1/*
2 * Copyright (C) 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 <UniquePtr.h>
18
19#include <gtest/gtest.h>
20
21#include <keymaster/android_keymaster.h>
22#include <keymaster/android_keymaster_utils.h>
23#include <keymaster/keymaster_tags.h>
24
25#include "android_keymaster_test_utils.h"
26
27namespace keymaster {
28namespace test {
29
30/**
31 * Serialize and deserialize a message.
32 */
33template <typename Message>
34Message* round_trip(int32_t ver, const Message& message, size_t expected_size) {
35    size_t size = message.SerializedSize();
36    EXPECT_EQ(expected_size, size);
37    if (size == 0)
38        return NULL;
39
40    UniquePtr<uint8_t[]> buf(new uint8_t[size]);
41    EXPECT_EQ(buf.get() + size, message.Serialize(buf.get(), buf.get() + size));
42
43    Message* deserialized = new Message(ver);
44    const uint8_t* p = buf.get();
45    EXPECT_TRUE(deserialized->Deserialize(&p, p + size));
46    EXPECT_EQ((ptrdiff_t)size, p - buf.get());
47    return deserialized;
48}
49
50struct EmptyKeymasterResponse : public KeymasterResponse {
51    EmptyKeymasterResponse(int32_t ver) : KeymasterResponse(ver) {}
52    size_t NonErrorSerializedSize() const { return 1; }
53    uint8_t* NonErrorSerialize(uint8_t* buf, const uint8_t* /* end */) const {
54        *buf++ = 0;
55        return buf;
56    }
57    bool NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) {
58        if (*buf_ptr >= end)
59            return false;
60        EXPECT_EQ(0, **buf_ptr);
61        (*buf_ptr)++;
62        return true;
63    }
64};
65
66TEST(RoundTrip, EmptyKeymasterResponse) {
67    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
68        EmptyKeymasterResponse msg(ver);
69        msg.error = KM_ERROR_OK;
70
71        UniquePtr<EmptyKeymasterResponse> deserialized(round_trip(ver, msg, 5));
72    }
73}
74
75TEST(RoundTrip, EmptyKeymasterResponseError) {
76    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
77        EmptyKeymasterResponse msg(ver);
78        msg.error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
79
80        UniquePtr<EmptyKeymasterResponse> deserialized(round_trip(ver, msg, 4));
81    }
82}
83
84TEST(RoundTrip, SupportedByAlgorithmRequest) {
85    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
86        SupportedByAlgorithmRequest req(ver);
87        req.algorithm = KM_ALGORITHM_EC;
88
89        UniquePtr<SupportedByAlgorithmRequest> deserialized(round_trip(ver, req, 4));
90        EXPECT_EQ(KM_ALGORITHM_EC, deserialized->algorithm);
91    }
92}
93
94TEST(RoundTrip, SupportedByAlgorithmAndPurposeRequest) {
95    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
96        SupportedByAlgorithmAndPurposeRequest req(ver);
97        req.algorithm = KM_ALGORITHM_EC;
98        req.purpose = KM_PURPOSE_DECRYPT;
99
100        UniquePtr<SupportedByAlgorithmAndPurposeRequest> deserialized(round_trip(ver, req, 8));
101        EXPECT_EQ(KM_ALGORITHM_EC, deserialized->algorithm);
102        EXPECT_EQ(KM_PURPOSE_DECRYPT, deserialized->purpose);
103    }
104}
105
106TEST(RoundTrip, SupportedResponse) {
107    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
108        SupportedResponse<keymaster_digest_t> rsp(ver);
109        keymaster_digest_t digests[] = {KM_DIGEST_NONE, KM_DIGEST_MD5, KM_DIGEST_SHA1};
110        rsp.error = KM_ERROR_OK;
111        rsp.SetResults(digests);
112
113        UniquePtr<SupportedResponse<keymaster_digest_t>> deserialized(round_trip(ver, rsp, 20));
114        EXPECT_EQ(array_length(digests), deserialized->results_length);
115        EXPECT_EQ(0, memcmp(deserialized->results, digests, array_size(digests)));
116    }
117}
118
119static keymaster_key_param_t params[] = {
120    Authorization(TAG_PURPOSE, KM_PURPOSE_SIGN),
121    Authorization(TAG_PURPOSE, KM_PURPOSE_VERIFY),
122    Authorization(TAG_ALGORITHM, KM_ALGORITHM_RSA),
123    Authorization(TAG_USER_ID, 7),
124    Authorization(TAG_USER_AUTH_TYPE, HW_AUTH_PASSWORD),
125    Authorization(TAG_APPLICATION_ID, "app_id", 6),
126    Authorization(TAG_AUTH_TIMEOUT, 300),
127};
128uint8_t TEST_DATA[] = "a key blob";
129
130TEST(RoundTrip, GenerateKeyRequest) {
131    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
132        GenerateKeyRequest req(ver);
133        req.key_description.Reinitialize(params, array_length(params));
134        UniquePtr<GenerateKeyRequest> deserialized(round_trip(ver, req, 78));
135        EXPECT_EQ(deserialized->key_description, req.key_description);
136    }
137}
138
139TEST(RoundTrip, GenerateKeyResponse) {
140    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
141        GenerateKeyResponse rsp(ver);
142        rsp.error = KM_ERROR_OK;
143        rsp.key_blob.key_material = dup_array(TEST_DATA);
144        rsp.key_blob.key_material_size = array_length(TEST_DATA);
145        rsp.enforced.Reinitialize(params, array_length(params));
146
147        UniquePtr<GenerateKeyResponse> deserialized(round_trip(ver, rsp, 109));
148        EXPECT_EQ(KM_ERROR_OK, deserialized->error);
149        EXPECT_EQ(deserialized->enforced, rsp.enforced);
150        EXPECT_EQ(deserialized->unenforced, rsp.unenforced);
151    }
152}
153
154TEST(RoundTrip, GenerateKeyResponseTestError) {
155    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
156        GenerateKeyResponse rsp(ver);
157        rsp.error = KM_ERROR_UNSUPPORTED_ALGORITHM;
158        rsp.key_blob.key_material = dup_array(TEST_DATA);
159        rsp.key_blob.key_material_size = array_length(TEST_DATA);
160        rsp.enforced.Reinitialize(params, array_length(params));
161
162        UniquePtr<GenerateKeyResponse> deserialized(round_trip(ver, rsp, 4));
163        EXPECT_EQ(KM_ERROR_UNSUPPORTED_ALGORITHM, deserialized->error);
164        EXPECT_EQ(0U, deserialized->enforced.size());
165        EXPECT_EQ(0U, deserialized->unenforced.size());
166        EXPECT_EQ(0U, deserialized->key_blob.key_material_size);
167    }
168}
169
170TEST(RoundTrip, GetKeyCharacteristicsRequest) {
171    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
172        GetKeyCharacteristicsRequest req(ver);
173        req.additional_params.Reinitialize(params, array_length(params));
174        req.SetKeyMaterial("foo", 3);
175
176        UniquePtr<GetKeyCharacteristicsRequest> deserialized(round_trip(ver, req, 85));
177        EXPECT_EQ(7U, deserialized->additional_params.size());
178        EXPECT_EQ(3U, deserialized->key_blob.key_material_size);
179        EXPECT_EQ(0, memcmp(deserialized->key_blob.key_material, "foo", 3));
180    }
181}
182
183TEST(RoundTrip, GetKeyCharacteristicsResponse) {
184    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
185        GetKeyCharacteristicsResponse msg(ver);
186        msg.error = KM_ERROR_OK;
187        msg.enforced.Reinitialize(params, array_length(params));
188        msg.unenforced.Reinitialize(params, array_length(params));
189
190        UniquePtr<GetKeyCharacteristicsResponse> deserialized(round_trip(ver, msg, 160));
191        EXPECT_EQ(msg.enforced, deserialized->enforced);
192        EXPECT_EQ(msg.unenforced, deserialized->unenforced);
193    }
194}
195
196TEST(RoundTrip, BeginOperationRequest) {
197    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
198        BeginOperationRequest msg(ver);
199        msg.purpose = KM_PURPOSE_SIGN;
200        msg.SetKeyMaterial("foo", 3);
201        msg.additional_params.Reinitialize(params, array_length(params));
202
203        UniquePtr<BeginOperationRequest> deserialized(round_trip(ver, msg, 89));
204        EXPECT_EQ(KM_PURPOSE_SIGN, deserialized->purpose);
205        EXPECT_EQ(3U, deserialized->key_blob.key_material_size);
206        EXPECT_EQ(0, memcmp(deserialized->key_blob.key_material, "foo", 3));
207        EXPECT_EQ(msg.additional_params, deserialized->additional_params);
208    }
209}
210
211TEST(RoundTrip, BeginOperationResponse) {
212    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
213        BeginOperationResponse msg(ver);
214        msg.error = KM_ERROR_OK;
215        msg.op_handle = 0xDEADBEEF;
216        msg.output_params.push_back(Authorization(TAG_NONCE, "foo", 3));
217
218        UniquePtr<BeginOperationResponse> deserialized;
219        switch (ver) {
220        case 0:
221            deserialized.reset(round_trip(ver, msg, 12));
222            break;
223        case 1:
224        case 2:
225        case 3:
226            deserialized.reset(round_trip(ver, msg, 39));
227            break;
228        default:
229            FAIL();
230        }
231
232        EXPECT_EQ(KM_ERROR_OK, deserialized->error);
233        EXPECT_EQ(0xDEADBEEF, deserialized->op_handle);
234
235        switch (ver) {
236        case 0:
237            EXPECT_EQ(0U, deserialized->output_params.size());
238            break;
239        case 1:
240        case 2:
241        case 3:
242            EXPECT_EQ(msg.output_params, deserialized->output_params);
243            break;
244        default:
245            FAIL();
246        }
247    }
248}
249
250TEST(RoundTrip, BeginOperationResponseError) {
251    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
252        BeginOperationResponse msg(ver);
253        msg.error = KM_ERROR_INVALID_OPERATION_HANDLE;
254        msg.op_handle = 0xDEADBEEF;
255
256        UniquePtr<BeginOperationResponse> deserialized(round_trip(ver, msg, 4));
257        EXPECT_EQ(KM_ERROR_INVALID_OPERATION_HANDLE, deserialized->error);
258    }
259}
260
261TEST(RoundTrip, UpdateOperationRequest) {
262    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
263        UpdateOperationRequest msg(ver);
264        msg.op_handle = 0xDEADBEEF;
265        msg.input.Reinitialize("foo", 3);
266
267        UniquePtr<UpdateOperationRequest> deserialized;
268        switch (ver) {
269        case 0:
270            deserialized.reset(round_trip(ver, msg, 15));
271            break;
272        case 1:
273        case 2:
274        case 3:
275            deserialized.reset(round_trip(ver, msg, 27));
276            break;
277        default:
278            FAIL();
279        }
280        EXPECT_EQ(3U, deserialized->input.available_read());
281        EXPECT_EQ(0, memcmp(deserialized->input.peek_read(), "foo", 3));
282    }
283}
284
285TEST(RoundTrip, UpdateOperationResponse) {
286    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
287        UpdateOperationResponse msg(ver);
288        msg.error = KM_ERROR_OK;
289        msg.output.Reinitialize("foo", 3);
290        msg.input_consumed = 99;
291        msg.output_params.push_back(TAG_APPLICATION_ID, "bar", 3);
292
293        UniquePtr<UpdateOperationResponse> deserialized;
294        switch (ver) {
295        case 0:
296            deserialized.reset(round_trip(ver, msg, 11));
297            break;
298        case 1:
299            deserialized.reset(round_trip(ver, msg, 15));
300            break;
301        case 2:
302        case 3:
303            deserialized.reset(round_trip(ver, msg, 42));
304            break;
305        default:
306            FAIL();
307        }
308        EXPECT_EQ(KM_ERROR_OK, deserialized->error);
309        EXPECT_EQ(3U, deserialized->output.available_read());
310        EXPECT_EQ(0, memcmp(deserialized->output.peek_read(), "foo", 3));
311
312        switch (ver) {
313        case 0:
314            EXPECT_EQ(0U, deserialized->input_consumed);
315            break;
316        case 1:
317            EXPECT_EQ(99U, deserialized->input_consumed);
318            break;
319        case 2:
320        case 3:
321            EXPECT_EQ(99U, deserialized->input_consumed);
322            EXPECT_EQ(1U, deserialized->output_params.size());
323            break;
324        default:
325            FAIL();
326        }
327    }
328}
329
330TEST(RoundTrip, FinishOperationRequest) {
331    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
332        FinishOperationRequest msg(ver);
333        msg.op_handle = 0xDEADBEEF;
334        msg.signature.Reinitialize("bar", 3);
335        msg.input.Reinitialize("baz", 3);
336
337        UniquePtr<FinishOperationRequest> deserialized;
338        switch (ver) {
339        case 0:
340            deserialized.reset(round_trip(ver, msg, 15));
341            break;
342        case 1:
343        case 2:
344            deserialized.reset(round_trip(ver, msg, 27));
345            break;
346        case 3:
347            deserialized.reset(round_trip(ver, msg, 34));
348            break;
349        default:
350            FAIL();
351        }
352        EXPECT_EQ(0xDEADBEEF, deserialized->op_handle);
353        EXPECT_EQ(3U, deserialized->signature.available_read());
354        EXPECT_EQ(0, memcmp(deserialized->signature.peek_read(), "bar", 3));
355    }
356}
357
358TEST(Round_Trip, FinishOperationResponse) {
359    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
360        FinishOperationResponse msg(ver);
361        msg.error = KM_ERROR_OK;
362        msg.output.Reinitialize("foo", 3);
363
364        UniquePtr<FinishOperationResponse> deserialized;
365        switch (ver) {
366        case 0:
367        case 1:
368            deserialized.reset(round_trip(ver, msg, 11));
369            break;
370        case 2:
371        case 3:
372            deserialized.reset(round_trip(ver, msg, 23));
373            break;
374        default:
375            FAIL();
376        }
377        EXPECT_EQ(msg.error, deserialized->error);
378        EXPECT_EQ(msg.output.available_read(), deserialized->output.available_read());
379        EXPECT_EQ(0, memcmp(msg.output.peek_read(), deserialized->output.peek_read(),
380                            msg.output.available_read()));
381    }
382}
383
384TEST(RoundTrip, ImportKeyRequest) {
385    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
386        ImportKeyRequest msg(ver);
387        msg.key_description.Reinitialize(params, array_length(params));
388        msg.key_format = KM_KEY_FORMAT_X509;
389        msg.SetKeyMaterial("foo", 3);
390
391        UniquePtr<ImportKeyRequest> deserialized(round_trip(ver, msg, 89));
392        EXPECT_EQ(msg.key_description, deserialized->key_description);
393        EXPECT_EQ(msg.key_format, deserialized->key_format);
394        EXPECT_EQ(msg.key_data_length, deserialized->key_data_length);
395        EXPECT_EQ(0, memcmp(msg.key_data, deserialized->key_data, msg.key_data_length));
396    }
397}
398
399TEST(RoundTrip, ImportKeyResponse) {
400    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
401        ImportKeyResponse msg(ver);
402        msg.error = KM_ERROR_OK;
403        msg.SetKeyMaterial("foo", 3);
404        msg.enforced.Reinitialize(params, array_length(params));
405        msg.unenforced.Reinitialize(params, array_length(params));
406
407        UniquePtr<ImportKeyResponse> deserialized(round_trip(ver, msg, 167));
408        EXPECT_EQ(msg.error, deserialized->error);
409        EXPECT_EQ(msg.key_blob.key_material_size, deserialized->key_blob.key_material_size);
410        EXPECT_EQ(0, memcmp(msg.key_blob.key_material, deserialized->key_blob.key_material,
411                            msg.key_blob.key_material_size));
412        EXPECT_EQ(msg.enforced, deserialized->enforced);
413        EXPECT_EQ(msg.unenforced, deserialized->unenforced);
414    }
415}
416
417TEST(RoundTrip, ExportKeyRequest) {
418    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
419        ExportKeyRequest msg(ver);
420        msg.additional_params.Reinitialize(params, array_length(params));
421        msg.key_format = KM_KEY_FORMAT_X509;
422        msg.SetKeyMaterial("foo", 3);
423
424        UniquePtr<ExportKeyRequest> deserialized(round_trip(ver, msg, 89));
425        EXPECT_EQ(msg.additional_params, deserialized->additional_params);
426        EXPECT_EQ(msg.key_format, deserialized->key_format);
427        EXPECT_EQ(3U, deserialized->key_blob.key_material_size);
428        EXPECT_EQ(0, memcmp("foo", deserialized->key_blob.key_material, 3));
429    }
430}
431
432TEST(RoundTrip, ExportKeyResponse) {
433    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
434        ExportKeyResponse msg(ver);
435        msg.error = KM_ERROR_OK;
436        msg.SetKeyMaterial("foo", 3);
437
438        UniquePtr<ExportKeyResponse> deserialized(round_trip(ver, msg, 11));
439        EXPECT_EQ(3U, deserialized->key_data_length);
440        EXPECT_EQ(0, memcmp("foo", deserialized->key_data, 3));
441    }
442}
443
444TEST(RoundTrip, DeleteKeyRequest) {
445    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
446        DeleteKeyRequest msg(ver);
447        msg.SetKeyMaterial("foo", 3);
448
449        UniquePtr<DeleteKeyRequest> deserialized(round_trip(ver, msg, 7));
450        EXPECT_EQ(3U, deserialized->key_blob.key_material_size);
451        EXPECT_EQ(0, memcmp("foo", deserialized->key_blob.key_material, 3));
452    }
453}
454
455TEST(RoundTrip, DeleteKeyResponse) {
456    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
457        DeleteKeyResponse msg(ver);
458        UniquePtr<DeleteKeyResponse> deserialized(round_trip(ver, msg, 4));
459    }
460}
461
462TEST(RoundTrip, DeleteAllKeysRequest) {
463    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
464        DeleteAllKeysRequest msg(ver);
465        UniquePtr<DeleteAllKeysRequest> deserialized(round_trip(ver, msg, 0));
466    }
467}
468
469TEST(RoundTrip, DeleteAllKeysResponse) {
470    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
471        DeleteAllKeysResponse msg(ver);
472        UniquePtr<DeleteAllKeysResponse> deserialized(round_trip(ver, msg, 4));
473    }
474}
475
476TEST(RoundTrip, GetVersionRequest) {
477    GetVersionRequest msg;
478
479    size_t size = msg.SerializedSize();
480    ASSERT_EQ(0U, size);
481
482    UniquePtr<uint8_t[]> buf(new uint8_t[size]);
483    EXPECT_EQ(buf.get() + size, msg.Serialize(buf.get(), buf.get() + size));
484
485    GetVersionRequest deserialized;
486    const uint8_t* p = buf.get();
487    EXPECT_TRUE(deserialized.Deserialize(&p, p + size));
488    EXPECT_EQ((ptrdiff_t)size, p - buf.get());
489}
490
491TEST(RoundTrip, GetVersionResponse) {
492    GetVersionResponse msg;
493    msg.error = KM_ERROR_OK;
494    msg.major_ver = 9;
495    msg.minor_ver = 98;
496    msg.subminor_ver = 38;
497
498    size_t size = msg.SerializedSize();
499    ASSERT_EQ(7U, size);
500
501    UniquePtr<uint8_t[]> buf(new uint8_t[size]);
502    EXPECT_EQ(buf.get() + size, msg.Serialize(buf.get(), buf.get() + size));
503
504    GetVersionResponse deserialized;
505    const uint8_t* p = buf.get();
506    EXPECT_TRUE(deserialized.Deserialize(&p, p + size));
507    EXPECT_EQ((ptrdiff_t)size, p - buf.get());
508    EXPECT_EQ(9U, msg.major_ver);
509    EXPECT_EQ(98U, msg.minor_ver);
510    EXPECT_EQ(38U, msg.subminor_ver);
511}
512
513TEST(RoundTrip, AddEntropyRequest) {
514    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
515        AddEntropyRequest msg(ver);
516        msg.random_data.Reinitialize("foo", 3);
517
518        UniquePtr<AddEntropyRequest> deserialized(round_trip(ver, msg, 7));
519        EXPECT_EQ(3U, deserialized->random_data.available_read());
520        EXPECT_EQ(0, memcmp("foo", deserialized->random_data.peek_read(), 3));
521    }
522}
523
524TEST(RoundTrip, AddEntropyResponse) {
525    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
526        AddEntropyResponse msg(ver);
527        UniquePtr<AddEntropyResponse> deserialized(round_trip(ver, msg, 4));
528    }
529}
530
531TEST(RoundTrip, AbortOperationRequest) {
532    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
533        AbortOperationRequest msg(ver);
534        UniquePtr<AbortOperationRequest> deserialized(round_trip(ver, msg, 8));
535    }
536}
537
538TEST(RoundTrip, AbortOperationResponse) {
539    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
540        AbortOperationResponse msg(ver);
541        UniquePtr<AbortOperationResponse> deserialized(round_trip(ver, msg, 4));
542    }
543}
544
545TEST(RoundTrip, AttestKeyRequest) {
546    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
547        AttestKeyRequest msg(ver);
548        msg.SetKeyMaterial("foo", 3);
549        msg.attest_params.Reinitialize(params, array_length(params));
550
551        UniquePtr<AttestKeyRequest> deserialized(round_trip(ver, msg, 85));
552        EXPECT_EQ(3U, deserialized->key_blob.key_material_size);
553        EXPECT_EQ(0, memcmp("foo", deserialized->key_blob.key_material, 3));
554        EXPECT_EQ(msg.attest_params, deserialized->attest_params);
555    }
556}
557
558TEST(RoundTrip, AttestKeyResponse) {
559    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
560        AttestKeyResponse msg(ver);
561        msg.error = KM_ERROR_OK;
562        EXPECT_TRUE(msg.AllocateChain(3));
563        msg.certificate_chain.entries[0] = {dup_buffer("foo", 3), 3};
564        msg.certificate_chain.entries[1] = {dup_buffer("bar", 3), 3};
565        msg.certificate_chain.entries[2] = {dup_buffer("baz", 3), 3};
566
567        UniquePtr<AttestKeyResponse> deserialized(round_trip(ver, msg, 29));
568        keymaster_cert_chain_t* chain = &deserialized->certificate_chain;
569
570        EXPECT_NE(nullptr, chain->entries);
571        EXPECT_EQ(3U, chain->entry_count);
572        EXPECT_EQ(3U, chain->entries[0].data_length);
573        EXPECT_EQ(0, memcmp("foo", chain->entries[0].data, 3));
574        EXPECT_EQ(3U, chain->entries[1].data_length);
575        EXPECT_EQ(0, memcmp("bar", chain->entries[1].data, 3));
576        EXPECT_EQ(3U, chain->entries[2].data_length);
577        EXPECT_EQ(0, memcmp("baz", chain->entries[2].data, 3));
578    }
579}
580
581TEST(RoundTrip, UpgradeKeyRequest) {
582    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
583        UpgradeKeyRequest msg(ver);
584        msg.SetKeyMaterial("foo", 3);
585        msg.upgrade_params.Reinitialize(params, array_length(params));
586
587        UniquePtr<UpgradeKeyRequest> deserialized(round_trip(ver, msg, 85));
588        EXPECT_EQ(3U, deserialized->key_blob.key_material_size);
589        EXPECT_EQ(0, memcmp("foo", deserialized->key_blob.key_material, 3));
590        EXPECT_EQ(msg.upgrade_params, deserialized->upgrade_params);
591    }
592}
593
594TEST(RoundTrip, UpgradeKeyResponse) {
595    for (int ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
596        UpgradeKeyResponse req(ver);
597        req.error = KM_ERROR_OK;
598        req.upgraded_key.key_material = dup_array(TEST_DATA);
599        req.upgraded_key.key_material_size = array_length(TEST_DATA);
600
601        UniquePtr<UpgradeKeyResponse> deserialized(round_trip(ver, req, 19));
602        EXPECT_EQ(KM_ERROR_OK, deserialized->error);
603        EXPECT_EQ(req.upgraded_key.key_material_size, deserialized->upgraded_key.key_material_size);
604        EXPECT_EQ(0, memcmp(req.upgraded_key.key_material, deserialized->upgraded_key.key_material,
605                            req.upgraded_key.key_material_size));
606    }
607}
608
609uint8_t msgbuf[] = {
610    220, 88,  183, 255, 71,  1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
611    0,   173, 0,   0,   0,   228, 174, 98,  187, 191, 135, 253, 200, 51,  230, 114, 247, 151, 109,
612    237, 79,  87,  32,  94,  5,   204, 46,  154, 30,  91,  6,   103, 148, 254, 129, 65,  171, 228,
613    167, 224, 163, 9,   15,  206, 90,  58,  11,  205, 55,  211, 33,  87,  178, 149, 91,  28,  236,
614    218, 112, 231, 34,  82,  82,  134, 103, 137, 115, 27,  156, 102, 159, 220, 226, 89,  42,  25,
615    37,  9,   84,  239, 76,  161, 198, 72,  167, 163, 39,  91,  148, 191, 17,  191, 87,  169, 179,
616    136, 10,  194, 154, 4,   40,  107, 109, 61,  161, 20,  176, 247, 13,  214, 106, 229, 45,  17,
617    5,   60,  189, 64,  39,  166, 208, 14,  57,  25,  140, 148, 25,  177, 246, 189, 43,  181, 88,
618    204, 29,  126, 224, 100, 143, 93,  60,  57,  249, 55,  0,   87,  83,  227, 224, 166, 59,  214,
619    81,  144, 129, 58,  6,   57,  46,  254, 232, 41,  220, 209, 230, 167, 138, 158, 94,  180, 125,
620    247, 26,  162, 116, 238, 202, 187, 100, 65,  13,  180, 44,  245, 159, 83,  161, 176, 58,  72,
621    236, 109, 105, 160, 0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
622    0,   11,  0,   0,   0,   98,  0,   0,   0,   1,   0,   0,   32,  2,   0,   0,   0,   1,   0,
623    0,   32,  3,   0,   0,   0,   2,   0,   0,   16,  1,   0,   0,   0,   3,   0,   0,   48,  0,
624    1,   0,   0,   200, 0,   0,   80,  3,   0,   0,   0,   0,   0,   0,   0,   244, 1,   0,   112,
625    1,   246, 1,   0,   112, 1,   189, 2,   0,   96,  144, 178, 236, 250, 255, 255, 255, 255, 145,
626    1,   0,   96,  144, 226, 33,  60,  222, 2,   0,   0,   189, 2,   0,   96,  0,   0,   0,   0,
627    0,   0,   0,   0,   190, 2,   0,   16,  1,   0,   0,   0,   12,  0,   0,   0,   0,   0,   0,
628    0,   0,   0,   0,   0,   0,   0,   0,   0,   110, 0,   0,   0,   0,   0,   0,   0,   11,  0,
629    0,   0,   98,  0,   0,   0,   1,   0,   0,   32,  2,   0,   0,   0,   1,   0,   0,   32,  3,
630    0,   0,   0,   2,   0,   0,   16,  1,   0,   0,   0,   3,   0,   0,   48,  0,   1,   0,   0,
631    200, 0,   0,   80,  3,   0,   0,   0,   0,   0,   0,   0,   244, 1,   0,   112, 1,   246, 1,
632    0,   112, 1,   189, 2,   0,   96,  144, 178, 236, 250, 255, 255, 255, 255, 145, 1,   0,   96,
633    144, 226, 33,  60,  222, 2,   0,   0,   189, 2,   0,   96,  0,   0,   0,   0,   0,   0,   0,
634    0,   190, 2,   0,   16,  1,   0,   0,   0,
635};
636
637/*
638 * These tests don't have any assertions or expectations. They just try to parse garbage, to see if
639 * the result will be a crash.  This is especially informative when run under Valgrind memcheck.
640 */
641
642template <typename Message> void parse_garbage() {
643    for (int32_t ver = 0; ver <= MAX_MESSAGE_VERSION; ++ver) {
644        Message msg(ver);
645        const uint8_t* end = msgbuf + array_length(msgbuf);
646        for (size_t i = 0; i < array_length(msgbuf); ++i) {
647            const uint8_t* begin = msgbuf + i;
648            const uint8_t* p = begin;
649            msg.Deserialize(&p, end);
650        }
651    }
652
653    time_t now = time(NULL);
654    std::cout << "Seeding rand() with " << now << " for fuzz test." << std::endl;
655    srand(now);
656
657    // Fill large buffer with random bytes.
658    const int kBufSize = 10000;
659    UniquePtr<uint8_t[]> buf(new uint8_t[kBufSize]);
660    for (size_t i = 0; i < kBufSize; ++i)
661        buf[i] = static_cast<uint8_t>(rand());
662
663    for (uint32_t ver = 0; ver < MAX_MESSAGE_VERSION; ++ver) {
664        Message msg(ver);
665        const uint8_t* end = buf.get() + kBufSize;
666        for (size_t i = 0; i < kBufSize; ++i) {
667            const uint8_t* begin = buf.get() + i;
668            const uint8_t* p = begin;
669            msg.Deserialize(&p, end);
670        }
671    }
672}
673
674#define GARBAGE_TEST(Message)                                                                      \
675    TEST(GarbageTest, Message) { parse_garbage<Message>(); }
676
677GARBAGE_TEST(AbortOperationRequest);
678GARBAGE_TEST(AbortOperationResponse);
679GARBAGE_TEST(AddEntropyRequest);
680GARBAGE_TEST(AddEntropyResponse);
681GARBAGE_TEST(BeginOperationRequest);
682GARBAGE_TEST(BeginOperationResponse);
683GARBAGE_TEST(DeleteAllKeysRequest);
684GARBAGE_TEST(DeleteAllKeysResponse);
685GARBAGE_TEST(DeleteKeyRequest);
686GARBAGE_TEST(DeleteKeyResponse);
687GARBAGE_TEST(ExportKeyRequest);
688GARBAGE_TEST(ExportKeyResponse);
689GARBAGE_TEST(FinishOperationRequest);
690GARBAGE_TEST(FinishOperationResponse);
691GARBAGE_TEST(GenerateKeyRequest);
692GARBAGE_TEST(GenerateKeyResponse);
693GARBAGE_TEST(GetKeyCharacteristicsRequest);
694GARBAGE_TEST(GetKeyCharacteristicsResponse);
695GARBAGE_TEST(ImportKeyRequest);
696GARBAGE_TEST(ImportKeyResponse);
697GARBAGE_TEST(SupportedByAlgorithmAndPurposeRequest)
698GARBAGE_TEST(SupportedByAlgorithmRequest)
699GARBAGE_TEST(UpdateOperationRequest);
700GARBAGE_TEST(UpdateOperationResponse);
701GARBAGE_TEST(AttestKeyRequest);
702GARBAGE_TEST(AttestKeyResponse);
703GARBAGE_TEST(UpgradeKeyRequest);
704GARBAGE_TEST(UpgradeKeyResponse);
705
706// The macro doesn't work on this one.
707TEST(GarbageTest, SupportedResponse) {
708    parse_garbage<SupportedResponse<keymaster_digest_t>>();
709}
710
711}  // namespace test
712
713}  // namespace keymaster
714