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 FSecCryptoCastCipher.cpp
19 * @brief This file contains the implementation of Tizen::Security::Crypto::CastCipher.
21 #include <unique_ptr.h>
22 #include <openssl/evp.h>
23 #include <openssl/crypto.h>
24 #include <FBaseResult.h>
25 #include <FBaseErrors.h>
26 #include <FSecCryptoCastCipher.h>
27 #include <FSecSecretKey.h>
28 #include <FBaseSysLog.h>
29 #include "FSecCrypto_SymmetricCipher.h"
31 using namespace Tizen::Base;
34 namespace Tizen { namespace Security { namespace Crypto
37 static const int _TRANSFORMATION_STRING_PART_1_LENGTH = 3;
38 static const int _TRANSFORMATION_STRING_PART_2_BEGIN = 4;
39 static const int _TRANSFORMATION_STRING_PART_2_LENGTH_A = 9;
40 static const int _TRANSFORMATION_STRING_PART_2_LENGTH_B = 12;
42 CastCipher::CastCipher(void)
43 : __pSymmetricCipher(null) // Default is CAST/CBC/128
44 , __pCipherAlgorithm(null)
45 , __pCastCipherImpl(null)
49 CastCipher::~CastCipher(void)
51 delete __pSymmetricCipher;
55 CastCipher::Construct(const Tizen::Base::String& transformation, CipherOperation opMode)
59 String cipherMode = null;
60 String padding = null;
62 SysAssertf(__pSymmetricCipher == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class");
64 __pSymmetricCipher = new (std::nothrow) _SymmetricCipher();
65 SysTryReturn(NID_SEC_CRYPTO, __pSymmetricCipher != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
67 SysTryCatch(NID_SEC_CRYPTO, transformation.GetLength() > 0, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The transformation string should be valid.");
69 // Setting the key size, cipher mode and padding for the Cast Construct
70 r = transformation.SubString(0, _TRANSFORMATION_STRING_PART_1_LENGTH, cipherMode);
71 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
73 if (cipherMode.CompareTo(L"128") == E_SUCCESS)
75 __pCipherAlgorithm = EVP_cast5_cbc();
80 SysLogException(NID_SEC_CRYPTO, r, "The cipher algorithm for requested key length is not supported.");
84 r = transformation.SubString(_TRANSFORMATION_STRING_PART_2_BEGIN, _TRANSFORMATION_STRING_PART_2_LENGTH_A, padding);
85 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
87 if (padding.CompareTo(L"NOPADDING") == E_SUCCESS)
93 r = transformation.SubString(_TRANSFORMATION_STRING_PART_2_BEGIN, _TRANSFORMATION_STRING_PART_2_LENGTH_B, padding);
94 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
96 if (padding.CompareTo(L"PKCS7PADDING") == E_SUCCESS)
103 SysLogException(NID_SEC_CRYPTO, r, "The cipher algorithm for requested padding is not supported.");
108 // sets the transformation
109 r = __pSymmetricCipher->SetTransformation(__pCipherAlgorithm, padVal);
110 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Failed to do set transformation operation.", GetErrorMessage(r));
112 SysTryCatch(NID_SEC_CRYPTO, opMode == CIPHER_ENCRYPT || opMode == CIPHER_DECRYPT,
113 r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The opMode should be valid.");
115 // sets the SetCipherOpearation
116 r = __pSymmetricCipher->SetCipherOperation(opMode);
117 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] The cipher operation request should be valid.", GetErrorMessage(r));
122 delete __pSymmetricCipher;
123 __pSymmetricCipher = null;
129 CastCipher::SetKey(const Tizen::Security::ISecretKey& key)
131 result r = E_SUCCESS;
134 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
136 std::unique_ptr<ByteBuffer> pKey(key.GetEncodedN());
137 SysTryReturnResult(NID_SEC_CRYPTO, pKey != null, E_INVALID_ARG, "Input key data should be valid.");
139 keyLen = static_cast< int >(pKey->GetRemaining());
140 SysTryReturnResult(NID_SEC_CRYPTO, keyLen == __pCipherAlgorithm->key_len, E_INVALID_ARG, "Input key length should be equal to algorithm key length.");
142 r = __pSymmetricCipher->SetKey(key);
143 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to do set key operation.", GetErrorMessage(r));
149 CastCipher::SetInitialVector(const Tizen::Base::ByteBuffer& initialVector)
151 result r = E_SUCCESS;
153 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
155 r = __pSymmetricCipher->SetInitialVector(initialVector);
156 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to do set initial vector operation.", GetErrorMessage(r));
162 CastCipher::EncryptN(const Tizen::Base::ByteBuffer& input)
164 ByteBuffer* pOutput = null;
168 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
170 pOutput = __pSymmetricCipher->DoCipherN(input);
171 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do encrypt operation", GetErrorMessage(GetLastResult()));
177 CastCipher::DecryptN(const Tizen::Base::ByteBuffer& input)
179 ByteBuffer* pOutput = null;
183 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
185 pOutput = __pSymmetricCipher->DoCipherN(input);
186 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do decrypt operation", GetErrorMessage(GetLastResult()));
192 CastCipher::Initialize(void)
194 result r = E_SUCCESS;
196 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
198 r = __pSymmetricCipher->Initialize();
199 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to do initialize operation.", GetErrorMessage(r));
205 CastCipher::UpdateN(const Tizen::Base::ByteBuffer& input)
207 ByteBuffer* pOutput = null;
211 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
213 pOutput = __pSymmetricCipher->UpdateN(input);
214 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do update operation", GetErrorMessage(GetLastResult()));
220 CastCipher::FinalizeN(void)
222 ByteBuffer* pOutput = null;
226 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
228 pOutput = __pSymmetricCipher->FinalizeN();
229 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do finalize operation", GetErrorMessage(GetLastResult()));
235 CastCipher::WrapN(const Tizen::Base::ByteBuffer& secretKey)
239 SetLastResult(E_UNSUPPORTED_ALGORITHM);
245 CastCipher::UnwrapN(const Tizen::Base::ByteBuffer& wrappedKey)
249 SetLastResult(E_UNSUPPORTED_ALGORITHM);
254 } } } //Tizen::Security::Crypto