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 FSecCryptoSkipJackCipher.cpp
19 * @brief This header file contains the declaration of Tizen::Security::Crypto::SkipJackCipher.
22 #include <openssl/evp.h>
23 #include <openssl/crypto.h>
24 #include <FBaseByteBuffer.h>
25 #include <FSecCryptoSkipJackCipher.h>
26 #include <FSecSecretKey.h>
27 #include <FBaseSysLog.h>
28 #include "FSecCrypto_SkipJackModes.h"
29 #include "FSecCrypto_SymmetricCipher.h"
31 using namespace Tizen::Base;
33 const EVP_CIPHER* EvpSkipJackCbc80();
34 const EVP_CIPHER* EvpSkipJackEcb80();
37 namespace Tizen { namespace Security { namespace Crypto
40 static const int _TRANSFORMATION_STRING_PART_1_LENGTH = 3;
41 static const int _TRANSFORMATION_STRING_PART_2_BEGIN = 4;
42 static const int _TRANSFORMATION_STRING_PART_2_LENGTH_A = 9;
43 static const int _TRANSFORMATION_STRING_PART_2_LENGTH_B = 12;
45 SkipJackCipher::SkipJackCipher(void)
46 : __pSymmetricCipher(null) // Default is SKIPJACK/CBC/80
47 , __pCipherAlgorithm(null)
48 , __pSkipJackCipherImpl(null)
52 SkipJackCipher::~SkipJackCipher(void)
54 delete __pSymmetricCipher;
58 SkipJackCipher::Construct(const Tizen::Base::String& transformation, CipherOperation opMode)
62 String cipherMode = null;
63 String padding = null;
65 SysAssertf(__pSymmetricCipher == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class");
67 __pSymmetricCipher = new (std::nothrow) _SymmetricCipher();
68 SysTryReturn(NID_SEC_CRYPTO, __pSymmetricCipher != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
70 SysTryCatch(NID_SEC_CRYPTO, transformation.GetLength() > 0, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The transformation string should be valid.");
72 // Setting the key size, cipher mode and padding for the SkipJack Construct
73 r = transformation.SubString(0, _TRANSFORMATION_STRING_PART_1_LENGTH, cipherMode);
74 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
76 if (cipherMode.CompareTo(L"CBC") == E_SUCCESS)
78 __pCipherAlgorithm = EvpSkipJackCbc80();
80 else if (cipherMode.CompareTo(L"ECB") == E_SUCCESS)
82 __pCipherAlgorithm = EvpSkipJackEcb80();
87 SysLogException(NID_SEC_CRYPTO, r, "The cipher algorithm for requested mode is not supported.");
91 r = transformation.SubString(_TRANSFORMATION_STRING_PART_2_BEGIN, _TRANSFORMATION_STRING_PART_2_LENGTH_A, padding);
92 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
94 if (padding.CompareTo(L"NOPADDING") == E_SUCCESS)
100 r = transformation.SubString(_TRANSFORMATION_STRING_PART_2_BEGIN, _TRANSFORMATION_STRING_PART_2_LENGTH_B, padding);
101 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
103 if (padding.CompareTo(L"PKCS7PADDING") == E_SUCCESS)
110 SysLogException(NID_SEC_CRYPTO, r, "The cipher algorithm for requested padding is not supported.");
115 // sets the transformation
116 r = __pSymmetricCipher->SetTransformation(__pCipherAlgorithm, padVal);
117 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Failed to do set transformation operation.", GetErrorMessage(r));
119 SysTryCatch(NID_SEC_CRYPTO, opMode == CIPHER_ENCRYPT || opMode == CIPHER_DECRYPT,
120 r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The opMode should be valid.");
122 // sets the SetCipherOpearation
123 r = __pSymmetricCipher->SetCipherOperation(opMode);
124 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] The cipher operation request should be valid.", GetErrorMessage(r));
129 delete __pSymmetricCipher;
130 __pSymmetricCipher = null;
136 SkipJackCipher::SetKey(const Tizen::Security::ISecretKey& key)
138 result r = E_SUCCESS;
141 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
143 std::unique_ptr<ByteBuffer> pKey(key.GetEncodedN());
144 SysTryReturnResult(NID_SEC_CRYPTO, pKey != null, E_INVALID_ARG, "Key data should be valid.");
146 keyLen = static_cast< int >(pKey->GetRemaining());
147 SysTryReturnResult(NID_SEC_CRYPTO, keyLen == __pCipherAlgorithm->key_len, E_INVALID_ARG, "Input key length should be equal to algorithm key length.");
149 r = __pSymmetricCipher->SetKey(key);
150 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to do set key operation.", GetErrorMessage(r));
156 SkipJackCipher::SetInitialVector(const Tizen::Base::ByteBuffer& initialVector)
158 result r = E_SUCCESS;
160 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
162 r = __pSymmetricCipher->SetInitialVector(initialVector);
163 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to do set initial vector operation.", GetErrorMessage(r));
169 SkipJackCipher::EncryptN(const Tizen::Base::ByteBuffer& input)
171 ByteBuffer* pOutput = null;
175 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
177 pOutput = __pSymmetricCipher->DoCipherN(input);
178 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do encrypt operation", GetErrorMessage(GetLastResult()));
184 SkipJackCipher::DecryptN(const Tizen::Base::ByteBuffer& input)
186 ByteBuffer* pOutput = null;
190 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
192 pOutput = __pSymmetricCipher->DoCipherN(input);
193 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do decrypt operation", GetErrorMessage(GetLastResult()));
199 SkipJackCipher::Initialize(void)
201 result r = E_SUCCESS;
203 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
205 r = __pSymmetricCipher->Initialize();
206 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to do initialize operation.", GetErrorMessage(r));
212 SkipJackCipher::UpdateN(const Tizen::Base::ByteBuffer& input)
214 ByteBuffer* pOutput = null;
218 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
220 pOutput = __pSymmetricCipher->UpdateN(input);
221 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do update operation", GetErrorMessage(GetLastResult()));
227 SkipJackCipher::FinalizeN(void)
229 ByteBuffer* pOutput = null;
233 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
235 pOutput = __pSymmetricCipher->FinalizeN();
236 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do finalize operation", GetErrorMessage(GetLastResult()));
242 SkipJackCipher::WrapN(const Tizen::Base::ByteBuffer& secretKey)
246 SetLastResult(E_UNSUPPORTED_ALGORITHM);
252 SkipJackCipher::UnwrapN(const Tizen::Base::ByteBuffer& wrappedKey)
256 SetLastResult(E_UNSUPPORTED_ALGORITHM);
261 } } } //Tizen::Security::Crypto