Merge "Revert "Fix duplicated alarms."" into tizen_2.2
[platform/framework/native/appfw.git] / src / security / FSecSecretKeyGenerator.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                FSecSecretKeyGenerator.cpp
19  * @brief               This file contains the implementation of SecretKeyGenerator class.
20  */
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>
30
31 using namespace Tizen::Base;
32
33
34 namespace Tizen { namespace Security
35 {
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;
40
41 SecretKeyGenerator::SecretKeyGenerator(void)
42         : __keyGenSize(0)
43         , __pRandom(null)
44         , __pSecretKeyGeneratorImpl(null)
45 {
46 }
47
48 SecretKeyGenerator::~SecretKeyGenerator(void)
49 {
50         delete __pRandom;
51 }
52
53 result
54 SecretKeyGenerator::Construct(const Tizen::Base::ByteBuffer& keyBuffer)
55 {
56         result r = E_SUCCESS;
57
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");
59
60         r = __keyBytes.Construct(keyBuffer);
61         SysTryReturn(NID_SEC, !IsFailed(r), r, r, "[%s] Input key data should be valid.", GetErrorMessage(r));
62
63         return r;
64 }
65
66 result
67 SecretKeyGenerator::Construct(const Tizen::Base::String& algorithm)
68 {
69         result r = E_SUCCESS;
70
71         SysAssertf(__pRandom == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class");
72
73         __algorithm = algorithm;
74
75         if (!__algorithm.CompareTo(L"AES"))
76         {
77                 __pRandom = new (std::nothrow) AesSecureRandom();
78                 __keyGenSize = _MAX_AES_KEY_GEN_SIZE;
79         }
80         else if (!__algorithm.CompareTo(L"DES"))
81         {
82                 __pRandom = new (std::nothrow) DesSecureRandom();
83                 __keyGenSize = _MAX_DES_KEY_GEN_SIZE;
84         }
85         else if (!__algorithm.CompareTo(L"3DES"))
86         {
87                 __pRandom = new (std::nothrow) DesEdeSecureRandom();
88                 __keyGenSize = _MAX_DES_EDE_KEY_GEN_SIZE;
89         }
90         else
91         {
92                 return E_INVALID_ARG;
93         }
94
95         SysTryReturn(NID_SEC, __pRandom != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
96
97         return r;
98 }
99
100 result
101 SecretKeyGenerator::Construct(int keySize)
102 {
103         result r = E_SUCCESS;
104
105         SysAssertf(__pRandom == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class");
106
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).");
108
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.");
111
112         __keyGenSize = keySize;
113
114         return r;
115
116 }
117
118 ISecretKey*
119 SecretKeyGenerator::GenerateKeyN(void)
120 {
121         result r = E_SUCCESS;
122
123         ClearLastResult();
124
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.");
127
128         if (__pRandom != null)
129         {
130                 std::unique_ptr<ByteBuffer> pRandomBytes(__pRandom->GenerateRandomBytesN(__keyGenSize));
131                 SysTryReturn(NID_SEC, pRandomBytes, null, GetLastResult(), "[%s] Failed to generate random bytes.", GetErrorMessage(GetLastResult()));
132
133                 r = pSecKey->SetKey(*pRandomBytes.get());
134                 SysTryReturn(NID_SEC, !IsFailed(r), null, r, "[%s] Failed to do set key operation.", GetErrorMessage(r));
135         }
136         else
137         {
138                 r = pSecKey->SetKey(__keyBytes);
139                 SysTryReturn(NID_SEC, !IsFailed(r), null, r, "[%s] Failed to do set key operation.", GetErrorMessage(r));
140         }
141
142         return pSecKey.release();
143 }
144
145 } } //Tizen::Security