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 FSecCryptoAesCipher.cpp
19 * @brief This file contains the declaration of Tizen::Security::Crypto::AesCipher.
23 #include <unique_ptr.h>
24 #include <openssl/evp.h>
25 #include <openssl/crypto.h>
26 #include <FBaseResult.h>
27 #include <FBaseErrors.h>
28 #include <FSecCryptoAesCipher.h>
29 #include <FSecSecretKey.h>
30 #include <FBaseSysLog.h>
31 #include "FSecCrypto_SymmetricCipher.h"
33 using namespace Tizen::Base;
36 namespace Tizen { namespace Security { namespace Crypto
39 static const int _TRANSFORMATION_STRING_PART_1_LENGTH = 3;
40 static const int _TRANSFORMATION_STRING_PART_2_BEGIN = 4;
41 static const int _TRANSFORMATION_STRING_PART_2_LENGTH = 3;
42 static const int _TRANSFORMATION_STRING_PART_3_BEGIN = 8;
43 static const int _TRANSFORMATION_STRING_PART_3_LENGTH_A = 9;
44 static const int _TRANSFORMATION_STRING_PART_3_LENGTH_B = 12;
46 AesCipher::AesCipher(void)
47 : __pSymmetricCipher(null) // Default is AES/CBC/128
48 , __pCipherAlgorithm(null)
49 , __pAesCipherImpl(null)
53 AesCipher::~AesCipher(void)
55 delete __pSymmetricCipher;
59 AesCipher::Construct(const Tizen::Base::String& transformation, CipherOperation opMode)
63 String cipherMode = null;
64 String padding = null;
67 SysAssertf(__pSymmetricCipher == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class");
69 __pSymmetricCipher = new (std::nothrow) _SymmetricCipher();
70 SysTryReturn(NID_SEC_CRYPTO, __pSymmetricCipher != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
72 SysTryCatch(NID_SEC_CRYPTO, transformation.GetLength() > 0, r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The transformation string should be valid.");
74 // Setting the key size, cipher mode and padding for the Aes Construct
75 r = transformation.SubString(0, _TRANSFORMATION_STRING_PART_1_LENGTH, cipherMode);
76 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
78 if (cipherMode.CompareTo(L"CBC") == E_SUCCESS)
80 r = transformation.SubString(_TRANSFORMATION_STRING_PART_2_BEGIN, _TRANSFORMATION_STRING_PART_2_LENGTH, keyBit);
81 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
83 if (keyBit.CompareTo(L"128") == E_SUCCESS)
85 __pCipherAlgorithm = EVP_aes_128_cbc();
87 else if (keyBit.CompareTo(L"192") == E_SUCCESS)
89 __pCipherAlgorithm = EVP_aes_192_cbc();
91 else if (keyBit.CompareTo(L"256") == E_SUCCESS)
93 __pCipherAlgorithm = EVP_aes_256_cbc();
98 SysLogException(NID_SEC_CRYPTO, r, "The cipher algorithm for requested key length is not supported.");
102 else if (cipherMode.CompareTo(L"ECB") == E_SUCCESS)
104 r = transformation.SubString(_TRANSFORMATION_STRING_PART_2_BEGIN, _TRANSFORMATION_STRING_PART_2_LENGTH, keyBit);
105 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
107 if (keyBit.CompareTo(L"128") == E_SUCCESS)
109 __pCipherAlgorithm = EVP_aes_128_ecb();
111 else if (keyBit.CompareTo(L"192") == E_SUCCESS)
113 __pCipherAlgorithm = EVP_aes_192_ecb();
115 else if (keyBit.CompareTo(L"256") == E_SUCCESS)
117 __pCipherAlgorithm = EVP_aes_256_ecb();
122 SysLogException(NID_SEC_CRYPTO, r, "The cipher algorithm for requested key length is not supported.");
129 SysLogException(NID_SEC_CRYPTO, r, "The cipher algorithm for requested mode is not supported.");
133 r = transformation.SubString(_TRANSFORMATION_STRING_PART_3_BEGIN, _TRANSFORMATION_STRING_PART_3_LENGTH_A, padding);
134 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
136 if (padding.CompareTo(L"NOPADDING") == E_SUCCESS)
142 r = transformation.SubString(_TRANSFORMATION_STRING_PART_3_BEGIN, _TRANSFORMATION_STRING_PART_3_LENGTH_B, padding);
143 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), r = E_INVALID_ARG, E_INVALID_ARG, "The transformation string should be valid.");
145 if (padding.CompareTo(L"PKCS7PADDING") == E_SUCCESS)
152 SysLogException(NID_SEC_CRYPTO, r, "The cipher algorithm for requested padding is not supported.");
157 // sets the Transformation String
158 r = __pSymmetricCipher->SetTransformation(__pCipherAlgorithm, padVal);
159 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Failed to do set transformation operation.", GetErrorMessage(r));
161 SysTryCatch(NID_SEC_CRYPTO, opMode == CIPHER_ENCRYPT || opMode == CIPHER_DECRYPT || opMode == CIPHER_WRAP || opMode == CIPHER_UNWRAP,
162 r = E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The opMode should be valid.");
164 // sets the CipherOpearation
165 r = __pSymmetricCipher->SetCipherOperation(opMode);
166 SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] The cipher operation request should be valid.", GetErrorMessage(r));
171 delete __pSymmetricCipher;
172 __pSymmetricCipher = null;
178 AesCipher::SetKey(const Tizen::Security::ISecretKey& key)
180 result r = E_SUCCESS;
183 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
185 std::unique_ptr<ByteBuffer> pKey(key.GetEncodedN());
186 SysTryReturnResult(NID_SEC_CRYPTO, pKey != null, E_INVALID_ARG, "Input key data should be valid.");
188 keyLen = static_cast< int >(pKey->GetRemaining());
189 SysTryReturnResult(NID_SEC_CRYPTO, keyLen == __pCipherAlgorithm->key_len, E_INVALID_ARG, "Input key length should be equal to algorithm key length.");
191 r = __pSymmetricCipher->SetKey(key);
192 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to do set key operation.", GetErrorMessage(r));
198 AesCipher::SetInitialVector(const Tizen::Base::ByteBuffer& initialVector)
200 result r = E_SUCCESS;
202 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
204 r = __pSymmetricCipher->SetInitialVector(initialVector);
205 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to do set initial vector operation.", GetErrorMessage(r));
211 AesCipher::EncryptN(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->DoCipherN(input);
220 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do encrypt operation", GetErrorMessage(GetLastResult()));
226 AesCipher::DecryptN(const Tizen::Base::ByteBuffer& input)
228 ByteBuffer* pOutput = null;
232 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
234 pOutput = __pSymmetricCipher->DoCipherN(input);
235 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do decrypt operation", GetErrorMessage(GetLastResult()));
241 AesCipher::Initialize(void)
243 result r = E_SUCCESS;
245 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
247 r = __pSymmetricCipher->Initialize();
248 SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), r, r, "[%s] Failed to do initialize operation.", GetErrorMessage(r));
254 AesCipher::UpdateN(const Tizen::Base::ByteBuffer& input)
256 ByteBuffer* pOutput = null;
260 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
262 pOutput = __pSymmetricCipher->UpdateN(input);
263 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do update operation", GetErrorMessage(GetLastResult()));
269 AesCipher::FinalizeN(void)
271 ByteBuffer* pOutput = null;
275 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
277 pOutput = __pSymmetricCipher->FinalizeN();
278 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do finalize operation", GetErrorMessage(GetLastResult()));
284 AesCipher::WrapN(const Tizen::Base::ByteBuffer& secretKey)
286 ByteBuffer* pOutput = null;
290 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
292 pOutput = __pSymmetricCipher->WrapN(secretKey);
293 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do wrap operation", GetErrorMessage(GetLastResult()));
299 AesCipher::UnwrapN(const Tizen::Base::ByteBuffer& wrappedKey)
301 ByteBuffer* pOutput = null;
305 SysAssertf(__pSymmetricCipher != null, "Not yet constructed. Construct() should be called before use.");
307 pOutput = __pSymmetricCipher->UnwrapN(wrappedKey);
308 SysTryReturn(NID_SEC_CRYPTO, pOutput != null, null, GetLastResult(), "[%s]Failed to do unwrap operation", GetErrorMessage(GetLastResult()));
313 } } } //Tizen::Security::Crypto