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