+
+/**
+ * Opens a file.
+ *
+ * @param path Path to a file.
+ * @param mode Mode.
+ * @return Stream handle.
+ * @throw ExtractFileFailed If error (other than EINTR) occurs.
+ */
+FILE* openFile(const std::string& path, const std::string& mode)
+{
+ FILE* result = NULL;
+
+ do
+ {
+ result = fopen(path.c_str(), mode.c_str());
+ } while ((NULL == result) && (EINTR == errno));
+
+ if (NULL == result)
+ {
+ ThrowMsg(Jobs::WidgetInstall::Exceptions::EncryptionFailed,
+ "Could not open file " << path);
+ }
+
+ return result;
+}
+
+/**
+ * Reads bytes from a stream.
+ *
+ * @param buffer Buffer to read the bytes into.
+ * @param count Number of bytes to read.
+ * @param stream Stream to read from.
+ * @return Number of bytes read
+ * @throw ExtractFileFailed If error (other than EINTR) occurs.
+ */
+std::size_t readBytes(unsigned char* buffer, std::size_t count, FILE* stream)
+{
+ std::size_t result = std::fread(buffer,
+ sizeof(unsigned char),
+ count,
+ stream);
+
+ if (result != count)
+ {
+ int error = errno;
+ if (0 != std::ferror(stream))
+ {
+ if (EINTR != error)
+ {
+ ThrowMsg(Jobs::WidgetInstall::Exceptions::ErrorExternalInstallingFailure,
+ "Error while reading data" <<
+ " [" << DPL::GetErrnoString(error) << "]");
+ }
+ }
+ }
+
+ return result;
+}
+
+/**
+ * Writes bytes to a stream.
+ *
+ * @param buffer Data to write.
+ * @param count Number of bytes.
+ * @param stream Stream to write to.
+ * @throw ExtractFileFailed If error (other than EINTR) occurs.
+ */
+void writeBytes(unsigned char* buffer, std::size_t count, FILE* stream)
+{
+ std::size_t bytesWritten = 0;
+ std::size_t bytesToWrite = 0;
+ do
+ {
+ bytesToWrite = count - bytesWritten;
+ bytesWritten = std::fwrite(buffer + bytesWritten,
+ sizeof(unsigned char),
+ count - bytesWritten,
+ stream);
+ if ((bytesWritten != bytesToWrite) && (EINTR != errno))
+ {
+ int error = errno;
+ ThrowMsg(Jobs::WidgetInstall::Exceptions::EncryptionFailed,
+ "Error while writing data" <<
+ " [" << DPL::GetErrnoString(error) << "]");
+ }
+ } while ((bytesWritten != bytesToWrite) && (EINTR == errno));
+}
+
+/*
+ * get encrypted string from trustzone
+*/
+Tizen::Base::ByteBuffer* EncryptChunkByTrustZone(
+ Tizen::Base::ByteBuffer* appInfo,
+ const unsigned char *plainBuffer,
+ int pBufSize)
+{
+ using namespace Tizen::Base;
+ if(!initWebApp)
+ {
+ char* pAppId = null;
+ pAppId = (char*)calloc(appInfo->GetRemaining()+1, 1);
+ memcpy(pAppId, appInfo->GetPointer(), appInfo->GetRemaining());
+ InitWebAppInfo(pAppId, "");
+ free (pAppId);
+ initWebApp = true;
+ }
+
+ Tizen::Security::Crypto::_TrustZoneService* pInstance;
+ pInstance = Tizen::Security::Crypto::_TrustZoneService::GetInstance();
+
+ ByteBuffer pBuf;
+ pBuf.Construct(pBufSize);
+ const byte *pByte = reinterpret_cast<const byte*>(plainBuffer);
+ if (pBuf.SetArray(pByte, 0, pBufSize) != E_SUCCESS) {
+ LogDebug("Couldnot set pBuf");
+ return NULL;
+ }
+ pBuf.Flip();
+
+ return pInstance->_TrustZoneService::EncryptN(*appInfo, pBuf);
+}
+
+Tizen::Base::ByteBuffer* TEST_dec(
+ Tizen::Base::ByteBuffer* appInfo,
+ const unsigned char *plainBuffer,
+ int pBufSize)
+{
+ using namespace Tizen::Base;
+
+ Tizen::Security::Crypto::_TrustZoneService* pInstance;
+ pInstance = Tizen::Security::Crypto::_TrustZoneService::GetInstance();
+
+ ByteBuffer pBuf;
+ pBuf.Construct(pBufSize);
+ const byte *pByte = reinterpret_cast<const byte*>(plainBuffer);
+ if (pBuf.SetArray(pByte, 0, pBufSize) != E_SUCCESS) {
+ LogDebug("Couldnot set pBuf");
+ return NULL;
+ }
+ pBuf.Flip();
+
+ return pInstance->_TrustZoneService::DecryptN(*appInfo, pBuf);
+}