2 // Open Service Platform
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FSecPkcsPkcs05PbKdf2Parameters.cpp
20 * @brief This is the implementation file for Pkcs05PbKdf2Parameters class.
22 * This header file contains the implementation of Pkcs05PbKdf2Parameters class.
26 #include <unique_ptr.h>
27 #include <FBaseSysLog.h>
28 #include <FBaseByteBuffer.h>
29 #include <FBaseResult.h>
30 #include <FBaseInteger.h>
31 #include <FSecPkcsPkcs05PbKdf2Parameters.h>
32 #include <FSecPkcsIAlgorithmParameters.h>
33 #include <FSecPkcsAlgorithmIdentifier.h>
35 #include "FSecPkcs_Pkcs05PbKdf2ParametersImpl.h"
37 using namespace Tizen::Base;
39 namespace Tizen { namespace Security { namespace Pkcs
42 Pkcs05PbKdf2Parameters::Pkcs05PbKdf2Parameters(void)
43 : __pPkcs05PbKdf2ParametersImpl(null)
48 Pkcs05PbKdf2Parameters::~Pkcs05PbKdf2Parameters(void)
50 delete __pPkcs05PbKdf2ParametersImpl;
54 Pkcs05PbKdf2Parameters::Construct(const Tizen::Base::ByteBuffer& encodedData)
58 SysAssertf(__pPkcs05PbKdf2ParametersImpl == null,
59 "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
61 __pPkcs05PbKdf2ParametersImpl = new (std::nothrow) _Pkcs05PbKdf2ParametersImpl();
62 SysTryReturnResult(NID_SEC_CRYPTO, __pPkcs05PbKdf2ParametersImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory in insufficient.");
64 r = __pPkcs05PbKdf2ParametersImpl->Construct(encodedData);
65 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
71 delete __pPkcs05PbKdf2ParametersImpl;
72 __pPkcs05PbKdf2ParametersImpl = null;
78 Pkcs05PbKdf2Parameters::Construct(const Tizen::Base::ByteBuffer& saltBuffer, int iterationCount, int derivedKeyLength)
82 SysAssertf(__pPkcs05PbKdf2ParametersImpl == null,
83 "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
85 __pPkcs05PbKdf2ParametersImpl = new (std::nothrow) _Pkcs05PbKdf2ParametersImpl();
86 SysTryReturnResult(NID_SEC_CRYPTO, __pPkcs05PbKdf2ParametersImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory in insufficient.");
88 r = __pPkcs05PbKdf2ParametersImpl->Construct(saltBuffer, iterationCount, derivedKeyLength);
89 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
95 delete __pPkcs05PbKdf2ParametersImpl;
96 __pPkcs05PbKdf2ParametersImpl = null;
103 Pkcs05PbKdf2Parameters::Construct(const Tizen::Base::ByteBuffer& saltBuffer, int iterationCount, const AlgorithmIdentifier& prf, int derivedKeyLength)
105 result r = E_SUCCESS;
107 SysAssertf(__pPkcs05PbKdf2ParametersImpl == null,
108 "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
110 __pPkcs05PbKdf2ParametersImpl = new (std::nothrow) _Pkcs05PbKdf2ParametersImpl();
111 SysTryReturnResult(NID_SEC_CRYPTO, __pPkcs05PbKdf2ParametersImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory in insufficient.");
113 r = __pPkcs05PbKdf2ParametersImpl->Construct(saltBuffer, iterationCount, prf, derivedKeyLength);
114 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
120 delete __pPkcs05PbKdf2ParametersImpl;
121 __pPkcs05PbKdf2ParametersImpl = null;
127 Pkcs05PbKdf2Parameters::GetEncodedDataN(void) const
129 result r = E_SUCCESS;
131 SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
132 std::unique_ptr< ByteBuffer > pEncKdfParam(__pPkcs05PbKdf2ParametersImpl->GetEncodedDataN());
133 SysTryReturn(NID_SEC_CRYPTO, pEncKdfParam != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(r));
135 return pEncKdfParam.release();
140 Pkcs05PbKdf2Parameters::GetSaltValue(void) const
142 SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
144 return __pPkcs05PbKdf2ParametersImpl->GetSaltValue();
148 Pkcs05PbKdf2Parameters::GetIterationCount(void) const
152 SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
154 return __pPkcs05PbKdf2ParametersImpl->__iterationCount;
157 Pkcs05PbKdf2Parameters::GetDerivedKeyLength(void) const
161 SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
163 return __pPkcs05PbKdf2ParametersImpl->__derivedKeyLength;
166 const AlgorithmIdentifier&
167 Pkcs05PbKdf2Parameters::GetPrf(void) const
171 SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
173 return __pPkcs05PbKdf2ParametersImpl->GetPrf();
176 IAlgorithmParameters*
177 Pkcs05PbKdf2Parameters::CloneN(void) const
179 result r = E_SUCCESS;
183 SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
184 SysAssertf(__pPkcs05PbKdf2ParametersImpl->GetSaltValue().GetRemaining() > 0, "Not yet constructed. Construct () should be called before use.");
186 std::unique_ptr< Pkcs05PbKdf2Parameters > pKdf2Parameters(new (std::nothrow) Pkcs05PbKdf2Parameters());
187 SysTryReturn(NID_SEC_CRYPTO, pKdf2Parameters, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
189 if (__pPkcs05PbKdf2ParametersImpl->GetPrf().GetAlgorithmObjectId().GetLength() > 0)
191 r = pKdf2Parameters->Construct(__pPkcs05PbKdf2ParametersImpl->GetSaltValue(), __pPkcs05PbKdf2ParametersImpl->GetIterationCount(), __pPkcs05PbKdf2ParametersImpl->GetPrf(), __pPkcs05PbKdf2ParametersImpl->GetDerivedKeyLength());
195 r = pKdf2Parameters->Construct(__pPkcs05PbKdf2ParametersImpl->GetSaltValue(), __pPkcs05PbKdf2ParametersImpl->GetIterationCount(), __pPkcs05PbKdf2ParametersImpl->GetDerivedKeyLength());
198 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, r, "[%s] Failed to construct key derivation parameters.", GetErrorMessage(r));
200 return pKdf2Parameters.release();
203 PkcsAlgorithmParameterType
204 Pkcs05PbKdf2Parameters::GetType(void) const
208 SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
210 return __pPkcs05PbKdf2ParametersImpl->GetType();
215 Pkcs05PbKdf2Parameters::Equals(const Object& obj) const
219 SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
220 const Pkcs05PbKdf2Parameters* pOther = dynamic_cast< const Pkcs05PbKdf2Parameters* >(&obj);
226 return __pPkcs05PbKdf2ParametersImpl->Equals(*pOther->__pPkcs05PbKdf2ParametersImpl);
231 Pkcs05PbKdf2Parameters::GetHashCode(void) const
235 SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
237 return __pPkcs05PbKdf2ParametersImpl->GetHashCode();
240 } } } // end of namespace Pkcs