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 FSecDesSecureRandom.cpp
20 * @brief This is the implementation file for DesSecureRandom class.
22 #include <unique_ptr.h>
23 #include <openssl/evp.h>
24 #include <FBaseResult.h>
25 #include <FBaseErrors.h>
26 #include <FSecDesSecureRandom.h>
27 #include <FBaseSysLog.h>
28 #include "FSec_Prng.h"
30 using namespace Tizen::Base;
31 using namespace Tizen::Security;
34 namespace Tizen { namespace Security
37 DesSecureRandom::DesSecureRandom(void)
38 : __pDesSecureRandomImpl(null)
42 DesSecureRandom::~DesSecureRandom(void)
47 DesSecureRandom::GenerateRandomBytesN(int numBytes)
49 ByteBuffer* pRandomBytes = null;
53 SysTryReturn(NID_SEC, numBytes > 0, null, E_INVALID_ARG, "[E_INVALID_ARG] The input number of bytes should be positive.");
55 std::unique_ptr<byte[]> pTempBuf(new (std::nothrow) byte[numBytes]);
56 SysTryReturn(NID_SEC, pTempBuf != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
58 memset(pTempBuf.get(), 0, numBytes);
60 //call the GetRandomBytes of Prng class
61 pRandomBytes = _Prng::GetRandomBytesN(EVP_des_ecb(), numBytes);
62 SysTryReturn(NID_SEC, pRandomBytes != null, null, GetLastResult(), "[%s]Failed to fill random bytes in buffer.", GetErrorMessage(GetLastResult()));