2 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
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.
18 * @file FSecPkcsPkcs05PbKdf2Parameters.cpp
19 * @brief This is the implementation file for Pkcs05PbKdf2Parameters class.
21 * This header file contains the implementation of Pkcs05PbKdf2Parameters class.
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>
34 #include "FSecPkcs_Pkcs05PbKdf2ParametersImpl.h"
36 using namespace Tizen::Base;
38 namespace Tizen { namespace Security { namespace Pkcs
41 Pkcs05PbKdf2Parameters::Pkcs05PbKdf2Parameters(void)
42 : __pPkcs05PbKdf2ParametersImpl(null)
47 Pkcs05PbKdf2Parameters::~Pkcs05PbKdf2Parameters(void)
49 delete __pPkcs05PbKdf2ParametersImpl;
53 Pkcs05PbKdf2Parameters::Construct(const Tizen::Base::ByteBuffer& encodedData)
57 SysAssertf(__pPkcs05PbKdf2ParametersImpl == null,
58 "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
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.");
63 r = __pPkcs05PbKdf2ParametersImpl->Construct(encodedData);
64 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
70 delete __pPkcs05PbKdf2ParametersImpl;
71 __pPkcs05PbKdf2ParametersImpl = null;
77 Pkcs05PbKdf2Parameters::Construct(const Tizen::Base::ByteBuffer& saltBuffer, int iterationCount, int derivedKeyLength)
81 SysAssertf(__pPkcs05PbKdf2ParametersImpl == null,
82 "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
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.");
87 r = __pPkcs05PbKdf2ParametersImpl->Construct(saltBuffer, iterationCount, derivedKeyLength);
88 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
94 delete __pPkcs05PbKdf2ParametersImpl;
95 __pPkcs05PbKdf2ParametersImpl = null;
102 Pkcs05PbKdf2Parameters::Construct(const Tizen::Base::ByteBuffer& saltBuffer, int iterationCount, const AlgorithmIdentifier& prf, int derivedKeyLength)
104 result r = E_SUCCESS;
106 SysAssertf(__pPkcs05PbKdf2ParametersImpl == null,
107 "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
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.");
112 r = __pPkcs05PbKdf2ParametersImpl->Construct(saltBuffer, iterationCount, prf, derivedKeyLength);
113 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
119 delete __pPkcs05PbKdf2ParametersImpl;
120 __pPkcs05PbKdf2ParametersImpl = null;
126 Pkcs05PbKdf2Parameters::GetEncodedDataN(void) const
128 result r = E_SUCCESS;
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));
134 return pEncKdfParam.release();
139 Pkcs05PbKdf2Parameters::GetSaltValue(void) const
141 SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
143 return __pPkcs05PbKdf2ParametersImpl->GetSaltValue();
147 Pkcs05PbKdf2Parameters::GetIterationCount(void) const
151 SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
153 return __pPkcs05PbKdf2ParametersImpl->__iterationCount;
156 Pkcs05PbKdf2Parameters::GetDerivedKeyLength(void) const
160 SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
162 return __pPkcs05PbKdf2ParametersImpl->__derivedKeyLength;
165 const AlgorithmIdentifier&
166 Pkcs05PbKdf2Parameters::GetPrf(void) const
170 SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
172 return __pPkcs05PbKdf2ParametersImpl->GetPrf();
175 IAlgorithmParameters*
176 Pkcs05PbKdf2Parameters::CloneN(void) const
178 result r = E_SUCCESS;
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.");
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.");
188 if (__pPkcs05PbKdf2ParametersImpl->GetPrf().GetAlgorithmObjectId().GetLength() > 0)
190 r = pKdf2Parameters->Construct(__pPkcs05PbKdf2ParametersImpl->GetSaltValue(), __pPkcs05PbKdf2ParametersImpl->GetIterationCount(), __pPkcs05PbKdf2ParametersImpl->GetPrf(), __pPkcs05PbKdf2ParametersImpl->GetDerivedKeyLength());
194 r = pKdf2Parameters->Construct(__pPkcs05PbKdf2ParametersImpl->GetSaltValue(), __pPkcs05PbKdf2ParametersImpl->GetIterationCount(), __pPkcs05PbKdf2ParametersImpl->GetDerivedKeyLength());
197 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, r, "[%s] Failed to construct key derivation parameters.", GetErrorMessage(r));
199 return pKdf2Parameters.release();
202 PkcsAlgorithmParameterType
203 Pkcs05PbKdf2Parameters::GetType(void) const
207 SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
209 return __pPkcs05PbKdf2ParametersImpl->GetType();
214 Pkcs05PbKdf2Parameters::Equals(const Object& obj) const
218 SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
219 const Pkcs05PbKdf2Parameters* pOther = dynamic_cast< const Pkcs05PbKdf2Parameters* >(&obj);
225 return __pPkcs05PbKdf2ParametersImpl->Equals(*pOther->__pPkcs05PbKdf2ParametersImpl);
230 Pkcs05PbKdf2Parameters::GetHashCode(void) const
234 SysAssertf(__pPkcs05PbKdf2ParametersImpl != null, "Not yet constructed. Reconstructor the object.");
236 return __pPkcs05PbKdf2ParametersImpl->GetHashCode();
239 } } } // end of namespace Pkcs