[2.2.1] Apply reviewed header file (Base to Collection)
[platform/framework/native/appfw.git] / inc / FSecCryptoISignature.h
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                        FSecCryptoISignature.h
19  * @brief               This is the header file for the %ISignature interface.
20  *
21  * This header file contains the declarations of the %ISignature interface.
22  */
23 #ifndef _FSEC_CRYPTO_ISIGNATURE_H_
24 #define _FSEC_CRYPTO_ISIGNATURE_H_
25
26 #include <FBaseString.h>
27 #include <FBaseByteBuffer.h>
28 #include <FSecIKey.h>
29
30 namespace Tizen { namespace Security { namespace Crypto
31 {
32
33 /**
34  *      @interface      ISignature
35  *      @brief          This interface provides the functionality of a digital signature algorithm.
36  *
37  *      @since          2.0
38  *
39  *      The %ISignature interface provides the functionality of a digital signature algorithm. @n
40  *
41  *      For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/security/ciphers.htm">Ciphers</a>. @n
42  *
43  *      The following example demonstrates how to use the %ISignature interface.
44  *
45  *  @code
46  *
47  *      void
48  *      MyClass::TestSignatureSample(void)
49  *      {
50  *              result r = E_FAILURE;
51  *              ISignature *pSignature = null;
52  *              IKeyPairGenerator *pKeyPairGen = null;
53  *              KeyPair *pKeyPair = null;
54  *              IPrivateKey *pPriKey = null;
55  *              IPublicKey *pPubKey = null;
56  *              ByteBuffer input;
57  *              ByteBuffer *pOutput = null;
58  *              ByteBuffer keyBytes;
59  *              ByteBuffer iv;
60  *              byte pArray[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P'};
61  *              int size = 1024;
62  *              bool res = false;
63  *
64  *              r = input.Construct(16);
65  *              r = input.SetArray(pArray, 0, 16);
66  *              input.Flip();
67  *
68  *              pSignature = new RsaSignature();
69  *              if (pSignature == null)
70  *              {
71  *                      goto CATCH;
72  *              }
73  *
74  *              // Generates the key.
75  *              pKeyPairGen = new KeyPairGenerator();
76  *              if (pKeyPairGen == null)
77  *              {
78  *                      goto CATCH;
79  *              }
80  *
81  *              r = pKeyPairGen->Construct(size);
82  *              if (IsFailed(r))
83  *              {
84  *                      goto CATCH;
85  *              }
86  *
87  *              pKeyPair = pKeyPairGen->GenerateKeyPairN();
88  *              if (pKeyPair == null)
89  *              {
90  *                      goto CATCH;
91  *              }
92  *
93  *              pPubKey = pKeyPair->GetPublicKey();
94  *              if (pPubKey == null)
95  *              {
96  *                      goto CATCH;
97  *              }
98  *
99  *              pPriKey = pKeyPair->GetPrivateKey();
100  *              if (pPriKey == null)
101  *              {
102  *                      goto CATCH;
103  *              }
104  *
105  *              r = pSignature->SetPrivateKey(*pPriKey);
106  *              if (IsFailed(r))
107  *              {
108  *                      goto CATCH;
109  *              }
110  *
111  *              pOutput = pSignature->SignN(input);
112  *              if (pOutput == null)
113  *              {
114  *                      r = GetLastResult();
115  *                      goto CATCH;
116  *              }
117  *
118  *              r = pSignature->SetPublicKey(*pPubKey);
119  *              if (IsFailed(r))
120  *              {
121  *                      goto CATCH;
122  *              }
123  *
124  *              res = pSignature->Verify(input, *pOutput);
125  *              if (res == false)
126  *              {
127  *                      r = GetLastResult();
128  *                      goto CATCH;
129  *              }
130  *
131  *              r       = E_SUCCESS;
132  *
133  *  CATCH:
134  *              delete pSignature;
135  *              delete pOutput;
136  *              delete pKeyPairGen;
137  *              delete pKeyPair;
138  *   }
139  *
140  *  @endcode
141  */
142
143 class _OSP_EXPORT_ ISignature
144 {
145
146 public:
147         /**
148          * This polymorphic destructor should be overridden if required. This way, the destructors of the derived classes @n
149          * are called when the destructor of this interface is called.
150          *
151          *  @since              2.0
152          */
153         virtual ~ISignature(void) {}
154
155         /**
156          *      Sets an asymmetric private key for signature.
157          *
158          *      @since          2.0
159          *
160          *      @return         An error code
161          *      @param[in]      key                                                     An instance of IKey
162          *      @exception      E_SUCCESS                                       The method is successful.
163          *      @exception      E_INVALID_ARG                           The specified @c key is invalid.
164          *      @exception      E_OUT_OF_MEMORY                         The memory is insufficient.
165          */
166         virtual result SetPrivateKey(const Tizen::Security::IKey& key) = 0;
167
168         /**
169          *      Sets an asymmetric public key for verification.
170          *
171          *      @since          2.0
172          *
173          *      @return         An error code
174          *      @param[in]      key                                                     An instance of IKey
175          *      @exception      E_SUCCESS                                       The method is successful.
176          *      @exception      E_INVALID_ARG                           The specified @c key is invalid.
177          *      @exception      E_OUT_OF_MEMORY                         The memory is insufficient.
178          */
179         virtual result SetPublicKey(const Tizen::Security::IKey& key) = 0;
180
181         /**
182          *      Signs the data.
183          *
184          *      @since          2.0
185          *
186          *      @return         A pointer to the Tizen::Base::ByteBuffer instance that contains the output, @n
187          *                              else @c null if an error occurs
188          *      @param[in]      input                                           An instance of Tizen::Base::ByteBuffer
189          *      @exception      E_SUCCESS                                       The method is successful.
190          *      @exception      E_INVALID_ARG                           The input Tizen::Base::ByteBuffer is empty or contains invalid data.
191          *      @exception      E_OUT_OF_MEMORY                         The memory is insufficient.
192          *      @exception      E_KEY_NOT_FOUND                         The specified key is not found.
193          *      @exception      E_SYSTEM                                        A system error has occurred. @n
194          *                                                                                      The method has failed to operate with the openssl library, or
195          *                                                                                      the Tizen::Base::ByteBuffer operation has failed.
196          */
197         virtual Tizen::Base::ByteBuffer* SignN(const Tizen::Base::ByteBuffer& input) = 0;
198
199         /**
200          *      Verifies the data. @n
201          *      The verification is done by comparing the @c signedData to the signature created by the @c data.
202          *
203          *      @since          2.0
204          *
205          *      @return         @c true if the signed data is correct, @n
206          *                              else @c false
207          *      @param[in]      data                                            An instance of Tizen::Base::ByteBuffer that contains the original data
208          *      @param[in]      signedData                                      An instance of Tizen::Base::ByteBuffer that contains the signed data
209          *      @exception      E_SUCCESS                                       The method is successful.
210          *      @exception      E_INVALID_ARG                           The input Tizen::Base::ByteBuffer is empty or contains invalid data.
211          *      @exception      E_OUT_OF_MEMORY                         The memory is insufficient.
212          *      @exception      E_KEY_NOT_FOUND                         The specified key is not found.
213          *      @exception      E_SYSTEM                                        A system error has occurred. @n
214          *                                                                                      The method has failed to operate with the openssl library, or
215          *                                                                                      the Tizen::Base::ByteBuffer operation has failed.
216          */
217         virtual bool Verify(const Tizen::Base::ByteBuffer& data, const Tizen::Base::ByteBuffer& signedData) = 0;
218
219 protected:
220         //
221         //      This method is for internal use only. Using this method can cause behavioral, security-related,
222         //      and consistency-related issues in the application.
223         //
224         //      This method is reserved and may change its name at any time without prior notice.
225         //
226         //      @since 2.0
227         //
228         virtual void ISignature_Reserved1(void) {}
229
230 }; //ISignature
231
232 } } } //Tizen::Security::Crypto
233
234 #endif //_FSEC_CRYPTO_ISIGNATURE_H_