2 * Copyright (c) 2000 - 2015 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
17 * @file encryption-decryption-env.cpp
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
22 #include <encryption-decryption-env.h>
26 EncryptionError SyncApi::encrypt(ckmc_param_list_h params,
27 const char *key_alias,
29 const ckmc_raw_buffer_s& decrypted,
30 ckmc_raw_buffer_s **ppencrypted)
32 return ckmcError2Result(ckmc_encrypt_data(params, key_alias, password, decrypted, ppencrypted));
35 EncryptionError SyncApi::decrypt(ckmc_param_list_h params,
36 const char *key_alias,
38 const ckmc_raw_buffer_s& encrypted,
39 ckmc_raw_buffer_s **ppdecrypted)
41 return ckmcError2Result(ckmc_decrypt_data(params, key_alias, password, encrypted, ppdecrypted));
44 EncryptionError SyncApi::ckmcError2Result(int error) {
46 case CKMC_ERROR_NONE: return EncryptionError::SUCCESS;
47 case CKMC_ERROR_INVALID_PARAMETER: return EncryptionError::INVALID_PARAM;
48 case CKMC_ERROR_SERVER_ERROR: return EncryptionError::SERVER_ERROR;
49 case CKMC_ERROR_DB_ALIAS_UNKNOWN: return EncryptionError::ALIAS_UNKNOWN;
50 case CKMC_ERROR_AUTHENTICATION_FAILED: return EncryptionError::AUTH_FAILED;
51 default: return EncryptionError::OTHER;
57 void AsyncApi::Observer::ReceivedError(int error) {
60 void AsyncApi::Observer::ReceivedEncrypted(RawBuffer && buffer) {
61 m_buffer = std::move(buffer);
65 void AsyncApi::Observer::ReceivedDecrypted(RawBuffer && buffer) {
66 m_buffer = std::move(buffer);
70 void AsyncApi::Observer::WaitForResponse() {
71 std::unique_lock<std::mutex> lock(m_mutex);
72 m_cv.wait(lock, [this] {return m_finished;});
74 void AsyncApi::Observer::Finished(int error)
81 EncryptionError AsyncApi::crypt(cryptoFn operation,
82 ckmc_param_list_h params,
83 const char *key_alias,
85 const ckmc_raw_buffer_s& in,
86 ckmc_raw_buffer_s **ppout)
88 // C++ API doesn't have to check that
89 if(!params || !key_alias || !ppout)
90 return EncryptionError::INVALID_PARAM;
92 CKM::ManagerAsync mgr;
93 std::shared_ptr<Observer> obs = std::make_shared<Observer>();
96 const CryptoAlgorithm* ca = reinterpret_cast<const CryptoAlgorithm*>(params);
104 RawBuffer inBuffer(in.data, in.data + in.size);
107 (mgr.*operation)(obs, *ca, key_alias, pass, inBuffer);
108 obs->WaitForResponse();
109 if(obs->m_error != CKM_API_SUCCESS)
110 return ckmError2Result(obs->m_error);
112 int ret = ckmc_buffer_new(obs->m_buffer.data(), obs->m_buffer.size(), ppout);
113 if (ret != CKMC_ERROR_NONE)
114 return EncryptionError::OTHER;
116 return EncryptionError::SUCCESS;
119 EncryptionError AsyncApi::encrypt(ckmc_param_list_h params,
120 const char *key_alias,
121 const char *password,
122 const ckmc_raw_buffer_s& plain,
123 ckmc_raw_buffer_s **ppencrypted)
125 return crypt(&CKM::ManagerAsync::encrypt, params, key_alias, password, plain, ppencrypted);
128 EncryptionError AsyncApi::decrypt(ckmc_param_list_h params,
129 const char *key_alias,
130 const char *password,
131 const ckmc_raw_buffer_s& encrypted,
132 ckmc_raw_buffer_s **ppdecrypted)
134 return crypt(&CKM::ManagerAsync::decrypt, params, key_alias, password, encrypted, ppdecrypted);
137 EncryptionError AsyncApi::ckmError2Result(int error)
140 case CKM_API_SUCCESS: return EncryptionError::SUCCESS;
141 case CKM_API_ERROR_INPUT_PARAM: return EncryptionError::INVALID_PARAM;
142 case CKM_API_ERROR_SERVER_ERROR: return EncryptionError::SERVER_ERROR;
143 case CKM_API_ERROR_DB_ALIAS_UNKNOWN: return EncryptionError::ALIAS_UNKNOWN;
144 case CKM_API_ERROR_AUTHENTICATION_FAILED: return EncryptionError::AUTH_FAILED;
145 default: return EncryptionError::OTHER;