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.h
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
26 #include <condition_variable>
28 #include <ckmc/ckmc-type.h>
29 #include <ckmc/ckmc-manager.h>
30 #include <ckm/ckm-manager-async.h>
43 virtual EncryptionError encrypt(ckmc_param_list_h params,
44 const char *key_alias,
46 const ckmc_raw_buffer_s& decrypted,
47 ckmc_raw_buffer_s **ppencrypted) = 0;
49 virtual EncryptionError decrypt(ckmc_param_list_h params,
50 const char *key_alias,
52 const ckmc_raw_buffer_s& encrypted,
53 ckmc_raw_buffer_s **ppdecrypted) = 0;
55 virtual bool symmetricOnly() const { return false; }
57 virtual ~EncryptionApi() {}
60 class SyncApi : public EncryptionApi
63 EncryptionError encrypt(ckmc_param_list_h params,
64 const char *key_alias,
66 const ckmc_raw_buffer_s& decrypted,
67 ckmc_raw_buffer_s **ppencrypted) override;
69 EncryptionError decrypt(ckmc_param_list_h params,
70 const char *key_alias,
72 const ckmc_raw_buffer_s& encrypted,
73 ckmc_raw_buffer_s **ppdecrypted) override;
75 static EncryptionError ckmcError2Result(int error);
78 struct AsyncApi : public EncryptionApi
81 struct Observer : public CKM::ManagerAsync::Observer {
82 Observer() : m_finished(false), m_error(CKM_API_SUCCESS) {}
84 void ReceivedError(int error);
85 void ReceivedEncrypted(CKM::RawBuffer && buffer);
86 void ReceivedDecrypted(CKM::RawBuffer && buffer);
87 void WaitForResponse();
88 void Finished(int error = CKMC_ERROR_NONE);
91 std::condition_variable m_cv;
94 CKM::RawBuffer m_buffer;
98 EncryptionError encrypt(ckmc_param_list_h params,
99 const char *key_alias,
100 const char *password,
101 const ckmc_raw_buffer_s& decrypted,
102 ckmc_raw_buffer_s **ppencrypted) override;
104 EncryptionError decrypt(ckmc_param_list_h params,
105 const char *key_alias,
106 const char *password,
107 const ckmc_raw_buffer_s& encrypted,
108 ckmc_raw_buffer_s **ppdecrypted) override;
110 typedef void (CKM::ManagerAsync::*cryptoFn)(const CKM::ManagerAsync::ObserverPtr&,
111 const CKM::CryptoAlgorithm&,
113 const CKM::Password&,
114 const CKM::RawBuffer&);
116 EncryptionError crypt(cryptoFn operation,
117 ckmc_param_list_h params,
118 const char *key_alias,
119 const char *password,
120 const ckmc_raw_buffer_s& in,
121 ckmc_raw_buffer_s **ppout);
123 static EncryptionError ckmError2Result(int error);
126 class CipherApi : public EncryptionApi
129 EncryptionError encrypt(ckmc_param_list_h params,
130 const char *key_alias,
131 const char *password,
132 const ckmc_raw_buffer_s& decrypted,
133 ckmc_raw_buffer_s **ppencrypted) override;
135 EncryptionError decrypt(ckmc_param_list_h params,
136 const char *key_alias,
137 const char *password,
138 const ckmc_raw_buffer_s& encrypted,
139 ckmc_raw_buffer_s **ppdecrypted) override;
141 bool symmetricOnly() const override { return true; }
144 int crypt(ckmc_cipher_ctx_h ctx, unsigned char *ptr, size_t left, CKM::RawBuffer& output);
145 EncryptionError ckmcError2Result(int error);