367 const char* szFunc =
"OTEnvelope::Decrypt";
370 (thePassword.isPassword() && (thePassword.getPasswordSize() > 0)) ||
371 (thePassword.isMemory() && (thePassword.getMemorySize() > 0)));
374 OTPassword theRawSymmetricKey;
377 theKey.GetRawKeyFromPassphrase(thePassword, theRawSymmetricKey)) {
378 otErr << szFunc <<
": Failed trying to retrieve raw symmetric key "
379 "using password. (Wrong password?)\n";
384 uint32_t nRunningTotal = 0;
386 m_dataContents.
reset();
397 uint16_t env_type_n = 0;
399 if (0 == (nRead = m_dataContents.
OTfread(
400 reinterpret_cast<uint8_t*>(&env_type_n),
401 static_cast<uint32_t>(
sizeof(env_type_n))))) {
402 otErr << szFunc <<
": Error reading Envelope Type. Expected "
403 "asymmetric(1) or symmetric (2).\n";
406 nRunningTotal += nRead;
407 OT_ASSERT(nRead == static_cast<uint32_t>(
sizeof(env_type_n)));
411 const uint16_t env_type =
412 static_cast<uint16_t
>(ntohs(static_cast<uint16_t>(env_type_n)));
417 const uint32_t l_env_type =
static_cast<uint32_t
>(env_type);
418 otErr << szFunc <<
": Error: Expected Envelope for Symmetric key (type "
419 "2) but instead found type: " << l_env_type <<
".\n";
425 const uint32_t max_iv_length =
432 uint32_t iv_size_n = 0;
434 if (0 == (nRead = m_dataContents.
OTfread(
435 reinterpret_cast<uint8_t*>(&iv_size_n),
436 static_cast<uint32_t>(
sizeof(iv_size_n))))) {
437 otErr << szFunc <<
": Error reading IV Size.\n";
440 nRunningTotal += nRead;
441 OT_ASSERT(nRead == static_cast<uint32_t>(
sizeof(iv_size_n)));
445 const uint32_t iv_size_host_order = ntohl(iv_size_n);
447 if (iv_size_host_order > max_iv_length) {
448 otErr << szFunc <<
": Error: iv_size ("
449 <<
static_cast<int64_t
>(iv_size_host_order)
450 <<
") is larger than max_iv_length ("
451 << static_cast<int64_t>(max_iv_length) <<
").\n";
459 theIV.SetPayloadSize(iv_size_host_order);
461 if (0 == (nRead = m_dataContents.
OTfread(
462 static_cast<uint8_t*>(
463 const_cast<void*>(theIV.GetPayloadPointer())),
464 static_cast<uint32_t
>(iv_size_host_order)))) {
465 otErr << szFunc <<
": Error reading initialization vector.\n";
468 nRunningTotal += nRead;
469 OT_ASSERT(nRead == static_cast<uint32_t>(iv_size_host_order));
481 static_cast<const void*>(
482 static_cast<const uint8_t*>(m_dataContents.
GetPointer()) +
484 m_dataContents.
GetSize() - nRunningTotal);
492 static_cast<const char*>(
493 theCipherText.GetPayloadPointer()),
494 theCipherText.GetSize(),
495 theIV, thePlaintext);
507 uint32_t nIndex = thePlaintext.GetSize() - 1;
508 (
static_cast<uint8_t*
>(
509 const_cast<void*
>(thePlaintext.GetPointer())))[nIndex] =
'\0';
513 theOutput.Set(static_cast<const char*>(thePlaintext.GetPointer()));
static EXPORT OTCrypto * It()
static EXPORT uint32_t SymmetricIvSize()
OTLOG_IMPORT OTLogStream otErr
const void * GetPointer() const
virtual bool Decrypt(const OTPassword &theRawSymmetricKey, const char *szInput, uint32_t lInputLength, const OTPayload &theIV, OTCrypto_Decrypt_Output theDecryptedOutput) const =0
EXPORT uint32_t OTfread(uint8_t *data, uint32_t size)