Skip to content

Commit 202ab0c

Browse files
committed
Add encryption algorithm and key derivation algorithm to the 7z format.
1 parent 117640f commit 202ab0c

58 files changed

Lines changed: 5120 additions & 213 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

C/7zVersion.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
#define MY_VERSION_CPU MY_VERSION
1111
#endif
1212

13-
#define MY_DATE "2026-04-27"
13+
#define MY_DATE "2026-06-12"
1414
#undef MY_COPYRIGHT
1515
#undef MY_VERSION_COPYRIGHT_DATE
16-
#define MY_AUTHOR_NAME "Igor Pavlov, Tino Reichardt, Sergey G. Brester"
16+
#define MY_AUTHOR_NAME "Igor Pavlov, Tino Reichardt, Sergey G. Brester, fzxx"
1717
#define MY_COPYRIGHT_PD "Igor Pavlov : Public domain"
18-
#define MY_COPYRIGHT_CR "Copyright (c) 1999- Igor Pavlov, 2016- Tino Reichardt, 2022- Sergey G. Brester"
18+
#define MY_COPYRIGHT_CR "Copyright (c) 1999- Igor Pavlov, 2016- Tino Reichardt, 2022- Sergey G. Brester, 2026- fzxx"
1919

2020
#ifdef USE_COPYRIGHT_CR
2121
#define MY_COPYRIGHT MY_COPYRIGHT_CR

C/7zVersion.rc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ BEGIN \
3434
BEGIN \
3535
BLOCK "040904b0" \
3636
BEGIN \
37-
VALUE "CompanyName", "Igor Pavlov, Tino Reichardt" \
37+
VALUE "CompanyName", "Igor Pavlov, Tino Reichardt, Sergey G. Brester, fzxx" \
3838
VALUE "FileDescription", descr \
3939
VALUE "FileVersion", MY_VERSION \
4040
VALUE "InternalName", intName \

CPP/7zip/7zip.mak

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ $(FASTLZMA2_OBJS): ../../../../C/fast-lzma2/$(*B).c
283283
{../../Compress}.cpp{$O}.obj::
284284
$(COMPLB)
285285
{../../Crypto}.cpp{$O}.obj::
286-
$(CCOMPLB)
286+
$(COMPLB_O2)
287287
{../../../../C}.c{$O}.obj::
288288
$(CCOMPLB)
289289
{../../../../C/brotli}.c{$O}.obj::

CPP/7zip/7zip_gcc.mak

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,16 +828,30 @@ $O/7zAes.o: ../../Crypto/7zAes.cpp
828828
$(CXX) $(CXXFLAGS) $<
829829
$O/7zAesRegister.o: ../../Crypto/7zAesRegister.cpp
830830
$(CXX) $(CXXFLAGS) $<
831+
$O/7zKeyDerivation.o: ../../Crypto/7zKeyDerivation.cpp
832+
$(CXX) $(CXXFLAGS) $<
833+
$O/Ascon.o: ../../Crypto/Ascon.cpp
834+
$(CXX) $(CXXFLAGS) $<
835+
$O/Cascade.o: ../../Crypto/Cascade.cpp
836+
$(CXX) $(CXXFLAGS) $<
837+
$O/CascadeRegister.o: ../../Crypto/CascadeRegister.cpp
838+
$(CXX) $(CXXFLAGS) $<
839+
$O/HkdfBlake2sp.o: ../../Crypto/HkdfBlake2sp.cpp
840+
$(CXX) $(CXXFLAGS) $<
831841
$O/HmacSha1.o: ../../Crypto/HmacSha1.cpp
832842
$(CXX) $(CXXFLAGS) $<
833843
$O/HmacSha256.o: ../../Crypto/HmacSha256.cpp
834844
$(CXX) $(CXXFLAGS) $<
845+
$O/HmacSha512.o: ../../Crypto/HmacSha512.cpp
846+
$(CXX) $(CXXFLAGS) $<
835847
$O/MyAes.o: ../../Crypto/MyAes.cpp
836848
$(CXX) $(CXXFLAGS) $<
837849
$O/MyAesReg.o: ../../Crypto/MyAesReg.cpp
838850
$(CXX) $(CXXFLAGS) $<
839851
$O/Pbkdf2HmacSha1.o: ../../Crypto/Pbkdf2HmacSha1.cpp
840852
$(CXX) $(CXXFLAGS) $<
853+
$O/Pbkdf2HmacSha512.o: ../../Crypto/Pbkdf2HmacSha512.cpp
854+
$(CXX) $(CXXFLAGS) $<
841855
$O/RandGen.o: ../../Crypto/RandGen.cpp
842856
$(CXX) $(CXXFLAGS) $<
843857
$O/Rar20Crypto.o: ../../Crypto/Rar20Crypto.cpp
@@ -848,6 +862,14 @@ $O/RarAes.o: ../../Crypto/RarAes.cpp
848862
$(CXX) $(CXXFLAGS) $<
849863
$O/WzAes.o: ../../Crypto/WzAes.cpp
850864
$(CXX) $(CXXFLAGS) $<
865+
$O/XChaCha20.o: ../../Crypto/XChaCha20.cpp
866+
$(CXX) $(CXXFLAGS) $<
867+
$O/XChaCha20Register.o: ../../Crypto/XChaCha20Register.cpp
868+
$(CXX) $(CXXFLAGS) $<
869+
$O/XChaCha20Poly1305.o: ../../Crypto/XChaCha20Poly1305.cpp
870+
$(CXX) $(CXXFLAGS) $<
871+
$O/XChaCha20Poly1305Register.o: ../../Crypto/XChaCha20Poly1305Register.cpp
872+
$(CXX) $(CXXFLAGS) $<
851873
$O/ZipCrypto.o: ../../Crypto/ZipCrypto.cpp
852874
$(CXX) $(CXXFLAGS) $<
853875
$O/ZipStrong.o: ../../Crypto/ZipStrong.cpp

CPP/7zip/Archive/7z/7zCompressionMode.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include "../../Common/MethodId.h"
77
#include "../../Common/MethodProps.h"
88

9+
#include "7zHeader.h"
10+
911
namespace NArchive {
1012
namespace N7z {
1113

@@ -64,6 +66,7 @@ struct CCompressionMethodMode
6466

6567
UString Password; // _Wipe
6668
UInt64 MemoryUsageLimit;
69+
CMethodId EncryptionMethodId;
6770

6871
bool IsEmpty() const { return (Methods.IsEmpty() && !PasswordIsDefined); }
6972
CCompressionMethodMode():
@@ -78,6 +81,7 @@ struct CCompressionMethodMode
7881
, NumThreadGroups(0)
7982
#endif
8083
, MemoryUsageLimit((UInt64)1 << 30)
84+
, EncryptionMethodId(k_AES)
8185
{}
8286

8387
#ifdef Z7_CPP_IS_SUPPORTED_default

CPP/7zip/Archive/7z/7zDecode.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,27 @@ HRESULT CDecoder::Decode(
584584
progress2 = new CDecProgress(compressProgress);
585585

586586
ISequentialOutStream *outStreamPointer = outStream;
587-
return _mixer->Code(inStreamPointers, &outStreamPointer,
587+
const HRESULT codeResult = _mixer->Code(inStreamPointers, &outStreamPointer,
588588
progress2 ? (ICompressProgressInfo *)progress2 : compressProgress,
589589
dataAfterEnd_Error);
590+
591+
if (codeResult == S_OK)
592+
{
593+
for (i = 0; i < folderInfo.Coders.Size(); i++)
594+
{
595+
Z7_DECL_CMyComPtr_QI_FROM(
596+
ICryptoAuthVerify,
597+
authVerify, _mixer->GetCoder(i).GetUnknown())
598+
if (authVerify)
599+
{
600+
Int32 authResult = 0;
601+
RINOK(authVerify->CryptoAuthVerify(&authResult))
602+
if (authResult != 0)
603+
return E_FAIL;
604+
}
605+
}
606+
}
607+
return codeResult;
590608
}
591609

592610
#ifdef USE_MIXER_ST

CPP/7zip/Archive/7z/7zEncode.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ HRESULT CEncoder::EncoderConstr()
562562
throw 1;
563563

564564
CMethodFull method;
565-
method.Id = k_AES;
565+
method.Id = _options.EncryptionMethodId;
566566
method.NumStreams = 1;
567567
_options.Methods.Add(method);
568568

@@ -687,7 +687,7 @@ HRESULT CEncoder::EncoderConstr()
687687
{
688688
CMethodFull method;
689689
method.NumStreams = 1;
690-
method.Id = k_AES;
690+
method.Id = _options.EncryptionMethodId;
691691
_options.Methods.Add(method);
692692

693693
NCoderMixer2::CCoderStreamsInfo cod;

CPP/7zip/Archive/7z/7zHandler.cpp

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,11 @@ bool CHandler::IsFolderEncrypted(CNum folderIndex) const
307307
for (unsigned j = 0; j < idSize; j++)
308308
id64 = ((id64 << 8) | longID[j]);
309309
inByte.SkipDataNoCheck(idSize);
310-
if (id64 == k_AES)
310+
if (id64 == k_AES
311+
|| id64 == k_XCHACHA20
312+
|| id64 == k_XCHACHA20_POLY1305
313+
|| id64 == k_AES_XCHACHA20_POLY1305
314+
|| id64 == k_AES_XCHACHA20_ASCON)
311315
return true;
312316
if ((mainByte & 0x20) != 0)
313317
inByte.SkipDataNoCheck(inByte.ReadNum());
@@ -706,6 +710,46 @@ HRESULT CHandler::ObtainBlockMethods(CNum folderIndex, PROPVARIANT *prop, CHandl
706710
ConvertUInt32ToString(numCyclesPower, s);
707711
}
708712
break;
713+
case k_XCHACHA20:
714+
if (info) continue;
715+
name = "XChaCha20";
716+
if (propsSize >= 1)
717+
{
718+
const Byte firstByte = props[0];
719+
const UInt32 numCyclesPower = firstByte & 0x3F;
720+
ConvertUInt32ToString(numCyclesPower, s);
721+
}
722+
break;
723+
case k_XCHACHA20_POLY1305:
724+
if (info) continue;
725+
name = "XChaCha20-Poly1305";
726+
if (propsSize >= 1)
727+
{
728+
const Byte firstByte = props[0];
729+
const UInt32 numCyclesPower = firstByte & 0x3F;
730+
ConvertUInt32ToString(numCyclesPower, s);
731+
}
732+
break;
733+
case k_AES_XCHACHA20_ASCON:
734+
if (info) continue;
735+
name = "AES+XChaCha20+Ascon";
736+
if (propsSize >= 1)
737+
{
738+
const Byte firstByte = props[0];
739+
const UInt32 numCyclesPower = firstByte & 0x3F;
740+
ConvertUInt32ToString(numCyclesPower, s);
741+
}
742+
break;
743+
case k_AES_XCHACHA20_POLY1305:
744+
if (info) continue;
745+
name = "AES+XChaCha20-Poly1305";
746+
if (propsSize >= 1)
747+
{
748+
const Byte firstByte = props[0];
749+
const UInt32 numCyclesPower = firstByte & 0x3F;
750+
ConvertUInt32ToString(numCyclesPower, s);
751+
}
752+
break;
709753
}
710754
}
711755

CPP/7zip/Archive/7z/7zHandler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class COutHandler: public CMultiMethodProps
6060
UInt32 _decoderCompatibilityVersion;
6161
CUIntVector _enabledFilters;
6262
CUIntVector _disabledFilters;
63+
CMethodId _encryptionMethodId;
6364

6465
void InitSolidFiles() { _numSolidFiles = (UInt64)(Int64)(-1); }
6566
void InitSolidSize() { _numSolidBytes = (UInt64)(Int64)(-1); }

CPP/7zip/Archive/7z/7zHandlerOut.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,7 @@ Z7_COM7F_IMF(CHandler::UpdateItems(ISequentialOutStream *outStream, UInt32 numIt
767767

768768
if (methodMode.PasswordIsDefined)
769769
{
770+
methodMode.EncryptionMethodId = _encryptionMethodId;
770771
if (_encryptHeadersSpecified)
771772
encryptHeaders = _encryptHeaders;
772773
#ifndef Z7_NO_CRYPTO
@@ -778,6 +779,7 @@ Z7_COM7F_IMF(CHandler::UpdateItems(ISequentialOutStream *outStream, UInt32 numIt
778779
{
779780
headerMethod.PasswordIsDefined = methodMode.PasswordIsDefined;
780781
headerMethod.Password = methodMode.Password;
782+
headerMethod.EncryptionMethodId = _encryptionMethodId;
781783
}
782784
}
783785

@@ -892,6 +894,7 @@ void COutHandler::InitProps7z()
892894
_decoderCompatibilityVersion = k_decoderCompatibilityVersion;
893895
_enabledFilters.Clear();
894896
_disabledFilters.Clear();
897+
_encryptionMethodId = k_AES;
895898
}
896899

897900
void COutHandler::InitProps()
@@ -1040,6 +1043,29 @@ HRESULT COutHandler::SetProperty(const wchar_t *nameSpec, const PROPVARIANT &val
10401043
_encryptHeadersSpecified = true;
10411044
return S_OK;
10421045
}
1046+
1047+
if (name.IsEqualTo("em"))
1048+
{
1049+
if (value.vt != VT_BSTR)
1050+
return E_INVALIDARG;
1051+
UString m = value.bstrVal;
1052+
m.RemoveChar(L'-');
1053+
m.RemoveChar(L'+');
1054+
m.MakeLower_Ascii();
1055+
if (m.IsEqualTo("aes256"))
1056+
_encryptionMethodId = k_AES;
1057+
else if (m.IsEqualTo("xchacha20"))
1058+
_encryptionMethodId = k_XCHACHA20;
1059+
else if (m.IsEqualTo("xchacha20poly1305"))
1060+
_encryptionMethodId = k_XCHACHA20_POLY1305;
1061+
else if (m.IsEqualTo("axa") || m.IsEqualTo("aesxchacha20ascon"))
1062+
_encryptionMethodId = k_AES_XCHACHA20_ASCON;
1063+
else if (m.IsEqualTo("axp") || m.IsEqualTo("aesxchacha20poly1305"))
1064+
_encryptionMethodId = k_AES_XCHACHA20_POLY1305;
1065+
else
1066+
return E_INVALIDARG;
1067+
return S_OK;
1068+
}
10431069

10441070
{
10451071
bool processed;

0 commit comments

Comments
 (0)