Zentaro Kavanagh | ebe7d4d | 2018-10-25 17:53:34 -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 | #ifndef LIBTPMCRYPTO_TPM_CRYPTO_IMPL_H_ |
| 6 | #define LIBTPMCRYPTO_TPM_CRYPTO_IMPL_H_ |
| 7 | |
| 8 | #include <limits> |
| 9 | #include <memory> |
| 10 | #include <string> |
| 11 | #include <utility> |
| 12 | |
May Lippert | f4c04da | 2019-01-18 13:40:25 -0800 | [diff] [blame] | 13 | #include <brillo/brillo_export.h> |
| 14 | |
Zentaro Kavanagh | ebe7d4d | 2018-10-25 17:53:34 -0700 | [diff] [blame] | 15 | #include "libtpmcrypto/tpm.h" |
| 16 | #include "libtpmcrypto/tpm_crypto.h" |
| 17 | |
| 18 | namespace tpmcrypto { |
| 19 | |
May Lippert | f4c04da | 2019-01-18 13:40:25 -0800 | [diff] [blame] | 20 | class BRILLO_EXPORT TpmCryptoImpl : public TpmCrypto { |
Zentaro Kavanagh | ebe7d4d | 2018-10-25 17:53:34 -0700 | [diff] [blame] | 21 | public: |
| 22 | using RandBytesFn = std::function<int(uint8_t*, int)>; |
| 23 | |
| 24 | TpmCryptoImpl(); |
| 25 | explicit TpmCryptoImpl(std::unique_ptr<Tpm> tpm); |
| 26 | TpmCryptoImpl(std::unique_ptr<Tpm> tpm, RandBytesFn rand_bytes_fn); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 27 | TpmCryptoImpl(const TpmCryptoImpl&) = delete; |
| 28 | TpmCryptoImpl& operator=(const TpmCryptoImpl&) = delete; |
| 29 | |
Zentaro Kavanagh | ebe7d4d | 2018-10-25 17:53:34 -0700 | [diff] [blame] | 30 | ~TpmCryptoImpl() override; |
| 31 | |
| 32 | bool Encrypt(const brillo::SecureBlob& data, |
| 33 | std::string* encrypted_data) override WARN_UNUSED_RESULT; |
| 34 | |
| 35 | bool Decrypt(const std::string& encrypted_data, |
| 36 | brillo::SecureBlob* data) override WARN_UNUSED_RESULT; |
| 37 | |
| 38 | private: |
| 39 | // Creates a randomly generated aes key and seals it to the TPM's PCR0. |
| 40 | bool CreateSealedKey(brillo::SecureBlob* aes_key, |
Tom Hughes | 8ed2639 | 2020-08-24 18:00:03 -0700 | [diff] [blame] | 41 | brillo::SecureBlob* sealed_key) const WARN_UNUSED_RESULT; |
Zentaro Kavanagh | ebe7d4d | 2018-10-25 17:53:34 -0700 | [diff] [blame] | 42 | |
| 43 | // Encrypts the given data using the aes_key. Sealed key is necessary to |
| 44 | // wrap into the returned data to allow for decryption. |
| 45 | bool EncryptData(const brillo::SecureBlob& data, |
| 46 | const brillo::SecureBlob& aes_key, |
| 47 | const brillo::SecureBlob& sealed_key, |
Tom Hughes | 8ed2639 | 2020-08-24 18:00:03 -0700 | [diff] [blame] | 48 | std::string* encrypted_data) const WARN_UNUSED_RESULT; |
Zentaro Kavanagh | ebe7d4d | 2018-10-25 17:53:34 -0700 | [diff] [blame] | 49 | |
| 50 | // Gets random bytes and returns them in a SecureBlob. |
Tom Hughes | 8ed2639 | 2020-08-24 18:00:03 -0700 | [diff] [blame] | 51 | bool GetRandomDataSecureBlob(size_t length, brillo::SecureBlob* data) const |
| 52 | WARN_UNUSED_RESULT; |
Zentaro Kavanagh | ebe7d4d | 2018-10-25 17:53:34 -0700 | [diff] [blame] | 53 | |
| 54 | // The TPM implementation |
| 55 | std::unique_ptr<Tpm> tpm_; |
| 56 | RandBytesFn rand_bytes_fn_; |
Zentaro Kavanagh | ebe7d4d | 2018-10-25 17:53:34 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | } // namespace tpmcrypto |
| 60 | |
| 61 | #endif // LIBTPMCRYPTO_TPM_CRYPTO_IMPL_H_ |