Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / security / pkcs / FSecPkcsPkcs05Schemes.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                FSecPkcsPkcs05Schemes.cpp
19  * @brief               This is the implementation file for Pkcs05Schemes class.
20  *
21  * This header file contains the implementation of Pkcs05Schemes class.
22  *
23  */
24
25 #include <unique_ptr.h>
26 #include <FBaseSysLog.h>
27 #include <FBaseByteBuffer.h>
28 #include <FBaseResult.h>
29 #include <FSecPkcsPkcs05Schemes.h>
30 #include <FSecPkcsAlgorithmIdentifier.h>
31 #include <FSecPkcsPkcs05PbKdf2Parameters.h>
32 #include <FSecPkcsPkcs05PbEs2Parameters.h>
33 #include <FSecPkcsPkcs05PbMacParameters.h>
34 #include "FSecPkcs_Pkcs05SchemesImpl.h"
35
36 using namespace Tizen::Base;
37
38 namespace Tizen { namespace Security { namespace Pkcs
39 {
40
41 Pkcs05Schemes::Pkcs05Schemes(void)
42         : __pPkcs05SchemesImpl(null)
43 {
44
45 }
46
47 Pkcs05Schemes::~Pkcs05Schemes(void)
48 {
49         delete __pPkcs05SchemesImpl;
50 }
51
52 result
53 Pkcs05Schemes::Construct(const Tizen::Base::ByteBuffer& password, int derivedKeyLength)
54 {
55         result r = E_SUCCESS;
56
57         SysAssertf(__pPkcs05SchemesImpl == null,
58                            "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
59
60         __pPkcs05SchemesImpl = new (std::nothrow) _Pkcs05SchemesImpl();
61         SysTryReturnResult(NID_SEC_CRYPTO, __pPkcs05SchemesImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory in insufficient.");
62
63         r = __pPkcs05SchemesImpl->Construct(password, derivedKeyLength);
64         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
65
66         return r;
67
68 CATCH:
69
70         delete __pPkcs05SchemesImpl;
71         __pPkcs05SchemesImpl = null;
72
73         return r;
74 }
75
76
77 ByteBuffer*
78 Pkcs05Schemes::GenerateKeyKdf2N(const Pkcs05PbKdf2Parameters& params)
79 {
80         result r = E_SUCCESS;
81
82         ClearLastResult();
83
84         SysAssertf(__pPkcs05SchemesImpl != null, "Not yet constructed. Reconstructor the object.");
85         std::unique_ptr< ByteBuffer > pOut(__pPkcs05SchemesImpl->GenerateKeyKdf2N(params));
86         SysTryReturn(NID_SEC_CRYPTO, pOut != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(r));
87
88         return pOut.release();
89
90 }
91
92 ByteBuffer*
93 Pkcs05Schemes::EncryptionScheme2N(const Pkcs05PbEs2Parameters& params, const Tizen::Base::ByteBuffer& message)
94 {
95         result r = E_SUCCESS;
96
97         ClearLastResult();
98
99         SysAssertf(__pPkcs05SchemesImpl != null, "Not yet constructed. Reconstructor the object.");
100         std::unique_ptr< ByteBuffer > pOutBuffer(__pPkcs05SchemesImpl->EncryptionScheme2N(params, message));
101         SysTryReturn(NID_SEC_CRYPTO, pOutBuffer != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(r));
102
103         return pOutBuffer.release();
104 }
105
106 ByteBuffer*
107 Pkcs05Schemes::DecryptionScheme2N(const Pkcs05PbEs2Parameters& params, const Tizen::Base::ByteBuffer& message)
108 {
109         result r = E_SUCCESS;
110
111         ClearLastResult();
112
113         SysAssertf(__pPkcs05SchemesImpl != null, "Not yet constructed. Reconstructor the object.");
114         std::unique_ptr< ByteBuffer > pOutBuffer(__pPkcs05SchemesImpl->DecryptionScheme2N(params, message));
115         SysTryReturn(NID_SEC_CRYPTO, pOutBuffer != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(r));
116
117         return pOutBuffer.release();
118 }
119
120 ByteBuffer*
121 Pkcs05Schemes::GetPbHMacN(const Pkcs05PbMacParameters& params, const Tizen::Base::ByteBuffer& message)
122 {
123         result r = E_SUCCESS;
124
125         ClearLastResult();
126
127         SysAssertf(__pPkcs05SchemesImpl != null, "Not yet constructed. Reconstructor the object.");
128         std::unique_ptr< ByteBuffer > pOutput(__pPkcs05SchemesImpl->GetPbHMacN(params, message));
129         SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(r));
130
131         return pOutput.release();
132
133 }
134
135 result
136 Pkcs05Schemes::VerifyPbMac(const Pkcs05PbMacParameters& params, const Tizen::Base::ByteBuffer& message, const Tizen::Base::ByteBuffer& mac)
137 {
138         result r = E_SUCCESS;
139
140         SysAssertf(__pPkcs05SchemesImpl != null, "Not yet constructed. Reconstructor the object.");
141         r = __pPkcs05SchemesImpl->VerifyPbMac(params, message, mac);
142         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
143
144         return r;
145
146 }
147
148 } } } // end of namespace Pkcs05