Merge "Add to support a multi-window mode" into tizen_2.2
[platform/framework/native/appfw.git] / src / security / FSecAesSecureRandom.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file                FSecAesSecureRandom.cpp
19  * @brief               This is the implementation file for AesSecureRandom class.
20  */
21 #include <unique_ptr.h>
22 #include <openssl/evp.h>
23 #include <FBaseResult.h>
24 #include <FBaseErrors.h>
25 #include <FSecAesSecureRandom.h>
26 #include <FBaseSysLog.h>
27 #include "FSec_Prng.h"
28
29 using namespace Tizen::Base;
30 using namespace Tizen::Security;
31
32
33 namespace Tizen { namespace Security
34 {
35
36 AesSecureRandom::AesSecureRandom(void)
37         : __pAesSecureRandomImpl(null)
38 {
39 }
40
41 AesSecureRandom::~AesSecureRandom(void)
42 {
43 }
44
45 ByteBuffer*
46 AesSecureRandom::GenerateRandomBytesN(int numBytes)
47 {
48         ByteBuffer* pRandomBytes = null;
49
50         ClearLastResult();
51
52         SysTryReturn(NID_SEC, numBytes > 0, null, E_INVALID_ARG, "[E_INVALID_ARG] The input number of bytes should be positive.");
53
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.");
56
57         memset(pTempBuf.get(), 0, numBytes);
58
59         //call the GetRandomBytes of Prng class
60         pRandomBytes = _Prng::GetRandomBytesN(EVP_aes_128_ecb(), numBytes);
61         SysTryReturn(NID_SEC, pRandomBytes != null, null, GetLastResult(), "[%s]Failed to fill random bytes in buffer.", GetErrorMessage(GetLastResult()));
62
63         return pRandomBytes;
64 }
65
66 } } //Tizen::Security