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 FIo_SecureIoUtil.cpp
19 * @brief This is the implementation file for the %_SecureIoUilt class.
25 #include <sys/types.h>
32 #include <unique_ptr.h>
34 #include <FBaseString.h>
36 #include <FBaseResult.h>
37 #include <FSecSecretKey.h>
38 #include <FSecSecretKeyGenerator.h>
39 #include <FSecCryptoSha1Hash.h>
40 #include <FSecCryptoAesCipher.h>
42 #include <FBase_NativeError.h>
43 #include <FBaseSysLog.h>
44 #include <FBase_StringConverter.h>
45 #include <FIo_SecureIoUtil.h>
46 #include <FIo_FileUtil.h>
47 #include <FApp_AppInfo.h>
50 using namespace Tizen::Base;
51 using namespace Tizen::Security;
52 using namespace Tizen::Security::Crypto;
53 using namespace Tizen::App;
55 namespace Tizen { namespace Io
58 static const int SECURE_IO_4_BITS = 4;
59 static const int SECURE_IO_8_BITS = 8;
60 static const int SECURE_IO_3_BYTES = 3;
61 static const int SECURE_IO_4_BYTES = 4;
62 static const int SECURE_IO_5_BYTES = 5;
63 static const int SECURE_IO_8_BYTES = 8;
64 static const int SECURE_IO_9_BYTES = 9;
65 static const int SECURE_IO_HEX_0F = 0x0f;
66 static const int SECURE_IO_HEX_FC = 0xfc;
67 static const int SECURE_IO_HEX_FD = 0xfd;
68 static const int SECURE_IO_HEX_FE = 0xfe;
69 static const int SECURE_IO_HEX_FF = 0xff;
70 static const int DATABASE_KEY_LENGTH = 16;
71 static const int AES_KEY_LENGTH = 16;
73 const char SECURE_FILE_HEADER_STRING[] = {0x74, 0x69, 0x7a, 0x65, 0x6e, 0x20, 0x73, 0x65, 0x63, 0x75, 0x72, 0x65, 0x66, 0x69, 0x6c, 0x65};
74 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};
75 static const wchar_t* _CIPHER_INFORMATION = L"CBC/128/NOPADDING";
78 _SecureIoUtil::GetSecureKeyN(const ByteBuffer* pUserKey)
82 unique_ptr< Tizen::Base::ByteBuffer > pKey(new (std::nothrow) ByteBuffer());
83 SysTryReturn(NID_IO, pKey != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
85 r = pKey->Construct(AES_KEY_LENGTH);
86 SysTryReturn(NID_IO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
88 if (pUserKey->GetRemaining() >= AES_KEY_LENGTH)
90 r = pKey->SetArray(pUserKey->GetPointer(), 0, AES_KEY_LENGTH);
91 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
97 int tempLength = pUserKey->GetRemaining();
98 int loopCount = AES_KEY_LENGTH / tempLength;
100 for(int i = 0; i < loopCount; i++)
103 r = pKey->SetArray(pUserKey->GetPointer(), 0, tempLength);
104 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
108 if (AES_KEY_LENGTH % tempLength)
110 r = pKey->SetArray(pUserKey->GetPointer(), 0, (AES_KEY_LENGTH % tempLength));
111 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
121 return pKey.release();
125 _SecureIoUtil::DoCipherAesN(const String transformation, const ByteBuffer& input, const ByteBuffer& key, CipherOperation encMode)
127 result r = E_SUCCESS;
130 SecretKeyGenerator secKeyGenerator;
132 static byte ivArray[] = {0x3E, 0xB5, 0x01, 0x45, 0xE4, 0xF8, 0x75, 0x3F, 0x08, 0x9D, 0x9F, 0x57, 0x3B, 0x63, 0xEF, 0x4B };
134 r = aesCipher.Construct(transformation, encMode);
135 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
137 r = ivBuffer.Construct(sizeof(ivArray));
138 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
140 r = ivBuffer.SetArray(ivArray, 0, sizeof(ivArray));
141 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
145 r = aesCipher.SetInitialVector(ivBuffer);
146 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
148 r = secKeyGenerator.Construct(key);
149 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Propagated.", GetErrorMessage(r));
151 unique_ptr< ISecretKey > pKey(reinterpret_cast< ISecretKey* >(secKeyGenerator.GenerateKeyN()));
152 SysTryReturn(NID_IO, pKey != null, null, GetLastResult(), "[%s] Failed to generate key.", GetErrorMessage(GetLastResult()));
154 r = aesCipher.SetKey(*pKey.get());
155 SysTryReturn(NID_IO, !IsFailed(r), null, r, "[%s] Failed to set secret key.", GetErrorMessage(r));
157 unique_ptr< ByteBuffer > pOutBuf(null);
159 if (encMode == CIPHER_ENCRYPT)
161 pOutBuf = unique_ptr< ByteBuffer >(aesCipher.EncryptN(input));
165 pOutBuf = unique_ptr< ByteBuffer >(aesCipher.DecryptN(input));
168 SysTryReturn(NID_IO, pOutBuf != null, null, GetLastResult(), "[%s] Failed to decrypt data.", GetErrorMessage(GetLastResult()));
170 SetLastResult(E_SUCCESS);
171 return pOutBuf.release();
175 _SecureIoUtil::IsEndOfFile(_NormalFile* pNormalFile)
177 result r = E_SUCCESS;
180 int current = pNormalFile->Tell();
181 SysTryReturn(NID_IO, current != -1, false, GetLastResult(), "[%s] Failed to tell in file.", GetErrorMessage(GetLastResult()));
183 int ret = pNormalFile->Read(&flag, 1); //ignore ret as we are only looking for EOF.
184 if (GetLastResult() == E_END_OF_FILE && ret == 0)
189 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, current);
190 SysTryReturn(NID_IO, !IsFailed(r), false, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
196 _SecureIoUtil::InsertSecureFileHeader(_NormalFile* pNormalFile)
198 result r = E_SUCCESS;
200 byte secureHeader[SECURE_FILE_HEADER_SIZE_V1];
201 byte reservedValue[SECURE_IO_STATIC_BIN_LEN] = {0xCA, 0xFE, 0xBE, 0xBE, 0xDA, 0xEF, 0xEB, 0xEB};
204 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, 0);
205 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
207 ret = pNormalFile->Read(secureHeader, SECURE_FILE_HEADER_SIZE_V1);
208 if (ret < SECURE_FILE_HEADER_SIZE_V1)
212 if (ret == 0 && IsEndOfFile(pNormalFile)) //is true
214 memcpy(secureHeader, SECURE_FILE_HEADER_STRING, SECURE_FILE_HEADER_STRING_SIZE);
215 memcpy(secureHeader + SECURE_FILE_HEADER_STRING_SIZE, reservedValue, SECURE_IO_STATIC_BIN_LEN);
216 secureHeader[SECURE_FILE_HEADER_STRING_SIZE + SECURE_IO_STATIC_BIN_LEN] = SECURE_IO_2_BYTES;
217 secureHeader[SECURE_FILE_HEADER_STRING_SIZE + SECURE_IO_STATIC_BIN_LEN + SECURE_IO_FLAG_SIZE] = 0;
218 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);
219 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);
221 r = pNormalFile->Write(secureHeader, SECURE_FILE_HEADER_SIZE_V1);
222 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] [%s] Failed to write in file.", GetErrorMessage(r));
227 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
229 } //Else it means that header is already written
235 _SecureIoUtil::SaveLengthOfFile(_NormalFile* pNormalFile)
237 result r = E_SUCCESS;
239 byte lengthOfFile[SECURE_IO_LOF_SIZE] = {0, 0, 0, 0};
242 r = pNormalFile->Seek(FILESEEKPOSITION_END, 0);
243 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
245 length = pNormalFile->Tell();
246 SysTryReturn(NID_IO, length != -1, GetLastResult(), GetLastResult(), "[%s] Failed to tell in file.", GetErrorMessage(GetLastResult()));
248 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, SECURE_FILE_HEADER_STRING_SIZE + SECURE_IO_10_BYTES);
249 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
251 if (sizeof(length) != SECURE_IO_LOF_SIZE)
253 memcpy(lengthOfFile + SECURE_IO_LOF_SIZE - sizeof(length), &length, sizeof(length));
257 memcpy(lengthOfFile, &length, SECURE_IO_LOF_SIZE);
260 r = pNormalFile->Write(lengthOfFile, SECURE_IO_LOF_SIZE);
261 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to write in file.", GetErrorMessage(r));
267 _SecureIoUtil::SelectCipherBlock(_NormalFile* pNormalFile, long virtualFilePointer, int* pPosition)
269 result r = E_SUCCESS;
273 long blockDataSize = 0;
274 byte secureHeader[SECURE_FILE_HEADER_SIZE_V1] = {0, };
275 byte blockHeader[CIPHER_BLOCK_HEADER_SIZE] = {0, 0, 0};
276 byte* pBlockDataCur = null;
279 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, 0);
282 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
284 ret = pNormalFile->Read(secureHeader, SECURE_FILE_HEADER_SIZE_V1);
285 if (ret < SECURE_FILE_HEADER_SIZE_V1)
289 if (ret == 0 && IsEndOfFile(pNormalFile))
294 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
298 //read 1byte to check padding size
301 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, count * (CIPHER_BLOCK_SIZE + CIPHER_BLOCK_HEADER_SIZE) + SECURE_FILE_HEADER_SIZE_V1);
302 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
304 ret = pNormalFile->Read(blockHeader, CIPHER_BLOCK_HEADER_SIZE);
305 if (ret < CIPHER_BLOCK_HEADER_SIZE) //less data read
309 if (ret == 0 && IsEndOfFile(pNormalFile))
314 SysLog(NID_IO, "[%s] Failed to read data from file", GetErrorMessage(r));
320 pBlockDataCur = (byte*) (&blockDataSize);
321 *pBlockDataCur = blockHeader[SECURE_IO_2_BYTES];
322 *(pBlockDataCur + 1) = blockHeader[1];
323 *(pBlockDataCur + SECURE_IO_2_BYTES) = blockHeader[0];
325 if (virtualFilePointer <= dataSize + blockDataSize)
327 *pPosition = virtualFilePointer - dataSize;
328 if (*pPosition == CIPHER_BLOCK_SIZE)
330 r = pNormalFile->Seek(FILESEEKPOSITION_CURRENT, CIPHER_BLOCK_SIZE);
335 r = pNormalFile->Seek(FILESEEKPOSITION_CURRENT, -CIPHER_BLOCK_HEADER_SIZE);
339 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
344 dataSize = dataSize + blockDataSize;
354 _SecureIoUtil::DetectNewLine(byte* pData, int readSize, int preReadSize, int* pTerminationCur, int* newLineSize)
360 for (count = preReadSize; count < readSize; count++)
362 if (pData[count] == '\r' && pData[count + 1] == '\n')
364 *pTerminationCur = count;
365 *newLineSize = SECURE_IO_2_BYTES;
369 if (pData[count] == '\n')
371 *pTerminationCur = count;
381 _SecureIoUtil::MakeCipherBlock(byte* pData, int dataSize, byte** ppEncryptedData, int* pEncryptedDataSize, const ByteBuffer& key)
383 result r = E_SUCCESS;
385 int lastDataSize = 0;
388 byte* pTempRealloc = null;
389 byte* pTempEncryptedData = null;
392 SysTryReturnResult(NID_SEC, dataSize > 0, E_INVALID_ARG, "Input data is not valid.");
395 *pEncryptedDataSize = 0;
397 blockCount = dataSize / CIPHER_BLOCK_SIZE;
398 lastDataSize = dataSize % CIPHER_BLOCK_SIZE;
399 if (lastDataSize != 0)
401 blockCount = blockCount + 1;
405 lastDataSize = CIPHER_BLOCK_SIZE;
408 for (int i = 0; i < blockCount; i++)
410 if (i == blockCount - 1)
412 tempSize = lastDataSize;
416 tempSize = CIPHER_BLOCK_SIZE;
419 inputSize = tempSize;
421 if (tempSize % ONE_BLOCK_SIZE != 0)
423 tempSize = (tempSize / ONE_BLOCK_SIZE) * ONE_BLOCK_SIZE + ONE_BLOCK_SIZE;
426 r = input.Construct(tempSize);
427 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
429 r = input.SetArray((byte*) (pData + i * CIPHER_BLOCK_SIZE), 0, inputSize);
430 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
432 if (inputSize < tempSize)
434 unique_ptr< char[] > pFill(new (std::nothrow) char[tempSize - inputSize]);
435 SysTryCatch(NID_IO, pFill != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
437 memset(pFill.get(), 0, tempSize - inputSize);
439 r = input.SetArray(reinterpret_cast< byte* >(pFill.get()), 0, tempSize - inputSize);
440 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
446 unique_ptr< ByteBuffer > pEncryptedBuffer(DoCipherAesN(_CIPHER_INFORMATION, input, key, CIPHER_ENCRYPT));
447 SysTryCatch(NID_IO, pEncryptedBuffer != null, r = E_IO, E_IO, "[E_IO] Failed to encrypt data.");
449 pTempEncryptedData = const_cast< byte* >(pEncryptedBuffer->GetPointer());
450 SysTryCatch(NID_IO, pTempEncryptedData != null, , r, "[%s] Failed to get encrypted data.", GetErrorMessage(r));
452 pTempRealloc = (byte*) new (std::nothrow) byte[*pEncryptedDataSize + tempSize + CIPHER_BLOCK_HEADER_SIZE];
453 SysTryCatch(NID_IO, pTempRealloc != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
455 if (*ppEncryptedData)
457 memcpy(pTempRealloc, *ppEncryptedData, *pEncryptedDataSize);
459 delete[]* ppEncryptedData;
460 *ppEncryptedData = null;
463 *ppEncryptedData = pTempRealloc;
465 if (i == blockCount - 1)
468 *(*ppEncryptedData + i * (CIPHER_BLOCK_SIZE + CIPHER_BLOCK_HEADER_SIZE) + SECURE_IO_2_BYTES) \
469 = (byte) (lastDataSize & SECURE_IO_HEX_FF);
470 *(*ppEncryptedData + i * (CIPHER_BLOCK_SIZE + CIPHER_BLOCK_HEADER_SIZE) + 1) \
471 = (byte) ((lastDataSize >> SECURE_IO_8_BITS) & SECURE_IO_HEX_FF);
472 *(*ppEncryptedData + i * (CIPHER_BLOCK_SIZE + CIPHER_BLOCK_HEADER_SIZE)) \
473 = (byte) ((lastDataSize >> (SECURE_IO_8_BITS * SECURE_IO_2_BYTES)) & SECURE_IO_HEX_FF);
477 *(*ppEncryptedData + i * (CIPHER_BLOCK_SIZE + CIPHER_BLOCK_HEADER_SIZE) + SECURE_IO_2_BYTES) \
478 = (byte) (CIPHER_BLOCK_SIZE & SECURE_IO_HEX_FF);
479 *(*ppEncryptedData + i * (CIPHER_BLOCK_SIZE + CIPHER_BLOCK_HEADER_SIZE) + 1) \
480 = (byte) ((CIPHER_BLOCK_SIZE >> SECURE_IO_8_BITS) & SECURE_IO_HEX_FF);
481 *(*ppEncryptedData + i * (CIPHER_BLOCK_SIZE + CIPHER_BLOCK_HEADER_SIZE)) \
482 = (byte) ((CIPHER_BLOCK_SIZE >> (SECURE_IO_8_BITS * SECURE_IO_2_BYTES)) & SECURE_IO_HEX_FF);
485 memcpy(*ppEncryptedData + i * (CIPHER_BLOCK_SIZE + CIPHER_BLOCK_HEADER_SIZE) + CIPHER_BLOCK_HEADER_SIZE, pTempEncryptedData, tempSize);
486 *pEncryptedDataSize = *pEncryptedDataSize + tempSize + CIPHER_BLOCK_HEADER_SIZE;
493 *pEncryptedDataSize = 0;
494 delete[]* ppEncryptedData;
495 *ppEncryptedData = null;
502 _SecureIoUtil::WriteDataInCipherBlock(_NormalFile* pNormalFile, int dataPosition, bool eofSet, int dataSize, byte* pData, const ByteBuffer& key)
504 result r = E_SUCCESS;
505 int availableSize = 0;
506 int encryptedDataSize = 0;
508 long blockDataSize = 0;
509 long tempBlockDataSize = 0;
510 bool isFirstBlockSet = false;
511 byte* pBlockDataCur = null;
512 byte* pCipherBlockData = null;
513 byte* pDataPos = null;
514 byte blockHeader[CIPHER_BLOCK_HEADER_SIZE] = {0, 0, 0};
522 if (eofSet != false && dataPosition == 0)
524 r = MakeCipherBlock(pDataPos, dataSize, &pCipherBlockData, &encryptedDataSize, key);
525 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
527 unique_ptr< byte[] > pEncryptedBlockData(pCipherBlockData);
528 pCipherBlockData = null;
530 SysTryReturn(NID_IO, pEncryptedBlockData != null, r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
532 r = pNormalFile->Write(pEncryptedBlockData.get(), encryptedDataSize);
533 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
538 ret = pNormalFile->Read(blockHeader, CIPHER_BLOCK_HEADER_SIZE);
539 if (ret < CIPHER_BLOCK_HEADER_SIZE)
543 if (ret == 0 && IsEndOfFile(pNormalFile))
548 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
553 pBlockDataCur = (byte*) (&blockDataSize);
554 *pBlockDataCur = blockHeader[SECURE_IO_2_BYTES];
555 *(pBlockDataCur + 1) = blockHeader[1];
556 *(pBlockDataCur + SECURE_IO_2_BYTES) = blockHeader[0];
558 unique_ptr< byte[] > pTempEncryptedData(new (std::nothrow) byte[CIPHER_BLOCK_SIZE]);
559 SysTryReturnResult(NID_IO, pTempEncryptedData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
561 memset(pTempEncryptedData.get(), 0, CIPHER_BLOCK_SIZE);
563 if (blockDataSize % ONE_BLOCK_SIZE != 0)
565 tempBlockDataSize = blockDataSize + ONE_BLOCK_SIZE - blockDataSize % ONE_BLOCK_SIZE;
569 tempBlockDataSize = blockDataSize;
572 ret = pNormalFile->Read(pTempEncryptedData.get(), tempBlockDataSize);
573 if (ret < tempBlockDataSize)
577 if (ret == 0 && IsEndOfFile(pNormalFile))
582 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
586 r = input.Construct(tempBlockDataSize);
587 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
589 r = input.SetArray(pTempEncryptedData.get(), 0, tempBlockDataSize);
591 pTempEncryptedData.reset(null);
593 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
597 unique_ptr< ByteBuffer > pDecryptedBuffer(DoCipherAesN(_CIPHER_INFORMATION, input, key, CIPHER_DECRYPT));
598 SysTryReturn(NID_IO, pDecryptedBuffer != null, E_IO, E_IO, "[E_IO] Failed to encrypt data.");
600 unique_ptr< byte[] > pDecryptedBlockData(new (std::nothrow) byte[tempBlockDataSize]);
601 SysTryReturnResult(NID_IO, pDecryptedBlockData != null, E_OUT_OF_MEMORY, "The memory is insufficient");
603 r = pDecryptedBuffer->GetArray(reinterpret_cast< byte* >(pDecryptedBlockData.get()), 0, tempBlockDataSize);
604 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to get decrypted buffer.", GetErrorMessage(r));
606 pDecryptedBuffer->Flip();
608 pDecryptedBuffer.reset(null);
610 if (isFirstBlockSet == false)
612 availableSize = CIPHER_BLOCK_SIZE - dataPosition;
613 if (availableSize >= dataSize)
615 if (tempBlockDataSize < dataPosition + dataSize)
617 unique_ptr< byte[] > pTempRealloc(new (std::nothrow) byte[dataPosition + dataSize]);
618 SysTryReturnResult(NID_IO, pTempRealloc != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
620 memcpy(pTempRealloc.get(), pDecryptedBlockData.get(), tempBlockDataSize);
622 pDecryptedBlockData.reset(null);
623 pDecryptedBlockData = move(pTempRealloc);
626 memcpy(reinterpret_cast< byte* >(pDecryptedBlockData.get()) + dataPosition, pDataPos, dataSize);
628 if (blockDataSize < dataPosition + dataSize)
630 r = MakeCipherBlock(pDecryptedBlockData.get(), dataPosition + dataSize, &pCipherBlockData, &encryptedDataSize, key);
634 r = MakeCipherBlock(pDecryptedBlockData.get(), blockDataSize, &pCipherBlockData, &encryptedDataSize, key);
637 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
639 unique_ptr< byte[] > pEncryptedBlockData(pCipherBlockData);
640 pCipherBlockData = null;
642 SysTryReturn(NID_IO, pEncryptedBlockData != null, r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
644 r = pNormalFile->Seek(FILESEEKPOSITION_CURRENT, -(tempBlockDataSize + CIPHER_BLOCK_HEADER_SIZE));
645 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
647 r = pNormalFile->Write(pEncryptedBlockData.get(), encryptedDataSize);
648 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to write in file.", GetErrorMessage(r));
653 else //(availableSize < dataSize)
655 if (tempBlockDataSize < dataPosition + availableSize)
657 unique_ptr< byte[] > pTempRelloc(new (std::nothrow) byte[dataPosition + availableSize]);
658 SysTryReturnResult(NID_IO, pTempRelloc != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
660 memcpy(pTempRelloc.get(), pDecryptedBlockData.get(), tempBlockDataSize);
662 pDecryptedBlockData.reset(null);
663 pDecryptedBlockData = move(pTempRelloc);
666 memcpy(reinterpret_cast< byte* >(pDecryptedBlockData.get()) + dataPosition, pDataPos, availableSize);
668 pDataPos = pDataPos + availableSize;
669 dataSize = dataSize - availableSize;
671 r = MakeCipherBlock(pDecryptedBlockData.get(), CIPHER_BLOCK_SIZE, &pCipherBlockData, &encryptedDataSize, key);
672 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
674 unique_ptr< byte[] > pEncryptedBlockData(pCipherBlockData);
675 pCipherBlockData = null;
676 SysTryReturn(NID_IO, pEncryptedBlockData != null, r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
678 r = pNormalFile->Seek(FILESEEKPOSITION_CURRENT, -(tempBlockDataSize + CIPHER_BLOCK_HEADER_SIZE));
679 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
681 r = pNormalFile->Write(pEncryptedBlockData.get(), encryptedDataSize);
682 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to write in file.", GetErrorMessage(r));
688 isFirstBlockSet = true;
692 availableSize = CIPHER_BLOCK_SIZE;
693 if (availableSize >= dataSize)
695 if (tempBlockDataSize < dataSize)
697 unique_ptr< byte[] > pTempRelloc(new (std::nothrow) byte[dataSize]);
698 SysTryReturnResult(NID_IO, pTempRelloc != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
700 memcpy(pTempRelloc.get(), pDecryptedBlockData.get(), tempBlockDataSize);
702 pDecryptedBlockData.reset(null);
703 pDecryptedBlockData = move(pTempRelloc);
706 memcpy(pDecryptedBlockData.get(), pDataPos, dataSize);
708 if (blockDataSize < dataPosition + dataSize)
710 r = MakeCipherBlock(pDecryptedBlockData.get(), dataSize, &pCipherBlockData, &encryptedDataSize, key);
714 r = MakeCipherBlock(pDecryptedBlockData.get(), blockDataSize, &pCipherBlockData, &encryptedDataSize, key);
717 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
719 unique_ptr< byte[] > pEncryptedBlockData(pCipherBlockData);
720 pCipherBlockData = null;
722 SysTryReturn(NID_IO, pEncryptedBlockData != null, r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
724 r = pNormalFile->Seek(FILESEEKPOSITION_CURRENT, -(tempBlockDataSize + CIPHER_BLOCK_HEADER_SIZE));
725 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
727 r = pNormalFile->Write(pEncryptedBlockData.get(), encryptedDataSize);
728 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to write in file.", GetErrorMessage(r));
732 else //availableSize < dataSize
734 if (tempBlockDataSize < availableSize)
736 unique_ptr< byte[] > pTempRelloc(new (std::nothrow) byte[availableSize]);
737 SysTryReturnResult(NID_IO, pTempRelloc != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
739 memcpy(pTempRelloc.get(), pDecryptedBlockData.get(), tempBlockDataSize);
741 pDecryptedBlockData.reset(null);
742 pDecryptedBlockData = move(pTempRelloc);
745 memcpy(pDecryptedBlockData.get(), pDataPos, availableSize);
747 pDataPos = pDataPos + availableSize;
748 dataSize = dataSize - availableSize;
750 r = MakeCipherBlock(pDecryptedBlockData.get(), CIPHER_BLOCK_SIZE, &pCipherBlockData, &encryptedDataSize, key);
751 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
753 unique_ptr< byte[] > pEncryptedBlockData(pCipherBlockData);
754 pCipherBlockData = null;
756 SysTryReturn(NID_IO, pEncryptedBlockData != null, r, r, "[%s] Failed to make cipher block for file.", GetErrorMessage(r));
758 r = pNormalFile->Seek(FILESEEKPOSITION_CURRENT, -(tempBlockDataSize + CIPHER_BLOCK_HEADER_SIZE));
759 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
761 r = pNormalFile->Write(pEncryptedBlockData.get(), encryptedDataSize);
762 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to write in file.", GetErrorMessage(r));
769 eofSet = IsEndOfFile(pNormalFile);
776 _SecureIoUtil::ReadDataFromCipherBlock(_NormalFile* pNormalFile, int dataPosition, int bufferSize, byte* pData, _DataFormat dataMode, int* readItems, const ByteBuffer& key)
778 result r = E_SUCCESS;
781 int terminationCur = 0;
785 long blockDataSize = 0;
786 long tempBlockDataSize = 0;
787 byte* pBlockDataCur = null;
788 byte* pDecryptedBlockData = null;
789 byte blockHeader[CIPHER_BLOCK_HEADER_SIZE] = {0, 0, 0};
793 ret = pNormalFile->Read(blockHeader, CIPHER_BLOCK_HEADER_SIZE);
794 if (ret < CIPHER_BLOCK_HEADER_SIZE)
798 if (ret == 0 && IsEndOfFile(pNormalFile))
803 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
808 pBlockDataCur = (byte*) (&blockDataSize);
809 *pBlockDataCur = blockHeader[SECURE_IO_2_BYTES];
810 *(pBlockDataCur + 1) = blockHeader[1];
811 *(pBlockDataCur + SECURE_IO_2_BYTES) = blockHeader[0];
813 unique_ptr< byte[] > pBlockData(new (std::nothrow) byte[CIPHER_BLOCK_SIZE]);
814 SysTryReturnResult(NID_IO, pBlockData != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
816 memset(pBlockData.get(), 0, CIPHER_BLOCK_SIZE);
818 if (blockDataSize % ONE_BLOCK_SIZE != 0)
820 tempBlockDataSize = blockDataSize + ONE_BLOCK_SIZE - blockDataSize % ONE_BLOCK_SIZE;
824 tempBlockDataSize = blockDataSize;
827 ret = pNormalFile->Read(pBlockData.get(), tempBlockDataSize);
828 if (ret < tempBlockDataSize)
832 if (ret == 0 && IsEndOfFile(pNormalFile))
837 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
841 readingSize = blockDataSize - dataPosition;
842 SysTryReturn(NID_IO, readingSize != 0, r = E_END_OF_FILE, r, "[%s] Failed to get positive value of readingSize.", GetErrorMessage(r));
844 r = input.Construct(tempBlockDataSize);
845 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
847 r = input.SetArray(pBlockData.get(), 0, tempBlockDataSize);
848 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
852 unique_ptr< ByteBuffer > pDecryptedBuffer(DoCipherAesN(_CIPHER_INFORMATION, input, key, CIPHER_DECRYPT));
853 SysTryReturn(NID_IO, pDecryptedBuffer != null, E_IO, E_IO, "[E_IO] Failed to encrypt data.");
855 pDecryptedBlockData = const_cast< byte* >(pDecryptedBuffer->GetPointer());
856 SysTryReturn(NID_IO, pDecryptedBlockData, r, r, "[%s] Failed to get encrypted data.", GetErrorMessage(r));
858 unique_ptr< byte[] > pTemp(new (std::nothrow) byte[readingSize]);
859 SysTryReturnResult(NID_IO, pTemp != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
861 memcpy(pTemp.get(), pDecryptedBlockData + dataPosition, readingSize);
863 pDecryptedBuffer.reset(null);
867 eofSet = IsEndOfFile(pNormalFile);
869 if (dataMode == DATA_FORMAT_BYTE && (readingSize >= bufferSize))
871 memcpy(pData, pTemp.get(), bufferSize);
872 *readItems = bufferSize;
875 else if (dataMode == DATA_FORMAT_STRING && ((readingSize >= bufferSize) || DetectNewLine(pTemp.get(), readingSize, tempSize, &terminationCur, &newLineSize)))
877 if (terminationCur != 0)
879 memcpy(pData, pTemp.get(), terminationCur);
880 *(pData + terminationCur) = '\n';
881 *readItems = terminationCur + newLineSize;
885 memcpy(pData, pTemp.get(), bufferSize);
886 *readItems = bufferSize;
890 else if (eofSet && (readingSize < bufferSize))
892 memcpy(pData, pTemp.get(), readingSize);
893 *readItems = readingSize;
898 ret = pNormalFile->Read(blockHeader, CIPHER_BLOCK_HEADER_SIZE);
899 if (ret < CIPHER_BLOCK_HEADER_SIZE)
903 if (ret == 0 && IsEndOfFile(pNormalFile))
909 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
914 pBlockDataCur = (byte*) (&blockDataSize);
915 *pBlockDataCur = blockHeader[SECURE_IO_2_BYTES];
916 *(pBlockDataCur + 1) = blockHeader[1];
917 *(pBlockDataCur + SECURE_IO_2_BYTES) = blockHeader[0];
919 memset(pBlockData.get(), 0, CIPHER_BLOCK_SIZE);
921 if (blockDataSize % ONE_BLOCK_SIZE != 0)
923 tempBlockDataSize = blockDataSize + ONE_BLOCK_SIZE - blockDataSize % ONE_BLOCK_SIZE;
927 tempBlockDataSize = blockDataSize;
930 ret = pNormalFile->Read(pBlockData.get(), tempBlockDataSize);
931 if (ret < tempBlockDataSize)
935 if (ret == 0 && IsEndOfFile(pNormalFile))
940 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
944 r = input.Construct(tempBlockDataSize);
945 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
947 r = input.SetArray(pBlockData.get(), 0, tempBlockDataSize);
948 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
952 unique_ptr< ByteBuffer > pDecryptedNextBuffer(DoCipherAesN(_CIPHER_INFORMATION, input, key, CIPHER_DECRYPT));
953 SysTryReturn(NID_IO, pDecryptedNextBuffer != null, E_IO, E_IO, "[E_IO] Failed to encrypt data.");
955 pDecryptedBlockData = const_cast< byte* >(pDecryptedNextBuffer->GetPointer());
956 SysTryReturn(NID_IO, pDecryptedBlockData != null, r, r, "[%s] Failed to get encrypted data.", GetErrorMessage(r));
958 tempSize = readingSize;
959 readingSize = readingSize + blockDataSize;
961 unique_ptr< byte[] > pTempRealloc(new (std::nothrow) byte[readingSize]);
962 SysTryReturnResult(NID_IO, pTempRealloc != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
964 memcpy(pTempRealloc.get(), pTemp.get(), tempSize);
967 pTemp = move(pTempRealloc);
969 memcpy(reinterpret_cast< byte* >(pTemp.get()) + tempSize, pDecryptedBlockData, blockDataSize);
978 _SecureIoUtil::GetDataLengh(_NormalFile* pNormalFile, long int* dataLength)
980 result r = E_SUCCESS;
982 long blockDataSize = 0;
983 byte* pBlockDataCur = null;
984 byte secureHeader[SECURE_FILE_HEADER_SIZE_V1];
985 byte blockHeader[CIPHER_BLOCK_HEADER_SIZE] = {0, 0, 0};
990 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, 0);
991 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
993 ret = pNormalFile->Read(secureHeader, SECURE_FILE_HEADER_SIZE_V1);
994 if (ret < SECURE_FILE_HEADER_SIZE_V1)
998 if (ret == 0 && IsEndOfFile(pNormalFile))
1003 SysLog(NID_IO, "[%s] Failed to seek in file", GetErrorMessage(r));
1008 ret = pNormalFile->Read(blockHeader, CIPHER_BLOCK_HEADER_SIZE);
1009 if (ret < CIPHER_BLOCK_HEADER_SIZE)
1011 r = GetLastResult();
1013 if (ret == 0 && IsEndOfFile(pNormalFile))
1019 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1024 pBlockDataCur = (byte*) (&blockDataSize);
1025 *pBlockDataCur = blockHeader[SECURE_IO_2_BYTES];
1026 *(pBlockDataCur + 1) = blockHeader[1];
1027 *(pBlockDataCur + SECURE_IO_2_BYTES) = blockHeader[0];
1028 *dataLength = *dataLength + blockDataSize;
1030 if (blockDataSize % ONE_BLOCK_SIZE != 0)
1032 r = pNormalFile->Seek(FILESEEKPOSITION_CURRENT, blockDataSize + ONE_BLOCK_SIZE - blockDataSize % ONE_BLOCK_SIZE);
1036 r = pNormalFile->Seek(FILESEEKPOSITION_CURRENT, blockDataSize);
1039 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1043 return pNormalFile->Seek(FILESEEKPOSITION_END, 0);
1047 _SecureIoUtil::CheckSecureFileHeader(const Tizen::Base::String& filePath, const ByteBuffer* pSecretKey)
1049 result r = E_SUCCESS;
1051 _NormalFile normalFile;
1052 byte secureFileHeader[SECURE_FILE_HEADER_SIZE_V1];
1053 byte reservedValue[SECURE_IO_STATIC_BIN_LEN] = {0xCA, 0xFE, 0xBE, 0xBE, 0xDA, 0xEF, 0xEB, 0xEB};
1057 r = normalFile.Construct(filePath, "r");
1058 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to call constuct.", GetErrorMessage(r));
1060 ret = normalFile.Read(secureFileHeader, SECURE_FILE_HEADER_SIZE_V1);
1061 if (ret < SECURE_FILE_HEADER_SIZE_V1)
1063 r = GetLastResult();
1064 if (IsEndOfFile(&normalFile))
1066 if (ret && pSecretKey)
1068 return E_INVALID_ARG;
1078 //check if the file is encrypted file. (in normal and secure mode)
1079 ret = memcmp(secureFileHeader, SECURE_FILE_HEADER_STRING, SECURE_FILE_HEADER_STRING_SIZE);
1080 ret |= memcmp(secureFileHeader + SECURE_FILE_HEADER_STRING_SIZE, reservedValue, SECURE_IO_STATIC_BIN_LEN);
1082 SysTryReturnResult(NID_IO, !(ret && pSecretKey), E_INVALID_ARG, "Failed to match secure header in file.");
1088 _SecureIoUtil::SetRegistryFlag(void* pFileSecure, _FlagState flag)
1090 result r = E_SUCCESS;
1093 byte currentFlag = 0;
1094 _FileImpl* pFile = (_FileImpl*) pFileSecure;
1097 current = pFile->Tell();
1098 SysTryReturn(NID_IO, current != -1, GetLastResult(), GetLastResult(), "[%s] Tell file has failed.", GetErrorMessage(GetLastResult()));
1100 r = pFile->Seek(FILESEEKPOSITION_BEGIN, SECURE_REG_HEADER_STRING_SIZE + SECURE_IO_STATIC_BIN_LEN);
1101 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1103 readItems = pFile->Read(¤tFlag, 1);
1104 SysTryReturn(NID_IO, readItems == 1, r = E_INVALID_STATE, E_INVALID_STATE, "[E_INVALID_STATE] Tell file has failed.");
1106 if (flag == FLAG_STATE_CVF)
1108 currentFlag = currentFlag | SECURE_IO_2_BYTES;
1109 currentFlag = currentFlag & SECURE_IO_HEX_FE;
1111 else if (flag == FLAG_STATE_CTF)
1113 currentFlag = currentFlag | 1;
1114 currentFlag = currentFlag & SECURE_IO_HEX_FD;
1116 else if (flag == FLAG_STATE_CVF_CTF)
1118 currentFlag = currentFlag | SECURE_IO_3_BYTES;
1120 else if (flag == FLAG_STATE_NONE)
1122 currentFlag = currentFlag & SECURE_IO_HEX_FC;
1126 return E_INVALID_STATE;
1129 r = pFile->Seek(FILESEEKPOSITION_BEGIN, SECURE_REG_HEADER_STRING_SIZE + SECURE_IO_STATIC_BIN_LEN);
1130 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1132 r = pFile->Write(¤tFlag, 1);
1133 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to write data in file.", GetErrorMessage(r));
1135 r = pFile->Seek(FILESEEKPOSITION_BEGIN, current);
1136 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1142 _SecureIoUtil::CheckRegistryFlag(byte* pHeader)
1145 byte flag = *(pHeader + SECURE_REG_HEADER_STRING_SIZE + SECURE_IO_STATIC_BIN_LEN);
1147 if (((flag | SECURE_IO_HEX_FD) == SECURE_IO_HEX_FF) && ((flag | SECURE_IO_HEX_FE) != SECURE_IO_HEX_FF))
1149 return FLAG_STATE_CVF;
1151 else if (((flag | SECURE_IO_HEX_FD) != SECURE_IO_HEX_FF) && ((flag | SECURE_IO_HEX_FE) == SECURE_IO_HEX_FF))
1153 return FLAG_STATE_CTF;
1155 else if ((flag | SECURE_IO_HEX_FC) == SECURE_IO_HEX_FF)
1157 return FLAG_STATE_CVF_CTF;
1161 return FLAG_STATE_NONE;
1166 _SecureIoUtil::MakeRegistryReplica(const String fileName, byte* pNameIndex)
1168 result r = E_SUCCESS;
1170 int nameIndex = *pNameIndex;
1175 SysTryReturn(NID_IO, nameIndex <= SECURE_IO_HEX_64, replicaName, E_INVALID_ARG, "[E_INVALID_ARG] Index number is too big.");
1179 replicaName = fileName + L".rpa ";
1180 if (!File::IsFileExist(replicaName))
1187 replicaName = fileName + L"_";
1188 replicaName.Append((int) nameIndex);
1189 replicaName.Append(L".rpa");
1191 if (!File::IsFileExist(replicaName))
1200 *pNameIndex = nameIndex;
1202 if (_FileImpl::IsFileExist(fileName))
1204 r = _FileImpl::Copy(fileName, replicaName, true);
1205 SysTryReturn(NID_IO, !IsFailed(r), replicaName, r, "[%s] Failed to copy file.", GetErrorMessage(r));
1209 // open registry replica file
1211 r = fileImpl.Construct(replicaName, "w", false, null);
1212 SysTryReturn(NID_IO, !IsFailed(r), replicaName, r, "[%s] Failed to create empty file.", GetErrorMessage(r));
1220 _SecureIoUtil::DeleteRegistryReplica(byte* pHeader, const String fileName)
1222 result r = E_SUCCESS;
1224 byte nameIndex = *(pHeader + SECURE_REG_HEADER_STRING_SIZE + SECURE_IO_9_BYTES);
1228 SysTryReturnResult(NID_IO, nameIndex <= SECURE_IO_HEX_64, E_INVALID_ARG, "Index number is too big.");
1232 replicaName = fileName + L".rpa ";
1236 replicaName = fileName + L"_";
1237 replicaName.Append((int) nameIndex);
1238 replicaName.Append(L".rpa");
1241 _FileImpl::Remove(replicaName);
1247 _SecureIoUtil::LoadRegistryReplica(byte** pHeader, int* pHeaderLen, const String fileName)
1249 result r = E_SUCCESS;
1252 char indexStr[MAX_BLOCK_INDEX_LEN];
1255 _FileImpl replicaFile;
1258 memset(indexStr, 0, MAX_BLOCK_INDEX_LEN);
1259 nameIndex = *(*pHeader + SECURE_REG_HEADER_STRING_SIZE + SECURE_IO_9_BYTES);
1265 SysTryReturnResult(NID_IO, nameIndex <= SECURE_IO_HEX_64, E_INVALID_ARG, "Index number is too big.");
1269 replicaName = fileName + L".rpa ";
1273 replicaName = fileName + L"_";
1274 replicaName.Append((int) nameIndex);
1275 replicaName.Append(L".rpa");
1278 if (Tizen::Io::_FileImpl::IsFileExist(replicaName))
1280 r = replicaFile.Construct(replicaName, "r", false, null);
1281 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Constructing the file has failed.", GetErrorMessage(r));
1283 r = replicaFile.Seek(FILESEEKPOSITION_END, 0);
1284 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1286 length = replicaFile.Tell();
1289 *pHeader = new (std::nothrow) byte[length];
1290 SysTryCatch(NID_IO, *pHeader != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1291 *pHeaderLen = length;
1293 r = replicaFile.Seek(FILESEEKPOSITION_BEGIN, 0);
1294 SysTryCatch(NID_IO, !IsFailed(r), , r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1296 readItems = replicaFile.Read(*pHeader, length);
1297 SysTryCatch(NID_IO, readItems >= length, r = GetLastResult(), GetLastResult(), "[%s] Failed to read data from file.", GetErrorMessage(GetLastResult()));
1314 _SecureIoUtil::CheckSecureRegistryHeader(byte** pHeader, int* pHeaderLen, const String strFilePathPath, bool secureMode)
1316 result r = E_SUCCESS;
1320 _FlagState flag = FLAG_STATE_NONE;
1321 byte reservedValue[SECURE_IO_STATIC_BIN_LEN] = {0xCA, 0xFE, 0xBE, 0xBE, 0xDA, 0xEF, 0xEB, 0xEB};
1322 byte* pSecureRegistryHeader = *pHeader;
1325 //check if the registry is encrypted . (in normal and secure mode)
1326 ret = memcmp(pSecureRegistryHeader, SECURE_REG_HEADER_STRING, SECURE_REG_HEADER_STRING_SIZE);
1327 ret |= memcmp(pSecureRegistryHeader + SECURE_REG_HEADER_STRING_SIZE, reservedValue, SECURE_IO_STATIC_BIN_LEN);
1328 SysTryReturnResult(NID_IO, !(ret && secureMode == true), E_INVALID_ARG, "Failed to match secure header in file.");
1332 flag = CheckRegistryFlag(pSecureRegistryHeader);
1334 if (_FileImpl::IsFileExist(strFilePathPath) && !_SecureIoUtil::IsEmpty(strFilePathPath))
1336 r = fileImpl.Construct(strFilePathPath, "a+", false, null);
1337 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Constructing the file has failed.", GetErrorMessage(r));
1339 if (flag == FLAG_STATE_CVF_CTF)
1341 r = DeleteRegistryReplica(pSecureRegistryHeader, strFilePathPath);
1342 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to delete registry replica.", GetErrorMessage(r));
1344 r = SetRegistryFlag(&fileImpl, FLAG_STATE_CVF);
1345 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to set registry flags.", GetErrorMessage(r));
1348 else if (flag == FLAG_STATE_CTF)
1350 r = LoadRegistryReplica(pHeader, &readLen, strFilePathPath);
1351 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to load registry replica.", GetErrorMessage(r));
1353 SysTryReturn(NID_IO, *pHeader, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Failed to read secure registry file header.");
1355 pSecureRegistryHeader = *pHeader;
1356 *pHeaderLen = readLen;
1358 r = DeleteRegistryReplica(pSecureRegistryHeader, strFilePathPath);
1359 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to delete registry replica.", GetErrorMessage(r));
1361 r = SetRegistryFlag(&fileImpl, FLAG_STATE_CVF);
1362 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to set registry flags.", GetErrorMessage(r));
1364 else if (flag == FLAG_STATE_CVF)
1370 r = E_INVALID_STATE;
1379 _SecureIoUtil::CheckFlag(_NormalFile* pNormalFile, _FlagState* pFlag)
1381 result r = E_SUCCESS;
1386 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, SECURE_FILE_HEADER_STRING_SIZE + SECURE_IO_STATIC_BIN_LEN);
1391 ret = pNormalFile->Read(&flag, SECURE_IO_FLAG_SIZE);
1395 r = GetLastResult();
1396 SysTryReturnResult(NID_IO, !(ret == 0 && r == E_END_OF_FILE), E_INVALID_ARG, "Failed to read secure file header.");
1398 r = GetLastResult();
1400 if (ret == 0 && IsEndOfFile(pNormalFile))
1405 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1410 if (((flag | SECURE_IO_HEX_FD) == SECURE_IO_HEX_FF) && ((flag | SECURE_IO_HEX_FE) != SECURE_IO_HEX_FF))
1412 *pFlag = FLAG_STATE_CVF;
1414 else if (((flag | SECURE_IO_HEX_FD) != SECURE_IO_HEX_FF) && ((flag | SECURE_IO_HEX_FE) == SECURE_IO_HEX_FF))
1416 *pFlag = FLAG_STATE_CTF;
1418 else if ((flag | SECURE_IO_HEX_FC) == SECURE_IO_HEX_FF)
1420 *pFlag = FLAG_STATE_CVF_CTF;
1424 *pFlag = FLAG_STATE_NONE;
1431 _SecureIoUtil::SetFlag(_NormalFile* pNormalFile, _FlagState flag)
1433 result r = E_SUCCESS;
1436 byte currentFlag = 0;
1439 current = pNormalFile->Tell();
1442 r = GetLastResult();
1443 SysLog(NID_IO, "[%s] Failed to tell in file", GetErrorMessage(r));
1447 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, SECURE_FILE_HEADER_STRING_SIZE + SECURE_IO_STATIC_BIN_LEN);
1448 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file", GetErrorMessage(r));
1450 readItems = pNormalFile->Read(¤tFlag, SECURE_IO_FLAG_SIZE);
1453 r = GetLastResult();
1455 if (readItems == 0 && IsEndOfFile(pNormalFile))
1460 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1464 if (flag == FLAG_STATE_CVF)
1466 currentFlag = currentFlag | SECURE_IO_2_BYTES;
1467 currentFlag = currentFlag & SECURE_IO_HEX_FE;
1469 else if (flag == FLAG_STATE_CTF)
1471 currentFlag = currentFlag | 1;
1472 currentFlag = currentFlag & SECURE_IO_HEX_FD;
1474 else if (flag == FLAG_STATE_CVF_CTF)
1476 currentFlag = currentFlag | SECURE_IO_3_BYTES;
1478 else if (flag == FLAG_STATE_NONE)
1480 currentFlag = currentFlag & SECURE_IO_HEX_FC;
1484 SysAssert(false); // should not happen!
1486 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, SECURE_FILE_HEADER_STRING_SIZE + SECURE_IO_STATIC_BIN_LEN);
1487 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file", GetErrorMessage(r));
1489 r = pNormalFile->Write(¤tFlag, SECURE_IO_FLAG_SIZE);
1490 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to write in file", GetErrorMessage(r));
1492 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, current);
1493 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file", GetErrorMessage(r));
1499 _SecureIoUtil::DeleteBlockReplica(_NormalFile* pNormalFile, const String filePath)
1501 result r = E_SUCCESS;
1507 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, SECURE_FILE_HEADER_STRING_SIZE + SECURE_IO_9_BYTES);
1508 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file", GetErrorMessage(r));
1510 readItems = pNormalFile->Read(&nameIndex, SECURE_IO_INDEX_SIZE);
1513 r = GetLastResult();
1515 if (readItems == 0 && IsEndOfFile(pNormalFile))
1520 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1524 SysTryReturnResult(NID_IO, nameIndex <= SECURE_IO_HEX_64, E_INVALID_ARG, "Index number is too big.");
1528 replicaName = filePath + L".rpa ";
1532 replicaName = filePath + L"_";
1533 replicaName.Append((int) nameIndex);
1534 replicaName.Append(L".rpa");
1537 _FileImpl::Remove(replicaName);
1543 _SecureIoUtil::RestoreCorruptBlock(_NormalFile* pNormalFile, String fileName)
1545 result r = E_SUCCESS;
1546 String blockName = null;
1548 byte blockHeader[CIPHER_BLOCK_HEADER_SIZE] = {0, 0, 0};
1549 byte* pBlockDataCur = null;
1550 _NormalFile replicaFile;
1551 int startBlockNum = 0;
1557 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, SECURE_FILE_HEADER_STRING_SIZE + SECURE_IO_9_BYTES);
1558 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file", GetErrorMessage(r));
1560 readItems = pNormalFile->Read(&nameIndex, SECURE_IO_INDEX_SIZE); // read index of temporary block name
1561 if (readItems < SECURE_IO_INDEX_SIZE)
1563 r = GetLastResult();
1565 if (readItems == 0 && IsEndOfFile(pNormalFile))
1570 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1574 readItems = pNormalFile->Read(&dataLen, SECURE_IO_LOF_SIZE);
1575 if (readItems < SECURE_IO_LOF_SIZE)
1577 r = GetLastResult();
1579 if (readItems == 0 && IsEndOfFile(pNormalFile))
1584 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1588 SysTryReturnResult(NID_IO, nameIndex < SECURE_IO_HEX_64, E_INVALID_ARG, "Index number is too big.");
1592 blockName = fileName + L".rpa ";
1596 blockName = fileName + L"_";
1597 blockName.Append((int) nameIndex);
1598 blockName.Append(L".rpa");
1601 if (_FileUtil::IsFileExist(blockName))
1603 r = replicaFile.Construct(blockName, "r");
1604 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to conctruct replica file.", GetErrorMessage(r));
1606 readItems = replicaFile.Read(blockHeader, CIPHER_BLOCK_HEADER_SIZE); //Read start block number (3bytes)
1607 if (readItems < CIPHER_BLOCK_HEADER_SIZE)
1609 r = GetLastResult();
1611 if (readItems == 0 && IsEndOfFile(pNormalFile))
1616 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1620 pBlockDataCur = (byte*) (&startBlockNum);
1621 *pBlockDataCur = blockHeader[SECURE_IO_2_BYTES];
1622 *(pBlockDataCur + 1) = blockHeader[1];
1623 *(pBlockDataCur + SECURE_IO_2_BYTES) = blockHeader[0];
1625 r = replicaFile.Seek(FILESEEKPOSITION_END, 0);
1626 fileLength = replicaFile.Tell();
1627 SysTryReturn(NID_IO, fileLength != -1, GetLastResult(), GetLastResult(), "[%s] Failed to tell in file.", GetErrorMessage(GetLastResult()));
1629 r = replicaFile.Seek(FILESEEKPOSITION_BEGIN, CIPHER_BLOCK_HEADER_SIZE);
1630 unique_ptr< byte[] > pBlock(new (std::nothrow) byte[fileLength - CIPHER_BLOCK_HEADER_SIZE]);
1631 SysTryReturnResult(NID_IO, pBlock != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1633 memset(pBlock.get(), 0, fileLength - CIPHER_BLOCK_HEADER_SIZE);
1635 readItems = replicaFile.Read(pBlock.get(), fileLength - CIPHER_BLOCK_HEADER_SIZE);
1636 if (readItems < (fileLength - CIPHER_BLOCK_HEADER_SIZE))
1638 r = GetLastResult();
1640 if (readItems == 0 && IsEndOfFile(pNormalFile))
1645 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1649 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, SECURE_FILE_HEADER_SIZE_V1 + (CIPHER_BLOCK_SIZE + CIPHER_BLOCK_HEADER_SIZE) * startBlockNum);
1651 r = pNormalFile->Write(pBlock.get(), fileLength - CIPHER_BLOCK_HEADER_SIZE);
1652 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to write in file.", GetErrorMessage(r));
1656 r = pNormalFile->Truncate(dataLen);
1657 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to truncate in file.", GetErrorMessage(r));
1663 _SecureIoUtil::MakeCipherBlockReplica(_NormalFile* pNormalFile, const String filePath, int dataPosition, int dataSize)
1665 result r = E_SUCCESS;
1666 int totalDataSize = 0;
1670 int startBlockNum = 0;
1672 long blockDataSize = 0;
1673 bool isEndOfBlock = false;
1675 byte* pBlockDataCur = null;
1676 byte blockHeader[CIPHER_BLOCK_HEADER_SIZE] = {0, 0, 0};
1678 _NormalFile tempBlockFile;
1680 totalDataSize = dataPosition + dataSize;
1683 headPos = pNormalFile->Tell();
1684 SysTryReturn(NID_IO, headPos != -1, GetLastResult(), GetLastResult(), "[%s] Failed to tell in file.", GetErrorMessage(GetLastResult()));
1686 ret = pNormalFile->Read(blockHeader, CIPHER_BLOCK_HEADER_SIZE);
1687 if (ret < CIPHER_BLOCK_HEADER_SIZE)
1689 r = GetLastResult();
1691 if (ret == 0 && IsEndOfFile(pNormalFile))
1696 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1703 pBlockDataCur = (byte*) (&blockDataSize);
1704 *pBlockDataCur = blockHeader[SECURE_IO_2_BYTES];
1705 *(pBlockDataCur + 1) = blockHeader[1];
1706 *(pBlockDataCur + SECURE_IO_2_BYTES) = blockHeader[0];
1707 blockSize = blockSize + blockDataSize;
1709 if (blockSize >= totalDataSize)
1711 isEndOfBlock = true;
1714 if (blockDataSize % ONE_BLOCK_SIZE != 0)
1716 r = pNormalFile->Seek(FILESEEKPOSITION_CURRENT, blockDataSize + ONE_BLOCK_SIZE - blockDataSize % ONE_BLOCK_SIZE);
1720 r = pNormalFile->Seek(FILESEEKPOSITION_CURRENT, blockDataSize);
1723 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1730 if (IsEndOfFile(pNormalFile))
1735 ret = pNormalFile->Read(blockHeader, CIPHER_BLOCK_HEADER_SIZE);
1736 if (ret < CIPHER_BLOCK_HEADER_SIZE)
1738 r = GetLastResult();
1740 if (ret == 0 && IsEndOfFile(pNormalFile))
1745 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1751 tailPos = pNormalFile->Tell();
1752 SysTryReturn(NID_IO, tailPos != -1, GetLastResult(), GetLastResult(), "[%s] Failed to tell in file.", GetErrorMessage(GetLastResult()));
1754 startBlockNum = (headPos - SECURE_FILE_HEADER_SIZE_V1) / (CIPHER_BLOCK_SIZE + CIPHER_BLOCK_HEADER_SIZE);
1756 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, headPos);
1757 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1759 unique_ptr< byte[] > pBlockReplica(new (std::nothrow) byte[tailPos - headPos + CIPHER_BLOCK_HEADER_SIZE]);
1760 SysTryReturnResult(NID_IO, pBlockReplica != null, E_OUT_OF_MEMORY, "The memory is insufficient.");
1762 *(reinterpret_cast< byte* >(pBlockReplica.get()) + SECURE_IO_2_BYTES) = (byte) (startBlockNum & SECURE_IO_HEX_FF);
1763 *(reinterpret_cast< byte* >(pBlockReplica.get()) + 1) = (byte) ((startBlockNum >> SECURE_IO_8_BITS) & SECURE_IO_HEX_FF);
1764 *(reinterpret_cast< byte* >(pBlockReplica.get())) = (byte) ((startBlockNum >> (SECURE_IO_8_BITS * SECURE_IO_2_BYTES)) & SECURE_IO_HEX_FF);
1766 ret = pNormalFile->Read(reinterpret_cast< byte* >(pBlockReplica.get()) + CIPHER_BLOCK_HEADER_SIZE, tailPos - headPos);
1767 if (ret < (tailPos - headPos))
1769 r = GetLastResult();
1771 if (ret == 0 && IsEndOfFile(pNormalFile))
1776 SysLog(NID_IO, "[%s] Failed to read data from file.", GetErrorMessage(r));
1782 SysTryReturnResult(NID_IO, nameIndex <= SECURE_IO_HEX_64, E_INVALID_ARG, "Index number is too big.");
1786 replicaName = filePath + L".rpa ";
1787 if (!File::IsFileExist(replicaName))
1794 replicaName = filePath + L"_";
1795 replicaName.Append((int) nameIndex);
1796 replicaName.Append(L".rpa");
1798 if (!File::IsFileExist(replicaName))
1806 r = tempBlockFile.Construct(replicaName, "w");
1807 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to call construct.", GetErrorMessage(r));
1809 r = tempBlockFile.Write(pBlockReplica.get(), tailPos - headPos + CIPHER_BLOCK_HEADER_SIZE);
1810 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to write in file.", GetErrorMessage(r));
1812 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, 25);
1813 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1815 r = pNormalFile->Write(&nameIndex, 1);
1816 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to write in file.", GetErrorMessage(r));
1818 r = pNormalFile->Seek(FILESEEKPOSITION_BEGIN, headPos);
1819 SysTryReturn(NID_IO, !IsFailed(r), r, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1825 _SecureIoUtil::IsNormalDatabase(_DatabaseMode mode)
1828 if (mode == DATABASE_MODE_NORMAL)
1836 _SecureIoUtil::IsSecureDatabase(_DatabaseMode mode)
1839 if (mode == DATABASE_MODE_SECURE)
1847 _SecureIoUtil::GetDatabaseMode(char* pStrPhysicalDbPath)
1849 result r = E_SUCCESS;
1851 unsigned long readBytes = 0;
1852 char buffer[SQLITE_HEADER_SIZE] = {0, };
1853 char magicNum1[SECURE_IO_MAGIC_NUMBER_SIZE] = {0xCA, 0xFE, 0xBE, 0xBE};
1854 char magicNum2[SECURE_IO_MAGIC_NUMBER_SIZE] = {0xDA, 0xEF, 0xEF, 0xEB};
1855 _NormalFile normalFile;
1859 r = normalFile.Construct(pStrPhysicalDbPath, "r");
1860 SysTryReturn(NID_IO, !IsFailed(r), DATABASE_MODE_ERROR, r, "[%s] Failed to open file (%s) in read mode", GetErrorMessage(r), pStrPhysicalDbPath);
1862 r = normalFile.Seek(FILESEEKPOSITION_END, 0);
1863 SysTryReturn(NID_IO, !IsFailed(r), DATABASE_MODE_ERROR, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1865 fileLength = normalFile.Tell();
1866 if (fileLength <= 0)
1868 return DATABASE_MODE_NONE;
1871 r = normalFile.Seek(FILESEEKPOSITION_BEGIN, 0);
1872 SysTryReturn(NID_IO, !IsFailed(r), DATABASE_MODE_ERROR, r, "[%s] Failed to seek in file.", GetErrorMessage(r));
1874 readBytes = normalFile.Read(buffer, SQLITE_HEADER_SIZE);
1875 r = GetLastResult();
1876 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));
1878 if (!strncmp(buffer, "SQLite format 3", SQLITE_FORMAT_STR_SIZE))
1880 return DATABASE_MODE_NORMAL;
1882 else if (!memcmp((void*) buffer, magicNum1, SECURE_IO_MAGIC_NUMBER_SIZE) && !memcmp((void*) (buffer + SECURE_IO_12_BYTES), magicNum2, SECURE_IO_MAGIC_NUMBER_SIZE))
1884 return DATABASE_MODE_SECURE;
1887 return DATABASE_MODE_ERROR;
1891 _SecureIoUtil::GenerateDatabaseKeyN(const Tizen::Base::ByteBuffer* pUserKey)
1896 byte* pDbKey = null;
1900 unique_ptr< ByteBuffer > pKeyBuffer(GetSecureKeyN(pUserKey));
1902 SysTryReturn(NID_IO, pKeyBuffer != null, null, E_INVALID_ARG, "[E_INVALID_ARG] Failed to generate secure key.");
1904 pKey = const_cast< byte* >(pKeyBuffer->GetPointer());
1906 pDbKey = new (std::nothrow) byte[DATABASE_KEY_LENGTH * SECURE_IO_2_BYTES + 1];
1907 SysTryReturn(NID_IO, pDbKey != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1909 memset(pDbKey, 0, DATABASE_KEY_LENGTH * SECURE_IO_2_BYTES + 1);
1911 for (int i = 0; i < DATABASE_KEY_LENGTH; i++)
1913 lowByte = pKey[i] & SECURE_IO_HEX_0F;
1914 highByte = (pKey[i] >> SECURE_IO_4_BITS) & SECURE_IO_HEX_0F;
1915 if (lowByte <= SECURE_IO_9_BYTES)
1917 pDbKey[i * SECURE_IO_2_BYTES + 1] = lowByte + '0';
1921 pDbKey[i * SECURE_IO_2_BYTES + 1] = lowByte - SECURE_IO_10_BYTES + 'A';
1924 if (highByte <= SECURE_IO_9_BYTES)
1926 pDbKey[i * SECURE_IO_2_BYTES] = highByte + '0';
1930 pDbKey[i * SECURE_IO_2_BYTES] = highByte - SECURE_IO_10_BYTES + 'A';
1938 _SecureIoUtil::IsEmpty(const String& filePath)
1940 result r = E_SUCCESS;
1941 FileAttributes attribute;
1944 r = File::GetAttributes(filePath, attribute);
1951 if (attribute.GetFileSize() == 0)