sync with master
[platform/framework/native/appfw.git] / src / security / pkcs / FSecPkcs_Pkcs05SchemesImpl.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2013 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                FSecPkcs_Pkcs05SchemesImpl.h
20  * @brief               This is the header file for the %_Pkcs05SchemesImpl class.
21  *
22  * This header file contains the declarations of the %_Pkcs05SchemesImpl class.
23  */
24
25 #ifndef _FSEC_PKCS_INTERNAL_PKCS_05_SCHEMES_H_
26 #define _FSEC_PKCS_INTERNAL_PKCS_05_SCHEMES_H_
27
28 #include <FBaseByteBuffer.h>
29 #include <FSecPkcsPkcs05PbKdf2Parameters.h>
30 #include <FSecPkcsPkcs05PbEs2Parameters.h>
31 #include <FSecPkcsPkcs05PbMacParameters.h>
32 #include <FSecPkcsPkcs05Schemes.h>
33
34 struct env_md_st;
35
36 namespace Tizen { namespace Security { namespace Pkcs
37 {
38
39 /**
40   * @class              _Pkcs05SchemesImpl
41   * @brief              This class implements the functionalities specified by the pkcs05 schemes like Key derivation function,
42   *                                             encryption and signature schemes.
43   * @since              2.1
44   *
45   */
46
47 class _Pkcs05SchemesImpl
48 {
49 public:
50         /**
51          * The object is not fully constructed after this constructor is called. @n
52          * For full construction, the Construct() method must be called right after calling this constructor.
53          *
54          * @since       2.1
55          */
56         _Pkcs05SchemesImpl(void);
57
58         /**
59          * This destructor overrides Tizen::Base::Object::~Object().
60          *
61          * @since       2.1
62          */
63         virtual ~_Pkcs05SchemesImpl(void);
64
65         /**
66          * Initializes the %_Pkcs05SchemesImpl with supplied input parameters values.
67          *
68          * @since               2.1
69          *
70          * @return              An error code.
71          * @param[in]   password                                        An instance of ByteBuffer , holding the password.
72          * @param[in]   derivedKeyLength        A positive integer contains the length of derived key.@n
73          *                                                                                                                      It is a variable length in PbEs2 based on underlying encryption operation.
74          * @exception   E_SUCCESS                                       The method is successful.
75          * @exception   E_INVALID_ARG                   The specified input parameter is invalid.
76          */
77         result Construct(const Tizen::Base::ByteBuffer& password, int derivedKeyLength);
78
79         /**
80          * This API will generate the Password based derived key using PBKDF2 scheme.
81          * The length of the derived key is essentially unbounded. However, the maximum effective search space for
82          * the derived key may be limited by the structure of the underlying pseudo random function.
83          *
84          * @since               2.1
85          *
86          * @return              A pointer to ByteBuffer contains the derived key, @n
87          *                              else @c null if an error occurs.
88          * @param[in]   params                                          An instance of PbKdf2Parameters, containing Kdf2 parameter.
89          *                                                                                                                      salt value, iteration count and key length.
90          * @exception   E_SUCCESS                                       The method is successful.
91          * @exception   E_INVALID_ARG                   The specified input parameter is invalid.
92          * @exception   E_SYSTEM                                        The method cannot proceed due to a severe system error.
93          * @remarks             The specific error code can be accessed using the GetLastResult() method.
94          */
95         Tizen::Base::ByteBuffer* GenerateKeyKdf2N(const Pkcs05PbKdf2Parameters& params);
96
97         /**
98          * This API implements EncryptionScheme2N API which combines the GenerateKeyKdf2N function and encrypts the
99          * message M under a password P to produce a ciphertext C, by applying selected key derivation function KDF
100          * (which is GenerateKeyKdf2N) and selected underlying encryption scheme (which can be any block chiper).
101          *
102          * @since               2.1
103          *
104          * @return              Pointer to ByteBuffer contains the encrypted message, @n
105          *                              else @c null if an error occurs.
106          * @param[in]   params                                          An instance of PbEs2Parameters, parameter structure holding AlgorithmIdentifier for key derivation
107          *                                                                                                                      function and AlgorithmIdentifier for underlying encryption scheme.
108          * @param[in]   message                                         An instance of ByteBuffer, holding the plain text to encrypt.
109          * @exception   E_SUCCESS                                       The method is successful.
110          * @exception   E_INVALID_ARG                   The specified input parameter is invalid.
111          * @exception   E_SYSTEM                                        The method cannot proceed due to a severe system error.
112          * @remarks             The specific error code can be accessed using the GetLastResult() method.
113          */
114         Tizen::Base::ByteBuffer* EncryptionScheme2N(const Pkcs05PbEs2Parameters& params, const Tizen::Base::ByteBuffer& message);
115
116         /**
117          * This API implements DecryptionScheme2N API which combines the GenerateKeyKdf2N function and decrypts the ciphertext C,
118          * under a password P to recover message M , by applying selected key derivation function KDF(which is GenerateKeyKdf2N)
119          * and selected underlying decryption algorithm (which can be any block cipher).
120          *
121          * @since               2.1
122          *
123          * @return              Pointer to ByteBuffer contains the plain text message.
124          * @param[in]   params                                          An instance of PbEs2Parameters, parameter structure holding AlgorithmIdentifier for key derivation
125          *                                                                                                                      function and AlgorithmIdentifier for underlying encryption scheme.
126          * @param[in]   message                                         An instance of ByteBuffer, holding the cipher text to decrypt.
127          * @exception   E_SUCCESS                                       The method is successful.
128          * @exception   E_INVALID_ARG                   The specified input parameter is invalid.
129          * @exception   E_SYSTEM                                        The method cannot proceed due to a severe system error.
130          * @remarks             The specific error code can be accessed using the GetLastResult() method.
131          */
132         Tizen::Base::ByteBuffer* DecryptionScheme2N(const Pkcs05PbEs2Parameters& params, const Tizen::Base::ByteBuffer& message);
133
134         /**
135          * This API implements GetPbHMacN API process a message M under a password P to generate a message authentication code T,
136          * by applying selected key derivation function KDF(which is GenerateKeyKdf2N) and selected underlying message authentication scheme
137          * (which can be any HMAC algorithm). GenerateMacN combines a password-based key derivation function, which shall be
138          * GenerateKeyKdf2N for this version of PKCS #5, with an underlying message authentication scheme
139          * The key length and any other parameters for the underlying message authentication scheme
140          * depend on the scheme.
141          *
142          * @since               2.1
143          *
144          * @return              Pointer to ByteBuffer containing the HMAC generated from message, @n
145          *                              else @c null if an error occurs.
146          * @param[in]   params                                          An instance of bMacParameters, parameter structure holding AlgorithmIdentifier for key derivation
147          *                                                                                                                      function and AlgorithmIdentifier for underlying encryption scheme.
148          * @param[in]   message                                         An instance of ByteBuffer, holding the message for which MAC needs to be generated.
149          * @exception   E_SUCCESS                                       The method is successful.
150          * @exception   E_INVALID_ARG                   The specified input parameter is invalid.
151          * @exception   E_SYSTEM                                        The method cannot proceed due to a severe system error.
152          * @remarks             The specific error code can be accessed using the GetLastResult() method.
153          */
154         Tizen::Base::ByteBuffer* GetPbHMacN(const Pkcs05PbMacParameters& params, const Tizen::Base::ByteBuffer& message);
155
156         /**
157          * This API implements VerifyMac API which processes a message M under a password P to verify a message authentication code T,
158          * by applying a selected key derivation function KDF2 and a selected underlying message authentication scheme combines a
159          * password-based key derivation function, which shall be GenerateKeyKdf2N for this version of PKCS #5, with an underlying
160          * message authentication scheme (which can be any SHA-1 or any SHA-2 algorithm). The key length and any other parameters
161          * for the underlying message authentication scheme depend on the scheme.
162          *
163          * @since               2.1
164          *
165          * @return              An error code
166          * @param[in]   params                                                  An instance of PbMacParameters, parameter structure holding AlgorithmIdentifier for key derivation
167          *                                                                                                                              function and AlgorithmIdentifier for underlying encryption scheme.
168          * @param[in]   message                                                 An instance of ByteBuffer, holding the message M.
169          * @param[in]   mac                                                                     An instance of ByteBuffer, holding the HMAC mac that needs to be verified against message.
170          * @exception   E_SUCCESS                                               The method is successful.
171          * @exception   E_INVALID_ARG                           The specified input parameter is invalid.
172          * @exception   E_SYSTEM                                                The method cannot proceed due to a severe system error.
173          * @remarks             The specific error code can be accessed using the GetLastResult() method.
174          */
175         result VerifyPbMac(const Pkcs05PbMacParameters& params, const Tizen::Base::ByteBuffer& message, const Tizen::Base::ByteBuffer& mac);
176
177         static _Pkcs05SchemesImpl* GetInstance(Pkcs05Schemes& pkcs05Schemes);
178         static const _Pkcs05SchemesImpl* GetInstance(const Pkcs05Schemes& pkcs05Schemes);
179
180 private:
181         // This copy constructor is intentionally declared as private to prohibit @n
182         // copying of objects by users.
183         //
184         // @since               2.1
185         _Pkcs05SchemesImpl(const _Pkcs05SchemesImpl& rhs);
186
187         // The implementation of this copy assignment operator is intentionally blank and @n
188         // declared as private to prohibit copying of objects.
189         //
190         // @since               2.1
191         _Pkcs05SchemesImpl& operator =(const _Pkcs05SchemesImpl& rhs);
192
193 private:
194         Tizen::Base::ByteBuffer __password;
195         const env_md_st* __pMacAlgo;
196         int __derivedKeyLength;
197         friend class Pkcs05Schemes;
198
199 }; //_Pkcs05SchemesImpl
200
201 } } } // Tizen::Security::Pkcs
202
203 #endif //_FSEC_PKCS_INTERNAL_PKCS_05_SCHEMES_H_