1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #ifndef TRUSTY_KEYMASTER_SERIALIZABLE_H_
26 #define TRUSTY_KEYMASTER_SERIALIZABLE_H_
27 
28 #include <trusty/keymaster.h>
29 
30 /**
31  * Simple serialization routines for dynamically sized keymaster messages.
32  */
33 
34 /**
35  * Appends |data_len| bytes at |data| to |buf|. Performs no bounds checking,
36  * assumes sufficient memory allocated at |buf|. Returns |buf| + |data_len|.
37  */
38 uint8_t* append_to_buf(uint8_t* buf, const void* data, size_t data_len);
39 
40 /**
41  * Appends |val| to |buf|. Performs no bounds checking. Returns |buf| +
42  * sizeof(uint32_t).
43  */
44 uint8_t* append_uint32_to_buf(uint8_t* buf, uint32_t val);
45 
46 /**
47  * Appends a sized buffer to |buf|. First appends |data_len| to |buf|, then
48  * appends |data_len| bytes at |data| to |buf|. Performs no bounds checking.
49  * Returns |buf| + sizeof(uint32_t) + |data_len|.
50  */
51 uint8_t* append_sized_buf_to_buf(uint8_t* buf,
52                                  const uint8_t* data,
53                                  uint32_t data_len);
54 
55 /**
56  * Serializes a km_boot_params structure. On success, allocates |*out_size|
57  * bytes to |*out| and writes the serialized |params| to |*out|. Caller takes
58  * ownership of |*out|. Returns one of trusty_err.
59  */
60 int km_boot_params_serialize(const struct km_boot_params* params,
61                              uint8_t** out,
62                              uint32_t* out_size);
63 
64 /**
65  * Serializes a km_attestation_data structure. On success, allocates |*out_size|
66  * bytes to |*out| and writes the serialized |data| to |*out|. Caller takes
67  * ownership of |*out|. Returns one of trusty_err.
68  */
69 int km_attestation_data_serialize(const struct km_attestation_data* data,
70                                   uint8_t** out,
71                                   uint32_t* out_size);
72 
73 /**
74  * Serializes a km_raw_buffer structure. On success, allocates |*out_size|
75  * bytes to |*out| and writes the serialized |data| to |*out|. Caller takes
76  * ownership of |*out|. Returns one of trusty_err.
77  */
78 int km_raw_buffer_serialize(const struct km_raw_buffer* buf,
79                             uint8_t** out,
80                             uint32_t* out_size);
81 
82 #endif /* TRUSTY_KEYMASTER_SERIALIZABLE_H_ */
83