2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FSecCryptoCastCipher.cpp
20 * @brief This file contains the implementation of Tizen::Security::Crypto::CastCipher.
22 #include <unique_ptr.h>
23 #include <openssl/evp.h>
24 #include <openssl/crypto.h>
25 #include <FBaseResult.h>
26 #include <FBaseErrors.h>
27 #include <FSecCryptoCastCipher.h>
28 #include <FSecSecretKey.h>
29 #include <FBaseSysLog.h>
30 #include "FSecCrypto_SymmetricCipher.h"
32 using namespace Tizen::Base;
35 namespace Tizen { namespace Security { namespace Crypto
38 static const int _TRANSFORMATION_STRING_PART_1_LENGTH = 3;
39 static const int _TRANSFORMATION_STRING_PART_2_BEGIN = 4;
40 static const int _TRANSFORMATION_STRING_PART_2_LENGTH_A = 9;
41 static const int _TRANSFORMATION_STRING_PART_2_LENGTH_B = 12;
43 CastCipher::CastCipher(void)
44 : __pSymmetricCipher(null) // Default is CAST/CBC/128
45 , __pCipherAlgorithm(null)
46 , __pCastCipherImpl(null)
50 CastCipher::~CastCipher(void)
52 delete __pSymmetricCipher;
56 CastCipher::Construct(const Tizen::Base::String& transformation, CipherOperation opMode)
60 String cipherMode = null;
61 String padding = null;
63 SysAssertf(__pSymmetricCipher == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class");
65 __pSymmetricCipher = new (std::nothrow) _SymmetricCipher();
66 SysTryReturn(NID_SEC_CRYPTO, __pSymmetricCipher != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
68 SysTryCatch(NID_SEC_CRYPTO, transformation.GetLength() > 0, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The transformation string should be valid.");
70 // Setting the key size, cipher mode and padding for the Cast Construct
71 r = transformation.SubString(0, _TRANSFORMATION_STRING_PART_1_LENGTH, cipherMode);
72 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
74 if (cipherMode.CompareTo(L"128") == E_SUCCESS)
76 __pCipherAlgorithm = EVP_cast5_cbc();
81 SysLogException(NID_SEC_CRYPTO, r, "The cipher algorithm for requested key length is not supported.");
85 r = transformation.SubString(_TRANSFORMATION_STRING_PART_2_BEGIN, _TRANSFORMATION_STRING_PART_2_LENGTH_A, padding);
86 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
88 if (padding.CompareTo(L"NOPADDING") == E_SUCCESS)
94 r = transformation.SubString(_TRANSFORMATION_STRING_PART_2_BEGIN, _TRANSFORMATION_STRING_PART_2_LENGTH_B, padding);
95 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
97 if (padding.CompareTo(L"PKCS7PADDING") == E_SUCCESS)
104 SysLogException(NID_SEC_CRYPTO, r, "The cipher algorithm for requested padding is not supported.");
109 // sets the transformation
110 r = __pSymmetricCipher->SetTransformation(__pCipherAlgorithm, padVal);
111 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Failed to do set transformation operation.", GetErrorMessage(r));
113 SysTryCatch(NID_SEC_CRYPTO, opMode == CIPHER_ENCRYPT || opMode == CIPHER_DECRYPT,
114 r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The opMode should be valid.");
116 // sets the SetCipherOpearation
117 r = __pSymmetricCipher->SetCipherOperation(opMode);
118 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] The cipher operation request should be valid.", GetErrorMessage(r));
123 delete __pSymmetricCipher;
124 __pSymmetricCipher = null;
130 CastCipher::SetKey(const Tizen::Security::ISecretKey& key)
132 result r = E_SUCCESS;
135 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
137 std::unique_ptr<ByteBuffer> pKey(key.GetEncodedN());
138 SysTryReturnResult(NID_SEC_CRYPTO, pKey != null, E_INVALID_ARG, "Input key data should be valid.");
140 keyLen = static_cast< int >(pKey->GetRemaining());
141 SysTryReturnResult(NID_SEC_CRYPTO, keyLen == __pCipherAlgorithm->key_len, E_INVALID_ARG, "Input key length should be equal to algorithm key length.");
143 r = __pSymmetricCipher->SetKey(key);
144 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to do set key operation.", GetErrorMessage(r));
150 CastCipher::SetInitialVector(const Tizen::Base::ByteBuffer& initialVector)
152 result r = E_SUCCESS;
154 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
156 r = __pSymmetricCipher->SetInitialVector(initialVector);
157 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to do set initial vector operation.", GetErrorMessage(r));
163 CastCipher::EncryptN(const Tizen::Base::ByteBuffer& input)
165 ByteBuffer* pOutput = null;
169 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
171 pOutput = __pSymmetricCipher->DoCipherN(input);
172 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do encrypt operation", GetErrorMessage(GetLastResult()));
178 CastCipher::DecryptN(const Tizen::Base::ByteBuffer& input)
180 ByteBuffer* pOutput = null;
184 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
186 pOutput = __pSymmetricCipher->DoCipherN(input);
187 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do decrypt operation", GetErrorMessage(GetLastResult()));
193 CastCipher::Initialize(void)
195 result r = E_SUCCESS;
197 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
199 r = __pSymmetricCipher->Initialize();
200 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to do initialize operation.", GetErrorMessage(r));
206 CastCipher::UpdateN(const Tizen::Base::ByteBuffer& input)
208 ByteBuffer* pOutput = null;
212 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
214 pOutput = __pSymmetricCipher->UpdateN(input);
215 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do update operation", GetErrorMessage(GetLastResult()));
221 CastCipher::FinalizeN(void)
223 ByteBuffer* pOutput = null;
227 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
229 pOutput = __pSymmetricCipher->FinalizeN();
230 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do finalize operation", GetErrorMessage(GetLastResult()));
236 CastCipher::WrapN(const Tizen::Base::ByteBuffer& secretKey)
240 SetLastResult(E_UNSUPPORTED_ALGORITHM);
246 CastCipher::UnwrapN(const Tizen::Base::ByteBuffer& wrappedKey)
250 SetLastResult(E_UNSUPPORTED_ALGORITHM);
255 } } } //Tizen::Security::Crypto