Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / security / crypto / FSecCryptoSkipJackCipher.cpp
1 //
2 // Copyright (c) 2012 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                FSecCryptoSkipJackCipher.cpp
19  * @brief               This header file contains the declaration of Tizen::Security::Crypto::SkipJackCipher.
20  *
21  */
22 #include <openssl/evp.h>
23 #include <openssl/crypto.h>
24 #include <FBaseByteBuffer.h>
25 #include <FSecCryptoSkipJackCipher.h>
26 #include <FSecSecretKey.h>
27 #include <FBaseSysLog.h>
28 #include "FSecCrypto_SkipJackModes.h"
29 #include "FSecCrypto_SymmetricCipher.h"
30
31 using namespace Tizen::Base;
32
33 const EVP_CIPHER* EvpSkipJackCbc80();
34 const EVP_CIPHER* EvpSkipJackEcb80();
35
36
37 namespace Tizen { namespace Security { namespace Crypto
38 {
39
40 static const int _TRANSFORMATION_STRING_PART_1_LENGTH = 3;
41 static const int _TRANSFORMATION_STRING_PART_2_BEGIN = 4;
42 static const int _TRANSFORMATION_STRING_PART_2_LENGTH_A = 9;
43 static const int _TRANSFORMATION_STRING_PART_2_LENGTH_B = 12;
44
45 SkipJackCipher::SkipJackCipher(void)
46         : __pSymmetricCipher(null)      // Default is SKIPJACK/CBC/80
47         , __pCipherAlgorithm(null)
48         , __pSkipJackCipherImpl(null)
49 {
50 }
51
52 SkipJackCipher::~SkipJackCipher(void)
53 {
54         delete __pSymmetricCipher;
55 }
56
57 result
58 SkipJackCipher::Construct(const Tizen::Base::String& transformation, CipherOperation opMode)
59 {
60         result r = E_SUCCESS;
61         bool padVal = false;
62         String cipherMode = null;
63         String padding = null;
64
65         SysAssertf(__pSymmetricCipher == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class");
66
67         __pSymmetricCipher = new (std::nothrow) _SymmetricCipher();
68         SysTryReturn(NID_SEC_CRYPTO, __pSymmetricCipher != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
69
70         SysTryCatch(NID_SEC_CRYPTO, transformation.GetLength() > 0, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The transformation string should be valid.");
71
72         // Setting the key size, cipher mode and padding for the SkipJack Construct
73         r = transformation.SubString(0, _TRANSFORMATION_STRING_PART_1_LENGTH, cipherMode);
74         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
75
76         if (cipherMode.CompareTo(L"CBC") == E_SUCCESS)
77         {
78                 __pCipherAlgorithm = EvpSkipJackCbc80();
79         }
80         else if (cipherMode.CompareTo(L"ECB") == E_SUCCESS)
81         {
82                 __pCipherAlgorithm = EvpSkipJackEcb80();
83         }
84         else
85         {
86                 r = E_INVALID_ARG;
87                 SysLogException(NID_SEC_CRYPTO, r, "The cipher algorithm for requested mode is not supported.");
88                 goto CATCH;
89         }
90
91         r = transformation.SubString(_TRANSFORMATION_STRING_PART_2_BEGIN, _TRANSFORMATION_STRING_PART_2_LENGTH_A, padding);
92         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
93
94         if (padding.CompareTo(L"NOPADDING") == E_SUCCESS)
95         {
96                 padVal = false;
97         }
98         else
99         {
100                 r = transformation.SubString(_TRANSFORMATION_STRING_PART_2_BEGIN, _TRANSFORMATION_STRING_PART_2_LENGTH_B, padding);
101                 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
102
103                 if (padding.CompareTo(L"PKCS7PADDING") == E_SUCCESS)
104                 {
105                         padVal = true;
106                 }
107                 else
108                 {
109                         r = E_INVALID_ARG;
110                         SysLogException(NID_SEC_CRYPTO, r, "The cipher algorithm for requested padding is not supported.");
111                         goto CATCH;
112                 }
113         }
114
115         // sets the transformation
116         r = __pSymmetricCipher->SetTransformation(__pCipherAlgorithm, padVal);
117         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Failed to do set transformation operation.", GetErrorMessage(r));
118
119         SysTryCatch(NID_SEC_CRYPTO, opMode == CIPHER_ENCRYPT || opMode == CIPHER_DECRYPT,
120                         r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The opMode should be valid.");
121
122         // sets the SetCipherOpearation
123         r = __pSymmetricCipher->SetCipherOperation(opMode);
124         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] The cipher operation request should be valid.", GetErrorMessage(r));
125
126 CATCH:
127         if (IsFailed(r))
128         {
129                 delete __pSymmetricCipher;
130                 __pSymmetricCipher = null;
131         }
132         return r;
133 }
134
135 result
136 SkipJackCipher::SetKey(const Tizen::Security::ISecretKey& key)
137 {
138         result r = E_SUCCESS;
139         int keyLen = 0;
140
141         SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
142
143         std::unique_ptr<ByteBuffer> pKey(key.GetEncodedN());
144         SysTryReturnResult(NID_SEC_CRYPTO, pKey != null, E_INVALID_ARG, "Key data should be valid.");
145
146         keyLen = static_cast< int >(pKey->GetRemaining());
147         SysTryReturnResult(NID_SEC_CRYPTO, keyLen == __pCipherAlgorithm->key_len, E_INVALID_ARG, "Input key length should be equal to algorithm key length.");
148
149         r = __pSymmetricCipher->SetKey(key);
150         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to do set key operation.", GetErrorMessage(r));
151
152         return r;
153 }
154
155 result
156 SkipJackCipher::SetInitialVector(const Tizen::Base::ByteBuffer& initialVector)
157 {
158         result r = E_SUCCESS;
159
160         SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
161
162         r = __pSymmetricCipher->SetInitialVector(initialVector);
163         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to do set initial vector operation.", GetErrorMessage(r));
164
165         return r;
166 }
167
168 ByteBuffer*
169 SkipJackCipher::EncryptN(const Tizen::Base::ByteBuffer& input)
170 {
171         ByteBuffer* pOutput = null;
172
173         ClearLastResult();
174
175         SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
176
177         pOutput = __pSymmetricCipher->DoCipherN(input);
178         SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do encrypt operation", GetErrorMessage(GetLastResult()));
179
180         return pOutput;
181 }
182
183 ByteBuffer*
184 SkipJackCipher::DecryptN(const Tizen::Base::ByteBuffer& input)
185 {
186         ByteBuffer* pOutput = null;
187
188         ClearLastResult();
189
190         SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
191
192         pOutput = __pSymmetricCipher->DoCipherN(input);
193         SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do decrypt operation", GetErrorMessage(GetLastResult()));
194
195         return pOutput;
196 }
197
198 result
199 SkipJackCipher::Initialize(void)
200 {
201         result r = E_SUCCESS;
202
203         SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
204
205         r = __pSymmetricCipher->Initialize();
206         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to do initialize operation.", GetErrorMessage(r));
207
208         return r;
209 }
210
211 ByteBuffer*
212 SkipJackCipher::UpdateN(const Tizen::Base::ByteBuffer& input)
213 {
214         ByteBuffer* pOutput = null;
215
216         ClearLastResult();
217
218         SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
219
220         pOutput = __pSymmetricCipher->UpdateN(input);
221         SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do update operation", GetErrorMessage(GetLastResult()));
222
223         return pOutput;
224 }
225
226 ByteBuffer*
227 SkipJackCipher::FinalizeN(void)
228 {
229         ByteBuffer* pOutput = null;
230
231         ClearLastResult();
232
233         SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
234
235         pOutput = __pSymmetricCipher->FinalizeN();
236         SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do finalize operation", GetErrorMessage(GetLastResult()));
237
238         return pOutput;
239 }
240
241 ByteBuffer*
242 SkipJackCipher::WrapN(const Tizen::Base::ByteBuffer& secretKey)
243 {
244         ClearLastResult();
245
246         SetLastResult(E_UNSUPPORTED_ALGORITHM);
247
248         return null;
249 }
250
251 ByteBuffer*
252 SkipJackCipher::UnwrapN(const Tizen::Base::ByteBuffer& wrappedKey)
253 {
254         ClearLastResult();
255
256         SetLastResult(E_UNSUPPORTED_ALGORITHM);
257
258         return null;
259 }
260
261 } } } //Tizen::Security::Crypto