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