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 FSecCryptoRc4Cipher.cpp
19 * @brief This file contains the implementation of Tizen::Security::Crypto::Rc4Cipher.
21 #include <FBaseResult.h>
22 #include <FBaseErrors.h>
23 #include <FSecCryptoRc4Cipher.h>
24 #include <FSecSecretKey.h>
25 #include <FBaseSysLog.h>
26 #include <openssl/evp.h>
27 #include <openssl/crypto.h>
28 #include "FSecCrypto_SymmetricCipher.h"
30 using namespace Tizen::Base;
33 namespace Tizen { namespace Security { namespace Crypto
36 Rc4Cipher::Rc4Cipher(void)
37 : __pSymmetricCipher(null) // Default is None
38 , __pCipherAlgorithm(null)
39 , __pRc4CipherImpl(null)
43 Rc4Cipher::~Rc4Cipher(void)
45 delete __pSymmetricCipher;
49 Rc4Cipher::Construct(const Tizen::Base::String& transformation, CipherOperation opMode)
53 SysAssertf(__pSymmetricCipher == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class");
55 __pSymmetricCipher = new (std::nothrow) _SymmetricCipher();
56 SysTryReturn(NID_SEC_CRYPTO, __pSymmetricCipher != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
58 SysTryCatch(NID_SEC_CRYPTO, transformation.GetLength() > 0, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The transformation string should be valid.");
60 // Setting the key size, cipher mode and padding for the Rc4 Construct
61 if (transformation.CompareTo(L"NONE") == E_SUCCESS)
63 __pCipherAlgorithm = EVP_rc4();
68 SysLogException(NID_SEC_CRYPTO, r, "The requested cipher algorithm is not supported.");
72 // sets the transformation
73 r = __pSymmetricCipher->SetTransformation(__pCipherAlgorithm, false);
74 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Failed to do set transformation operation.", GetErrorMessage(r));
76 SysTryCatch(NID_SEC_CRYPTO, opMode == CIPHER_ENCRYPT || opMode == CIPHER_DECRYPT,
77 r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The opMode should be valid.");
79 // sets the SetCipherOpearation
80 r = __pSymmetricCipher->SetCipherOperation(opMode);
81 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] The cipher operation request should be valid.", GetErrorMessage(r));
86 delete __pSymmetricCipher;
87 __pSymmetricCipher = null;
94 Rc4Cipher::SetKey(const Tizen::Security::ISecretKey& key)
98 ByteBuffer* pKey = null;
100 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
102 pKey = key.GetEncodedN();
103 SysTryReturn(NID_SEC_CRYPTO, pKey != null, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The input key data should be valid.");
105 keyLen = static_cast< int >(pKey->GetRemaining());
106 SysTryCatch(NID_SEC_CRYPTO, keyLen > 0, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Input key length should be >= algorithm key length.");
108 r = __pSymmetricCipher->SetKey(key);
109 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Failed to do set key operation.", GetErrorMessage(r));
117 Rc4Cipher::SetInitialVector(const Tizen::Base::ByteBuffer& initialVector)
119 result r = E_SUCCESS;
121 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
123 r = __pSymmetricCipher->SetInitialVector(initialVector);
124 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to do set initial vector operation.", GetErrorMessage(r));
130 Rc4Cipher::EncryptN(const Tizen::Base::ByteBuffer& input)
132 ByteBuffer* pOutput = null;
136 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
138 pOutput = __pSymmetricCipher->DoCipherN(input, true);
139 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do encrypt operation", GetErrorMessage(GetLastResult()));
145 Rc4Cipher::DecryptN(const Tizen::Base::ByteBuffer& input)
147 ByteBuffer* pOutput = null;
151 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
153 pOutput = __pSymmetricCipher->DoCipherN(input, true);
154 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do decrypt operation", GetErrorMessage(GetLastResult()));
160 Rc4Cipher::Initialize(void)
162 result r = E_SUCCESS;
164 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
166 r = __pSymmetricCipher->Initialize(true);
167 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to do initialize operation.", GetErrorMessage(r));
173 Rc4Cipher::UpdateN(const Tizen::Base::ByteBuffer& input)
175 ByteBuffer* pOutput = null;
179 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
181 pOutput = __pSymmetricCipher->UpdateN(input);
182 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do update operation", GetErrorMessage(GetLastResult()));
188 Rc4Cipher::FinalizeN(void)
190 ByteBuffer* pOutput = null;
194 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
196 pOutput = __pSymmetricCipher->FinalizeN();
197 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do finalize operation", GetErrorMessage(GetLastResult()));
203 Rc4Cipher::WrapN(const Tizen::Base::ByteBuffer& secretKey)
207 SetLastResult(E_UNSUPPORTED_ALGORITHM);
213 Rc4Cipher::UnwrapN(const Tizen::Base::ByteBuffer& wrappedKey)
217 SetLastResult(E_UNSUPPORTED_ALGORITHM);
222 } } } //Tizen::Security::Crypto