sync with tizen_2.0
[platform/framework/native/appfw.git] / src / security / FSecDesSecureRandom.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file                FSecDesSecureRandom.cpp
20  * @brief               This is the implementation file for DesSecureRandom class.
21  */
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"
29
30 using namespace Tizen::Base;
31 using namespace Tizen::Security;
32
33
34 namespace Tizen { namespace Security
35 {
36
37 DesSecureRandom::DesSecureRandom(void)
38         : __pDesSecureRandomImpl(null)
39 {
40 }
41
42 DesSecureRandom::~DesSecureRandom(void)
43 {
44 }
45
46 ByteBuffer*
47 DesSecureRandom::GenerateRandomBytesN(int numBytes)
48 {
49         ByteBuffer* pRandomBytes = null;
50
51         ClearLastResult();
52
53         SysTryReturn(NID_SEC, numBytes > 0, null, E_INVALID_ARG, "[E_INVALID_ARG] The input number of bytes should be positive.");
54
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.");
57
58         memset(pTempBuf.get(), 0, numBytes);
59
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()));
63
64         return pRandomBytes;
65 }
66
67 } } //Tizen::Security