1af99e55fc5c0b7a2ee8b849d74dce0dbb307bed
[platform/core/test/security-tests.git] / src / ckm / unprivileged / encryption-decryption-env.h
1 /*
2  *  Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
15  */
16 /*
17  * @file       encryption-decryption-env.h
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #pragma once
23
24 #include <memory>
25 #include <mutex>
26 #include <condition_variable>
27
28 #include <ckmc/ckmc-type.h>
29 #include <ckmc/ckmc-manager.h>
30 #include <ckm/ckm-manager-async.h>
31
32 enum EncryptionError{
33     SUCCESS,
34     INVALID_PARAM,
35     SERVER_ERROR,
36     ALIAS_UNKNOWN,
37     AUTH_FAILED,
38     OTHER,
39 };
40
41 struct EncryptionApi
42 {
43     virtual EncryptionError encrypt(ckmc_param_list_h params,
44                                     const char *key_alias,
45                                     const char *password,
46                                     const ckmc_raw_buffer_s& decrypted,
47                                     ckmc_raw_buffer_s **ppencrypted) = 0;
48
49     virtual EncryptionError decrypt(ckmc_param_list_h params,
50                                     const char *key_alias,
51                                     const char *password,
52                                     const ckmc_raw_buffer_s& encrypted,
53                                     ckmc_raw_buffer_s **ppdecrypted) = 0;
54 };
55
56 class SyncApi : public EncryptionApi
57 {
58 public:
59     virtual EncryptionError encrypt(ckmc_param_list_h params,
60                                     const char *key_alias,
61                                     const char *password,
62                                     const ckmc_raw_buffer_s& decrypted,
63                                     ckmc_raw_buffer_s **ppencrypted);
64
65     virtual EncryptionError decrypt(ckmc_param_list_h params,
66                                     const char *key_alias,
67                                     const char *password,
68                                     const ckmc_raw_buffer_s& encrypted,
69                                     ckmc_raw_buffer_s **ppdecrypted);
70 private:
71     static EncryptionError ckmcError2Result(int error);
72 };
73
74 struct AsyncApi : public EncryptionApi
75 {
76 private:
77     struct Observer : public CKM::ManagerAsync::Observer {
78         Observer() : m_finished(false), m_error(CKM_API_SUCCESS) {}
79
80         void ReceivedError(int error);
81         void ReceivedEncrypted(CKM::RawBuffer && buffer);
82         void ReceivedDecrypted(CKM::RawBuffer && buffer);
83         void WaitForResponse();
84         void Finished(int error = CKMC_ERROR_NONE);
85
86         std::mutex m_mutex;
87         std::condition_variable m_cv;
88         bool m_finished;
89         int m_error;
90         CKM::RawBuffer m_buffer;
91     };
92
93 public:
94     EncryptionError encrypt(ckmc_param_list_h params,
95                             const char *key_alias,
96                             const char *password,
97                             const ckmc_raw_buffer_s& decrypted,
98                             ckmc_raw_buffer_s **ppencrypted);
99
100     EncryptionError decrypt(ckmc_param_list_h params,
101                             const char *key_alias,
102                             const char *password,
103                             const ckmc_raw_buffer_s& encrypted,
104                             ckmc_raw_buffer_s **ppdecrypted);
105 private:
106     typedef void (CKM::ManagerAsync::*cryptoFn)(const CKM::ManagerAsync::ObserverPtr&,
107                                                 const CKM::CryptoAlgorithm&,
108                                                 const CKM::Alias&,
109                                                 const CKM::Password&,
110                                                 const CKM::RawBuffer&);
111
112     EncryptionError crypt(cryptoFn operation,
113                           ckmc_param_list_h params,
114                           const char *key_alias,
115                           const char *password,
116                           const ckmc_raw_buffer_s& in,
117                           ckmc_raw_buffer_s **ppout);
118
119     static EncryptionError ckmError2Result(int error);
120 };
121
122
123