2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FIo_SecureIoUtil.cpp
20 * @brief This is the implementation file for the %_SecureIoUilt class.
26 #include <sys/types.h>
33 #include <unique_ptr.h>
35 #include <FBaseString.h>
37 #include <FBaseResult.h>
38 #include <FSecSecretKey.h>
39 #include <FSecSecretKeyGenerator.h>
40 #include <FSecCryptoSha1Hash.h>
41 #include <FSecCryptoAesCipher.h>
43 #include <FBase_NativeError.h>
44 #include <FBaseSysLog.h>
45 #include <FSec_DeviceKeyGenerator.h>
46 #include <FBase_StringConverter.h>
47 #include <FIo_SecureIoUtil.h>
48 #include <FIo_FileUtil.h>
49 #include <FApp_AppInfo.h>
52 using namespace Tizen::Base;
53 using namespace Tizen::Security;
54 using namespace Tizen::Security::Crypto;
55 using namespace Tizen::App;
57 namespace Tizen { namespace Io
60 static const int SECURE_IO_4_BITS = 4;
61 static const int SECURE_IO_8_BITS = 8;
62 static const int SECURE_IO_3_BYTES = 3;
63 static const int SECURE_IO_4_BYTES = 4;
64 static const int SECURE_IO_5_BYTES = 5;
65 static const int SECURE_IO_8_BYTES = 8;
66 static const int SECURE_IO_9_BYTES = 9;
67 static const int SECURE_IO_HEX_0F = 0x0f;
68 static const int SECURE_IO_HEX_FC = 0xfc;
69 static const int SECURE_IO_HEX_FD = 0xfd;
70 static const int SECURE_IO_HEX_FE = 0xfe;
71 static const int SECURE_IO_HEX_FF = 0xff;
72 static const int DATABASE_KEY_LENGTH = 16;
73 static const int AES_KEY_LENGTH = 16;
75 const char SECURE_FILE_HEADER_STRING[] = {0x74, 0x69, 0x7a, 0x65, 0x6e, 0x20, 0x73, 0x65, 0x63, 0x75, 0x72, 0x65, 0x66, 0x69, 0x6c, 0x65};
76 const char SECURE_REG_HEADER_STRING[] = {0x74, 0x69, 0x7a, 0x65, 0x6e, 0x20, 0x73, 0x65, 0x63, 0x75, 0x72, 0x65, 0x72, 0x65, 0x67, 0x69, 0x73, 0x74, 0x72, 0x79};
77 static const wchar_t* _CIPHER_INFORMATION = L"CBC/128/NOPADDING";
80 _SecureIoUtil::GetSecureKeyN(const ByteBuffer* pUserKey)
84 unique_ptr< Tizen::Base::ByteBuffer > pKey(new (std::nothrow) ByteBuffer());
85 SysTryReturn(NID_IO, pKey != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
87 r = pKey->Construct(AES_KEY_LENGTH);
88 SysTryReturn(NID_IO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
90 if (pUserKey->GetRemaining() >= AES_KEY_LENGTH)
92 r = pKey->SetArray(pUserKey->GetPointer(), 0, AES_KEY_LENGTH);
93 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
99 int tempLength = pUserKey->GetRemaining();
100 int loopCount = AES_KEY_LENGTH / tempLength;
102 for(int i = 0; i < loopCount; i++)
105 r = pKey->SetArray(pUserKey->GetPointer(), 0, tempLength);
106 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
110 if (AES_KEY_LENGTH % tempLength)
112 r = pKey->SetArray(pUserKey->GetPointer(), 0, (AES_KEY_LENGTH % tempLength));
113 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
123 return pKey.release();
127 _SecureIoUtil::DoCipherAesN(const String transformation, const ByteBuffer& input, const ByteBuffer& key, CipherOperation encMode)
129 result r = E_SUCCESS;
132 SecretKeyGenerator secKeyGenerator;
134 static byte ivArray[] = {0x3E, 0xB5, 0x01, 0x45, 0xE4, 0xF8, 0x75, 0x3F, 0x08, 0x9D, 0x9F, 0x57, 0x3B, 0x63, 0xEF, 0x4B };
136 r = aesCipher.Construct(transformation, encMode);
137 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
139 r = ivBuffer.Construct(sizeof(ivArray));
140 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
142 r = ivBuffer.SetArray(ivArray, 0, sizeof(ivArray));
143 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
147 r = aesCipher.SetInitialVector(ivBuffer);
148 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
150 r = secKeyGenerator.Construct(key);
151 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
153 unique_ptr< ISecretKey > pKey(reinterpret_cast< ISecretKey* >(secKeyGenerator.GenerateKeyN()));
154 SysTryReturn(NID_IO, pKey != null, null, GetLastResult(), "[%s] Failed to generate key.", GetErrorMessage(GetLastResult()));
156 r = aesCipher.SetKey(*pKey.get());
157 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Failed to set secret key.", GetErrorMessage(r));
159 unique_ptr< ByteBuffer > pOutBuf(null);
161 if (encMode == CIPHER_ENCRYPT)
163 pOutBuf = unique_ptr< ByteBuffer >(aesCipher.EncryptN(input));
167 pOutBuf = unique_ptr< ByteBuffer >(aesCipher.DecryptN(input));
170 SysTryReturn(NID_IO, pOutBuf != null, null, GetLastResult(), "[%s] Failed to decrypt data.", GetErrorMessage(GetLastResult()));
172 SetLastResult(E_SUCCESS);
173 return pOutBuf.release();
177 _SecureIoUtil::IsEndOfFile(_NormalFile* pNormalFile)
179 result r = E_SUCCESS;
182 int current = pNormalFile->Tell();
183 SysTryReturn(NID_IO, current != -1, false, GetLastResult(), "[%s] Failed to tell in file.", GetErrorMessage(GetLastResult()));
185 int ret = pNormalFile->Read(&flag, 1); //ignore ret as we are only looking for EOF.
186 if (GetLastResult() == E_END_OF_FILE && ret == 0)
191 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, current);
192 SysTryReturn(NID_IO, !IsFailed(r), false, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
198 _SecureIoUtil::InsertSecureFileHeader(_NormalFile* pNormalFile)
200 result r = E_SUCCESS;
202 byte secureHeader[SECURE_FILE_HEADER_SIZE_V1];
203 byte reservedValue[SECURE_IO_STATIC_BIN_LEN] = {0xCA, 0xFE, 0xBE, 0xBE, 0xDA, 0xEF, 0xEB, 0xEB};
206 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, 0);
207 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
209 ret = pNormalFile->Read(secureHeader, SECURE_FILE_HEADER_SIZE_V1);
210 if (ret < SECURE_FILE_HEADER_SIZE_V1)
214 if (ret == 0 && IsEndOfFile(pNormalFile)) //is true
216 memcpy(secureHeader, SECURE_FILE_HEADER_STRING, SECURE_FILE_HEADER_STRING_SIZE);
217 memcpy(secureHeader + SECURE_FILE_HEADER_STRING_SIZE, reservedValue, SECURE_IO_STATIC_BIN_LEN);
218 secureHeader[SECURE_FILE_HEADER_STRING_SIZE + SECURE_IO_STATIC_BIN_LEN] = SECURE_IO_2_BYTES;
219 secureHeader[SECURE_FILE_HEADER_STRING_SIZE + SECURE_IO_STATIC_BIN_LEN + SECURE_IO_FLAG_SIZE] = 0;
220 memset(secureHeader + SECURE_FILE_HEADER_STRING_SIZE + SECURE_IO_STATIC_BIN_LEN + SECURE_IO_FLAG_SIZE + SECURE_IO_INDEX_SIZE, 0, SECURE_IO_LOF_SIZE);
221 memset(secureHeader + SECURE_FILE_HEADER_STRING_SIZE + SECURE_IO_STATIC_BIN_LEN + SECURE_IO_FLAG_SIZE + SECURE_IO_INDEX_SIZE + SECURE_IO_LOF_SIZE, 0, SECURE_IO_LOF_SIZE);
223 r = pNormalFile->Write(secureHeader, SECURE_FILE_HEADER_SIZE_V1);
224 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] [%s] Failed to write in file.", GetErrorMessage(r));
229 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
231 } //Else it means that header is already written
237 _SecureIoUtil::SaveLengthOfFile(_NormalFile* pNormalFile)
239 result r = E_SUCCESS;
241 byte lengthOfFile[SECURE_IO_LOF_SIZE] = {0, 0, 0, 0};
244 r = pNormalFile->Seek(FILESEEKPOSITION_END, 0);
245 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
247 length = pNormalFile->Tell();
248 SysTryReturn(NID_IO, length != -1, GetLastResult(), GetLastResult(), "[%s] Failed to tell in file.", GetErrorMessage(GetLastResult()));
250 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, SECURE_FILE_HEADER_STRING_SIZE + SECURE_IO_10_BYTES);
251 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
253 if (sizeof(length) != SECURE_IO_LOF_SIZE)
255 memcpy(lengthOfFile + SECURE_IO_LOF_SIZE - sizeof(length), &length, sizeof(length));
259 memcpy(lengthOfFile, &length, SECURE_IO_LOF_SIZE);
262 r = pNormalFile->Write(lengthOfFile, SECURE_IO_LOF_SIZE);
263 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to write in file.", GetErrorMessage(r));
269 _SecureIoUtil::SelectCipherBlock(_NormalFile* pNormalFile, long virtualFilePointer, int* pPosition)
271 result r = E_SUCCESS;
275 long blockDataSize = 0;
276 byte secureHeader[SECURE_FILE_HEADER_SIZE_V1] = {0, };
277 byte blockHeader[CIPHER_BLOCK_HEADER_SIZE] = {0, 0, 0};
278 byte* pBlockDataCur = null;
281 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, 0);
284 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
286 ret = pNormalFile->Read(secureHeader, SECURE_FILE_HEADER_SIZE_V1);
287 if (ret < SECURE_FILE_HEADER_SIZE_V1)
291 if (ret == 0 && IsEndOfFile(pNormalFile))
296 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
300 //read 1byte to check padding size
303 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, count * (CIPHER_BLOCK_SIZE + CIPHER_BLOCK_HEADER_SIZE) + SECURE_FILE_HEADER_SIZE_V1);
304 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
306 ret = pNormalFile->Read(blockHeader, CIPHER_BLOCK_HEADER_SIZE);
307 if (ret < CIPHER_BLOCK_HEADER_SIZE) //less data read
311 if (ret == 0 && IsEndOfFile(pNormalFile))
316 SysLog(NID_IO, "[%s] Failed to read data from file", GetErrorMessage(r));
322 pBlockDataCur = (byte*) (&blockDataSize);
323 *pBlockDataCur = blockHeader[SECURE_IO_2_BYTES];
324 *(pBlockDataCur + 1) = blockHeader[1];
325 *(pBlockDataCur + SECURE_IO_2_BYTES) = blockHeader[0];
327 if (virtualFilePointer <= dataSize + blockDataSize)
329 *pPosition = virtualFilePointer - dataSize;
330 if (*pPosition == CIPHER_BLOCK_SIZE)
332 r = pNormalFile->Seek(FILESEEKPOSITION_CURRENT, CIPHER_BLOCK_SIZE);
337 r = pNormalFile->Seek(FILESEEKPOSITION_CURRENT, -CIPHER_BLOCK_HEADER_SIZE);
341 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
346 dataSize = dataSize + blockDataSize;
356 _SecureIoUtil::DetectNewLine(byte* pData, int readSize, int preReadSize, int* pTerminationCur, int* newLineSize)
362 for (count = preReadSize; count < readSize; count++)
364 if (pData[count] == '\r' && pData[count + 1] == '\n')
366 *pTerminationCur = count;
367 *newLineSize = SECURE_IO_2_BYTES;
371 if (pData[count] == '\n')
373 *pTerminationCur = count;
383 _SecureIoUtil::MakeCipherBlock(byte* pData, int dataSize, byte** ppEncryptedData, int* pEncryptedDataSize, const ByteBuffer& key)
385 result r = E_SUCCESS;
387 int lastDataSize = 0;
390 byte* pTempRealloc = null;
391 byte* pTempEncryptedData = null;
394 SysTryReturnResult(NID_SEC, dataSize > 0, E_INVALID_ARG, "Input data is not valid.");
397 *pEncryptedDataSize = 0;
399 blockCount = dataSize / CIPHER_BLOCK_SIZE;
400 lastDataSize = dataSize % CIPHER_BLOCK_SIZE;
401 if (lastDataSize != 0)
403 blockCount = blockCount + 1;
407 lastDataSize = CIPHER_BLOCK_SIZE;
410 for (int i = 0; i < blockCount; i++)
412 if (i == blockCount - 1)
414 tempSize = lastDataSize;
418 tempSize = CIPHER_BLOCK_SIZE;
421 inputSize = tempSize;
423 if (tempSize % ONE_BLOCK_SIZE != 0)
425 tempSize = (tempSize / ONE_BLOCK_SIZE) * ONE_BLOCK_SIZE + ONE_BLOCK_SIZE;
428 r = input.Construct(tempSize);
429 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
431 r = input.SetArray((byte*) (pData + i * CIPHER_BLOCK_SIZE), 0, inputSize);
432 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
434 if (inputSize < tempSize)
436 unique_ptr< char[] > pFill(new (std::nothrow) char[tempSize - inputSize]);
437 SysTryCatch(NID_IO, pFill != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
439 memset(pFill.get(), 0, tempSize - inputSize);
441 r = input.SetArray(reinterpret_cast< byte* >(pFill.get()), 0, tempSize - inputSize);
442 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
448 unique_ptr< ByteBuffer > pEncryptedBuffer(DoCipherAesN(_CIPHER_INFORMATION, input, key, CIPHER_ENCRYPT));
449 SysTryCatch(NID_IO, pEncryptedBuffer != null, r = E_IO, E_IO, "[E_IO] Failed to encrypt data.");
451 pTempEncryptedData = const_cast< byte* >(pEncryptedBuffer->GetPointer());
452 SysTryCatch(NID_IO, pTempEncryptedData != null, , r, "[%s] Failed to get encrypted data.", GetErrorMessage(r));
454 pTempRealloc = (byte*) new (std::nothrow) byte[*pEncryptedDataSize + tempSize + CIPHER_BLOCK_HEADER_SIZE];
455 SysTryCatch(NID_IO, pTempRealloc != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
457 if (*ppEncryptedData)
459 memcpy(pTempRealloc, *ppEncryptedData, *pEncryptedDataSize);
461 delete[]* ppEncryptedData;
462 *ppEncryptedData = null;
465 *ppEncryptedData = pTempRealloc;
467 if (i == blockCount - 1)
470 *(*ppEncryptedData + i * (CIPHER_BLOCK_SIZE + CIPHER_BLOCK_HEADER_SIZE) + SECURE_IO_2_BYTES) \
471 = (byte) (lastDataSize & SECURE_IO_HEX_FF);
472 *(*ppEncryptedData + i * (CIPHER_BLOCK_SIZE + CIPHER_BLOCK_HEADER_SIZE) + 1) \
473 = (byte) ((lastDataSize >> SECURE_IO_8_BITS) & SECURE_IO_HEX_FF);
474 *(*ppEncryptedData + i * (CIPHER_BLOCK_SIZE + CIPHER_BLOCK_HEADER_SIZE)) \
475 = (byte) ((lastDataSize >> (SECURE_IO_8_BITS * SECURE_IO_2_BYTES)) & SECURE_IO_HEX_FF);
479 *(*ppEncryptedData + i * (CIPHER_BLOCK_SIZE + CIPHER_BLOCK_HEADER_SIZE) + SECURE_IO_2_BYTES) \
480 = (byte) (CIPHER_BLOCK_SIZE & SECURE_IO_HEX_FF);
481 *(*ppEncryptedData + i * (CIPHER_BLOCK_SIZE + CIPHER_BLOCK_HEADER_SIZE) + 1) \
482 = (byte) ((CIPHER_BLOCK_SIZE >> SECURE_IO_8_BITS) & SECURE_IO_HEX_FF);
483 *(*ppEncryptedData + i * (CIPHER_BLOCK_SIZE + CIPHER_BLOCK_HEADER_SIZE)) \
484 = (byte) ((CIPHER_BLOCK_SIZE >> (SECURE_IO_8_BITS * SECURE_IO_2_BYTES)) & SECURE_IO_HEX_FF);
487 memcpy(*ppEncryptedData + i * (CIPHER_BLOCK_SIZE + CIPHER_BLOCK_HEADER_SIZE) + CIPHER_BLOCK_HEADER_SIZE, pTempEncryptedData, tempSize);
488 *pEncryptedDataSize = *pEncryptedDataSize + tempSize + CIPHER_BLOCK_HEADER_SIZE;
495 *pEncryptedDataSize = 0;
496 delete[]* ppEncryptedData;
497 *ppEncryptedData = null;
504 _SecureIoUtil::WriteDataInCipherBlock(_NormalFile* pNormalFile, int dataPosition, bool eofSet, int dataSize, byte* pData, const ByteBuffer& key)
506 result r = E_SUCCESS;
507 int availableSize = 0;
508 int encryptedDataSize = 0;
510 long blockDataSize = 0;
511 long tempBlockDataSize = 0;
512 bool isFirstBlockSet = false;
513 byte* pBlockDataCur = null;
514 byte* pCipherBlockData = null;
515 byte* pDataPos = null;
516 byte blockHeader[CIPHER_BLOCK_HEADER_SIZE] = {0, 0, 0};
524 if (eofSet != false && dataPosition == 0)
526 r = MakeCipherBlock(pDataPos, dataSize, &pCipherBlockData, &encryptedDataSize, key);
527 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
529 unique_ptr< byte[] > pEncryptedBlockData(pCipherBlockData);
530 pCipherBlockData = null;
532 SysTryReturn(NID_IO, pEncryptedBlockData != null, r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
534 r = pNormalFile->Write(pEncryptedBlockData.get(), encryptedDataSize);
535 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
540 ret = pNormalFile->Read(blockHeader, CIPHER_BLOCK_HEADER_SIZE);
541 if (ret < CIPHER_BLOCK_HEADER_SIZE)
545 if (ret == 0 && IsEndOfFile(pNormalFile))
550 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
555 pBlockDataCur = (byte*) (&blockDataSize);
556 *pBlockDataCur = blockHeader[SECURE_IO_2_BYTES];
557 *(pBlockDataCur + 1) = blockHeader[1];
558 *(pBlockDataCur + SECURE_IO_2_BYTES) = blockHeader[0];
560 unique_ptr< byte[] > pTempEncryptedData(new (std::nothrow) byte[CIPHER_BLOCK_SIZE]);
561 SysTryReturnResult(NID_IO, pTempEncryptedData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
563 memset(pTempEncryptedData.get(), 0, CIPHER_BLOCK_SIZE);
565 if (blockDataSize % ONE_BLOCK_SIZE != 0)
567 tempBlockDataSize = blockDataSize + ONE_BLOCK_SIZE - blockDataSize % ONE_BLOCK_SIZE;
571 tempBlockDataSize = blockDataSize;
574 ret = pNormalFile->Read(pTempEncryptedData.get(), tempBlockDataSize);
575 if (ret < tempBlockDataSize)
579 if (ret == 0 && IsEndOfFile(pNormalFile))
584 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
588 r = input.Construct(tempBlockDataSize);
589 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
591 r = input.SetArray(pTempEncryptedData.get(), 0, tempBlockDataSize);
593 pTempEncryptedData.reset(null);
595 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
599 unique_ptr< ByteBuffer > pDecryptedBuffer(DoCipherAesN(_CIPHER_INFORMATION, input, key, CIPHER_DECRYPT));
600 SysTryReturn(NID_IO, pDecryptedBuffer != null, E_IO, E_IO, "[E_IO] Failed to encrypt data.");
602 unique_ptr< byte[] > pDecryptedBlockData(new (std::nothrow) byte[tempBlockDataSize]);
603 SysTryReturnResult(NID_IO, pDecryptedBlockData != null, E_OUT_OF_MEMORY, "The memory is insufficient");
605 r = pDecryptedBuffer->GetArray(reinterpret_cast< byte* >(pDecryptedBlockData.get()), 0, tempBlockDataSize);
606 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to get decrypted buffer.", GetErrorMessage(r));
608 pDecryptedBuffer->Flip();
610 pDecryptedBuffer.reset(null);
612 if (isFirstBlockSet == false)
614 availableSize = CIPHER_BLOCK_SIZE - dataPosition;
615 if (availableSize >= dataSize)
617 if (tempBlockDataSize < dataPosition + dataSize)
619 unique_ptr< byte[] > pTempRealloc(new (std::nothrow) byte[dataPosition + dataSize]);
620 SysTryReturnResult(NID_IO, pTempRealloc != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
622 memcpy(pTempRealloc.get(), pDecryptedBlockData.get(), tempBlockDataSize);
624 pDecryptedBlockData.reset(null);
625 pDecryptedBlockData = move(pTempRealloc);
628 memcpy(reinterpret_cast< byte* >(pDecryptedBlockData.get()) + dataPosition, pDataPos, dataSize);
630 if (blockDataSize < dataPosition + dataSize)
632 r = MakeCipherBlock(pDecryptedBlockData.get(), dataPosition + dataSize, &pCipherBlockData, &encryptedDataSize, key);
636 r = MakeCipherBlock(pDecryptedBlockData.get(), blockDataSize, &pCipherBlockData, &encryptedDataSize, key);
639 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
641 unique_ptr< byte[] > pEncryptedBlockData(pCipherBlockData);
642 pCipherBlockData = null;
644 SysTryReturn(NID_IO, pEncryptedBlockData != null, r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
646 r = pNormalFile->Seek(FILESEEKPOSITION_CURRENT, -(tempBlockDataSize + CIPHER_BLOCK_HEADER_SIZE));
647 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
649 r = pNormalFile->Write(pEncryptedBlockData.get(), encryptedDataSize);
650 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to write in file.", GetErrorMessage(r));
655 else //(availableSize < dataSize)
657 if (tempBlockDataSize < dataPosition + availableSize)
659 unique_ptr< byte[] > pTempRelloc(new (std::nothrow) byte[dataPosition + availableSize]);
660 SysTryReturnResult(NID_IO, pTempRelloc != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
662 memcpy(pTempRelloc.get(), pDecryptedBlockData.get(), tempBlockDataSize);
664 pDecryptedBlockData.reset(null);
665 pDecryptedBlockData = move(pTempRelloc);
668 memcpy(reinterpret_cast< byte* >(pDecryptedBlockData.get()) + dataPosition, pDataPos, availableSize);
670 pDataPos = pDataPos + availableSize;
671 dataSize = dataSize - availableSize;
673 r = MakeCipherBlock(pDecryptedBlockData.get(), CIPHER_BLOCK_SIZE, &pCipherBlockData, &encryptedDataSize, key);
674 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
676 unique_ptr< byte[] > pEncryptedBlockData(pCipherBlockData);
677 pCipherBlockData = null;
678 SysTryReturn(NID_IO, pEncryptedBlockData != null, r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
680 r = pNormalFile->Seek(FILESEEKPOSITION_CURRENT, -(tempBlockDataSize + CIPHER_BLOCK_HEADER_SIZE));
681 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
683 r = pNormalFile->Write(pEncryptedBlockData.get(), encryptedDataSize);
684 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to write in file.", GetErrorMessage(r));
690 isFirstBlockSet = true;
694 availableSize = CIPHER_BLOCK_SIZE;
695 if (availableSize >= dataSize)
697 if (tempBlockDataSize < dataSize)
699 unique_ptr< byte[] > pTempRelloc(new (std::nothrow) byte[dataSize]);
700 SysTryReturnResult(NID_IO, pTempRelloc != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
702 memcpy(pTempRelloc.get(), pDecryptedBlockData.get(), tempBlockDataSize);
704 pDecryptedBlockData.reset(null);
705 pDecryptedBlockData = move(pTempRelloc);
708 memcpy(pDecryptedBlockData.get(), pDataPos, dataSize);
710 if (blockDataSize < dataPosition + dataSize)
712 r = MakeCipherBlock(pDecryptedBlockData.get(), dataSize, &pCipherBlockData, &encryptedDataSize, key);
716 r = MakeCipherBlock(pDecryptedBlockData.get(), blockDataSize, &pCipherBlockData, &encryptedDataSize, key);
719 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
721 unique_ptr< byte[] > pEncryptedBlockData(pCipherBlockData);
722 pCipherBlockData = null;
724 SysTryReturn(NID_IO, pEncryptedBlockData != null, r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
726 r = pNormalFile->Seek(FILESEEKPOSITION_CURRENT, -(tempBlockDataSize + CIPHER_BLOCK_HEADER_SIZE));
727 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
729 r = pNormalFile->Write(pEncryptedBlockData.get(), encryptedDataSize);
730 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to write in file.", GetErrorMessage(r));
734 else //availableSize < dataSize
736 if (tempBlockDataSize < availableSize)
738 unique_ptr< byte[] > pTempRelloc(new (std::nothrow) byte[availableSize]);
739 SysTryReturnResult(NID_IO, pTempRelloc != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
741 memcpy(pTempRelloc.get(), pDecryptedBlockData.get(), tempBlockDataSize);
743 pDecryptedBlockData.reset(null);
744 pDecryptedBlockData = move(pTempRelloc);
747 memcpy(pDecryptedBlockData.get(), pDataPos, availableSize);
749 pDataPos = pDataPos + availableSize;
750 dataSize = dataSize - availableSize;
752 r = MakeCipherBlock(pDecryptedBlockData.get(), CIPHER_BLOCK_SIZE, &pCipherBlockData, &encryptedDataSize, key);
753 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
755 unique_ptr< byte[] > pEncryptedBlockData(pCipherBlockData);
756 pCipherBlockData = null;
758 SysTryReturn(NID_IO, pEncryptedBlockData != null, r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
760 r = pNormalFile->Seek(FILESEEKPOSITION_CURRENT, -(tempBlockDataSize + CIPHER_BLOCK_HEADER_SIZE));
761 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
763 r = pNormalFile->Write(pEncryptedBlockData.get(), encryptedDataSize);
764 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to write in file.", GetErrorMessage(r));
771 eofSet = IsEndOfFile(pNormalFile);
778 _SecureIoUtil::ReadDataFromCipherBlock(_NormalFile* pNormalFile, int dataPosition, int bufferSize, byte* pData, _DataFormat dataMode, int* readItems, const ByteBuffer& key)
780 result r = E_SUCCESS;
783 int terminationCur = 0;
787 long blockDataSize = 0;
788 long tempBlockDataSize = 0;
789 byte* pBlockDataCur = null;
790 byte* pDecryptedBlockData = null;
791 byte blockHeader[CIPHER_BLOCK_HEADER_SIZE] = {0, 0, 0};
795 ret = pNormalFile->Read(blockHeader, CIPHER_BLOCK_HEADER_SIZE);
796 if (ret < CIPHER_BLOCK_HEADER_SIZE)
800 if (ret == 0 && IsEndOfFile(pNormalFile))
805 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
810 pBlockDataCur = (byte*) (&blockDataSize);
811 *pBlockDataCur = blockHeader[SECURE_IO_2_BYTES];
812 *(pBlockDataCur + 1) = blockHeader[1];
813 *(pBlockDataCur + SECURE_IO_2_BYTES) = blockHeader[0];
815 unique_ptr< byte[] > pBlockData(new (std::nothrow) byte[CIPHER_BLOCK_SIZE]);
816 SysTryReturnResult(NID_IO, pBlockData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
818 memset(pBlockData.get(), 0, CIPHER_BLOCK_SIZE);
820 if (blockDataSize % ONE_BLOCK_SIZE != 0)
822 tempBlockDataSize = blockDataSize + ONE_BLOCK_SIZE - blockDataSize % ONE_BLOCK_SIZE;
826 tempBlockDataSize = blockDataSize;
829 ret = pNormalFile->Read(pBlockData.get(), tempBlockDataSize);
830 if (ret < tempBlockDataSize)
834 if (ret == 0 && IsEndOfFile(pNormalFile))
839 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
843 readingSize = blockDataSize - dataPosition;
844 SysTryReturn(NID_IO, readingSize != 0, r = E_END_OF_FILE, r, "[%s] Failed to get positive value of readingSize.", GetErrorMessage(r));
846 r = input.Construct(tempBlockDataSize);
847 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
849 r = input.SetArray(pBlockData.get(), 0, tempBlockDataSize);
850 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
854 unique_ptr< ByteBuffer > pDecryptedBuffer(DoCipherAesN(_CIPHER_INFORMATION, input, key, CIPHER_DECRYPT));
855 SysTryReturn(NID_IO, pDecryptedBuffer != null, E_IO, E_IO, "[E_IO] Failed to encrypt data.");
857 pDecryptedBlockData = const_cast< byte* >(pDecryptedBuffer->GetPointer());
858 SysTryReturn(NID_IO, pDecryptedBlockData, r, r, "[%s] Failed to get encrypted data.", GetErrorMessage(r));
860 unique_ptr< byte[] > pTemp(new (std::nothrow) byte[readingSize]);
861 SysTryReturnResult(NID_IO, pTemp != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
863 memcpy(pTemp.get(), pDecryptedBlockData + dataPosition, readingSize);
865 pDecryptedBuffer.reset(null);
869 eofSet = IsEndOfFile(pNormalFile);
871 if (dataMode == DATA_FORMAT_BYTE && (readingSize >= bufferSize))
873 memcpy(pData, pTemp.get(), bufferSize);
874 *readItems = bufferSize;
877 else if (dataMode == DATA_FORMAT_STRING && ((readingSize >= bufferSize) || DetectNewLine(pTemp.get(), readingSize, tempSize, &terminationCur, &newLineSize)))
879 if (terminationCur != 0)
881 memcpy(pData, pTemp.get(), terminationCur);
882 *(pData + terminationCur) = '\n';
883 *readItems = terminationCur + newLineSize;
887 memcpy(pData, pTemp.get(), bufferSize);
888 *readItems = bufferSize;
892 else if (eofSet && (readingSize < bufferSize))
894 memcpy(pData, pTemp.get(), readingSize);
895 *readItems = readingSize;
900 ret = pNormalFile->Read(blockHeader, CIPHER_BLOCK_HEADER_SIZE);
901 if (ret < CIPHER_BLOCK_HEADER_SIZE)
905 if (ret == 0 && IsEndOfFile(pNormalFile))
911 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
916 pBlockDataCur = (byte*) (&blockDataSize);
917 *pBlockDataCur = blockHeader[SECURE_IO_2_BYTES];
918 *(pBlockDataCur + 1) = blockHeader[1];
919 *(pBlockDataCur + SECURE_IO_2_BYTES) = blockHeader[0];
921 memset(pBlockData.get(), 0, CIPHER_BLOCK_SIZE);
923 if (blockDataSize % ONE_BLOCK_SIZE != 0)
925 tempBlockDataSize = blockDataSize + ONE_BLOCK_SIZE - blockDataSize % ONE_BLOCK_SIZE;
929 tempBlockDataSize = blockDataSize;
932 ret = pNormalFile->Read(pBlockData.get(), tempBlockDataSize);
933 if (ret < tempBlockDataSize)
937 if (ret == 0 && IsEndOfFile(pNormalFile))
942 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
946 r = input.Construct(tempBlockDataSize);
947 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
949 r = input.SetArray(pBlockData.get(), 0, tempBlockDataSize);
950 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
954 unique_ptr< ByteBuffer > pDecryptedNextBuffer(DoCipherAesN(_CIPHER_INFORMATION, input, key, CIPHER_DECRYPT));
955 SysTryReturn(NID_IO, pDecryptedNextBuffer != null, E_IO, E_IO, "[E_IO] Failed to encrypt data.");
957 pDecryptedBlockData = const_cast< byte* >(pDecryptedNextBuffer->GetPointer());
958 SysTryReturn(NID_IO, pDecryptedBlockData != null, r, r, "[%s] Failed to get encrypted data.", GetErrorMessage(r));
960 tempSize = readingSize;
961 readingSize = readingSize + blockDataSize;
963 unique_ptr< byte[] > pTempRealloc(new (std::nothrow) byte[readingSize]);
964 SysTryReturnResult(NID_IO, pTempRealloc != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
966 memcpy(pTempRealloc.get(), pTemp.get(), tempSize);
969 pTemp = move(pTempRealloc);
971 memcpy(reinterpret_cast< byte* >(pTemp.get()) + tempSize, pDecryptedBlockData, blockDataSize);
980 _SecureIoUtil::GetDataLengh(_NormalFile* pNormalFile, long int* dataLength)
982 result r = E_SUCCESS;
984 long blockDataSize = 0;
985 byte* pBlockDataCur = null;
986 byte secureHeader[SECURE_FILE_HEADER_SIZE_V1];
987 byte blockHeader[CIPHER_BLOCK_HEADER_SIZE] = {0, 0, 0};
992 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, 0);
993 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
995 ret = pNormalFile->Read(secureHeader, SECURE_FILE_HEADER_SIZE_V1);
996 if (ret < SECURE_FILE_HEADER_SIZE_V1)
1000 if (ret == 0 && IsEndOfFile(pNormalFile))
1005 SysLog(NID_IO, "[%s] Failed to seek in file", GetErrorMessage(r));
1010 ret = pNormalFile->Read(blockHeader, CIPHER_BLOCK_HEADER_SIZE);
1011 if (ret < CIPHER_BLOCK_HEADER_SIZE)
1013 r = GetLastResult();
1015 if (ret == 0 && IsEndOfFile(pNormalFile))
1021 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1026 pBlockDataCur = (byte*) (&blockDataSize);
1027 *pBlockDataCur = blockHeader[SECURE_IO_2_BYTES];
1028 *(pBlockDataCur + 1) = blockHeader[1];
1029 *(pBlockDataCur + SECURE_IO_2_BYTES) = blockHeader[0];
1030 *dataLength = *dataLength + blockDataSize;
1032 if (blockDataSize % ONE_BLOCK_SIZE != 0)
1034 r = pNormalFile->Seek(FILESEEKPOSITION_CURRENT, blockDataSize + ONE_BLOCK_SIZE - blockDataSize % ONE_BLOCK_SIZE);
1038 r = pNormalFile->Seek(FILESEEKPOSITION_CURRENT, blockDataSize);
1041 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1045 return pNormalFile->Seek(FILESEEKPOSITION_END, 0);
1049 _SecureIoUtil::CheckSecureFileHeader(const Tizen::Base::String& filePath, const ByteBuffer* pSecretKey)
1051 result r = E_SUCCESS;
1053 _NormalFile normalFile;
1054 byte secureFileHeader[SECURE_FILE_HEADER_SIZE_V1];
1055 byte reservedValue[SECURE_IO_STATIC_BIN_LEN] = {0xCA, 0xFE, 0xBE, 0xBE, 0xDA, 0xEF, 0xEB, 0xEB};
1059 r = normalFile.Construct(filePath, "r");
1060 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to call constuct.", GetErrorMessage(r));
1062 ret = normalFile.Read(secureFileHeader, SECURE_FILE_HEADER_SIZE_V1);
1063 if (ret < SECURE_FILE_HEADER_SIZE_V1)
1065 r = GetLastResult();
1066 if (IsEndOfFile(&normalFile))
1068 if (ret && pSecretKey)
1070 return E_INVALID_ARG;
1080 //check if the file is encrypted file. (in normal and secure mode)
1081 ret = memcmp(secureFileHeader, SECURE_FILE_HEADER_STRING, SECURE_FILE_HEADER_STRING_SIZE);
1082 ret |= memcmp(secureFileHeader + SECURE_FILE_HEADER_STRING_SIZE, reservedValue, SECURE_IO_STATIC_BIN_LEN);
1084 SysTryReturnResult(NID_IO, !(ret && pSecretKey), E_INVALID_ARG, "Failed to match secure header in file.");
1090 _SecureIoUtil::SetRegistryFlag(void* pFileSecure, _FlagState flag)
1092 result r = E_SUCCESS;
1095 byte currentFlag = 0;
1096 _FileImpl* pFile = (_FileImpl*) pFileSecure;
1099 current = pFile->Tell();
1100 SysTryReturn(NID_IO, current != -1, GetLastResult(), GetLastResult(), "[%s] Tell file has failed.", GetErrorMessage(GetLastResult()));
1102 r = pFile->Seek(FILESEEKPOSITION_BEGIN, SECURE_REG_HEADER_STRING_SIZE + SECURE_IO_STATIC_BIN_LEN);
1103 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1105 readItems = pFile->Read(¤tFlag, 1);
1106 SysTryReturn(NID_IO, readItems == 1, r = E_INVALID_STATE, E_INVALID_STATE, "[E_INVALID_STATE] Tell file has failed.");
1108 if (flag == FLAG_STATE_CVF)
1110 currentFlag = currentFlag | SECURE_IO_2_BYTES;
1111 currentFlag = currentFlag & SECURE_IO_HEX_FE;
1113 else if (flag == FLAG_STATE_CTF)
1115 currentFlag = currentFlag | 1;
1116 currentFlag = currentFlag & SECURE_IO_HEX_FD;
1118 else if (flag == FLAG_STATE_CVF_CTF)
1120 currentFlag = currentFlag | SECURE_IO_3_BYTES;
1122 else if (flag == FLAG_STATE_NONE)
1124 currentFlag = currentFlag & SECURE_IO_HEX_FC;
1128 return E_INVALID_STATE;
1131 r = pFile->Seek(FILESEEKPOSITION_BEGIN, SECURE_REG_HEADER_STRING_SIZE + SECURE_IO_STATIC_BIN_LEN);
1132 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1134 r = pFile->Write(¤tFlag, 1);
1135 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to write data in file.", GetErrorMessage(r));
1137 r = pFile->Seek(FILESEEKPOSITION_BEGIN, current);
1138 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1144 _SecureIoUtil::CheckRegistryFlag(byte* pHeader)
1147 byte flag = *(pHeader + SECURE_REG_HEADER_STRING_SIZE + SECURE_IO_STATIC_BIN_LEN);
1149 if (((flag | SECURE_IO_HEX_FD) == SECURE_IO_HEX_FF) && ((flag | SECURE_IO_HEX_FE) != SECURE_IO_HEX_FF))
1151 return FLAG_STATE_CVF;
1153 else if (((flag | SECURE_IO_HEX_FD) != SECURE_IO_HEX_FF) && ((flag | SECURE_IO_HEX_FE) == SECURE_IO_HEX_FF))
1155 return FLAG_STATE_CTF;
1157 else if ((flag | SECURE_IO_HEX_FC) == SECURE_IO_HEX_FF)
1159 return FLAG_STATE_CVF_CTF;
1163 return FLAG_STATE_NONE;
1168 _SecureIoUtil::MakeRegistryReplica(const String fileName, byte* pNameIndex)
1170 result r = E_SUCCESS;
1172 int nameIndex = *pNameIndex;
1177 SysTryReturn(NID_IO, nameIndex <= SECURE_IO_HEX_64, replicaName, E_INVALID_ARG, "[E_INVALID_ARG] Index number is too big.");
1181 replicaName = fileName + L".rpa ";
1182 if (!File::IsFileExist(replicaName))
1189 replicaName = fileName + L"_";
1190 replicaName.Append((int) nameIndex);
1191 replicaName.Append(L".rpa");
1193 if (!File::IsFileExist(replicaName))
1202 *pNameIndex = nameIndex;
1204 if (_FileImpl::IsFileExist(fileName))
1206 r = _FileImpl::Copy(fileName, replicaName, true);
1207 SysTryReturn(NID_IO, !IsFailed(r), replicaName, r, "[%s] Failed to copy file.", GetErrorMessage(r));
1211 // open registry replica file
1213 r = fileImpl.Construct(replicaName, "w", false, null);
1214 SysTryReturn(NID_IO, !IsFailed(r), replicaName, r, "[%s] Failed to create empty file.", GetErrorMessage(r));
1222 _SecureIoUtil::DeleteRegistryReplica(byte* pHeader, const String fileName)
1224 result r = E_SUCCESS;
1226 byte nameIndex = *(pHeader + SECURE_REG_HEADER_STRING_SIZE + SECURE_IO_9_BYTES);
1230 SysTryReturnResult(NID_IO, nameIndex <= SECURE_IO_HEX_64, E_INVALID_ARG, "Index number is too big.");
1234 replicaName = fileName + L".rpa ";
1238 replicaName = fileName + L"_";
1239 replicaName.Append((int) nameIndex);
1240 replicaName.Append(L".rpa");
1243 _FileImpl::Remove(replicaName);
1249 _SecureIoUtil::LoadRegistryReplica(byte** pHeader, int* pHeaderLen, const String fileName)
1251 result r = E_SUCCESS;
1254 char indexStr[MAX_BLOCK_INDEX_LEN];
1257 _FileImpl replicaFile;
1260 memset(indexStr, 0, MAX_BLOCK_INDEX_LEN);
1261 nameIndex = *(*pHeader + SECURE_REG_HEADER_STRING_SIZE + SECURE_IO_9_BYTES);
1267 SysTryReturnResult(NID_IO, nameIndex <= SECURE_IO_HEX_64, E_INVALID_ARG, "Index number is too big.");
1271 replicaName = fileName + L".rpa ";
1275 replicaName = fileName + L"_";
1276 replicaName.Append((int) nameIndex);
1277 replicaName.Append(L".rpa");
1280 if (Tizen::Io::_FileImpl::IsFileExist(replicaName))
1282 r = replicaFile.Construct(replicaName, "r", false, null);
1283 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Constructing the file has failed.", GetErrorMessage(r));
1285 r = replicaFile.Seek(FILESEEKPOSITION_END, 0);
1286 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1288 length = replicaFile.Tell();
1291 *pHeader = new (std::nothrow) byte[length];
1292 SysTryCatch(NID_IO, *pHeader != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1293 *pHeaderLen = length;
1295 r = replicaFile.Seek(FILESEEKPOSITION_BEGIN, 0);
1296 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1298 readItems = replicaFile.Read(*pHeader, length);
1299 SysTryCatch(NID_IO, readItems >= length, r = GetLastResult(), GetLastResult(), "[%s] Failed to read data from file.", GetErrorMessage(GetLastResult()));
1316 _SecureIoUtil::CheckSecureRegistryHeader(byte** pHeader, int* pHeaderLen, const String strFilePathPath, bool secureMode)
1318 result r = E_SUCCESS;
1322 _FlagState flag = FLAG_STATE_NONE;
1323 byte reservedValue[SECURE_IO_STATIC_BIN_LEN] = {0xCA, 0xFE, 0xBE, 0xBE, 0xDA, 0xEF, 0xEB, 0xEB};
1324 byte* pSecureRegistryHeader = *pHeader;
1327 //check if the registry is encrypted . (in normal and secure mode)
1328 ret = memcmp(pSecureRegistryHeader, SECURE_REG_HEADER_STRING, SECURE_REG_HEADER_STRING_SIZE);
1329 ret |= memcmp(pSecureRegistryHeader + SECURE_REG_HEADER_STRING_SIZE, reservedValue, SECURE_IO_STATIC_BIN_LEN);
1330 SysTryReturnResult(NID_IO, !(ret && secureMode == true), E_INVALID_ARG, "Failed to match secure header in file.");
1334 flag = CheckRegistryFlag(pSecureRegistryHeader);
1336 if (_FileImpl::IsFileExist(strFilePathPath) && !_SecureIoUtil::IsEmpty(strFilePathPath))
1338 r = fileImpl.Construct(strFilePathPath, "a+", false, null);
1339 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Constructing the file has failed.", GetErrorMessage(r));
1341 if (flag == FLAG_STATE_CVF_CTF)
1343 r = DeleteRegistryReplica(pSecureRegistryHeader, strFilePathPath);
1344 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to delete registry replica.", GetErrorMessage(r));
1346 r = SetRegistryFlag(&fileImpl, FLAG_STATE_CVF);
1347 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to set registry flags.", GetErrorMessage(r));
1350 else if (flag == FLAG_STATE_CTF)
1352 r = LoadRegistryReplica(pHeader, &readLen, strFilePathPath);
1353 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to load registry replica.", GetErrorMessage(r));
1355 SysTryReturn(NID_IO, *pHeader, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Failed to read secure registry file header.");
1357 pSecureRegistryHeader = *pHeader;
1358 *pHeaderLen = readLen;
1360 r = DeleteRegistryReplica(pSecureRegistryHeader, strFilePathPath);
1361 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to delete registry replica.", GetErrorMessage(r));
1363 r = SetRegistryFlag(&fileImpl, FLAG_STATE_CVF);
1364 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to set registry flags.", GetErrorMessage(r));
1366 else if (flag == FLAG_STATE_CVF)
1372 r = E_INVALID_STATE;
1381 _SecureIoUtil::CheckFlag(_NormalFile* pNormalFile, _FlagState* pFlag)
1383 result r = E_SUCCESS;
1388 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, SECURE_FILE_HEADER_STRING_SIZE + SECURE_IO_STATIC_BIN_LEN);
1393 ret = pNormalFile->Read(&flag, SECURE_IO_FLAG_SIZE);
1397 r = GetLastResult();
1398 SysTryReturnResult(NID_IO, !(ret == 0 && r == E_END_OF_FILE), E_INVALID_ARG, "Failed to read secure file header.");
1400 r = GetLastResult();
1402 if (ret == 0 && IsEndOfFile(pNormalFile))
1407 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1412 if (((flag | SECURE_IO_HEX_FD) == SECURE_IO_HEX_FF) && ((flag | SECURE_IO_HEX_FE) != SECURE_IO_HEX_FF))
1414 *pFlag = FLAG_STATE_CVF;
1416 else if (((flag | SECURE_IO_HEX_FD) != SECURE_IO_HEX_FF) && ((flag | SECURE_IO_HEX_FE) == SECURE_IO_HEX_FF))
1418 *pFlag = FLAG_STATE_CTF;
1420 else if ((flag | SECURE_IO_HEX_FC) == SECURE_IO_HEX_FF)
1422 *pFlag = FLAG_STATE_CVF_CTF;
1426 *pFlag = FLAG_STATE_NONE;
1433 _SecureIoUtil::SetFlag(_NormalFile* pNormalFile, _FlagState flag)
1435 result r = E_SUCCESS;
1438 byte currentFlag = 0;
1441 current = pNormalFile->Tell();
1444 r = GetLastResult();
1445 SysLog(NID_IO, "[%s] Failed to tell in file", GetErrorMessage(r));
1449 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, SECURE_FILE_HEADER_STRING_SIZE + SECURE_IO_STATIC_BIN_LEN);
1450 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file", GetErrorMessage(r));
1452 readItems = pNormalFile->Read(¤tFlag, SECURE_IO_FLAG_SIZE);
1455 r = GetLastResult();
1457 if (readItems == 0 && IsEndOfFile(pNormalFile))
1462 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1466 if (flag == FLAG_STATE_CVF)
1468 currentFlag = currentFlag | SECURE_IO_2_BYTES;
1469 currentFlag = currentFlag & SECURE_IO_HEX_FE;
1471 else if (flag == FLAG_STATE_CTF)
1473 currentFlag = currentFlag | 1;
1474 currentFlag = currentFlag & SECURE_IO_HEX_FD;
1476 else if (flag == FLAG_STATE_CVF_CTF)
1478 currentFlag = currentFlag | SECURE_IO_3_BYTES;
1480 else if (flag == FLAG_STATE_NONE)
1482 currentFlag = currentFlag & SECURE_IO_HEX_FC;
1486 SysAssert(false); // should not happen!
1488 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, SECURE_FILE_HEADER_STRING_SIZE + SECURE_IO_STATIC_BIN_LEN);
1489 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file", GetErrorMessage(r));
1491 r = pNormalFile->Write(¤tFlag, SECURE_IO_FLAG_SIZE);
1492 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to write in file", GetErrorMessage(r));
1494 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, current);
1495 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file", GetErrorMessage(r));
1501 _SecureIoUtil::DeleteBlockReplica(_NormalFile* pNormalFile, const String filePath)
1503 result r = E_SUCCESS;
1509 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, SECURE_FILE_HEADER_STRING_SIZE + SECURE_IO_9_BYTES);
1510 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file", GetErrorMessage(r));
1512 readItems = pNormalFile->Read(&nameIndex, SECURE_IO_INDEX_SIZE);
1515 r = GetLastResult();
1517 if (readItems == 0 && IsEndOfFile(pNormalFile))
1522 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1526 SysTryReturnResult(NID_IO, nameIndex <= SECURE_IO_HEX_64, E_INVALID_ARG, "Index number is too big.");
1530 replicaName = filePath + L".rpa ";
1534 replicaName = filePath + L"_";
1535 replicaName.Append((int) nameIndex);
1536 replicaName.Append(L".rpa");
1539 _FileImpl::Remove(replicaName);
1545 _SecureIoUtil::RestoreCorruptBlock(_NormalFile* pNormalFile, String fileName)
1547 result r = E_SUCCESS;
1548 String blockName = null;
1550 byte blockHeader[CIPHER_BLOCK_HEADER_SIZE] = {0, 0, 0};
1551 byte* pBlockDataCur = null;
1552 _NormalFile replicaFile;
1553 int startBlockNum = 0;
1559 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, SECURE_FILE_HEADER_STRING_SIZE + SECURE_IO_9_BYTES);
1560 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file", GetErrorMessage(r));
1562 readItems = pNormalFile->Read(&nameIndex, SECURE_IO_INDEX_SIZE); // read index of temporary block name
1563 if (readItems < SECURE_IO_INDEX_SIZE)
1565 r = GetLastResult();
1567 if (readItems == 0 && IsEndOfFile(pNormalFile))
1572 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1576 readItems = pNormalFile->Read(&dataLen, SECURE_IO_LOF_SIZE);
1577 if (readItems < SECURE_IO_LOF_SIZE)
1579 r = GetLastResult();
1581 if (readItems == 0 && IsEndOfFile(pNormalFile))
1586 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1590 SysTryReturnResult(NID_IO, nameIndex < SECURE_IO_HEX_64, E_INVALID_ARG, "Index number is too big.");
1594 blockName = fileName + L".rpa ";
1598 blockName = fileName + L"_";
1599 blockName.Append((int) nameIndex);
1600 blockName.Append(L".rpa");
1603 if (_FileUtil::IsFileExist(blockName))
1605 r = replicaFile.Construct(blockName, "r");
1606 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to conctruct replica file.", GetErrorMessage(r));
1608 readItems = replicaFile.Read(blockHeader, CIPHER_BLOCK_HEADER_SIZE); //Read start block number (3bytes)
1609 if (readItems < CIPHER_BLOCK_HEADER_SIZE)
1611 r = GetLastResult();
1613 if (readItems == 0 && IsEndOfFile(pNormalFile))
1618 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1622 pBlockDataCur = (byte*) (&startBlockNum);
1623 *pBlockDataCur = blockHeader[SECURE_IO_2_BYTES];
1624 *(pBlockDataCur + 1) = blockHeader[1];
1625 *(pBlockDataCur + SECURE_IO_2_BYTES) = blockHeader[0];
1627 r = replicaFile.Seek(FILESEEKPOSITION_END, 0);
1628 fileLength = replicaFile.Tell();
1629 SysTryReturn(NID_IO, fileLength != -1, GetLastResult(), GetLastResult(), "[%s] Failed to tell in file.", GetErrorMessage(GetLastResult()));
1631 r = replicaFile.Seek(FILESEEKPOSITION_BEGIN, CIPHER_BLOCK_HEADER_SIZE);
1632 unique_ptr< byte[] > pBlock(new (std::nothrow) byte[fileLength - CIPHER_BLOCK_HEADER_SIZE]);
1633 SysTryReturnResult(NID_IO, pBlock != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1635 memset(pBlock.get(), 0, fileLength - CIPHER_BLOCK_HEADER_SIZE);
1637 readItems = replicaFile.Read(pBlock.get(), fileLength - CIPHER_BLOCK_HEADER_SIZE);
1638 if (readItems < (fileLength - CIPHER_BLOCK_HEADER_SIZE))
1640 r = GetLastResult();
1642 if (readItems == 0 && IsEndOfFile(pNormalFile))
1647 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1651 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, SECURE_FILE_HEADER_SIZE_V1 + (CIPHER_BLOCK_SIZE + CIPHER_BLOCK_HEADER_SIZE) * startBlockNum);
1653 r = pNormalFile->Write(pBlock.get(), fileLength - CIPHER_BLOCK_HEADER_SIZE);
1654 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to write in file.", GetErrorMessage(r));
1658 r = pNormalFile->Truncate(dataLen);
1659 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to truncate in file.", GetErrorMessage(r));
1665 _SecureIoUtil::MakeCipherBlockReplica(_NormalFile* pNormalFile, const String filePath, int dataPosition, int dataSize)
1667 result r = E_SUCCESS;
1668 int totalDataSize = 0;
1672 int startBlockNum = 0;
1674 long blockDataSize = 0;
1675 bool isEndOfBlock = false;
1677 byte* pBlockDataCur = null;
1678 byte blockHeader[CIPHER_BLOCK_HEADER_SIZE] = {0, 0, 0};
1680 _NormalFile tempBlockFile;
1682 totalDataSize = dataPosition + dataSize;
1685 headPos = pNormalFile->Tell();
1686 SysTryReturn(NID_IO, headPos != -1, GetLastResult(), GetLastResult(), "[%s] Failed to tell in file.", GetErrorMessage(GetLastResult()));
1688 ret = pNormalFile->Read(blockHeader, CIPHER_BLOCK_HEADER_SIZE);
1689 if (ret < CIPHER_BLOCK_HEADER_SIZE)
1691 r = GetLastResult();
1693 if (ret == 0 && IsEndOfFile(pNormalFile))
1698 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1705 pBlockDataCur = (byte*) (&blockDataSize);
1706 *pBlockDataCur = blockHeader[SECURE_IO_2_BYTES];
1707 *(pBlockDataCur + 1) = blockHeader[1];
1708 *(pBlockDataCur + SECURE_IO_2_BYTES) = blockHeader[0];
1709 blockSize = blockSize + blockDataSize;
1711 if (blockSize >= totalDataSize)
1713 isEndOfBlock = true;
1716 if (blockDataSize % ONE_BLOCK_SIZE != 0)
1718 r = pNormalFile->Seek(FILESEEKPOSITION_CURRENT, blockDataSize + ONE_BLOCK_SIZE - blockDataSize % ONE_BLOCK_SIZE);
1722 r = pNormalFile->Seek(FILESEEKPOSITION_CURRENT, blockDataSize);
1725 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1732 if (IsEndOfFile(pNormalFile))
1737 ret = pNormalFile->Read(blockHeader, CIPHER_BLOCK_HEADER_SIZE);
1738 if (ret < CIPHER_BLOCK_HEADER_SIZE)
1740 r = GetLastResult();
1742 if (ret == 0 && IsEndOfFile(pNormalFile))
1747 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1753 tailPos = pNormalFile->Tell();
1754 SysTryReturn(NID_IO, tailPos != -1, GetLastResult(), GetLastResult(), "[%s] Failed to tell in file.", GetErrorMessage(GetLastResult()));
1756 startBlockNum = (headPos - SECURE_FILE_HEADER_SIZE_V1) / (CIPHER_BLOCK_SIZE + CIPHER_BLOCK_HEADER_SIZE);
1758 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, headPos);
1759 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1761 unique_ptr< byte[] > pBlockReplica(new (std::nothrow) byte[tailPos - headPos + CIPHER_BLOCK_HEADER_SIZE]);
1762 SysTryReturnResult(NID_IO, pBlockReplica != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1764 *(reinterpret_cast< byte* >(pBlockReplica.get()) + SECURE_IO_2_BYTES) = (byte) (startBlockNum & SECURE_IO_HEX_FF);
1765 *(reinterpret_cast< byte* >(pBlockReplica.get()) + 1) = (byte) ((startBlockNum >> SECURE_IO_8_BITS) & SECURE_IO_HEX_FF);
1766 *(reinterpret_cast< byte* >(pBlockReplica.get())) = (byte) ((startBlockNum >> (SECURE_IO_8_BITS * SECURE_IO_2_BYTES)) & SECURE_IO_HEX_FF);
1768 ret = pNormalFile->Read(reinterpret_cast< byte* >(pBlockReplica.get()) + CIPHER_BLOCK_HEADER_SIZE, tailPos - headPos);
1769 if (ret < (tailPos - headPos))
1771 r = GetLastResult();
1773 if (ret == 0 && IsEndOfFile(pNormalFile))
1778 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1784 SysTryReturnResult(NID_IO, nameIndex <= SECURE_IO_HEX_64, E_INVALID_ARG, "Index number is too big.");
1788 replicaName = filePath + L".rpa ";
1789 if (!File::IsFileExist(replicaName))
1796 replicaName = filePath + L"_";
1797 replicaName.Append((int) nameIndex);
1798 replicaName.Append(L".rpa");
1800 if (!File::IsFileExist(replicaName))
1808 r = tempBlockFile.Construct(replicaName, "w");
1809 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to call construct.", GetErrorMessage(r));
1811 r = tempBlockFile.Write(pBlockReplica.get(), tailPos - headPos + CIPHER_BLOCK_HEADER_SIZE);
1812 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to write in file.", GetErrorMessage(r));
1814 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, 25);
1815 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1817 r = pNormalFile->Write(&nameIndex, 1);
1818 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to write in file.", GetErrorMessage(r));
1820 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, headPos);
1821 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1827 _SecureIoUtil::IsNormalDatabase(_DatabaseMode mode)
1830 if (mode == DATABASE_MODE_NORMAL)
1838 _SecureIoUtil::IsSecureDatabase(_DatabaseMode mode)
1841 if (mode == DATABASE_MODE_SECURE)
1849 _SecureIoUtil::GetDatabaseMode(char* pStrPhysicalDbPath)
1851 result r = E_SUCCESS;
1853 unsigned long readBytes = 0;
1854 char buffer[SQLITE_HEADER_SIZE] = {0, };
1855 char magicNum1[SECURE_IO_MAGIC_NUMBER_SIZE] = {0xCA, 0xFE, 0xBE, 0xBE};
1856 char magicNum2[SECURE_IO_MAGIC_NUMBER_SIZE] = {0xDA, 0xEF, 0xEF, 0xEB};
1857 _NormalFile normalFile;
1861 r = normalFile.Construct(pStrPhysicalDbPath, "r");
1862 SysTryReturn(NID_IO, !IsFailed(r), DATABASE_MODE_ERROR, r, "[%s] Failed to open file (%s) in read mode", GetErrorMessage(r), pStrPhysicalDbPath);
1864 r = normalFile.Seek(FILESEEKPOSITION_END, 0);
1865 SysTryReturn(NID_IO, !IsFailed(r), DATABASE_MODE_ERROR, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1867 fileLength = normalFile.Tell();
1868 if (fileLength <= 0)
1870 return DATABASE_MODE_NONE;
1873 r = normalFile.Seek(FILESEEKPOSITION_BEGIN, 0);
1874 SysTryReturn(NID_IO, !IsFailed(r), DATABASE_MODE_ERROR, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1876 readBytes = normalFile.Read(buffer, SQLITE_HEADER_SIZE);
1877 r = GetLastResult();
1878 SysTryReturn(NID_IO, readBytes > 0, DATABASE_MODE_ERROR, r, "[%s] File is not opened for reading or else failed to read data from file.", GetErrorMessage(r));
1880 if (!strncmp(buffer, "SQLite format 3", SQLITE_FORMAT_STR_SIZE))
1882 return DATABASE_MODE_NORMAL;
1884 else if (!memcmp((void*) buffer, magicNum1, SECURE_IO_MAGIC_NUMBER_SIZE) && !memcmp((void*) (buffer + SECURE_IO_12_BYTES), magicNum2, SECURE_IO_MAGIC_NUMBER_SIZE))
1886 return DATABASE_MODE_SECURE;
1889 return DATABASE_MODE_ERROR;
1893 _SecureIoUtil::GenerateDatabaseKeyN(const Tizen::Base::ByteBuffer* pUserKey)
1898 byte* pDbKey = null;
1902 unique_ptr< ByteBuffer > pKeyBuffer(GetSecureKeyN(pUserKey));
1904 SysTryReturn(NID_IO, pKeyBuffer != null, null, E_INVALID_ARG, "[E_INVALID_ARG] Failed to generate secure key.");
1906 pKey = const_cast< byte* >(pKeyBuffer->GetPointer());
1908 pDbKey = new (std::nothrow) byte[DATABASE_KEY_LENGTH * SECURE_IO_2_BYTES + 1];
1909 SysTryReturn(NID_IO, pDbKey != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1911 memset(pDbKey, 0, DATABASE_KEY_LENGTH * SECURE_IO_2_BYTES + 1);
1913 for (int i = 0; i < DATABASE_KEY_LENGTH; i++)
1915 lowByte = pKey[i] & SECURE_IO_HEX_0F;
1916 highByte = (pKey[i] >> SECURE_IO_4_BITS) & SECURE_IO_HEX_0F;
1917 if (lowByte <= SECURE_IO_9_BYTES)
1919 pDbKey[i * SECURE_IO_2_BYTES + 1] = lowByte + '0';
1923 pDbKey[i * SECURE_IO_2_BYTES + 1] = lowByte - SECURE_IO_10_BYTES + 'A';
1926 if (highByte <= SECURE_IO_9_BYTES)
1928 pDbKey[i * SECURE_IO_2_BYTES] = highByte + '0';
1932 pDbKey[i * SECURE_IO_2_BYTES] = highByte - SECURE_IO_10_BYTES + 'A';
1940 _SecureIoUtil::IsEmpty(const String& filePath)
1942 result r = E_SUCCESS;
1943 FileAttributes attribute;
1946 r = File::GetAttributes(filePath, attribute);
1953 if (attribute.GetFileSize() == 0)