2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FSecCryptoISymmetricCipher.h
19 * @brief This is the header file for the %ISymmetricCipher interface.
21 * This header file contains the declarations of the %ISymmetricCipher interface.
23 #ifndef _FSEC_CRYPTO_ISYMMETRIC_CIPHER_H_
24 #define _FSEC_CRYPTO_ISYMMETRIC_CIPHER_H_
26 #include <FBaseString.h>
27 #include <FBaseByteBuffer.h>
28 #include <FSecISecretKey.h>
29 #include <FSecCryptoTypes.h>
32 namespace Tizen { namespace Security { namespace Crypto
35 * @interface ISymmetricCipher
36 * @brief This interface provides the functionalities of a symmetric cryptographic cipher for encryption and decryption.
40 * The %ISymmetricCipher interface provides the functionalities of a symmetric cryptographic cipher for encryption and decryption. @n
42 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/security/ciphers.htm">Ciphers</a>. @n
44 * The following example demonstrates how to use the %ISymmetricCipher interface.
48 * MyClass::TestSymmetricCipherSample(void)
50 * // #####AES CBC Test Vector(128-bits key)#####
51 * // PlainText: 48 Bytes
52 * const int messageLen = 48;
53 * static const byte message[messageLen] = {
54 * 0x87, 0x3C, 0x66, 0x1D, 0x2C, 0x0D, 0x49, 0x2D,
55 * 0x6C, 0x76, 0xBE, 0x44, 0x57, 0x39, 0xB8, 0x28,
56 * 0x84, 0x5E, 0x2A, 0x15, 0x18, 0x3B, 0x1D, 0x00,
57 * 0xA7, 0x6E, 0x80, 0x4D, 0x22, 0xF1, 0x2A, 0x6B,
58 * 0xBA, 0xFE, 0xA8, 0x02, 0x2B, 0xC2, 0x97, 0x01,
59 * 0x59, 0x0F, 0x3C, 0x2A, 0x67, 0x8B, 0x98, 0x69
62 * // CipherText: 48 Bytes
63 * const int cipherLen = 48;
64 * static const byte cipher[cipherLen] = {
65 * 0x10, 0x84, 0x9D, 0x24, 0xEB, 0x22, 0xE0, 0x7F,
66 * 0xA8, 0x57, 0xE9, 0xA0, 0x4F, 0xE2, 0x3D, 0xE5,
67 * 0xC1, 0x51, 0x7E, 0xEB, 0xF8, 0xB3, 0x3A, 0xA2,
68 * 0xDC, 0xF4, 0x8B, 0xDC, 0x14, 0x0A, 0xC7, 0x58,
69 * 0x85, 0x6B, 0x0D, 0xE9, 0x30, 0x8B, 0xA1, 0x71,
70 * 0xD5, 0x0B, 0x14, 0x97, 0xEF, 0xAD, 0x22, 0x8D
74 * const int secretKeyLen = 16;
75 * static const byte secretKey[secretKeyLen] = {
76 * 0x62, 0x5C, 0xC7, 0x7E, 0xEA, 0x7B, 0xA5, 0x4D,
77 * 0x47, 0xCE, 0xAF, 0x26, 0x9E, 0xB1, 0x6C, 0x2D
81 * const int ivectorLen = 16;
82 * static const byte ivector[ivectorLen] = {
83 * 0x3E, 0xB5, 0x01, 0x45, 0xE4, 0xF8, 0x75, 0x3F,
84 * 0x08, 0x9D, 0x9F, 0x57, 0x3B, 0x63, 0xEF, 0x4B
87 * result r = E_FAILURE;
88 * String transformation;
89 * ISymmetricCipher *pCipherEnc = null;
90 * ISymmetricCipher *pCipherDec = null;
91 * SecretKeyGenerator *pKeyGen = null;
92 * ISecretKey *pKey = null;
94 * ByteBuffer *pOutput = null;
95 * ByteBuffer *pOutput2 = null;
96 * ByteBuffer keyBytes;
100 * input.Construct(messageLen);
101 * input.SetArray(message, 0, messageLen);
105 * keyBytes.Construct(secretKeyLen);
106 * keyBytes.SetArray(secretKey, 0, secretKeyLen);
110 * iv.Construct(ivectorLen);
111 * iv.SetArray(ivector, 0, ivectorLen);
114 * pCipherEnc = new AesCipher();
115 * if (pCipherEnc == null)
120 * transformation = "CBC/128/NOPADDING";
122 * r = pCipherEnc->Construct(transformation, CIPHER_ENCRYPT);
128 * // Generates the key.
129 * pKeyGen = new SecretKeyGenerator();
130 * if (pKeyGen == null)
135 * r = pKeyGen->Construct(keyBytes);
141 * pKey = pKeyGen->GenerateKeyN();
147 * r = pCipherEnc->SetKey(*pKey);
153 * r = pCipherEnc->SetInitialVector(iv);
159 * pOutput = pCipherEnc->EncryptN(input);
160 * if (pOutput == null)
162 * r = GetLastResult();
166 * pCipherDec = new AesCipher();
167 * if (pCipherDec == null)
172 * r = pCipherDec->Construct(transformation, CIPHER_DECRYPT);
178 * r = pCipherDec->SetKey(*pKey);
184 * r = pCipherDec->SetInitialVector(iv);
190 * pOutput2 = pCipherDec->DecryptN(*pOutput);
191 * if (pOutput2 == null)
193 * r = GetLastResult();
197 * if (memcmp(pOutput->GetPointer(), cipher, cipherLen) != 0)
202 * if (memcmp(pOutput2->GetPointer(), input.GetPointer(), input.GetRemaining()) != 0)
221 class _OSP_EXPORT_ ISymmetricCipher
226 * This polymorphic destructor should be overridden if required. @n
227 * This way, the destructors of the derived classes are called when the destructor of this interface is called.
231 virtual ~ISymmetricCipher(void) {}
234 * Initializes this instance of %ISymmetricCipher with the specified operation mode and padding scheme.
238 * @return An error code
239 * @param[in] transformation The requested mode or key bit or padding scheme
240 * @param[in] opMode The cipher operation mode @n
241 * For example, @c CIPHER_ENCRYPT, @c CIPHER_DECRYPT, @c CIPHER_WRAP, or @c CIPHER_UNWRAP.
242 * @exception E_SUCCESS The method is successful.
243 * @exception E_OUT_OF_MEMORY The memory is insufficient.
244 * @exception E_INVALID_ARG The specified @c opMode does not contain a valid value for the cipher operation.
245 * @exception E_UNSUPPORTED_ALGORITHM The algorithm is not supported.
247 virtual result Construct(const Tizen::Base::String& transformation, enum CipherOperation opMode = Tizen::Security::Crypto::CIPHER_ENCRYPT) = 0;
250 * Sets the symmetric key for encryption or decryption.
254 * @return An error code
255 * @param[in] key An instance of ISecretKey to set
256 * @exception E_SUCCESS The method is successful.
257 * @exception E_INVALID_ARG The specified @c key is invalid.
258 * @exception E_OUT_OF_MEMORY The memory is insufficient.
260 virtual result SetKey(const Tizen::Security::ISecretKey& key) = 0;
263 * Sets the initial vector.
267 * @return An error code
268 * @param[in] initialVector The initial vector to set
269 * @exception E_SUCCESS The method is successful.
270 * @exception E_INVALID_ARG The specified input parameter is invalid.
271 * @exception E_OUT_OF_MEMORY The memory is insufficient.
273 virtual result SetInitialVector(const Tizen::Base::ByteBuffer& initialVector) = 0;
276 * Encrypts the data (single-part).
280 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
281 * else @c null if an error occurs
282 * @param[in] input An instance of Tizen::Base::ByteBuffer
283 * @exception E_SUCCESS The method is successful.
284 * @exception E_INVALID_ARG The input Tizen::Base::ByteBuffer is empty or contains invalid data.
285 * @exception E_OUT_OF_MEMORY The memory is insufficient.
286 * @exception E_KEY_NOT_FOUND The key is not found.
287 * @exception E_INVALID_OPERATION The specified cipher operation mode for this method is invalid.
288 * @exception E_OVERFLOW This operation has caused the memory to overflow.
289 * @exception E_SYSTEM Either of the following conditions has occurred:
290 * - A system error has occurred.
291 * - The method has failed to operate with the openssl library.
292 * - The Tizen::Base::ByteBuffer operation has failed.
294 virtual Tizen::Base::ByteBuffer* EncryptN(const Tizen::Base::ByteBuffer& input) = 0;
297 * Decrypts the data (single-part).
301 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
302 * else @c null if an error occurs
303 * @param[in] input An instance of Tizen::Base::ByteBuffer
304 * @exception E_SUCCESS The method is successful.
305 * @exception E_INVALID_ARG The input Tizen::Base::ByteBuffer is empty or contains invalid data.
306 * @exception E_OUT_OF_MEMORY The memory is insufficient.
307 * @exception E_KEY_NOT_FOUND The key is not found.
308 * @exception E_INVALID_OPERATION The specified cipher operation mode for this method is invalid.
309 * @exception E_OVERFLOW This operation has caused the memory to overflow.
310 * @exception E_SYSTEM Either of the following conditions has occurred:
311 * - A system error has occurred.
312 * - The method has failed to operate with the openssl library.
313 * - The Tizen::Base::ByteBuffer operation has failed.
315 virtual Tizen::Base::ByteBuffer* DecryptN(const Tizen::Base::ByteBuffer& input) = 0;
318 * Initializes the instance of %ISymmetricCipher for the multiple-part encryption or decryption operation.
323 * @return An error code
324 * @exception E_SUCCESS The method is successful.
325 * @exception E_OUT_OF_MEMORY The memory is insufficient.
326 * @exception E_KEY_NOT_FOUND The key is not found.
327 * @exception E_INVALID_OPERATION The specified cipher operation mode for this method is invalid.
328 * @exception E_SYSTEM Either of the following conditions has occurred:
329 * - A system error has occurred.
330 * - The method has failed to operate with the openssl library.
332 virtual result Initialize(void) = 0;
335 * Updates the instance of %ISymmetricCipher for the multiple-part encryption or decryption operation.
339 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
340 * else @c null if an error occurs
341 * @param[in] input An instance of Tizen::Base::ByteBuffer
342 * @exception E_SUCCESS The method is successful.
343 * @exception E_OUT_OF_MEMORY The memory is insufficient.
344 * @exception E_OVERFLOW This operation has caused the memory to overflow.
345 * @exception E_INVALID_ARG The input Tizen::Base::ByteBuffer is empty or contains invalid data.
346 * @exception E_SYSTEM Either of the following conditions has occurred:
347 * - A system error has occurred.
348 * - The method has failed to operate with the openssl library.
349 * - The Tizen::Base::ByteBuffer operation has failed.
350 * @remarks The specific error code can be accessed using the GetLastResult() method.
352 virtual Tizen::Base::ByteBuffer* UpdateN(const Tizen::Base::ByteBuffer& input) = 0;
355 * Finalizes the instance of %ISymmetricCipher for the multiple-part encryption or decryption operation.
359 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
360 * else @c null if an error occurs
361 * @exception E_SUCCESS The method is successful.
362 * @exception E_OUT_OF_MEMORY The memory is insufficient.
363 * @exception E_OVERFLOW This operation has caused the memory to overflow.
364 * @exception E_SYSTEM Either of the following conditions has occurred:
365 * - A system error has occurred.
366 * - The method has failed to operate with the openssl library.
367 * - The Tizen::Base::ByteBuffer operation has failed.
368 * @remarks The specific error code can be accessed using the GetLastResult() method.
370 virtual Tizen::Base::ByteBuffer* FinalizeN(void) = 0;
377 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
378 * else @c null if an error occurs
379 * @param[in] secretKey The secret key to wrap
380 * @exception E_SUCCESS The method is successful.
381 * @exception E_INVALID_ARG The input Tizen::Base::ByteBuffer is empty or contains invalid data.
382 * @exception E_OUT_OF_MEMORY The memory is insufficient.
383 * @exception E_KEY_NOT_FOUND The key is not found.
384 * @exception E_INVALID_OPERATION The specified cipher operation mode for this method is invalid.
385 * @exception E_SYSTEM Either of the following conditions has occurred:
386 * - A system error has occurred.
387 * - The method has failed to operate with the openssl library.
388 * - The Tizen::Base::ByteBuffer operation has failed.
390 * - The specific error code can be accessed using the GetLastResult() method.
391 * - This operation is only supported in AesCipher.
393 virtual Tizen::Base::ByteBuffer* WrapN(const Tizen::Base::ByteBuffer& secretKey) = 0;
396 * Unwraps a previously wrapped key.
400 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
401 * else @c null if an error occurs
402 * @param[in] wrappedKey The wrapped key to unwrap
403 * @exception E_SUCCESS The method is successful.
404 * @exception E_INVALID_ARG The specified @c wrappedKey Tizen::Base::ByteBuffer is invalid.
405 * @exception E_OUT_OF_MEMORY The memory is insufficient.
406 * @exception E_KEY_NOT_FOUND The key is not found.
407 * @exception E_INVALID_OPERATION The specified cipher operation mode for this method is invalid.
408 * @exception E_SYSTEM Either of the following conditions has occurred:
409 * - A system error has occurred.
410 * - The method has failed to operate with the openssl library.
411 * - The Tizen::Base::ByteBuffer operation has failed.
413 * - The specific error code can be accessed using the GetLastResult() method.
414 * - This operation is only supported in AesCipher.
416 virtual Tizen::Base::ByteBuffer* UnwrapN(const Tizen::Base::ByteBuffer& wrappedKey) = 0;
420 // This method is for internal use only. Using this method can cause behavioral, security-related,
421 // and consistency-related issues in the application.
423 // This method is reserved and may change its name at any time without prior notice.
427 virtual void ISymmetricCipher_Reserved1(void) {}
429 }; //ISymmetricCipher
431 } } } //Tizen::Security::Crypto
433 #endif //_FSEC_CRYPTO_ISYMMETRIC_CIPHER_H_