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