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. This way, the destructors of the derived classes @n
227 * 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 name of 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 a symmetric key for encryption or decryption.
254 * @return An error code
255 * @param[in] key An instance of ISecretKey
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
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 A system error has occurred. @n
290 * The method has failed to operate with the openssl library, or
291 * the Tizen::Base::ByteBuffer operation has failed.
293 virtual Tizen::Base::ByteBuffer* EncryptN(const Tizen::Base::ByteBuffer& input) = 0;
296 * Decrypts the data (single-part).
300 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
301 * else @c null if an error occurs
302 * @param[in] input An instance of Tizen::Base::ByteBuffer
303 * @exception E_SUCCESS The method is successful.
304 * @exception E_INVALID_ARG The input Tizen::Base::ByteBuffer is empty or contains invalid data.
305 * @exception E_OUT_OF_MEMORY The memory is insufficient.
306 * @exception E_KEY_NOT_FOUND The key is not found.
307 * @exception E_INVALID_OPERATION The specified cipher operation mode for this method is invalid.
308 * @exception E_OVERFLOW This operation has caused the memory to overflow.
309 * @exception E_SYSTEM A system error has occurred. @n
310 * The method has failed to operate with the openssl library, or
311 * the Tizen::Base::ByteBuffer operation has failed.
313 virtual Tizen::Base::ByteBuffer* DecryptN(const Tizen::Base::ByteBuffer& input) = 0;
316 * Initializes a multiple-part encryption or decryption operation.
320 * @return An error code
321 * @exception E_SUCCESS The method is successful.
322 * @exception E_OUT_OF_MEMORY The memory is insufficient.
323 * @exception E_KEY_NOT_FOUND The key is not found.
324 * @exception E_INVALID_OPERATION The specified cipher operation mode for this method is invalid.
325 * @exception E_SYSTEM A system error has occurred. @n
326 * The method has failed to operate with the openssl library.
328 virtual result Initialize(void) = 0;
331 * Updates a multiple-part encryption or decryption operation.
335 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
336 * else @c null if an error occurs
337 * @param[in] input An instance of Tizen::Base::ByteBuffer
338 * @exception E_SUCCESS The method is successful.
339 * @exception E_OUT_OF_MEMORY The memory is insufficient.
340 * @exception E_OVERFLOW This operation has caused the memory to overflow.
341 * @exception E_INVALID_ARG The input Tizen::Base::ByteBuffer is empty or contains invalid data.
342 * @exception E_SYSTEM A system error has occurred. @n
343 * The method has failed to operate with the openssl library, or
344 * the Tizen::Base::ByteBuffer operation has failed.
345 * @remarks The specific error code can be accessed using the GetLastResult() method.
347 virtual Tizen::Base::ByteBuffer* UpdateN(const Tizen::Base::ByteBuffer& input) = 0;
350 * Finalizes a multiple-part encryption or decryption operation.
354 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
355 * else @c null if an error occurs
356 * @exception E_SUCCESS The method is successful.
357 * @exception E_OUT_OF_MEMORY The memory is insufficient.
358 * @exception E_OVERFLOW This operation has caused the memory to overflow.
359 * @exception E_SYSTEM A system error has occurred. @n
360 * The method has failed to operate with the openssl library, or
361 * the Tizen::Base::ByteBuffer operation has failed.
362 * @remarks The specific error code can be accessed using the GetLastResult() method.
364 virtual Tizen::Base::ByteBuffer* FinalizeN(void) = 0;
367 * Wraps the specified key.
371 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
372 * else @c null if an error occurs
373 * @param[in] secretKey The secret key to wrap
374 * @exception E_SUCCESS The method is successful.
375 * @exception E_INVALID_ARG The input Tizen::Base::ByteBuffer is empty or contains invalid data.
376 * @exception E_OUT_OF_MEMORY The memory is insufficient.
377 * @exception E_KEY_NOT_FOUND The key is not found.
378 * @exception E_INVALID_OPERATION The specified cipher operation mode for this method is invalid.
379 * @exception E_SYSTEM A system error has occurred. @n
380 * The method has failed to operate with the openssl library, or
381 * the Tizen::Base::ByteBuffer operation has failed.
382 * @remarks The specific error code can be accessed using the GetLastResult() method.
383 * @remarks This operation is only supported in AesCipher.
385 virtual Tizen::Base::ByteBuffer* WrapN(const Tizen::Base::ByteBuffer& secretKey) = 0;
388 * Unwraps a previously wrapped key.
392 * @return A pointer to the Tizen::Base::ByteBuffer class that contains the output, @n
393 * else @c null if an error occurs
394 * @param[in] wrappedKey The wrapped key to unwrap
395 * @exception E_SUCCESS The method is successful.
396 * @exception E_INVALID_ARG The specified @c wrappedKey Tizen::Base::ByteBuffer is invalid.
397 * @exception E_OUT_OF_MEMORY The memory is insufficient.
398 * @exception E_KEY_NOT_FOUND The key is not found.
399 * @exception E_INVALID_OPERATION The specified cipher operation mode for this method is invalid.
400 * @exception E_SYSTEM A system error has occurred. @n
401 * The method has failed to operate with the openssl library, or
402 * the Tizen::Base::ByteBuffer operation has failed.
403 * @remarks The specific error code can be accessed using the GetLastResult() method.
404 * @remarks This operation is only supported in AesCipher.
406 virtual Tizen::Base::ByteBuffer* UnwrapN(const Tizen::Base::ByteBuffer& wrappedKey) = 0;
410 // This method is for internal use only. Using this method can cause behavioral, security-related,
411 // and consistency-related issues in the application.
413 // This method is reserved and may change its name at any time without prior notice.
417 virtual void ISymmetricCipher_Reserved1(void) {}
419 }; //ISymmetricCipher
421 } } } //Tizen::Security::Crypto
423 #endif //_FSEC_CRYPTO_ISYMMETRIC_CIPHER_H_