Zentaro Kavanagh | 26f0a73 | 2018-10-25 14:36:47 -0700 | [diff] [blame] | 1 | // Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "libtpmcrypto/tpm_proto_utils.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
| 9 | #include <base/logging.h> |
| 10 | #include <brillo/secure_blob.h> |
| 11 | |
| 12 | using brillo::SecureBlob; |
| 13 | |
| 14 | namespace tpmcrypto { |
| 15 | |
| 16 | bool CreateSerializedTpmCryptoProto(const SecureBlob& sealed_key, |
| 17 | const SecureBlob& iv, |
| 18 | const SecureBlob& tag, |
| 19 | const SecureBlob& encrypted_data, |
| 20 | std::string* serialized) { |
| 21 | TpmEncryptedData encrypted_pb; |
| 22 | encrypted_pb.set_sealed_key(sealed_key.data(), sealed_key.size()); |
| 23 | encrypted_pb.set_iv(iv.data(), iv.size()); |
| 24 | encrypted_pb.set_encrypted_data(encrypted_data.data(), encrypted_data.size()); |
| 25 | encrypted_pb.set_tag(tag.data(), tag.size()); |
| 26 | |
| 27 | if (!encrypted_pb.SerializeToString(serialized)) { |
| 28 | LOG(ERROR) << "Could not serialize TpmEncryptedData proto to string."; |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | return true; |
| 33 | } |
| 34 | |
| 35 | bool ParseTpmCryptoProto(const std::string& serialized, |
| 36 | SecureBlob* sealed_key, |
| 37 | SecureBlob* iv, |
| 38 | SecureBlob* tag, |
| 39 | SecureBlob* encrypted_data) { |
| 40 | TpmEncryptedData encrypted_pb; |
| 41 | if (!encrypted_pb.ParseFromString(serialized)) { |
| 42 | LOG(ERROR) << "Could not decrypt data as it was not a TpmEncryptedData " |
| 43 | << "protobuf"; |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | SecureBlob tmp_sealed_key(encrypted_pb.sealed_key().begin(), |
| 48 | encrypted_pb.sealed_key().end()); |
| 49 | SecureBlob tmp_iv(encrypted_pb.iv().begin(), encrypted_pb.iv().end()); |
| 50 | SecureBlob tmp_tag(encrypted_pb.tag().begin(), encrypted_pb.tag().end()); |
| 51 | SecureBlob tmp_encrypted_data(encrypted_pb.encrypted_data().begin(), |
| 52 | encrypted_pb.encrypted_data().end()); |
| 53 | |
| 54 | *sealed_key = std::move(tmp_sealed_key); |
| 55 | *iv = std::move(tmp_iv); |
| 56 | *tag = std::move(tmp_tag); |
| 57 | *encrypted_data = std::move(tmp_encrypted_data); |
| 58 | |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | } // namespace tpmcrypto |