Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 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 "google_keymaster_messages.h" |
| 18 | |
| 19 | namespace keymaster { |
| 20 | |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 21 | size_t KeymasterResponse::SerializedSize() const { |
| 22 | if (error != KM_ERROR_OK) |
| 23 | return sizeof(int32_t); |
| 24 | else |
| 25 | return sizeof(int32_t) + NonErrorSerializedSize(); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 26 | } |
| 27 | |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 28 | uint8_t* KeymasterResponse::Serialize(uint8_t* buf, const uint8_t* end) const { |
| 29 | buf = append_uint32_to_buf(buf, end, static_cast<uint32_t>(error)); |
| 30 | if (error == KM_ERROR_OK) |
| 31 | buf = NonErrorSerialize(buf, end); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 32 | return buf; |
| 33 | } |
| 34 | |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 35 | bool KeymasterResponse::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 36 | if (!copy_uint32_from_buf(buf_ptr, end, &error)) |
| 37 | return false; |
| 38 | if (error != KM_ERROR_OK) |
| 39 | return true; |
| 40 | return NonErrorDeserialize(buf_ptr, end); |
| 41 | } |
| 42 | |
| 43 | size_t SupportedAlgorithmsResponse::NonErrorSerializedSize() const { |
| 44 | return sizeof(uint32_t) + sizeof(uint32_t) * algorithms_length; |
| 45 | } |
| 46 | |
| 47 | uint8_t* SupportedAlgorithmsResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
| 48 | return append_uint32_array_to_buf(buf, end, algorithms, algorithms_length); |
| 49 | } |
| 50 | |
| 51 | bool SupportedAlgorithmsResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 52 | delete[] algorithms; |
| 53 | return copy_uint32_array_from_buf(buf_ptr, end, &algorithms, &algorithms_length); |
| 54 | } |
| 55 | |
| 56 | GenerateKeyResponse::~GenerateKeyResponse() { |
| 57 | delete[] key_blob.key_material; |
| 58 | } |
| 59 | |
| 60 | size_t GenerateKeyResponse::NonErrorSerializedSize() const { |
| 61 | return sizeof(uint32_t) /* key size */ + key_blob.key_material_size + |
| 62 | sizeof(uint32_t) /* enforced size */ + enforced.SerializedSize() + |
| 63 | sizeof(uint32_t) /* unenforced size */ + unenforced.SerializedSize(); |
| 64 | } |
| 65 | |
| 66 | uint8_t* GenerateKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
| 67 | buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size); |
| 68 | buf = append_uint32_to_buf(buf, end, enforced.SerializedSize()); |
| 69 | buf = enforced.Serialize(buf, end); |
| 70 | buf = append_uint32_to_buf(buf, end, unenforced.SerializedSize()); |
| 71 | return unenforced.Serialize(buf, end); |
| 72 | } |
| 73 | |
| 74 | bool GenerateKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 75 | delete[] key_blob.key_material; |
| 76 | |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 77 | uint32_t enforced_size; |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 78 | uint32_t unenforced_size; |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 79 | return copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size, |
| 80 | &key_blob.key_material) && |
| 81 | copy_uint32_from_buf(buf_ptr, end, &enforced_size) && |
| 82 | enforced.Deserialize(buf_ptr, *buf_ptr + enforced_size) && |
| 83 | copy_uint32_from_buf(buf_ptr, end, &unenforced_size) && |
| 84 | unenforced.Deserialize(buf_ptr, *buf_ptr + unenforced_size); |
| 85 | } |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 86 | |
Shawn Willden | da8485e | 2014-08-17 08:00:01 -0600 | [diff] [blame] | 87 | GetKeyCharacteristicsRequest::~GetKeyCharacteristicsRequest() { |
| 88 | delete[] key_blob.key_material; |
| 89 | } |
| 90 | |
| 91 | void GetKeyCharacteristicsRequest::SetKeyMaterial(const void* key_material, size_t length) { |
| 92 | delete[] key_blob.key_material; |
| 93 | key_blob.key_material = dup_buffer(key_material, length); |
| 94 | key_blob.key_material_size = length; |
| 95 | } |
| 96 | |
| 97 | size_t GetKeyCharacteristicsRequest::SerializedSize() const { |
| 98 | return sizeof(uint32_t) /* key blob size */ + key_blob.key_material_size + |
| 99 | sizeof(uint32_t) /* size of additional_params */ + additional_params.SerializedSize(); |
| 100 | } |
| 101 | |
| 102 | uint8_t* GetKeyCharacteristicsRequest::Serialize(uint8_t* buf, const uint8_t* end) const { |
| 103 | buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size); |
| 104 | buf = append_uint32_to_buf(buf, end, additional_params.SerializedSize()); |
| 105 | return additional_params.Serialize(buf, end); |
| 106 | } |
| 107 | |
| 108 | bool GetKeyCharacteristicsRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 109 | uint8_t additional_params_size; |
| 110 | return copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size, |
| 111 | &key_blob.key_material) && |
| 112 | copy_uint32_from_buf(buf_ptr, end, &additional_params_size) && |
| 113 | additional_params.Deserialize(buf_ptr, *buf_ptr + additional_params_size); |
| 114 | } |
| 115 | |
| 116 | size_t GetKeyCharacteristicsResponse::NonErrorSerializedSize() const { |
| 117 | return sizeof(uint32_t) /* enforced size */ + enforced.SerializedSize() + |
| 118 | sizeof(uint32_t) /* unenforced size */ + unenforced.SerializedSize(); |
| 119 | } |
| 120 | |
| 121 | uint8_t* GetKeyCharacteristicsResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
| 122 | buf = append_uint32_to_buf(buf, end, enforced.SerializedSize()); |
| 123 | buf = enforced.Serialize(buf, end); |
| 124 | buf = append_uint32_to_buf(buf, end, unenforced.SerializedSize()); |
| 125 | return unenforced.Serialize(buf, end); |
| 126 | } |
| 127 | |
| 128 | bool GetKeyCharacteristicsResponse::NonErrorDeserialize(const uint8_t** buf_ptr, |
| 129 | const uint8_t* end) { |
| 130 | uint32_t enforced_size; |
| 131 | uint32_t unenforced_size; |
| 132 | return copy_uint32_from_buf(buf_ptr, end, &enforced_size) && |
| 133 | enforced.Deserialize(buf_ptr, *buf_ptr + enforced_size) && |
| 134 | copy_uint32_from_buf(buf_ptr, end, &unenforced_size) && |
| 135 | unenforced.Deserialize(buf_ptr, *buf_ptr + unenforced_size); |
| 136 | } |
| 137 | |
| 138 | void BeginOperationRequest::SetKeyMaterial(const void* key_material, size_t length) { |
| 139 | delete[] key_blob.key_material; |
| 140 | key_blob.key_material = dup_buffer(key_material, length); |
| 141 | key_blob.key_material_size = length; |
| 142 | } |
| 143 | |
| 144 | size_t BeginOperationRequest::SerializedSize() const { |
| 145 | return sizeof(uint32_t) /* purpose */ + sizeof(uint32_t) /* key length */ + |
| 146 | key_blob.key_material_size + additional_params.SerializedSize(); |
| 147 | } |
| 148 | |
| 149 | uint8_t* BeginOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const { |
| 150 | buf = append_uint32_to_buf(buf, end, purpose); |
| 151 | buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size); |
| 152 | return additional_params.Serialize(buf, end); |
| 153 | } |
| 154 | |
| 155 | bool BeginOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 156 | return copy_uint32_from_buf(buf_ptr, end, &purpose) && |
| 157 | copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size, |
| 158 | &key_blob.key_material) && |
| 159 | additional_params.Deserialize(buf_ptr, end); |
| 160 | } |
| 161 | |
| 162 | size_t BeginOperationResponse::NonErrorSerializedSize() const { |
| 163 | return sizeof(op_handle); |
| 164 | } |
| 165 | |
| 166 | uint8_t* BeginOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
| 167 | return append_uint64_to_buf(buf, end, op_handle); |
| 168 | } |
| 169 | |
| 170 | bool BeginOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 171 | return copy_uint64_from_buf(buf_ptr, end, &op_handle); |
| 172 | } |
| 173 | |
| 174 | size_t UpdateOperationRequest::SerializedSize() const { |
| 175 | return sizeof(op_handle) + input.SerializedSize(); |
| 176 | } |
| 177 | |
| 178 | uint8_t* UpdateOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const { |
| 179 | buf = append_uint64_to_buf(buf, end, op_handle); |
| 180 | return input.Serialize(buf, end); |
| 181 | } |
| 182 | |
| 183 | bool UpdateOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 184 | return copy_uint64_from_buf(buf_ptr, end, &op_handle) && input.Deserialize(buf_ptr, end); |
| 185 | } |
| 186 | |
| 187 | size_t UpdateOperationResponse::NonErrorSerializedSize() const { |
| 188 | return output.SerializedSize(); |
| 189 | } |
| 190 | |
| 191 | uint8_t* UpdateOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
| 192 | return output.Serialize(buf, end); |
| 193 | } |
| 194 | |
| 195 | bool UpdateOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 196 | return output.Deserialize(buf_ptr, end); |
| 197 | } |
| 198 | |
| 199 | size_t FinishOperationRequest::SerializedSize() const { |
| 200 | return sizeof(op_handle) + signature.SerializedSize(); |
| 201 | } |
| 202 | |
| 203 | uint8_t* FinishOperationRequest::Serialize(uint8_t* buf, const uint8_t* end) const { |
| 204 | buf = append_uint64_to_buf(buf, end, op_handle); |
| 205 | return signature.Serialize(buf, end); |
| 206 | } |
| 207 | |
| 208 | bool FinishOperationRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 209 | return copy_uint64_from_buf(buf_ptr, end, &op_handle) && signature.Deserialize(buf_ptr, end); |
| 210 | } |
| 211 | |
| 212 | size_t FinishOperationResponse::NonErrorSerializedSize() const { |
| 213 | return output.SerializedSize(); |
| 214 | } |
| 215 | |
| 216 | uint8_t* FinishOperationResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
| 217 | return output.Serialize(buf, end); |
| 218 | } |
| 219 | |
| 220 | bool FinishOperationResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 221 | return output.Deserialize(buf_ptr, end); |
| 222 | } |
| 223 | |
| 224 | void ImportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) { |
| 225 | delete[] key_data; |
| 226 | key_data = dup_buffer(key_material, length); |
| 227 | key_data_length = length; |
| 228 | } |
| 229 | |
| 230 | size_t ImportKeyRequest::SerializedSize() const { |
| 231 | return sizeof(uint32_t) /* additional_params size */ + additional_params.SerializedSize() + |
| 232 | sizeof(uint32_t) /* key_format */ + sizeof(uint32_t) /* key_data_length */ + |
| 233 | key_data_length; |
| 234 | } |
| 235 | |
| 236 | uint8_t* ImportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const { |
| 237 | buf = append_uint32_to_buf(buf, end, additional_params.SerializedSize()); |
| 238 | buf = additional_params.Serialize(buf, end); |
| 239 | buf = append_uint32_to_buf(buf, end, key_format); |
| 240 | return append_size_and_data_to_buf(buf, end, key_data, key_data_length); |
| 241 | } |
| 242 | |
| 243 | bool ImportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 244 | uint32_t additional_params_size; |
| 245 | return copy_uint32_from_buf(buf_ptr, end, &additional_params_size) && |
| 246 | additional_params.Deserialize(buf_ptr, *buf_ptr + additional_params_size) && |
| 247 | copy_uint32_from_buf(buf_ptr, end, &key_format) && |
| 248 | copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &key_data); |
| 249 | } |
| 250 | |
| 251 | void ImportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) { |
| 252 | delete[] key_blob.key_material; |
| 253 | key_blob.key_material = dup_buffer(key_material, length); |
| 254 | key_blob.key_material_size = length; |
| 255 | } |
| 256 | |
| 257 | size_t ImportKeyResponse::NonErrorSerializedSize() const { |
| 258 | return sizeof(uint32_t) /* key_material length */ + key_blob.key_material_size + |
| 259 | sizeof(uint32_t) /* enforced length */ + enforced.SerializedSize() + |
| 260 | sizeof(uint32_t) /* unenforced length */ + unenforced.SerializedSize(); |
| 261 | } |
| 262 | |
| 263 | uint8_t* ImportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
| 264 | buf = append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size); |
| 265 | buf = append_uint32_to_buf(buf, end, enforced.SerializedSize()); |
| 266 | buf = enforced.Serialize(buf, end); |
| 267 | buf = append_uint32_to_buf(buf, end, unenforced.SerializedSize()); |
| 268 | return unenforced.Serialize(buf, end); |
| 269 | } |
| 270 | |
| 271 | bool ImportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 272 | uint8_t enforced_size; |
| 273 | uint8_t unenforced_size; |
| 274 | return copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size, |
| 275 | &key_blob.key_material) && |
| 276 | copy_uint32_from_buf(buf_ptr, end, &enforced_size) && |
| 277 | enforced.Deserialize(buf_ptr, *buf_ptr + enforced_size) && |
| 278 | copy_uint32_from_buf(buf_ptr, end, &unenforced_size) && |
| 279 | unenforced.Deserialize(buf_ptr, *buf_ptr + enforced_size); |
| 280 | } |
| 281 | |
| 282 | void ExportKeyRequest::SetKeyMaterial(const void* key_material, size_t length) { |
| 283 | delete[] key_blob.key_material; |
| 284 | key_blob.key_material = dup_buffer(key_material, length); |
| 285 | key_blob.key_material_size = length; |
| 286 | } |
| 287 | |
| 288 | size_t ExportKeyRequest::SerializedSize() const { |
| 289 | return sizeof(uint32_t) /* aditional_data length */ + additional_params.SerializedSize() + |
| 290 | sizeof(uint32_t) /* key_format */ + sizeof(uint32_t) /* key_material_size */ + |
| 291 | key_blob.key_material_size; |
| 292 | } |
| 293 | |
| 294 | uint8_t* ExportKeyRequest::Serialize(uint8_t* buf, const uint8_t* end) const { |
| 295 | buf = append_uint32_to_buf(buf, end, additional_params.SerializedSize()); |
| 296 | buf = additional_params.Serialize(buf, end); |
| 297 | buf = append_uint32_to_buf(buf, end, key_format); |
| 298 | return append_size_and_data_to_buf(buf, end, key_blob.key_material, key_blob.key_material_size); |
| 299 | } |
| 300 | |
| 301 | bool ExportKeyRequest::Deserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 302 | size_t additional_params_size; |
| 303 | return copy_uint32_from_buf(buf_ptr, end, &additional_params_size) && |
| 304 | additional_params.Deserialize(buf_ptr, *buf_ptr + additional_params_size) && |
| 305 | copy_uint32_from_buf(buf_ptr, end, &key_format) && |
| 306 | copy_size_and_data_from_buf(buf_ptr, end, &key_blob.key_material_size, |
| 307 | &key_blob.key_material); |
| 308 | } |
| 309 | |
| 310 | void ExportKeyResponse::SetKeyMaterial(const void* key_material, size_t length) { |
| 311 | delete[] key_data; |
| 312 | key_data = dup_buffer(key_material, length); |
| 313 | key_data_length = length; |
| 314 | } |
| 315 | |
| 316 | size_t ExportKeyResponse::NonErrorSerializedSize() const { |
| 317 | return sizeof(uint32_t) /* key_data_length */ + key_data_length; |
| 318 | } |
| 319 | |
| 320 | uint8_t* ExportKeyResponse::NonErrorSerialize(uint8_t* buf, const uint8_t* end) const { |
| 321 | return append_size_and_data_to_buf(buf, end, key_data, key_data_length); |
| 322 | } |
| 323 | |
| 324 | bool ExportKeyResponse::NonErrorDeserialize(const uint8_t** buf_ptr, const uint8_t* end) { |
| 325 | return copy_size_and_data_from_buf(buf_ptr, end, &key_data_length, &key_data); |
Shawn Willden | 128ffe0 | 2014-08-06 12:31:33 -0600 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | } // namespace keymaster |