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