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 FSecSecretKeyGenerator.cpp
20 * @brief This file contains the implementation of SecretKeyGenerator class.
22 #include <unique_ptr.h>
23 #include <FBaseResult.h>
24 #include <FBaseErrors.h>
25 #include <FSecSecretKey.h>
26 #include <FSecSecretKeyGenerator.h>
27 #include <FSecAesSecureRandom.h>
28 #include <FSecDesSecureRandom.h>
29 #include <FSecDesEdeSecureRandom.h>
30 #include <FBaseSysLog.h>
32 using namespace Tizen::Base;
35 namespace Tizen { namespace Security
37 static const int _MAX_DES_KEY_GEN_SIZE = 8;
38 static const int _MAX_AES_KEY_GEN_SIZE = 16;
39 static const int _MAX_DES_EDE_KEY_GEN_SIZE = 24;
40 static const int _DEFAULT_KEY_SIZE = 32;
42 SecretKeyGenerator::SecretKeyGenerator(void)
45 , __pSecretKeyGeneratorImpl(null)
49 SecretKeyGenerator::~SecretKeyGenerator(void)
55 SecretKeyGenerator::Construct(const Tizen::Base::ByteBuffer& keyBuffer)
59 SysAssertf(__keyBytes.GetPointer() == null && __keyBytes.GetRemaining() == 0, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class");
61 r = __keyBytes.Construct(keyBuffer);
62 SysTryReturn(NID_SEC, !IsFailed(r), r, r, "[%s] Input key data should be valid.", GetErrorMessage(r));
68 SecretKeyGenerator::Construct(const Tizen::Base::String& algorithm)
72 SysAssertf(__pRandom == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class");
74 __algorithm = algorithm;
76 if (!__algorithm.CompareTo(L"AES"))
78 __pRandom = new (std::nothrow) AesSecureRandom();
79 __keyGenSize = _MAX_AES_KEY_GEN_SIZE;
81 else if (!__algorithm.CompareTo(L"DES"))
83 __pRandom = new (std::nothrow) DesSecureRandom();
84 __keyGenSize = _MAX_DES_KEY_GEN_SIZE;
86 else if (!__algorithm.CompareTo(L"3DES"))
88 __pRandom = new (std::nothrow) DesEdeSecureRandom();
89 __keyGenSize = _MAX_DES_EDE_KEY_GEN_SIZE;
96 SysTryReturn(NID_SEC, __pRandom != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
102 SecretKeyGenerator::Construct(int keySize)
104 result r = E_SUCCESS;
106 SysAssertf(__pRandom == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class");
108 SysTryReturn(NID_SEC, keySize >= _DEFAULT_KEY_SIZE, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The input key size should not be smaller then default key size(32).");
110 __pRandom = new (std::nothrow) AesSecureRandom();
111 SysTryReturn(NID_SEC, __pRandom != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
113 __keyGenSize = keySize;
120 SecretKeyGenerator::GenerateKeyN(void)
122 result r = E_SUCCESS;
126 std::unique_ptr<SecretKey> pSecKey(new (std::nothrow) SecretKey());
127 SysTryReturn(NID_SEC, pSecKey != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
129 if (__pRandom != null)
131 std::unique_ptr<ByteBuffer> pRandomBytes(__pRandom->GenerateRandomBytesN(__keyGenSize));
132 SysTryReturn(NID_SEC, pRandomBytes, null, GetLastResult(), "[%s] Failed to generate random bytes.", GetErrorMessage(GetLastResult()));
134 r = pSecKey->SetKey(*pRandomBytes.get());
135 SysTryReturn(NID_SEC, !IsFailed(r), null, r, "[%s] Failed to do set key operation.", GetErrorMessage(r));
139 r = pSecKey->SetKey(__keyBytes);
140 SysTryReturn(NID_SEC, !IsFailed(r), null, r, "[%s] Failed to do set key operation.", GetErrorMessage(r));
143 return pSecKey.release();
146 } } //Tizen::Security