@@ -83,11 +83,9 @@ struct CDoc2Reader::Private {
8383
8484 std::vector<Lock> locks;
8585
86- std::unique_ptr<libcdoc::Crypto::Cipher> cipher;
87- std::unique_ptr<TaggedSource> tgs;
86+ std::unique_ptr<libcdoc::DecryptionSource> dec;
8887 std::unique_ptr<libcdoc::ZSource> zsrc;
8988 std::unique_ptr<libcdoc::TarSource> tar;
90-
9189};
9290
9391CDoc2Reader::~CDoc2Reader ()
@@ -389,29 +387,19 @@ CDoc2Reader::beginDecryption(const std::vector<uint8_t>& fmk)
389387 }
390388 priv->_at_nonce = false ;
391389 std::vector<uint8_t > cek = libcdoc::Crypto::expand (fmk, std::vector<uint8_t >(libcdoc::CDoc2::CEK.cbegin (), libcdoc::CDoc2::CEK.cend ()));
392- std::vector<uint8_t > nonce (libcdoc::CDoc2::NONCE_LEN);
393- if (priv->_src ->read (nonce.data (), libcdoc::CDoc2::NONCE_LEN) != libcdoc::CDoc2::NONCE_LEN) {
394- setLastError (" Error reading nonce" );
395- LOG_ERROR (" {}" , last_error);
396- return libcdoc::IO_ERROR;
397- }
398-
399390 LOG_TRACE_KEY (" cek: {}" , cek);
400- LOG_TRACE_KEY (" nonce: {}" , nonce);
401391
402- priv->cipher = std::make_unique<libcdoc::Crypto::Cipher>( EVP_chacha20_poly1305 (), cek, nonce, false );
392+ priv->dec = std::make_unique<libcdoc::DecryptionSource>(*priv-> _src , EVP_chacha20_poly1305 (), cek, libcdoc::CDoc2::NONCE_LEN );
403393 std::vector<uint8_t > aad (libcdoc::CDoc2::PAYLOAD.cbegin (), libcdoc::CDoc2::PAYLOAD.cend ());
404394 aad.insert (aad.end (), priv->header_data .cbegin (), priv->header_data .cend ());
405395 aad.insert (aad.end (), priv->headerHMAC .cbegin (), priv->headerHMAC .cend ());
406- if (! priv->cipher ->updateAAD (aad)) {
396+ if (priv->dec ->updateAAD (aad) != OK ) {
407397 setLastError (" Wrong decryption key (FMK)" );
408398 LOG_ERROR (" {}" , last_error);
409399 return libcdoc::WRONG_KEY;
410400 }
411401
412- priv->tgs = std::make_unique<TaggedSource>(priv->_src , false , 16 );
413- libcdoc::CipherSource *csrc = new libcdoc::CipherSource (priv->tgs .get (), false , priv->cipher .get ());
414- priv->zsrc = std::make_unique<libcdoc::ZSource>(csrc, true );
402+ priv->zsrc = std::make_unique<libcdoc::ZSource>(priv->dec .get (), false );
415403 priv->tar = std::make_unique<libcdoc::TarSource>(priv->zsrc .get (), false );
416404
417405 return libcdoc::OK;
@@ -455,21 +443,15 @@ CDoc2Reader::finishDecryption()
455443 LOG_WARN (" {}" , last_error);
456444 }
457445
458- LOG_TRACE_KEY (" tag: {}" , priv->tgs ->tag );
459-
460- priv->cipher ->setTag (priv->tgs ->tag );
461- if (!priv->cipher ->result ()) {
462- setLastError (" Stream tag is invalid" );
463- LOG_ERROR (" {}" , last_error);
464- return HASH_MISMATCH;
465- }
466446 setLastError ({});
467- priv->tgs .reset ();
468447 priv->zsrc .reset ();
469448 priv->tar .reset ();
470- priv->cipher ->clear ();
471- priv->cipher .reset ();
472- return OK;
449+ auto rv = priv->dec ->close ();
450+ priv->dec .reset ();
451+ if (rv != OK) {
452+ setLastError (" Crypto payload integrity check failed" );
453+ }
454+ return rv;
473455}
474456
475457CDoc2Reader::CDoc2Reader (libcdoc::DataSource *src, bool take_ownership)
0 commit comments