Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / security / pkcs / FSecPkcsPkcs05PbKdf2Parameters.cpp
1 //
2 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
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 /**
18  * @file                FSecPkcsPkcs05PbKdf2Parameters.cpp
19  * @brief               This is the implementation file for Pkcs05PbKdf2Parameters class.
20  *
21  * This header file contains the implementation of Pkcs05PbKdf2Parameters class.
22  *
23  */
24
25 #include <unique_ptr.h>
26 #include <FBaseSysLog.h>
27 #include <FBaseByteBuffer.h>
28 #include <FBaseResult.h>
29 #include <FBaseInteger.h>
30 #include <FSecPkcsPkcs05PbKdf2Parameters.h>
31 #include <FSecPkcsIAlgorithmParameters.h>
32 #include <FSecPkcsAlgorithmIdentifier.h>
33
34 #include "FSecPkcs_Pkcs05PbKdf2ParametersImpl.h"
35
36 using namespace Tizen::Base;
37
38 namespace Tizen { namespace Security { namespace Pkcs
39 {
40
41 Pkcs05PbKdf2Parameters::Pkcs05PbKdf2Parameters(void)
42         : __pPkcs05PbKdf2ParametersImpl(null)
43 {
44
45 }
46
47 Pkcs05PbKdf2Parameters::~Pkcs05PbKdf2Parameters(void)
48 {
49         delete __pPkcs05PbKdf2ParametersImpl;
50 }
51
52 result
53 Pkcs05PbKdf2Parameters::Construct(const Tizen::Base::ByteBuffer& encodedData)
54 {
55         result r = E_SUCCESS;
56
57         SysAssertf(__pPkcs05PbKdf2ParametersImpl == null,
58                            "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
59
60         __pPkcs05PbKdf2ParametersImpl = new (std::nothrow) _Pkcs05PbKdf2ParametersImpl();
61         SysTryReturnResult(NID_SEC_CRYPTO, __pPkcs05PbKdf2ParametersImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory in insufficient.");
62
63         r = __pPkcs05PbKdf2ParametersImpl->Construct(encodedData);
64         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
65
66         return r;
67
68 CATCH:
69
70         delete __pPkcs05PbKdf2ParametersImpl;
71         __pPkcs05PbKdf2ParametersImpl = null;
72
73         return r;
74 }
75
76 result
77 Pkcs05PbKdf2Parameters::Construct(const Tizen::Base::ByteBuffer& saltBuffer, int iterationCount, int derivedKeyLength)
78 {
79         result r = E_SUCCESS;
80
81         SysAssertf(__pPkcs05PbKdf2ParametersImpl == null,
82                            "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
83
84         __pPkcs05PbKdf2ParametersImpl = new (std::nothrow) _Pkcs05PbKdf2ParametersImpl();
85         SysTryReturnResult(NID_SEC_CRYPTO, __pPkcs05PbKdf2ParametersImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory in insufficient.");
86
87         r = __pPkcs05PbKdf2ParametersImpl->Construct(saltBuffer, iterationCount, derivedKeyLength);
88         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
89
90         return r;
91
92 CATCH:
93
94         delete __pPkcs05PbKdf2ParametersImpl;
95         __pPkcs05PbKdf2ParametersImpl = null;
96
97         return r;
98 }
99
100
101 result
102 Pkcs05PbKdf2Parameters::Construct(const Tizen::Base::ByteBuffer& saltBuffer, int iterationCount, const AlgorithmIdentifier& prf, int derivedKeyLength)
103 {
104         result r = E_SUCCESS;
105
106         SysAssertf(__pPkcs05PbKdf2ParametersImpl == null,
107                            "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
108
109         __pPkcs05PbKdf2ParametersImpl = new (std::nothrow) _Pkcs05PbKdf2ParametersImpl();
110         SysTryReturnResult(NID_SEC_CRYPTO, __pPkcs05PbKdf2ParametersImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory in insufficient.");
111
112         r = __pPkcs05PbKdf2ParametersImpl->Construct(saltBuffer, iterationCount, prf, derivedKeyLength);
113         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
114
115         return r;
116
117 CATCH:
118
119         delete __pPkcs05PbKdf2ParametersImpl;
120         __pPkcs05PbKdf2ParametersImpl = null;
121
122         return r;
123 }
124
125 ByteBuffer*
126 Pkcs05PbKdf2Parameters::GetEncodedDataN(void) const
127 {
128         result r = E_SUCCESS;
129
130         SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
131         std::unique_ptr< ByteBuffer > pEncKdfParam(__pPkcs05PbKdf2ParametersImpl->GetEncodedDataN());
132         SysTryReturn(NID_SEC_CRYPTO, pEncKdfParam != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(r));
133
134         return pEncKdfParam.release();
135
136 }
137
138 const ByteBuffer&
139 Pkcs05PbKdf2Parameters::GetSaltValue(void) const
140 {
141         SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
142
143         return __pPkcs05PbKdf2ParametersImpl->GetSaltValue();
144 }
145
146 int
147 Pkcs05PbKdf2Parameters::GetIterationCount(void) const
148 {
149         ClearLastResult();
150
151         SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
152
153         return __pPkcs05PbKdf2ParametersImpl->__iterationCount;
154 }
155 int
156 Pkcs05PbKdf2Parameters::GetDerivedKeyLength(void) const
157 {
158         ClearLastResult();
159
160         SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
161
162         return __pPkcs05PbKdf2ParametersImpl->__derivedKeyLength;
163 }
164
165 const AlgorithmIdentifier&
166 Pkcs05PbKdf2Parameters::GetPrf(void) const
167 {
168         ClearLastResult();
169
170         SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
171
172         return __pPkcs05PbKdf2ParametersImpl->GetPrf();
173 }
174
175 IAlgorithmParameters*
176 Pkcs05PbKdf2Parameters::CloneN(void) const
177 {
178         result r = E_SUCCESS;
179
180         ClearLastResult();
181
182         SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
183         SysAssertf(__pPkcs05PbKdf2ParametersImpl->GetSaltValue().GetRemaining() > 0, "Not yet constructed. Construct () should be called before use.");
184
185         std::unique_ptr< Pkcs05PbKdf2Parameters > pKdf2Parameters(new (std::nothrow) Pkcs05PbKdf2Parameters());
186         SysTryReturn(NID_SEC_CRYPTO, pKdf2Parameters, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
187
188         if (__pPkcs05PbKdf2ParametersImpl->GetPrf().GetAlgorithmObjectId().GetLength() > 0)
189         {
190                 r = pKdf2Parameters->Construct(__pPkcs05PbKdf2ParametersImpl->GetSaltValue(), __pPkcs05PbKdf2ParametersImpl->GetIterationCount(), __pPkcs05PbKdf2ParametersImpl->GetPrf(), __pPkcs05PbKdf2ParametersImpl->GetDerivedKeyLength());
191         }
192         else
193         {
194                 r = pKdf2Parameters->Construct(__pPkcs05PbKdf2ParametersImpl->GetSaltValue(), __pPkcs05PbKdf2ParametersImpl->GetIterationCount(), __pPkcs05PbKdf2ParametersImpl->GetDerivedKeyLength());
195         }
196
197         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, r, "[%s] Failed to construct key derivation parameters.", GetErrorMessage(r));
198
199         return pKdf2Parameters.release();
200 }
201
202 PkcsAlgorithmParameterType
203 Pkcs05PbKdf2Parameters::GetType(void) const
204 {
205         ClearLastResult();
206
207         SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
208
209         return __pPkcs05PbKdf2ParametersImpl->GetType();
210 }
211
212
213 bool
214 Pkcs05PbKdf2Parameters::Equals(const Object& obj) const
215 {
216         ClearLastResult();
217
218         SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
219         const Pkcs05PbKdf2Parameters* pOther = dynamic_cast< const Pkcs05PbKdf2Parameters* >(&obj);
220         if (pOther == null)
221         {
222                 return false;
223         }
224
225         return __pPkcs05PbKdf2ParametersImpl->Equals(*pOther->__pPkcs05PbKdf2ParametersImpl);
226
227 }
228
229 int
230 Pkcs05PbKdf2Parameters::GetHashCode(void) const
231 {
232         ClearLastResult();
233
234         SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
235
236         return __pPkcs05PbKdf2ParametersImpl->GetHashCode();
237 }
238
239 } } } // end of namespace Pkcs