2 * Copyright (c) 2015 - 2019 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
18 * @author Bartłomiej Grzelewski (b.grzelewski@samsung.com)
19 * @author Lukasz Kostyra (l.kostyra@samsung.com)
22 #include <dpl/log/log.h>
24 #include <crypto-backend.h>
26 #include <platform/decider.h>
28 #include <generic-backend/exception.h>
29 #include <sw-backend/store.h>
31 #ifdef TZ_BACKEND_ENABLED
32 #include <tz-backend/store.h>
33 #include <tz-backend/tz-context.h>
35 #include <tee_client_api.h>
36 #include <km_ta_defines.h>
37 #endif // TZ_BACKEND_ENABLED
49 std::string ValueToString(const T& value)
51 std::stringstream str;
52 // we need to re-cast because otherwise stringstream
53 // will write our value incorrectly
54 str << std::setfill('0') << std::setw(2 * sizeof(T)) << std::hex
55 << static_cast<uint64_t>(value);
59 CryptoBackend chooseCryptoBackend(const DataParams& params)
61 #ifdef TZ_BACKEND_ENABLED
62 if (params.size() != 1 && params.size() != 2) {
63 ThrowErr(Exc::Crypto::InternalError, "Invalid number of key parameters provided to decider");
66 // user directly point proper backend - we will not discuss with it
67 if (params[0].policy.backend == CKM::PolicyBackend::FORCE_SOFTWARE)
68 return CryptoBackend::OpenSSL;
70 // user directly point proper backend - we will not discuss with it
71 if (params[0].policy.backend == CKM::PolicyBackend::FORCE_HARDWARE)
72 return CryptoBackend::TrustZone;
74 if (params.size() == 1) {
75 // For now only software backend supports device encyption key
76 // TODO tz-backend could support the master key, but it would require
77 // hardcoding a known key ID and querying TA whether the key is
79 if (params[0].encrypted) {
80 return CryptoBackend::OpenSSL;
83 // tz-backend allows only for data binary export
84 if (params[0].policy.extractable && !params[0].data.isBinaryData()) {
85 return CryptoBackend::OpenSSL;
88 // Use TrustZone only with symmetric keys or unencrypted binary
89 // data until asymmetric cryptography is implemented
90 if (!params[0].data.isSKey() && !params[0].data.isBinaryData()) {
91 return CryptoBackend::OpenSSL;
93 } else if (params.size() == 2) {
94 // extractable private key can only be handled by OpenSSL
95 if (params[0].policy.extractable) {
96 return CryptoBackend::OpenSSL;
99 // ECDSA algorithm is unsupported by GP API 1.0
100 if (params[0].data.isEllipticCurve() || params[1].data.isEllipticCurve()) {
101 return CryptoBackend::OpenSSL;
106 LogDebug("Trying to open TA session...");
107 TZ::Internals::TrustZoneContext::Instance();
108 } catch (const Exc::Crypto::InternalError& e) {
109 LogDebug("...failed. Selecting SW backend.");
110 return CryptoBackend::OpenSSL;
113 LogDebug("...succeeded. Selecting TZ backend.");
114 return CryptoBackend::TrustZone;
116 #else // TZ_BACKEND_ENABLED
118 return CryptoBackend::OpenSSL;
119 #endif // TZ_BACKEND_ENABLED
125 : m_swStore(new SW::Store(CryptoBackend::OpenSSL))
126 #ifdef TZ_BACKEND_ENABLED
127 , m_tzStore(new TZ::Store(CryptoBackend::TrustZone))
132 GStore &Decider::getStore(const Token &token) const
134 return getStore(token.backendId);
137 GStore &Decider::getStore(CryptoBackend cryptoBackend) const
139 GStore *gStore = NULL;
141 if (cryptoBackend == CryptoBackend::OpenSSL)
142 gStore = m_swStore.get();
143 #ifdef TZ_BACKEND_ENABLED
144 if (cryptoBackend == CryptoBackend::TrustZone)
145 gStore = m_tzStore.get();
150 ThrowErr(Exc::Crypto::InternalError,
151 "Backend not available. BackendId: ", (int)cryptoBackend);
154 GStore &Decider::getStore(DataType data, const Policy &policy, bool encrypted) const
157 DataParam(data, policy, encrypted)
160 return getStore(chooseCryptoBackend(params));
163 GStore &Decider::getStore(const DataParams& params) const
165 return getStore(chooseCryptoBackend(params));
168 } // namespace Crypto