CKM: Cipher API tests
[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     virtual bool symmetricOnly() const { return false; }
56
57     virtual ~EncryptionApi() {}
58 };
59
60 class SyncApi : public EncryptionApi
61 {
62 public:
63     EncryptionError encrypt(ckmc_param_list_h params,
64                             const char *key_alias,
65                             const char *password,
66                             const ckmc_raw_buffer_s& decrypted,
67                             ckmc_raw_buffer_s **ppencrypted) override;
68
69     EncryptionError decrypt(ckmc_param_list_h params,
70                             const char *key_alias,
71                             const char *password,
72                             const ckmc_raw_buffer_s& encrypted,
73                             ckmc_raw_buffer_s **ppdecrypted) override;
74 private:
75     static EncryptionError ckmcError2Result(int error);
76 };
77
78 struct AsyncApi : public EncryptionApi
79 {
80 private:
81     struct Observer : public CKM::ManagerAsync::Observer {
82         Observer() : m_finished(false), m_error(CKM_API_SUCCESS) {}
83
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);
89
90         std::mutex m_mutex;
91         std::condition_variable m_cv;
92         bool m_finished;
93         int m_error;
94         CKM::RawBuffer m_buffer;
95     };
96
97 public:
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;
103
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;
109 private:
110     typedef void (CKM::ManagerAsync::*cryptoFn)(const CKM::ManagerAsync::ObserverPtr&,
111                                                 const CKM::CryptoAlgorithm&,
112                                                 const CKM::Alias&,
113                                                 const CKM::Password&,
114                                                 const CKM::RawBuffer&);
115
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);
122
123     static EncryptionError ckmError2Result(int error);
124 };
125
126 class CipherApi : public EncryptionApi
127 {
128 public:
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;
134
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;
140
141     bool symmetricOnly() const override { return true; }
142
143 private:
144     int crypt(ckmc_cipher_ctx_h ctx, unsigned char *ptr, size_t left, CKM::RawBuffer& output);
145     EncryptionError ckmcError2Result(int error);
146 };
147