2 // Copyright (c) 2012 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 FSecCryptoDesCipher.cpp
19 * @brief This file contains the declaration of Tizen::Security::Crypto::DesCipher.
23 #include <unique_ptr.h>
24 #include <openssl/evp.h>
25 #include <openssl/crypto.h>
26 #include <FBaseResult.h>
27 #include <FBaseErrors.h>
28 #include <FSecCryptoDesCipher.h>
29 #include <FSecSecretKey.h>
30 #include <FBaseSysLog.h>
31 #include "FSecCrypto_SymmetricCipher.h"
33 using namespace Tizen::Base;
36 namespace Tizen { namespace Security { namespace Crypto
39 static const int _TRANSFORMATION_STRING_PART_1_LENGTH = 3;
40 static const int _TRANSFORMATION_STRING_PART_2_BEGIN = 4;
41 static const int _TRANSFORMATION_STRING_PART_2_LENGTH_A = 9;
42 static const int _TRANSFORMATION_STRING_PART_2_LENGTH_B = 12;
44 DesCipher::DesCipher(void)
45 : __pSymmetricCipher(null) // Default is DES/CBC/128
46 , __pCipherAlgorithm(null)
47 , __pDesCipherImpl(null)
51 DesCipher::~DesCipher(void)
53 delete __pSymmetricCipher;
57 DesCipher::Construct(const Tizen::Base::String& transformation, CipherOperation opMode)
61 String cipherMode = null;
62 String padding = null;
64 SysAssertf(__pSymmetricCipher == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class");
66 __pSymmetricCipher = new (std::nothrow) _SymmetricCipher();
67 SysTryReturn(NID_SEC_CRYPTO, __pSymmetricCipher != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
69 SysTryCatch(NID_SEC_CRYPTO, transformation.GetLength() > 0, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The transformation string should be valid.");
71 // Setting the key size, cipher mode and padding for the Des Construct
72 r = transformation.SubString(0, _TRANSFORMATION_STRING_PART_1_LENGTH, cipherMode);
73 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
75 if (cipherMode.CompareTo(L"CBC") == E_SUCCESS)
77 __pCipherAlgorithm = EVP_des_cbc();
79 else if (cipherMode.CompareTo(L"ECB") == E_SUCCESS)
81 __pCipherAlgorithm = EVP_des_ecb();
86 SysLogException(NID_SEC_CRYPTO, r, "The cipher algorithm for requested mode is not supported.");
90 r = transformation.SubString(_TRANSFORMATION_STRING_PART_2_BEGIN, _TRANSFORMATION_STRING_PART_2_LENGTH_A, padding);
91 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
93 if (padding.CompareTo(L"NOPADDING") == E_SUCCESS)
99 r = transformation.SubString(_TRANSFORMATION_STRING_PART_2_BEGIN, _TRANSFORMATION_STRING_PART_2_LENGTH_B, padding);
100 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
102 if (padding.CompareTo(L"PKCS7PADDING") == E_SUCCESS)
109 SysLogException(NID_SEC_CRYPTO, r, "The cipher algorithm for requested padding is not supported.");
114 // sets the transformation
115 r = __pSymmetricCipher->SetTransformation(__pCipherAlgorithm, padVal);
116 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Failed to do set transformation operation.", GetErrorMessage(r));
118 SysTryCatch(NID_SEC_CRYPTO, opMode == CIPHER_ENCRYPT || opMode == CIPHER_DECRYPT,
119 r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The opMode should be valid.");
121 // sets the SetCipherOpearation
122 r = __pSymmetricCipher->SetCipherOperation(opMode);
123 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] The cipher operation request should be valid.", GetErrorMessage(r));
128 delete __pSymmetricCipher;
129 __pSymmetricCipher = null;
135 DesCipher::SetKey(const Tizen::Security::ISecretKey& key)
137 result r = E_SUCCESS;
140 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
142 std::unique_ptr<ByteBuffer> pKey(key.GetEncodedN());
143 SysTryReturnResult(NID_SEC_CRYPTO, pKey != null, E_INVALID_ARG, "Input key data should be valid.");
145 keyLen = static_cast< int >(pKey->GetRemaining());
146 SysTryReturnResult(NID_SEC_CRYPTO, keyLen == __pCipherAlgorithm->key_len, E_INVALID_ARG, "Input key length value should be equal to algorithm key length.");
148 r = __pSymmetricCipher->SetKey(key);
149 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to do set key operation.", GetErrorMessage(r));
155 DesCipher::SetInitialVector(const Tizen::Base::ByteBuffer& initialVector)
157 result r = E_SUCCESS;
159 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
161 r = __pSymmetricCipher->SetInitialVector(initialVector);
162 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to do set initial vector operation.", GetErrorMessage(r));
168 DesCipher::EncryptN(const Tizen::Base::ByteBuffer& input)
170 ByteBuffer* pOutput = null;
174 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
176 pOutput = __pSymmetricCipher->DoCipherN(input);
177 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do encrypt operation", GetErrorMessage(GetLastResult()));
183 DesCipher::DecryptN(const Tizen::Base::ByteBuffer& input)
185 ByteBuffer* pOutput = null;
189 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
191 pOutput = __pSymmetricCipher->DoCipherN(input);
192 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do decrypt operation", GetErrorMessage(GetLastResult()));
198 DesCipher::Initialize(void)
200 result r = E_SUCCESS;
202 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
204 r = __pSymmetricCipher->Initialize();
205 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to do initialize operation.", GetErrorMessage(r));
211 DesCipher::UpdateN(const Tizen::Base::ByteBuffer& input)
213 ByteBuffer* pOutput = null;
217 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
219 pOutput = __pSymmetricCipher->UpdateN(input);
220 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do update operation", GetErrorMessage(GetLastResult()));
226 DesCipher::FinalizeN(void)
228 ByteBuffer* pOutput = null;
232 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
234 pOutput = __pSymmetricCipher->FinalizeN();
235 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do finalize operation", GetErrorMessage(GetLastResult()));
241 DesCipher::WrapN(const Tizen::Base::ByteBuffer& secretKey)
245 SetLastResult(E_UNSUPPORTED_ALGORITHM);
251 DesCipher::UnwrapN(const Tizen::Base::ByteBuffer& wrappedKey)
255 SetLastResult(E_UNSUPPORTED_ALGORITHM);
260 } } } //Tizen::Security::Crypto