blob: 39e3eb363f5017b89dcc7f3722be828629661b76 [file] [log] [blame]
Zentaro Kavanaghebe7d4d2018-10-25 17:53:34 -07001// 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 Lippertf4c04da2019-01-18 13:40:25 -080013#include <brillo/brillo_export.h>
14
Zentaro Kavanaghebe7d4d2018-10-25 17:53:34 -070015#include "libtpmcrypto/tpm.h"
16#include "libtpmcrypto/tpm_crypto.h"
17
18namespace tpmcrypto {
19
May Lippertf4c04da2019-01-18 13:40:25 -080020class BRILLO_EXPORT TpmCryptoImpl : public TpmCrypto {
Zentaro Kavanaghebe7d4d2018-10-25 17:53:34 -070021 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 Fan6bc59e12020-11-11 02:51:06 +090027 TpmCryptoImpl(const TpmCryptoImpl&) = delete;
28 TpmCryptoImpl& operator=(const TpmCryptoImpl&) = delete;
29
Zentaro Kavanaghebe7d4d2018-10-25 17:53:34 -070030 ~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 Hughes8ed26392020-08-24 18:00:03 -070041 brillo::SecureBlob* sealed_key) const WARN_UNUSED_RESULT;
Zentaro Kavanaghebe7d4d2018-10-25 17:53:34 -070042
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 Hughes8ed26392020-08-24 18:00:03 -070048 std::string* encrypted_data) const WARN_UNUSED_RESULT;
Zentaro Kavanaghebe7d4d2018-10-25 17:53:34 -070049
50 // Gets random bytes and returns them in a SecureBlob.
Tom Hughes8ed26392020-08-24 18:00:03 -070051 bool GetRandomDataSecureBlob(size_t length, brillo::SecureBlob* data) const
52 WARN_UNUSED_RESULT;
Zentaro Kavanaghebe7d4d2018-10-25 17:53:34 -070053
54 // The TPM implementation
55 std::unique_ptr<Tpm> tpm_;
56 RandBytesFn rand_bytes_fn_;
Zentaro Kavanaghebe7d4d2018-10-25 17:53:34 -070057};
58
59} // namespace tpmcrypto
60
61#endif // LIBTPMCRYPTO_TPM_CRYPTO_IMPL_H_