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 FSecCryptoRc2Cipher.cpp
19 * @brief This file contains the implementation of Tizen::Security::Crypto::Rc2Cipher.
21 #include <openssl/evp.h>
22 #include <openssl/crypto.h>
23 #include <FBaseResult.h>
24 #include <FBaseErrors.h>
25 #include <FSecCryptoRc2Cipher.h>
26 #include <FSecSecretKey.h>
27 #include <FBaseSysLog.h>
28 #include "FSecCrypto_SymmetricCipher.h"
30 using namespace Tizen::Base;
33 namespace Tizen { namespace Security { namespace Crypto
36 static const int _TRANSFORMATION_STRING_PART_1_LENGTH = 3;
37 static const int _TRANSFORMATION_STRING_PART_2_BEGIN = 4;
38 static const int _TRANSFORMATION_STRING_PART_2_LENGTH_A = 9;
39 static const int _TRANSFORMATION_STRING_PART_2_LENGTH_B = 12;
41 Rc2Cipher::Rc2Cipher(void)
42 : __pSymmetricCipher(null) // Default is RC2/CBC/128
43 , __pCipherAlgorithm(null)
44 , __pRc2CipherImpl(null)
48 Rc2Cipher::~Rc2Cipher(void)
50 delete __pSymmetricCipher;
54 Rc2Cipher::Construct(const Tizen::Base::String& transformation, CipherOperation opMode)
58 String cipherMode = null;
59 String padding = null;
61 SysAssertf(__pSymmetricCipher == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class");
63 __pSymmetricCipher = new (std::nothrow) _SymmetricCipher();
64 SysTryReturn(NID_SEC_CRYPTO, __pSymmetricCipher != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
66 SysTryCatch(NID_SEC_CRYPTO, transformation.GetLength() > 0, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The transformation string should be valid.");
68 // Setting the key size, cipher mode and padding for the Rc2 Construct
69 r = transformation.SubString(0, _TRANSFORMATION_STRING_PART_1_LENGTH, cipherMode);
70 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
72 if (cipherMode.CompareTo(L"CBC") == E_SUCCESS)
74 __pCipherAlgorithm = EVP_rc2_cbc();
76 else if (cipherMode.CompareTo(L"ECB") == E_SUCCESS)
78 __pCipherAlgorithm = EVP_rc2_ecb();
83 SysLogException(NID_SEC_CRYPTO, r, "The cipher algorithm for requested mode is not supported.");
87 r = transformation.SubString(_TRANSFORMATION_STRING_PART_2_BEGIN, _TRANSFORMATION_STRING_PART_2_LENGTH_A, padding);
88 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
90 if (padding.CompareTo(L"NOPADDING") == E_SUCCESS)
96 r = transformation.SubString(_TRANSFORMATION_STRING_PART_2_BEGIN, _TRANSFORMATION_STRING_PART_2_LENGTH_B, padding);
97 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
99 if (padding.CompareTo(L"PKCS7PADDING") == E_SUCCESS)
106 SysLogException(NID_SEC_CRYPTO, r, "The cipher algorithm for requested padding is not supported.");
111 // sets the transformation
112 r = __pSymmetricCipher->SetTransformation(__pCipherAlgorithm, padVal);
113 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Failed to do set transformation operation.", GetErrorMessage(r));
115 SysTryCatch(NID_SEC_CRYPTO, opMode == CIPHER_ENCRYPT || opMode == CIPHER_DECRYPT,
116 r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The opMode should be valid.");
118 // sets the SetCipherOpearation
119 r = __pSymmetricCipher->SetCipherOperation(opMode);
120 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] The cipher operation request should be valid.", GetErrorMessage(r));
125 delete __pSymmetricCipher;
126 __pSymmetricCipher = null;
132 Rc2Cipher::SetKey(const Tizen::Security::ISecretKey& key)
134 result r = E_SUCCESS;
136 ByteBuffer* pKey = null;
138 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
140 pKey = key.GetEncodedN();
141 SysTryReturn(NID_SEC_CRYPTO, pKey != null, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Input key data should be valid.");
143 keyLen = static_cast< int >(pKey->GetRemaining());
144 SysTryCatch(NID_SEC_CRYPTO, keyLen >= 5, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Input key length should be >= algorithm key length.");
146 r = __pSymmetricCipher->SetKey(key);
147 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Failed to do set key operation.", GetErrorMessage(r));
155 Rc2Cipher::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 Rc2Cipher::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, true);
177 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do encrypt operation", GetErrorMessage(GetLastResult()));
183 Rc2Cipher::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, true);
192 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do decrypt operation", GetErrorMessage(GetLastResult()));
198 Rc2Cipher::Initialize(void)
200 result r = E_SUCCESS;
202 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
204 r = __pSymmetricCipher->Initialize(true);
205 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to do initialize operation.", GetErrorMessage(r));
211 Rc2Cipher::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 Rc2Cipher::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 Rc2Cipher::WrapN(const Tizen::Base::ByteBuffer& secretKey)
245 SetLastResult(E_UNSUPPORTED_ALGORITHM);
251 Rc2Cipher::UnwrapN(const Tizen::Base::ByteBuffer& wrappedKey)
255 SetLastResult(E_UNSUPPORTED_ALGORITHM);
260 } } } //Tizen::Security::Crypto