Merge "Fix duplicated alarms." into tizen_2.2
[platform/framework/native/appfw.git] / src / security / FSecDhKeyParameters.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                FSecDhKeyParameters.cpp
19  * @brief               This header file contains the declaration of Tizen::Security::Crypto::DhKeyParameters.
20  *
21  */
22 #include <unique_ptr.h>
23 #include <FBaseResult.h>
24 #include <FBaseErrors.h>
25 #include <FSecISecretKey.h>
26 #include <FSecSecretKey.h>
27 #include <FSecDhKeyParameters.h>
28 #include <FSecCryptoDhKeyExchange.h>
29 #include <FBaseSysLog.h>
30
31 using namespace Tizen::Base;
32
33
34 namespace Tizen { namespace Security
35 {
36
37 DhKeyParameters::DhKeyParameters(void)
38         : __privateKeySize(0)
39         , __pDhKeyParametersImpl(null)
40 {
41
42 }
43
44 DhKeyParameters::~DhKeyParameters(void)
45 {
46
47 }
48
49 result
50 DhKeyParameters::Construct(const Tizen::Base::ByteBuffer& primeNumber, const Tizen::Base::ByteBuffer& generatorNumber, int privateKeySize)
51 {
52         result r = E_SUCCESS;
53
54         SysAssertf(__prime.GetPointer() == null && __prime.GetRemaining() == 0 && __generator.GetPointer() == null && __generator.GetRemaining() == 0,
55                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class");
56
57         r = __prime.Construct(primeNumber);
58         SysTryReturn(NID_SEC, !IsFailed(r), r, r, "[%s] Input prime number is not valid.", GetErrorMessage(r));
59
60         r = __generator.Construct(generatorNumber);
61         SysTryReturn(NID_SEC, !IsFailed(r), r, r, "[%s] Input generator number is not valid.", GetErrorMessage(r));
62
63         __privateKeySize = privateKeySize;
64
65         return E_SUCCESS;
66 }
67
68 Tizen::Base::ByteBuffer*
69 DhKeyParameters::GetParameterValueN(KeyParameterType paramType) const
70 {
71         result r = E_SUCCESS;
72
73         ClearLastResult();
74
75         std::unique_ptr<ByteBuffer> pOutput(new (std::nothrow) ByteBuffer());
76         SysTryReturn(NID_SEC, pOutput != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
77
78         switch (paramType)
79         {
80         case KEY_PARAMETER_DH_P:
81                 r = pOutput->Construct(__prime);
82                 SysTryReturn(NID_SEC, !IsFailed(r), null, r, "[%s] Dh prime number should be valid.", GetErrorMessage(r));
83                 break;
84
85         case KEY_PARAMETER_DH_G:
86                 r = pOutput->Construct(__generator);
87                 SysTryReturn(NID_SEC, !IsFailed(r), null, r, "[%s] Dh generator number should be valid.", GetErrorMessage(r));
88                 break;
89
90         default:
91                 SysTryReturn(NID_SEC, false, null, E_UNSUPPORTED_TYPE, "[E_UNSUPPORTED_TYPE] The requested type is not supported");
92                 break;
93         }
94
95         SetLastResult(r);
96         return pOutput.release();
97 }
98
99 int
100 DhKeyParameters::GetParameterSize(KeyParameterType paramType) const
101 {
102         SysTryReturn(NID_SEC, paramType == KEY_PARAMETER_DH_PRIVATE_KEY_SIZE, 0, E_UNSUPPORTED_TYPE, "[E_UNSUPPORTED_TYPE] The input request is not supported.");
103         return __privateKeySize;
104 }
105
106 } } //Tizen::Security