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 FSecSecretKeyGenerator.cpp
19 * @brief This file contains the implementation of SecretKeyGenerator class.
21 #include <unique_ptr.h>
22 #include <FBaseResult.h>
23 #include <FBaseErrors.h>
24 #include <FSecSecretKey.h>
25 #include <FSecSecretKeyGenerator.h>
26 #include <FSecAesSecureRandom.h>
27 #include <FSecDesSecureRandom.h>
28 #include <FSecDesEdeSecureRandom.h>
29 #include <FBaseSysLog.h>
31 using namespace Tizen::Base;
34 namespace Tizen { namespace Security
36 static const int _MAX_DES_KEY_GEN_SIZE = 8;
37 static const int _MAX_AES_KEY_GEN_SIZE = 16;
38 static const int _MAX_DES_EDE_KEY_GEN_SIZE = 24;
39 static const int _DEFAULT_KEY_SIZE = 32;
41 SecretKeyGenerator::SecretKeyGenerator(void)
44 , __pSecretKeyGeneratorImpl(null)
48 SecretKeyGenerator::~SecretKeyGenerator(void)
54 SecretKeyGenerator::Construct(const Tizen::Base::ByteBuffer& keyBuffer)
58 SysAssertf(__keyBytes.GetPointer() == null && __keyBytes.GetRemaining() == 0, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class");
60 r = __keyBytes.Construct(keyBuffer);
61 SysTryReturn(NID_SEC, !IsFailed(r), r, r, "[%s] Input key data should be valid.", GetErrorMessage(r));
67 SecretKeyGenerator::Construct(const Tizen::Base::String& algorithm)
71 SysAssertf(__pRandom == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class");
73 __algorithm = algorithm;
75 if (!__algorithm.CompareTo(L"AES"))
77 __pRandom = new (std::nothrow) AesSecureRandom();
78 __keyGenSize = _MAX_AES_KEY_GEN_SIZE;
80 else if (!__algorithm.CompareTo(L"DES"))
82 __pRandom = new (std::nothrow) DesSecureRandom();
83 __keyGenSize = _MAX_DES_KEY_GEN_SIZE;
85 else if (!__algorithm.CompareTo(L"3DES"))
87 __pRandom = new (std::nothrow) DesEdeSecureRandom();
88 __keyGenSize = _MAX_DES_EDE_KEY_GEN_SIZE;
95 SysTryReturn(NID_SEC, __pRandom != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
101 SecretKeyGenerator::Construct(int keySize)
103 result r = E_SUCCESS;
105 SysAssertf(__pRandom == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class");
107 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).");
109 __pRandom = new (std::nothrow) AesSecureRandom();
110 SysTryReturn(NID_SEC, __pRandom != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
112 __keyGenSize = keySize;
119 SecretKeyGenerator::GenerateKeyN(void)
121 result r = E_SUCCESS;
125 std::unique_ptr<SecretKey> pSecKey(new (std::nothrow) SecretKey());
126 SysTryReturn(NID_SEC, pSecKey != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
128 if (__pRandom != null)
130 std::unique_ptr<ByteBuffer> pRandomBytes(__pRandom->GenerateRandomBytesN(__keyGenSize));
131 SysTryReturn(NID_SEC, pRandomBytes, null, GetLastResult(), "[%s] Failed to generate random bytes.", GetErrorMessage(GetLastResult()));
133 r = pSecKey->SetKey(*pRandomBytes.get());
134 SysTryReturn(NID_SEC, !IsFailed(r), null, r, "[%s] Failed to do set key operation.", GetErrorMessage(r));
138 r = pSecKey->SetKey(__keyBytes);
139 SysTryReturn(NID_SEC, !IsFailed(r), null, r, "[%s] Failed to do set key operation.", GetErrorMessage(r));
142 return pSecKey.release();
145 } } //Tizen::Security