diff --git a/doc/crypt.tex b/doc/crypt.tex
index 8f4e53bd2..3b8b69ffa 100644
--- a/doc/crypt.tex
+++ b/doc/crypt.tex
@@ -600,7 +600,7 @@ \subsection{Simple Encryption Demonstration}
\index{Cipher descriptor table}
\index{blowfish\_desc} \index{xtea\_desc} \index{rc2\_desc} \index{rc5\_desc} \index{rc6\_desc} \index{saferp\_desc} \index{aes\_desc} \index{twofish\_desc}
\index{des\_desc} \index{des3\_desc} \index{noekeon\_desc} \index{skipjack\_desc} \index{anubis\_desc} \index{khazad\_desc} \index{kseed\_desc} \index{kasumi\_desc} \index{camellia\_desc} \index{aes\_enc\_desc}
-\index{idea\_desc} \index{serpent\_desc} \index{tea\_desc}
+\index{idea\_desc} \index{serpent\_desc} \index{tea\_desc} \index{sm4\_desc}
\begin{figure}[hpbt]
\begin{small}
\begin{center}
@@ -633,6 +633,7 @@ \subsection{Simple Encryption Demonstration}
\hline IDEA & idea\_desc & 8 & 16 & 8 & 24 \\
\hline Serpent & serpent\_desc & 16 & 16, 24, 32 & 32 & 25 \\
\hline TEA & tea\_desc & 8 & 16 & 32 & 26 \\
+ \hline SM4 & sm4\_desc & 16 & 16 & 32 & 27 \\
\hline
\end{tabular}
\end{center}
diff --git a/libtomcrypt_VS2008.vcproj b/libtomcrypt_VS2008.vcproj
index 06fe0115d..972d32ac5 100644
--- a/libtomcrypt_VS2008.vcproj
+++ b/libtomcrypt_VS2008.vcproj
@@ -395,6 +395,10 @@
RelativePath="src\ciphers\skipjack.c"
>
+
+
diff --git a/makefile.mingw b/makefile.mingw
index a4d70da14..05c06b9e3 100644
--- a/makefile.mingw
+++ b/makefile.mingw
@@ -40,14 +40,14 @@ src/ciphers/aes/aes_enc_desc.o src/ciphers/aes/aesni.o src/ciphers/anubis.o src/
src/ciphers/camellia.o src/ciphers/cast5.o src/ciphers/des.o src/ciphers/idea.o src/ciphers/kasumi.o \
src/ciphers/khazad.o src/ciphers/kseed.o src/ciphers/multi2.o src/ciphers/noekeon.o src/ciphers/rc2.o \
src/ciphers/rc5.o src/ciphers/rc6.o src/ciphers/safer/safer.o src/ciphers/safer/saferp.o \
-src/ciphers/serpent.o src/ciphers/skipjack.o src/ciphers/tea.o src/ciphers/twofish/twofish.o \
-src/ciphers/xtea.o src/encauth/ccm/ccm_add_aad.o src/encauth/ccm/ccm_add_nonce.o \
-src/encauth/ccm/ccm_done.o src/encauth/ccm/ccm_init.o src/encauth/ccm/ccm_memory.o \
-src/encauth/ccm/ccm_process.o src/encauth/ccm/ccm_reset.o src/encauth/ccm/ccm_test.o \
-src/encauth/chachapoly/chacha20poly1305_add_aad.o src/encauth/chachapoly/chacha20poly1305_decrypt.o \
-src/encauth/chachapoly/chacha20poly1305_done.o src/encauth/chachapoly/chacha20poly1305_encrypt.o \
-src/encauth/chachapoly/chacha20poly1305_init.o src/encauth/chachapoly/chacha20poly1305_memory.o \
-src/encauth/chachapoly/chacha20poly1305_setiv.o \
+src/ciphers/serpent.o src/ciphers/skipjack.o src/ciphers/sm4.o src/ciphers/tea.o \
+src/ciphers/twofish/twofish.o src/ciphers/xtea.o src/encauth/ccm/ccm_add_aad.o \
+src/encauth/ccm/ccm_add_nonce.o src/encauth/ccm/ccm_done.o src/encauth/ccm/ccm_init.o \
+src/encauth/ccm/ccm_memory.o src/encauth/ccm/ccm_process.o src/encauth/ccm/ccm_reset.o \
+src/encauth/ccm/ccm_test.o src/encauth/chachapoly/chacha20poly1305_add_aad.o \
+src/encauth/chachapoly/chacha20poly1305_decrypt.o src/encauth/chachapoly/chacha20poly1305_done.o \
+src/encauth/chachapoly/chacha20poly1305_encrypt.o src/encauth/chachapoly/chacha20poly1305_init.o \
+src/encauth/chachapoly/chacha20poly1305_memory.o src/encauth/chachapoly/chacha20poly1305_setiv.o \
src/encauth/chachapoly/chacha20poly1305_setiv_rfc7905.o \
src/encauth/chachapoly/chacha20poly1305_test.o src/encauth/eax/eax_addheader.o \
src/encauth/eax/eax_decrypt.o src/encauth/eax/eax_decrypt_verify_memory.o src/encauth/eax/eax_done.o \
diff --git a/makefile.msvc b/makefile.msvc
index 05fdc6200..145efe8af 100644
--- a/makefile.msvc
+++ b/makefile.msvc
@@ -33,14 +33,14 @@ src/ciphers/aes/aes_enc_desc.obj src/ciphers/aes/aesni.obj src/ciphers/anubis.ob
src/ciphers/camellia.obj src/ciphers/cast5.obj src/ciphers/des.obj src/ciphers/idea.obj src/ciphers/kasumi.obj \
src/ciphers/khazad.obj src/ciphers/kseed.obj src/ciphers/multi2.obj src/ciphers/noekeon.obj src/ciphers/rc2.obj \
src/ciphers/rc5.obj src/ciphers/rc6.obj src/ciphers/safer/safer.obj src/ciphers/safer/saferp.obj \
-src/ciphers/serpent.obj src/ciphers/skipjack.obj src/ciphers/tea.obj src/ciphers/twofish/twofish.obj \
-src/ciphers/xtea.obj src/encauth/ccm/ccm_add_aad.obj src/encauth/ccm/ccm_add_nonce.obj \
-src/encauth/ccm/ccm_done.obj src/encauth/ccm/ccm_init.obj src/encauth/ccm/ccm_memory.obj \
-src/encauth/ccm/ccm_process.obj src/encauth/ccm/ccm_reset.obj src/encauth/ccm/ccm_test.obj \
-src/encauth/chachapoly/chacha20poly1305_add_aad.obj src/encauth/chachapoly/chacha20poly1305_decrypt.obj \
-src/encauth/chachapoly/chacha20poly1305_done.obj src/encauth/chachapoly/chacha20poly1305_encrypt.obj \
-src/encauth/chachapoly/chacha20poly1305_init.obj src/encauth/chachapoly/chacha20poly1305_memory.obj \
-src/encauth/chachapoly/chacha20poly1305_setiv.obj \
+src/ciphers/serpent.obj src/ciphers/skipjack.obj src/ciphers/sm4.obj src/ciphers/tea.obj \
+src/ciphers/twofish/twofish.obj src/ciphers/xtea.obj src/encauth/ccm/ccm_add_aad.obj \
+src/encauth/ccm/ccm_add_nonce.obj src/encauth/ccm/ccm_done.obj src/encauth/ccm/ccm_init.obj \
+src/encauth/ccm/ccm_memory.obj src/encauth/ccm/ccm_process.obj src/encauth/ccm/ccm_reset.obj \
+src/encauth/ccm/ccm_test.obj src/encauth/chachapoly/chacha20poly1305_add_aad.obj \
+src/encauth/chachapoly/chacha20poly1305_decrypt.obj src/encauth/chachapoly/chacha20poly1305_done.obj \
+src/encauth/chachapoly/chacha20poly1305_encrypt.obj src/encauth/chachapoly/chacha20poly1305_init.obj \
+src/encauth/chachapoly/chacha20poly1305_memory.obj src/encauth/chachapoly/chacha20poly1305_setiv.obj \
src/encauth/chachapoly/chacha20poly1305_setiv_rfc7905.obj \
src/encauth/chachapoly/chacha20poly1305_test.obj src/encauth/eax/eax_addheader.obj \
src/encauth/eax/eax_decrypt.obj src/encauth/eax/eax_decrypt_verify_memory.obj src/encauth/eax/eax_done.obj \
diff --git a/makefile.unix b/makefile.unix
index 75233cd19..3a05dec7b 100644
--- a/makefile.unix
+++ b/makefile.unix
@@ -54,14 +54,14 @@ src/ciphers/aes/aes_enc_desc.o src/ciphers/aes/aesni.o src/ciphers/anubis.o src/
src/ciphers/camellia.o src/ciphers/cast5.o src/ciphers/des.o src/ciphers/idea.o src/ciphers/kasumi.o \
src/ciphers/khazad.o src/ciphers/kseed.o src/ciphers/multi2.o src/ciphers/noekeon.o src/ciphers/rc2.o \
src/ciphers/rc5.o src/ciphers/rc6.o src/ciphers/safer/safer.o src/ciphers/safer/saferp.o \
-src/ciphers/serpent.o src/ciphers/skipjack.o src/ciphers/tea.o src/ciphers/twofish/twofish.o \
-src/ciphers/xtea.o src/encauth/ccm/ccm_add_aad.o src/encauth/ccm/ccm_add_nonce.o \
-src/encauth/ccm/ccm_done.o src/encauth/ccm/ccm_init.o src/encauth/ccm/ccm_memory.o \
-src/encauth/ccm/ccm_process.o src/encauth/ccm/ccm_reset.o src/encauth/ccm/ccm_test.o \
-src/encauth/chachapoly/chacha20poly1305_add_aad.o src/encauth/chachapoly/chacha20poly1305_decrypt.o \
-src/encauth/chachapoly/chacha20poly1305_done.o src/encauth/chachapoly/chacha20poly1305_encrypt.o \
-src/encauth/chachapoly/chacha20poly1305_init.o src/encauth/chachapoly/chacha20poly1305_memory.o \
-src/encauth/chachapoly/chacha20poly1305_setiv.o \
+src/ciphers/serpent.o src/ciphers/skipjack.o src/ciphers/sm4.o src/ciphers/tea.o \
+src/ciphers/twofish/twofish.o src/ciphers/xtea.o src/encauth/ccm/ccm_add_aad.o \
+src/encauth/ccm/ccm_add_nonce.o src/encauth/ccm/ccm_done.o src/encauth/ccm/ccm_init.o \
+src/encauth/ccm/ccm_memory.o src/encauth/ccm/ccm_process.o src/encauth/ccm/ccm_reset.o \
+src/encauth/ccm/ccm_test.o src/encauth/chachapoly/chacha20poly1305_add_aad.o \
+src/encauth/chachapoly/chacha20poly1305_decrypt.o src/encauth/chachapoly/chacha20poly1305_done.o \
+src/encauth/chachapoly/chacha20poly1305_encrypt.o src/encauth/chachapoly/chacha20poly1305_init.o \
+src/encauth/chachapoly/chacha20poly1305_memory.o src/encauth/chachapoly/chacha20poly1305_setiv.o \
src/encauth/chachapoly/chacha20poly1305_setiv_rfc7905.o \
src/encauth/chachapoly/chacha20poly1305_test.o src/encauth/eax/eax_addheader.o \
src/encauth/eax/eax_decrypt.o src/encauth/eax/eax_decrypt_verify_memory.o src/encauth/eax/eax_done.o \
diff --git a/makefile_include.mk b/makefile_include.mk
index e8e77bff7..62b7f98ed 100644
--- a/makefile_include.mk
+++ b/makefile_include.mk
@@ -219,14 +219,14 @@ src/ciphers/aes/aes_enc_desc.o src/ciphers/aes/aesni.o src/ciphers/anubis.o src/
src/ciphers/camellia.o src/ciphers/cast5.o src/ciphers/des.o src/ciphers/idea.o src/ciphers/kasumi.o \
src/ciphers/khazad.o src/ciphers/kseed.o src/ciphers/multi2.o src/ciphers/noekeon.o src/ciphers/rc2.o \
src/ciphers/rc5.o src/ciphers/rc6.o src/ciphers/safer/safer.o src/ciphers/safer/saferp.o \
-src/ciphers/serpent.o src/ciphers/skipjack.o src/ciphers/tea.o src/ciphers/twofish/twofish.o \
-src/ciphers/xtea.o src/encauth/ccm/ccm_add_aad.o src/encauth/ccm/ccm_add_nonce.o \
-src/encauth/ccm/ccm_done.o src/encauth/ccm/ccm_init.o src/encauth/ccm/ccm_memory.o \
-src/encauth/ccm/ccm_process.o src/encauth/ccm/ccm_reset.o src/encauth/ccm/ccm_test.o \
-src/encauth/chachapoly/chacha20poly1305_add_aad.o src/encauth/chachapoly/chacha20poly1305_decrypt.o \
-src/encauth/chachapoly/chacha20poly1305_done.o src/encauth/chachapoly/chacha20poly1305_encrypt.o \
-src/encauth/chachapoly/chacha20poly1305_init.o src/encauth/chachapoly/chacha20poly1305_memory.o \
-src/encauth/chachapoly/chacha20poly1305_setiv.o \
+src/ciphers/serpent.o src/ciphers/skipjack.o src/ciphers/sm4.o src/ciphers/tea.o \
+src/ciphers/twofish/twofish.o src/ciphers/xtea.o src/encauth/ccm/ccm_add_aad.o \
+src/encauth/ccm/ccm_add_nonce.o src/encauth/ccm/ccm_done.o src/encauth/ccm/ccm_init.o \
+src/encauth/ccm/ccm_memory.o src/encauth/ccm/ccm_process.o src/encauth/ccm/ccm_reset.o \
+src/encauth/ccm/ccm_test.o src/encauth/chachapoly/chacha20poly1305_add_aad.o \
+src/encauth/chachapoly/chacha20poly1305_decrypt.o src/encauth/chachapoly/chacha20poly1305_done.o \
+src/encauth/chachapoly/chacha20poly1305_encrypt.o src/encauth/chachapoly/chacha20poly1305_init.o \
+src/encauth/chachapoly/chacha20poly1305_memory.o src/encauth/chachapoly/chacha20poly1305_setiv.o \
src/encauth/chachapoly/chacha20poly1305_setiv_rfc7905.o \
src/encauth/chachapoly/chacha20poly1305_test.o src/encauth/eax/eax_addheader.o \
src/encauth/eax/eax_decrypt.o src/encauth/eax/eax_decrypt_verify_memory.o src/encauth/eax/eax_done.o \
diff --git a/notes/ccm_tv.txt b/notes/ccm_tv.txt
index 0795ed737..d945418ec 100644
--- a/notes/ccm_tv.txt
+++ b/notes/ccm_tv.txt
@@ -934,6 +934,239 @@ CCM-twofish (16 byte key)
31: 8D9551BAE42EA3A9CB4B8DEF9EC6A2AEA276440F09B4C543391B4DDA8997E9, 16098A7768FE2FDDFDF01544D099067E
32: 4D6E76A82DF819DF2DF4456FDCDA14984545794FCBF06EFA88600106604FF08E, F97EA56E5B15216DFF482CE10586C5AB
+CCM-sm4 (16 byte key)
+ 0: , 9DE0073E
+ 1: B8, 26AE5CCB
+ 2: 8C55, CF16B2F9
+ 3: 1FED2A, E20E80C9
+ 4: 9CF59EB3, 4AAE5880
+ 5: 59A80116CC, 38791290
+ 6: 8B89FF192419, 1C22C22C
+ 7: D0F160F1C454C2, 35F043F7
+ 8: 80878955618023DF, 3A551A6C
+ 9: 397F5AE0B4D2E01610, B35ED606
+ 10: 3D850083F79CB67A2018, AD88B5C7
+ 11: E484C523B184F66B0B8DF8, 554D7316
+ 12: 6E69B9FD418730645CA04E4D, 3B092AD2
+ 13: A27450AD338C433891D5A8C6C4, 939D72EC
+ 14: 6B8178513B2D99B1FF1CC627A2EB, 229C80E3
+ 15: C3AC2E2D6D690D8767605340282E9C, C835D4A0
+ 16: 93BBF3BC6806FF2CDD7D4E68F0D8FDBD, BD3D142F
+ 17: E5372D579254497DA22EFD3E7F4CEC8ABF, 2E3B6638
+ 18: 2579A8482271968B03FEA9DE18B65B8237CE, 87C8545F
+ 19: 9C7390BFDCB1B835E96210C7FDFB81CFC1887D, 58EE4A58
+ 20: BB1F3D8E62B12EB2DE9890364A09FA92DCC1A64E, CFB0B151
+ 21: A6030E17DB36B913FE4B7136DDCFCBCEBDA100B3C8, 27CDDA62
+ 22: 20369D0B93F987EDAF09A46BD3AF6265AAB6E9B4BD80, 23FD7F6B
+ 23: 50CB9D31B5B71A09B564C5772CAF59AB52C0D84A781A08, 800DCDAB
+ 24: E3C2EFCF4B006F590E76CD77363936494824B2806FC3EF70, 87C33C4A
+ 25: 4630B52ABBA519365687FF7C5252CCF5C6EF18E5AA61BB822D, CC4759B3
+ 26: E05EF860AD2BE26D87A93FCD85B5334C4321E1ECED6C71A33D37, E8AB5D70
+ 27: 47779C08DC8FA0D201C071A485EE44455B8DF73F1132ED6A206DCA, 20263898
+ 28: EA7FCAD1B339C7135CB58362C6EEEF000AA7B5BA2762F6EE813F72DD, 9C9AED3C
+ 29: 063FED97EB431D7C19D7F339FAE7267C27E3F55DECE3EB5AD7A981410D, B5212375
+ 30: 48E554D486A1B361172A64ECD6EA5696324C1882D7316D94566CA5A1A399, D922A8FC
+ 31: 06578AABC9632CAFB841008DC77612EBA75A243C2503216BCCE92DE1C13919, D88181A5
+ 32: 02B1CAA088824295D01E5216E2FE971ED29B84641D4C16C74F1527B33D0C8D52, A77CD6A3
+ 0: , ACDEB856A6FD
+ 1: 73, 17E13EBCE582
+ 2: 2F17, 006590D026A8
+ 3: 1313B1, 1160E91A3E1D
+ 4: 7B7AC513, BD3EF1E2BF17
+ 5: 6F6087C433, C4140A7D3CC6
+ 6: 0A1DF4EBFDB7, 6B61DD16A1C6
+ 7: D9A2356F720A6A, 74A94A601026
+ 8: 134A88A0B9299FC9, 5B2658D55ABA
+ 9: BA794509B25656C8D8, 481E5C1B566E
+ 10: 0833914DE8384D84CAFF, 03C3A0414406
+ 11: 6992EBB19599CCB63D7771, 0CA98975D74F
+ 12: 4AE8E307DBFBB54769C168CB, 667C0AEDA4D7
+ 13: C59738801EAF354F265FBE8DB3, 3F0EBD22328B
+ 14: 747A836D146C0E9F562A905B4275, 8F44258FA17E
+ 15: 2BFC297DF959F9FA1E315D2EB9303F, 77484AD59BD4
+ 16: 666E880AE4745F6A7E09952E1A844646, D59CF07C04C6
+ 17: D75DEFBD766597C0B22769030ACA20BFB8, 19F0082D1B8E
+ 18: 5EAD7FE1BF8513A6B76A213B4C68F8B7CCF1, FF8AC185008B
+ 19: 0BFFCC92DF19EA96814DAF62CA173F9CB34920, 7B74C69B11A7
+ 20: 31D7F5D10D1240813591EC0A71E8C5EC1D80C862, F70D5B53E0A8
+ 21: 1B39C0D592786AF65A29C8C28ABB6CCE26DF3E1136, A5B1CF84FAD9
+ 22: CBC4D4099BAA07EBF0464E39BDC5F33B833C5B6687EC, A92F17822284
+ 23: 6E0D9CF0B092DD98213E1373DEB01152D2E39A9E442D32, 55CE8103178D
+ 24: C8E3757F3443D51D237506576B900DEC3F1C9DF4F6F2BFCD, 6E632E464634
+ 25: 7324756247E38DBF2C503DB5346D1D171B5ED1201A75703E5B, DF680399AD08
+ 26: B5DD81796BDB3A6F9FA453D74572D998370EE4965AE59EE33429, 8AB45E71A43A
+ 27: 69328028EBCB8DD3CACB57893CC9920E0F35585E702AD14D583F78, 355081C62A6D
+ 28: DE4C771DB9E43A1C2AF9DC07D1788A140439EC5872FF105C342D8012, 539B190D84A7
+ 29: EF9B50471FF34CEBF83E93E3E5C0991E40C1785CA95401A6D792BA6B7B, 666CE3AA7FCD
+ 30: DCB27EDCFDD8667F55EF139F6CFFC5774A3CA75C6311225AF9F370E00822, 4D81D680C170
+ 31: 1C299CAC6B0D751BEE11EC4A186119BEABBB795CA84ED8B13253B3900853E4, 0D47DB1BEBE6
+ 32: 909D843A79DBA34D8F635139E4B9E5ECAE0F495EB25CBD66047A012E80A65587, 8C32551FD9D6
+ 0: , BEE3EEE22EE2A55E
+ 1: 12, 44557C1AF1A758B1
+ 2: 3CE2, A99B5A1D657033F8
+ 3: 7B7001, 3DC00127349A98A8
+ 4: 96165A62, 2B02ADF4CA7AB517
+ 5: 3A324BD309, 2E2AE02C292614F5
+ 6: 4ED0038C172E, B5B0FCB0CA89A35D
+ 7: 84C71158F3542B, 8BA5DB23A5B0FC72
+ 8: C274F79526C5FF3E, ACA0573547F1710A
+ 9: 47EC55A42C1DE04026, A487BD1A941A6BF8
+ 10: 79DDC67F4A5220F4CFFC, 224481A7C09E33DD
+ 11: 344908D7E55955774948AD, 4C74FE86F590BFCB
+ 12: 4604D7EE2FAD87CAF475D69A, 4FC362BB71E181F8
+ 13: 61AE670F2A821F5112D21CE19E, C689AEF825CAE8A1
+ 14: 9BD8ABE63A36939A1129B24B0D98, 242A8452BEBF70A5
+ 15: C1D4E4A7807A9C3465B4ACA6A7828A, 9784F18AD5B142A6
+ 16: 46B362B021621D28878023F1954D61F4, DDD1E12CB161F895
+ 17: 07845C27395A4E39AE4046D7F32CAFAB5B, 515940B36269025A
+ 18: 85721C20C286E5119BBE05E6D7A8773BF85D, 74C8750DCD3DBBA6
+ 19: BCAAB338910D75938D73C5BFAEDA318B444A0C, 2F625A963F1C78DC
+ 20: 94C7F915BC71605032071B51B0B5D107EF0694A1, 11CC5372D55C3E1C
+ 21: 806B3241C41542015C717EFC4304AA3E9A31D22D70, 6E6B375960B7645F
+ 22: 4A844251A1EB8F4A88421F7B08A7B9C8A5877DA4E2B5, AA876E54B8D7DD2A
+ 23: 9BBC9937F5719D3F8AD064E52C4F99E8B94EB6B8062DA8, 41957205E61ED9C1
+ 24: 1E610E7A49CF2097D4E4C52FF590C6F8FFF040E511CF69C8, C64ED3D01CDDE865
+ 25: B69C3F8E6F37B429E6952D00B43C5C7EBAC4577E5EC964A748, DFE2FDE592BBE35D
+ 26: 01961B6BCFF2B36562D2840674F66D4F54AFB124AA44AA4B19CC, B16D03C5BAEBE7C1
+ 27: 8CE8AF13CB3F91BF6F6818212A4AC6D1FA58180EABE9ABC7065F2A, ECA14DF0C15D4319
+ 28: 35D13257040C47EC49C7BF107F12590E058A3E16A6E177D72B27ABD0, 3EE3D2655BBC7447
+ 29: CD2B5DDF69230B00BBD6936980BA840FA9DC8ADB02DEDD25964EF9ED81, E9651B13894480FB
+ 30: CA0628E27ED6F6329ADD496B3D7B3A3BCA821104AAA3517C8640A48AC637, BA31774B56A4D8E7
+ 31: 2B14F14B3465A2637914B9595F4C30800CF731DBBB49E489F180C7994792B1, 9E030D5C37F5EF72
+ 32: CC28680779E04FA635C85A9701479869D15BB0D7C22FF59DB16CC6BD3849D0BE, 4D530BDAD9FD7828
+ 0: , 87C915256D009358DA0A
+ 1: D8, 9D4BC0A5B4DAF2397D95
+ 2: 0681, B4A045ACCD28732C7DD1
+ 3: DB43E7, 925A0887972BEF435D72
+ 4: 242CE90B, E6C965DE0E67CA11F2DD
+ 5: 3B7207AB15, 9ED9E7F329504EA4201E
+ 6: 36F791EC76E3, 4F922572513A1B532C43
+ 7: 8E2E5E5082C552, E77900DBBEBAA2D58982
+ 8: BFA9B851CECC9B33, D83AEB7906EA956E0965
+ 9: F726F0C80040C75231, F4BC200E2176707DA369
+ 10: 58F8567C189506BED75D, A914BB656B23ADFAE716
+ 11: 9A02A02C0ABD36732C7F07, 47AA449F45C49A0AE7CB
+ 12: F406B33115B7AA96CF3E563E, 578F96884B963930FA36
+ 13: 5FE47D69B6040C72560D9183AB, 79BE6DF768901F2BA925
+ 14: 60B5C1D99425A42E5949CEF0814A, FDE07202E1615E22942D
+ 15: 5324B9C4DD22826616D7328D9233FA, 1D204A8B90295811E1CA
+ 16: 51184989D72F1302C1FB991EE6887817, 3305CE14FA023AD15F0B
+ 17: 4B9ABA7891B67C60970DAFB965F82FD7F9, 375F0F2A373857DA8784
+ 18: 2D0E7790E2928423676FE339640EC6F1645C, 52F38C4BA18B33CFA417
+ 19: 960979B677EFA83DC37462A99568568C6D0EA8, D8F5C1E46FA747D3F0A9
+ 20: 162BAB7007593791A61E984DCA8E834172A3AFE0, 948B9100707451A24435
+ 21: 1EDA21A2F5CCD0A28556FD2CD59512D4585400B213, C6A326BCCE20BD96D1F8
+ 22: 150CE606AD0A0B279436938C67F09793446CFE973879, 1A2E3CA922D6A124F148
+ 23: 6BC1CCE08F9C09928322F812F41158BA4E48B7AC527E71, D01CEEE7D731BB7F8CB9
+ 24: 3D39F0B881AFC34914F62A1CB9EF9272071D7431C2D81E9B, 184FCB41E469269AB3B4
+ 25: 288679F9722495F7C694BBBC9837842D24656485529609D4BE, 891AC9B5415BF8775306
+ 26: D730CAA7687F30236E1B348D81A5BF6EB6A1285A63BDD0F1DEB4, 4E97DDDD7320DACA3C03
+ 27: 24E7D22AD0F632A3C1353432B8B57E98C088E5B889295F08C53EAA, 4331A522634F068A4564
+ 28: E0E58358FA24EF2771C06A35DE473D5F79C9DF5D0AB499054F1B4790, E33E5F11B23EE775CFDA
+ 29: B54EA1A35A42BD5909B2FAE801D8DCD85563AA390B383AD0E645338C23, 2223C284FBF7A6B5A7DF
+ 30: 81A164632FC1EDF2A7A8C512519866924FAD66711D6A1C925FB93F5B5B60, 396C53043B935D44C352
+ 31: 458E489C98B5CECF75E3DE4DD33CE9AFBF681F62053B4023EAD8CFC90A6018, 73AB8757298589A6AD67
+ 32: CD0BD066423441F124139E32AA52A50F3B15088CB9005312B60B5F47EB63F5DD, 62079ED0C9E0BA36D92B
+ 0: , CF62DE403E79B5AB43FAC7F6
+ 1: 3B, 3F9AED80CF5D1071AB4D9C5E
+ 2: 9272, EE73A9A91942717B32759114
+ 3: 9A1FE8, 2596D176EAE1D6BA887A22E8
+ 4: B772A5EE, 7F1D066F9A01906845E9209D
+ 5: 7781B0038A, 2BCE3F46C9574D197C38BE12
+ 6: 5FFD92A0FE9A, F8F05573CB15CC2E1CF0D87D
+ 7: 573182E5D2927E, 81E0F13418DA7856D6678EE7
+ 8: FC41211CF3154991, 4BC3EE8D1C263A879AB5E659
+ 9: 8F0355557F9F80165F, 00A4C5A76047D493455341A5
+ 10: 385645C4A5CC20247D5B, 400F7087730FC5C33419901E
+ 11: B81E867B737F63FDC27777, C27179A0B02F9B1FD807DF84
+ 12: 031545C1EA3A5AEB901BBEFA, 15DAD4065AB33691896A2B4F
+ 13: 419F4D096BB47707F07556D2E4, F707A6BC800FDFCFEE8C32A6
+ 14: AD494900556254DA08B582C04996, 846708CE1ABF7C80A886C9B4
+ 15: C95FBCBECA2118C433983BEA1E83FA, 9EC202665FF3C2B179C68EE4
+ 16: BCFDDFF1EE978C7B9F9FD1D6EB297AC5, 2CFBABD0F96E9A9BDBAB1D6C
+ 17: CF10E304AEF362B82C388B75411A6E361D, BB956F1B446A383344C31B3A
+ 18: A8C1F766C4C06E28B30AF1554D8E9BA21537, 0D5CCCCC1221ABA77F43A30F
+ 19: F3011F265C3CA57ABB67DD94E771C2790DD1EA, 36E56D33B5701D96DF46AA29
+ 20: 388486FF5B2564B1BE195BB91136388A84C7BE48, 196A661BB11D6D87E0377579
+ 21: 7127EC3B0D472460AF53D99E74C779C67E1770E69F, 38D31353A1C1BC3C04CD78F7
+ 22: EDB78B1E7F0B71DE82DE74E0ECB76FE8EF178E45862A, 3D4640CE45D1C1C6924BC609
+ 23: 709D4A6DFCB45366A4C503B16455E781D6F7167A44F9E9, 932844F41765DFA3EDDF8690
+ 24: 47FBA51D0941BE170D819B5569E2B49C6A1C6A5559E73080, D24EAE2FC037E19729EA7E1C
+ 25: 1736FA5524866BB54CC1DE0C41089BE0DF3779228BE74B3A7D, C718E4597AD9FBF2817EF301
+ 26: 1553C4DC88CF2C3D3EC1FDA42EDDB6DFA75A416B7B44B54BB933, A7ED3B1B5B6FB1D2758B2B3C
+ 27: C26D2A8A1BCD139A0569DAFB3045ECFC2E9B6585737A0DE1087FC6, 3A25E2A19168749453CB4B98
+ 28: 4AF40789632C758ABBA8FBA77C9AE2C78D56604909C1FE09E6FCD577, D272480526D82F63B82994E0
+ 29: 4B0C45CEB96F62E59B9CB3C23CF69D4AFE0AB3D7C900C1B81E4D6FE9DA, 4802C98DFB029B02CEECF731
+ 30: BD184296B38AAFB94B804CF3EBF350ADF7B5834B84BA5088241E73B69AFB, 65F56C1CF714ACF98C7B462C
+ 31: 042D5B4B4E1CA64076E6799E20D8C46C0E9FAFCCDAB60945A6772E1434D049, 2A48F71F00241662E87B8630
+ 32: A988536CABD504F0E03B2213AAF5888D5A00F1496EB37554C9CE47FBE4ACFC46, 8CE02A013587AE392DA4FB7A
+ 0: , 58F3B5346B3E6068A50DB36F837A
+ 1: 6C, A8668D8AF4F3BBEC5EDD233A1569
+ 2: 6CF8, 733D8FC62F38C28C2EA2A131DEF0
+ 3: 937CB9, B160441DC5C918C5E3F63997D6DE
+ 4: E438823B, 89B59F1C427F5305C8334EA1A03D
+ 5: 9CA0F63874, DB22616963019352286D277ABBF9
+ 6: 963FDA7E1A13, 7F549A59DDD6CC5FDA410F6BED9E
+ 7: 50344F2705F32F, EB5356A08E13924B5FBB65BD7925
+ 8: D54BC508FC984EEC, 2008F3C7E02DF9E74BA228D9D721
+ 9: E1A5E6600637B980AE, CEE8F6FBBE4B188F2AB1736F27CF
+ 10: D652A99F07A5C8ACA8A2, F80E9DB8B9347A13433520349B52
+ 11: 02A75BF0FC871885301C5D, 105539E6557337A79AC48D26ECEC
+ 12: 459FD0B4CAA48D4A528134F0, 46AEE4AFF16BC84B61A44BA89324
+ 13: 1C87BF3F3A90B2607BDB21570C, 0C8615AA3A6399860492A0F96783
+ 14: 2FC41996D186B82FEA1CB22B3DCF, 4B4C5F2A43F08E002070B67A8B68
+ 15: A7EF466976FF3652AC47B77396E6F7, 5DA6C7969D0BDC85EF7F2DCC0265
+ 16: 23537BD187392038501282749E76B4F4, 6E4C8A9858C819C50CC5ED640D8E
+ 17: 4C6FB5685F9E7DD67777B72249C57108AE, 14FBB70EE65187C5C7700FB07513
+ 18: 36FFA05A049F063FD621FD6CEDA3D699DDC0, 9ED06859B2C8F0464D994D114414
+ 19: EA09FB9D6D7D54CE463878F32E58720E7BF372, F6707A9E51842BB9FBC16E00F79B
+ 20: 38C7EF1C536475500E68C1C15D0F4C3CAB3B9DA1, BA4262E05148A36C94ECD6F3EE63
+ 21: 790EC38F53185EC7218869B4E3548478C43783F677, 7929FC58F07D543E5F2AABFAFC13
+ 22: AB34561C134A21A2E95309D2B1073A0002F7B6AB9959, D05245562F35019CFF78901BCB56
+ 23: 1D0BDB251089665A618CF8C1CD1FF316B3CAF88EA82140, F08C427F5E1EB7B22EA903C50AE5
+ 24: D009C870EB96966DFEA6C8A6F0E967A67E6DD6DDFC234EB1, F731E3CC444ABAA64E4656BE6EB5
+ 25: AA094590980BDAD62D922C1B43877C0D3E84B069A9DADB7143, 9F439F4A61855D597B499FEE936E
+ 26: 8844BBA2622598E3D28CA1C0895B00100342C2C205EF7BF48CF4, 9B9060B7CAE58663102F92D3F0CA
+ 27: 2AB9997E52237A818543600E8D28981EDCA301F7B263D89B2340C5, 8D22429BAF36C953B22FFB1D8CBB
+ 28: 30938BFA06A873EA493019AF0580A0D1F6D4F98312E8244FCFB88257, 687C12D7F8A9D9EE1410DDBEA412
+ 29: FAA295E33F11BEEBB803C1162748A25EE853E43D522CBECE89F0A9047C, FB99DD821D834B97103BD47A8DAE
+ 30: 31D93C1B2CB7AE8435004F3014B74B9E2E4FF88F96FDDD57830938F343F9, 72E080592A91FBF2EF7A12B736A9
+ 31: 94546FC14CD055CF10227C80AAAE6CD9138DC9C11720018915CA3E9E3616BC, 19630100135CBD2B41BC291684ED
+ 32: B8750749821BD2363917D1B9E3511BEFDC2A5E39C7F546698BF181EF439E9831, 618453F748AC9DA6F82753C2DD6F
+ 0: , 8CA58750FDD25071B46924C71059DCB9
+ 1: C1, 181BCCC4799A6AB959E819916A6363CA
+ 2: BE59, 64987A1A5F6E6D83F1D0259518898A93
+ 3: 45E54B, A4AB464E7E0FE2E19602DE9AFB6C0AB6
+ 4: 24437595, E303EB65856926571257D741354397A2
+ 5: EE61D1A46B, 3B36A5A81B91099A68A8C46236206288
+ 6: 6CF23F79CCCE, 41B92B476E7A456B940975D29A097AE8
+ 7: FEAC00A6114717, D155DACFD093BD52C94A946EFE896BA9
+ 8: A350E85BC70A816D, D0E027B25E460C52C2D635FAF5AA5FC5
+ 9: 56C0665D2925CE9F60, 597EFD6721F3825DC6B19F94B29DE914
+ 10: BBBA873737D2CAE039B2, 78B43F96FE5111EF177DDBA1E84A3D79
+ 11: 1F2932542214C30EDA6844, 59220481B8B1227E7CD230BF64C3C648
+ 12: 7329C72BB418174E3918CFF2, F115702A552639A2FEA689D739ECB3B9
+ 13: 94912D3DF1A76C7BE75DBA863A, 96EB73928DF46058B814A9BBFD61B148
+ 14: C75C4E841A4A6D5B44E99597C480, 0BB54B8712F123E0D70A4E0C253A3264
+ 15: 9FF78207B8909102D43EC2B1C5993B, 8C6A5A78E40AB8E3006E13AF8E5A3BB3
+ 16: E86FB463F0DB162ED88719AEA7D33FF3, A1F1FF57A7B4288CEE76918A7E731022
+ 17: 0E61E39BDBC12869402512C22BF760B3C4, A5C2B3CE910ACF3826ED509C29AFC906
+ 18: 6BFBEE8C30DCF9FC22AFD78F91A1A218F222, 8DE1CCEF80E96CF1C173352C4375E3F2
+ 19: CBFD8B945CCF2375C3D01127B7E1369FD9E200, 7B2F8FD9D3EC65479EBC480ABF8FC964
+ 20: AC64C5281038E9DF5F5290FB7EBFED8E293A5198, 94A5631658378A77B371AB06BF7F5337
+ 21: F84059C2EFDCBCA6285466EF46F546E7015CBD1694, 9B7C50BD8778F473320975EC1E9E2263
+ 22: 5F63187580F0768FA35C7F322263B3995AB8B8D485D9, 0723E2F953395E3E8A9D9A89B8B6C5D7
+ 23: 0F9A6BB13A8FC04BC3BBAFC9CBDB04F31449585ADFF3A5, 83AABB2F5A1C1ABD773786916442907B
+ 24: BBF7EAD3FC21703A0B3BCDB32F6B1967410F8B2532B10814, 23FA924B90BC5B78997BA13BA39E563B
+ 25: 2A9AD9B9CFD89490E7CD19E1E560CC9DF41679BC20023BB48E, A3894A21C77F4E3C70B55A47A0298674
+ 26: 4874868B08D7F1C222A972CF5450BAF885ACC0077DB709C5D2C2, EE1FB7C8894B8ED063C7C4122D3798B2
+ 27: 038488350F72615EC351C31E31AE037E1873022BE54FCA4E9E2F96, 00B24A6A1F2D3AF85A27678C6CCA554E
+ 28: 3D8D75C9257956A333F0D4F11F6A9DCB5EBDBD5070C0C65741116011, 6CF3234E89802C68B991BB1E50F81101
+ 29: 04C3AA265DC2B803D1D4881C50720C0BC016EA8E562C5AEB868B90DF14, EC5BF0A1415944A95C601497D4D50E1B
+ 30: 12AD8315A168D9BF80CD2F638E89AF90BDBB501C9ADFABEDE91A631CED87, 8294E2134FF6CD07027A22D7C12BE98A
+ 31: D57F978D50F158BEDACC97CD2F44B0C55E942AA12CDAA26AB0AEDB073BD8FF, 1EAE230E3A3E612ED6DC2066AD267CCD
+ 32: DFE759429208E69F0FC900276599E8E0F5E6D7DE90A18F567578056A93B835EA, EFD4105743BD5F5DC5DB5B66B05C66D8
+
CCM-noekeon (16 byte key)
0: , 35B7EA4F
1: 00, A39582A6
diff --git a/notes/cipher_tv.txt b/notes/cipher_tv.txt
index ff74dd46e..32fa8827f 100644
--- a/notes/cipher_tv.txt
+++ b/notes/cipher_tv.txt
@@ -1593,6 +1593,60 @@ Key Size: 24 bytes
49: B8AC2CDFF7AC22C1
+Cipher: sm4
+Key Size: 16 bytes
+ 0: 8F78763EE06013E0B7622C428FD0528D
+ 1: 5E0842E918473FE01EF6CB46F3D9A898
+ 2: A5A8568531E5BB075EEF3A2BAC3D2280
+ 3: 3C6C1F35C42230AE7ACB4503261A3FCC
+ 4: 714B6E7A1DBAFADF65AB68AD6ACB14D8
+ 5: C26139494681B05164A69A9A4AB0006D
+ 6: 4526798759C412D836DD99FCCC98B8E4
+ 7: 84D4EE025FEE4943B29B28D3A387E0FB
+ 8: 7709BE3897FAB7925C1BC3B43D700D7B
+ 9: 84DF23C156674EE9673F8F2A7BBAFF30
+10: 4E301AFE87590D01996CEE78BA72C34D
+11: BEA5DE3EFDE04FF8AEC7CFEFCBB6CF0F
+12: 3778443468868DF0459A00101B05D2D2
+13: 3C6A235913B3A2DDB1E168664620F21A
+14: 325A985761CD7B0B1CEEE21911536959
+15: 0B5B467CA3EEE26247942C1FDE22FBBE
+16: F8A8E7DC773E1FF0405195D76E4439F2
+17: 827A2BFFAF23FBEDE87A85C859147520
+18: F9D07586783913EE3D9E51C01E811E57
+19: BBD82643D3D72FB4F386456421AB752B
+20: 12334FE869D290A540F3E53D02B540AA
+21: A518C3E509EA25E7AD1880158859298D
+22: D32C013B04A2CCDD70DAA58224702753
+23: C6DC68393042703BA07A1DEBF6EDCE2F
+24: DA3B6853232452691596B8A3763EAAA6
+25: DD7AA423F5A5B712A727F33880139E52
+26: 84A3AF0CA812C8DECED5823BA7F0F1B0
+27: 61A552BCDB0CB7E950340A1691202A41
+28: 3DD9F13B5863086666A8F9B7F1E4C103
+29: B1D1FE27DF20443711C54088E8FFC29C
+30: 8CC70503998EC42AA4CE5B7D9194B8B8
+31: 18E85C128D2D4A5CA90E9D3A590D0789
+32: C4206BCBD76E67C6DA73C3C603138FB4
+33: 350BB41380979C56152D05586FE9BDF5
+34: 980B929CB48C93BEF12E1A1252131A05
+35: 5FD08A04C4568BBA0FCFD688F577CC85
+36: DFA0325BA89F1FB02670BDB24C79A4A9
+37: FE7D8D6F3D042A372CDF6B31F6287A72
+38: 2283F56384E490B4682A8FCD8F32BC11
+39: E9F99BD39F851FBDD95C9880466CFA4F
+40: 2C909B8E58BB81DDA517F6661DF7AE32
+41: FC788CBDBF46C3615C3DC8103B1B4090
+42: 3B6FBD288158B7D7D554AFE2B02F3F07
+43: C713EFFE8246F812DCAFFA725A73C8E1
+44: 9B8327F08E2FF85E97523D2FB0D3C326
+45: 052717D2EC347D5B55BE1655D480ADC9
+46: E7A9B7F3DE3E4F2F0F0529B050B43F4F
+47: C287E5BD8F046696B61FAF7E930D3965
+48: 511ADA8934C2609AA5EFA93C57405D2C
+49: 7D6AD2BA497AB34E41643F5094C3713C
+
+
Cipher: cast5
Key Size: 5 bytes
0: 9B32EF7653DAB4E6
diff --git a/notes/eax_tv.txt b/notes/eax_tv.txt
index 05d0ef7d8..efa15c174 100644
--- a/notes/eax_tv.txt
+++ b/notes/eax_tv.txt
@@ -351,6 +351,41 @@ EAX-3des (24 byte key)
15: 9E8C73EADA105749B5D8D97392EDC3, 2E749EE66C1E6A16
16: 600FA4149AF252C87B828C780AEFF8BC, 33D7D11DCDC19936
+EAX-sm4 (16 byte key)
+ 0: , 650501697FAF3887F97E691AD57FD916
+ 1: 56, 34D9D25271FA43908A88585CCA65BB56
+ 2: 0F1A, F5E3D4115EB84F74A2AB8289E696F366
+ 3: A06D88, 4425EAA479E448A034B7B997A6BE84C2
+ 4: BC33483A, 51188AB6D581A1335CD5C2E554B7C68F
+ 5: 353583E113, 45071CBF115D70CF0E7852B87E836B65
+ 6: 3093D4065F4B, 73E28033374C6512E6A135ABA04A6870
+ 7: 349796DFD3F24C, 1A3E0016A50C0018BAA966F99A320A7E
+ 8: 9D7797A751E31590, 30FD2B87E834DF19FED8357CD26BC88B
+ 9: 2ABCB97055D2BE28D0, D69EF4E92C0C50A52E053DE99CA94501
+ 10: BA7B8CA8FB59F7A0263B, F9DC5935366E22AE8B455461E949ED4F
+ 11: D4ADCF3E64D0B2F251479B, 34CFFFC5A89D7CF8BD836421038C7A42
+ 12: 924FC873A8F53C16F37D54A1, 8FA7A0073CC0BED6D34709FE9A7EC600
+ 13: 99AEE0A92D0D6D783FEEE6ABA2, 24F753FFB17EF010E0AA0947FC52BAA4
+ 14: 00BFC12E5B7E14F0EDDFC42A1816, BAAD3E2BED1F6ECFF81D2184C69B3484
+ 15: 4E85F4056D13919731FDAD6E8E974F, F24C6A5E6EB02D4DDF27CB757D1A2423
+ 16: C0F667A42E6B5598398B431814A18273, EC9E34B8E933583E16510ACE9B4DF06B
+ 17: 0F7B8922D29E828148A46FEAC29729032C, 4934C5F94389495195984CFF758E87AB
+ 18: 4C67F1ECF5BE22F16F20A25C79ADBD01ABEB, B6DAE0A254994AE40DF0F29DFAF50364
+ 19: 084D26AD7889C945C2AF7E633D870CE79B4729, 402DEAFE37357D5D9DBAF56D3C5F5800
+ 20: 621482F573F2BF7D3C4E1294D22CA36AD5B8BA56, C42036C948E4158127F7921B0548EB38
+ 21: 807E26EC65A32C6431E16D02F1013F60DA8D6F8FEB, A9451DB25AA897C6E74A7AB0D0CF5E89
+ 22: ECDF3996A964655239C7B1FF36B27AB40233CE920F9F, 7B6E0355E5AC027E52D046A5A21CDE2D
+ 23: 89D9DFA557700E6CCA7825B7055C3A84F50FC1DB2C2140, 850F5596560D461751382E1A2FAAE21F
+ 24: 4B4F825B41D728CFD1E3AEC60ED227A858A2C020EB0D785C, 63B44E988DF17AF1A5999A6CFE4068EA
+ 25: 7D885DEFFC7CFA06C7FC710C6ED78976A40C6D7EA82AA0F7FB, 88B2E50E500C0883141712B2BABEF5A9
+ 26: 65CFF84444B5CCF480E5D3062BDCB4662C5C2B9933841D6F04E5, DAD13A3AD0D51F98356B6FC69C934BB0
+ 27: 95C852409422E090E049218F228DB32CC302BAB911E2C871A21BF4, CF4D1EAF2886CF74479E239AC6CD585D
+ 28: CBAA0B7052DEC9C8DA334EF910FAE656EAAAEFBA5D6C1D5687D78536, 13E40EFAE3336AC5B51415504A9640B5
+ 29: DD0D60752F4EBD70509B1DAC9868A73151FE6950083E3976DC264A7399, C9AE7328F68C537E6C0C6D7A8A3E0D9C
+ 30: 8FF315081B4E87A2753038905A3EC46879E28C6458809E97EEEC7D77EB22, 3C3E1B732EB711E51D4F1F80B38B9741
+ 31: 41CAD696C51CA2F938C3BA517177A22AD17211D4A38F43A81436308E08CC41, 5DAEBD5D3C58CF6CEE1D1EF9FC171DAD
+ 32: BDF417BCDF679E2C981023BBB53D408929A4052F1F04BD59DE618C588786EA90, 1DBC8FA9D1F645E424FC74147ABA4C2C
+
EAX-cast5 (8 byte key)
0: , 382FB8F7E9F69FDC
1: 99, 20DA959849B3F7AB
diff --git a/notes/gcm_tv.txt b/notes/gcm_tv.txt
index 93900af74..5098f82c6 100644
--- a/notes/gcm_tv.txt
+++ b/notes/gcm_tv.txt
@@ -138,6 +138,40 @@ GCM-twofish (16 byte key)
31: AF52E18DE8608C39FA886377899C66AAF8612513C21C7B815E248D9F75D775, 39F0F712265F44A442A7581D8F314D1B
32: 64F9AE4A9E6735D348343EA7ADD617F0687C2883637468BBFA3664F9194F1683, 947A02C9994DA490BFC9E036305B26EA
+GCM-sm4 (16 byte key)
+ 1: 35, FF70BBCF7CCB35AFE8B806BBDA8CF462
+ 2: 418F, 4CB59D26F6F41BA4413339DE202394D5
+ 3: F01644, B06F26255E3BF20393B697283619F4CB
+ 4: AE4871E8, 1E7D9A18D453B5992DDB52975B8B9BD3
+ 5: C280528391, CF1C65604E006F9B79EDF4B6B6EF5B05
+ 6: B2F0185A5F9A, 33981E9B9A58CEA9FCD1219868E4907E
+ 7: 441988562A65B4, 4E6610F9F18F7AECCE0745B8227FB139
+ 8: F70D70319A3B6652, 09BC4E06455D10358AFB36C01AFD0494
+ 9: CEE87EE6FF0078A9A6, 8333A61DDCB965BF4293E6911864CF66
+ 10: 2599C4CC2007F4032AB3, F2F0238D73262488B89F2C25ADFC999E
+ 11: D2EC717E6897299EDB42F6, 05603ECDE2E18AFA4470DF97F8EE4899
+ 12: F8FD6DA197A96D5B3B06287C, 4BA295D367B50E71FACD53214151A7B1
+ 13: 0C7BCD801EBF114EFFFEA84A66, 379C586351C9D2054FC458E0DB5F878F
+ 14: 9C4F1D1D7C98A042AA62A7A28E97, 1A932604F135BB21C2EC2E7D73AF0C3F
+ 15: F80C96AC05523E55881D716BD9E0A6, 5CEA5F26E90EA3588964E374942CEC54
+ 16: F2272532A01D7FE05A47EB04ED422938, 294B483BAEED79A8AC42072D20F9028B
+ 17: 58EB27B530CA20D5A85BA7CE06E9112D2B, FCF9EEA2C4EF185199A67C172DFFFCB1
+ 18: 46CB6F7EC77EAA26A78C2FC5184741F0CDC7, F9ABC0744B86DDA1DA92F317EF9269E4
+ 19: 442355822E3E451F3FA92567BB3FC6488648B6, F51A079916440068677971C70D24C196
+ 20: 5241BBF0112C1802697B8AE0B324853839734FE7, 15A71FFFB4156F88A5585BC5587E9101
+ 21: DE432C494F1CDDE5F5055AFE2DA309E200E6014482, 517FBDAD471B0DE9671B831E797521E9
+ 22: D84AD8B34414E955B8AF0EE292107DE71788B29BFD59, 44C464F41512C71AFFAFF37326A429DB
+ 23: 0E21E74C073C639049EA361606D7C2C2DEF7EBD05FA233, 2169BFF97DD2D677D9512E1BAB6FD5C0
+ 24: 43E116D801E2EF765E9FEB07DD4DD33999796314064FC926, 90A32EDEE61181ED45B5736A31C7256D
+ 25: 506FD9ECAA69A9D608AE2E8D7DFA0E529E9249A57757A76BAB, D7E977F8CCDE9C2FA5FB52D38B9F326C
+ 26: 3D403A81FA1B1BA530E27D9864170BEF4241F373750F4891D117, 416580995A698D7F2B948F86DE09B9AE
+ 27: 1ACC9E429DBBBC790528F7156CFB81D7CD9C42950F97778D0A49F2, 0294F18AC56719B6F0B564D65DC3735D
+ 28: CBEE1B89C717D3DD688132EFC8C26E3A73D0F4371BE2011B33040631, 3D65AE78B1F7191BDCA08E1CA0040EB4
+ 29: CE869BFF3E7F73F41700ABCD63635277B7751838D43CC1EF94CC740A98, 118D7B0B39B7461BA4C465B2CB1E601F
+ 30: F795251BE1FC9D13087DF27051A5F765A95A052DD5BF3779B5110E333A23, CCBD865E6BA507DECD56A56178F2C934
+ 31: F6A93096ABAD485915A035C24341E1EFBB5B23AC7901E12FFFB5E9BEB23685, 07399EBB80C57C761CD355B6752EEE3B
+ 32: 195A9982F24E9310DAC69CCEB62B509E962B6EF55698B95AA2A7ED451B331249, D7472FFC15A99DE62CD2993AFADD9867
+
GCM-noekeon (16 byte key)
1: 05, E18F8A201F281A4F3F4FE6F43A001107
2: 55DF, BE4D95A0BC5966CC2E80FB544AD43B8B
diff --git a/notes/ocb3_tv.txt b/notes/ocb3_tv.txt
index c65a462c8..82b562e30 100644
--- a/notes/ocb3_tv.txt
+++ b/notes/ocb3_tv.txt
@@ -142,6 +142,41 @@ OCB3-twofish (16 byte key)
31: 8726247FAEB21D7F8927DC3E90267ECD744A8452C64B9EBE08D1A590BFA1F9, 8D75DAEC7E4438E7C701A392346EBBF6
32: 0DC0EE528456FEC203AFB12A55A4D3A901B9408958E5387EB42E75730D98B6A6, D7D682F57C1E1671BE459EE33BEA6B26
+OCB3-sm4 (16 byte key)
+ 0: , 5AE4874946EAB321286A5C30AA6B4B12
+ 1: FE, EAFB170A60CCAFC101780C51A35A77D7
+ 2: C914, 36606CD5546032064EBB2B2EFB962442
+ 3: 6CDDD3, D7DF2794D1169B1097701EC7EAC97ADC
+ 4: B9EF5F04, 6CEC2C898116792A42E3C823C1274A2A
+ 5: 77201EA353, 40B522CEDB325AC6684F6560C0282555
+ 6: 5A5A86063B5E, 9EAF522E35E9AC2D2AE6F50F707FE8A6
+ 7: B04D78AD924054, 4CA7F758F395154EBD95BF0BC8B28618
+ 8: C089204BEC680CFE, 4DF7DD68E7F468FD070CA876949BE593
+ 9: 4F368531FC342799C5, CAD9C6A6AB8DC5445C8D48BE005819D0
+ 10: EA61FEF7AEA9EEC392AF, 9FAA6F9A39CC88F9F4423534104E26E6
+ 11: 55A79A6EF7F1F75C42B172, 4C8D817200E7A9368866D5DC9619ACE1
+ 12: F0D1A7080D89189D052C7DC1, 7903ED2CCED2DA132DC480EDA4178650
+ 13: 7D55F3544210EBFD2BAC6BB18E, 903AAE3C64CD8993711BADACD2014BB9
+ 14: E8FB86041ABB30374C19B1FBC542, 03ED4A92F7CA8F280F6148E48D2483F4
+ 15: 5DAEBCDE1C64601175B5A6E446C9B9, 1240BA45F11091457D66FA5C58C08D6D
+ 16: 2A4F6718D7333C9415AE69CE5CEC431F, AF79781109795D4BBCBC3C55327C60C4
+ 17: 2FA374A6724EF7CADB0896A3BEBDD1661D, 0105D0D05BFB6E1D1990A20D9294FCD1
+ 18: 9FF356181B833CEA5A0903D3373A8C310B0D, 82AAF55237C5D3D587CA3188B6B57CBB
+ 19: 13CA7A42D6E55A7A03AA09BB77F234DA876A7A, 914B59E12935290A8D68D0F27762D745
+ 20: EB94DEC2EBAB6A8C2D1E412E4F3F337048B3B301, F43394E403FD4F7EE07CF59D4A6ECAF7
+ 21: 8BDDCE25D442F0BDAC4CD1F0FAAB6B0B6D049734AA, 6CAFB1D636A6112402477DE93B853AAE
+ 22: E2298B85A6A6E7271857851EA90E584743B7A0A029FB, 64105E77B6A8C5A4739A1393ADD6E37D
+ 23: ABB540427006B696F59666612E5D123A02CC3C0F908867, 35430C4394E6A7128FA4CD0AA66BBE9C
+ 24: 8B048DB31FDABF2746CF664ACE1B8B138937BCAD03612FBF, CFB7FD35560F1D0E3EC488B799471544
+ 25: 6936BCB5A10B52D27FD13D259D166C415689E1D7BB2F669EAE, FA4BFD853500C958DECF8D77A31A8F46
+ 26: 7C0415C2C28D6F9895755EF3E1214FCC96F3AF2C70DBF6948031, F6D370B244D7F5A6F301BCB5CED7DEDC
+ 27: 195B75E3D72FF9AA5492EBAC7638A8AD551EA9E6B541D9689A5465, 1D8A1BB3A38ECA89ADF8F3B48F34682F
+ 28: 70522F78E5DB3829D61540C641A6C2FCA75BEAD1BC7DC6C3D15C2E2A, DD5072319ED0E24E4D3EE931CB82B3BE
+ 29: FCB983E8F38EFF1931A01E7CEB5AAAF15AD969C84DA1BBCC39E4F45D68, ED463576EFD354B7FCBAC13EC8DC0708
+ 30: CD507929D9E5025781958420C66B1EA4F5397C2F467764591805AEB2A095, 6E0B6BEF6C38D4424926B6DE1B4C315E
+ 31: 782FD749F729A1546E3C105DA7AA2CC6541ABE4192443D3D400C4D6B88A78B, 207918EAC7F7B92C29D5DFB9C3779DA5
+ 32: 4E23AE32EF1C9877F43C0FC3C285B422387EA18BD30F7F8BCD1DE64B9BF7221F, FF6F490EA358433EC439AB1954C6D473
+
OCB3-noekeon (16 byte key)
0: , B23A40302652E204B694EA78AF5A8FCC
1: B1, 5245C8680A6F4520168B173A39661249
diff --git a/notes/ocb_tv.txt b/notes/ocb_tv.txt
index 3ccf18d4b..e517341e9 100644
--- a/notes/ocb_tv.txt
+++ b/notes/ocb_tv.txt
@@ -351,6 +351,41 @@ OCB-3des (24 byte key)
15: D6F9A6B2420174C499F9FE91178784, D3AAF969ED2F7215
16: 4F1CF285B8748C4F8F4D201C06B343CA, 203A2692C077F1B5
+OCB-sm4 (16 byte key)
+ 0: , 644D127C5F75F64F48808EEE68350817
+ 1: 6B, EE408CBD07CEC607EAA25CB0CF063DE4
+ 2: 74E6, FE58AB1D0A951F156D0B78597DE88981
+ 3: 049E45, 9674DCB58ED4981CFFCA8725B5BDCBD8
+ 4: 416F6FAC, AB0981B209CEB024447861D113FE74AE
+ 5: CC01AFD1B6, 9125092E4696FE0939A256C03A37FD8C
+ 6: 1B32E33A7BA2, 80714FA9C1EAE774CF9C754F5FE7374A
+ 7: 6F91118A6F5E0F, 7065639ADBE5265F0B61836950C39871
+ 8: C40E934B952D0108, A8D1ACC7CA93B14C2A187441C794D31E
+ 9: 06B7FF294A85CD8F8F, 3C433077C03E9B6A481B6A78565A865E
+ 10: 123093E41AAA91EEA2D1, 9EEF81BFAA0C7116C632DAE3D447BEC4
+ 11: 83288869EBB8D22772AA98, A52688653CC795A14D7CC5C4FB59EFD6
+ 12: FD8A2A59C3BA54DE090DFFAE, CA597A88711955563025FE6F6B6CB095
+ 13: DA0FD6F4B1A0A8084D92F543D3, 9F22A8F0B9EFF906DFE7438E0738FF4E
+ 14: 8D8090EFF0A44E96A4CB582A61E5, 11477309B525FA0B1A1B4B0F25DED574
+ 15: 95DF2F968380DAF60122B8FEA2614A, B25F1C94B4F963297EF31AD01819CAD6
+ 16: E63BB516DEF0A60F3503FC9578082588, 9E75B31F3A10D922A698610EDD224AC6
+ 17: 4141B7D60688A6B74659FA3F988749336C, 2A4A83E828CA79F3CBA7A417E4E9149F
+ 18: 68A58C0AAD6A8831231DD3537D5E6284FAD7, 4372DBFA17A102118A4750E719FCC9FF
+ 19: 6565EE0302F1AFE357E65B56980F04EB805228, 20E09947D4A4947CD24457589303BD69
+ 20: 0400E928A18B85498DB78CFAFC026CB07F3DD3A3, 9646E0737D7F75F109430336754DE155
+ 21: EC2A5AE19531D964B6C03C4ACE804909F3B0260DC6, 6DEE21A1B2A14FE5F8C13C3620F35EB4
+ 22: F32AEDAF0281234753F5D2903F1FEF505165D4543923, B8F486809ABD26E4CACF6C283CBFBAFA
+ 23: 482B2728C75FF1206E280FDDD082F2C93EE9C2C15A14E9, 70DA0D202F68BF7DCF126DFE2CC75CA9
+ 24: DB67772FC481A6D2F50ECCA59134984869C91436211A24A7, A7391E9F5698DF1BC984F127A874D8F6
+ 25: DF60A24E93101ED2F68D3CAAD966F51271603C8E611AB16C3F, A4C44700A6845A75B72C04C1395E9820
+ 26: E14DE8BC5A2A8417783267AB659DAC26A84A02B4EB5FA4BAAE7F, 1508521641C38536DAF3B2CE65BC060A
+ 27: 309D876E6CCB6C8FDF963D6143E2FC091DCF7FA75D002986502500, 323AE0DB790F786252F35547554990E2
+ 28: 7D8D3119EA42098509D0B1FB8FDB945E1C6C7AF4E1C9773F82A6D3DF, 8F97D69077AA1AD2BD7A8FBE6950E632
+ 29: 597A5B272750C5CCE0591CE40A4CE838F3C326A9BCAB160385D6D431C0, 2F9D9980E31419966C6F5DC8E6DA216D
+ 30: 676720E8BB37FAD3778C4289CA1442A4905F327798C7C3584FD9518F19CE, 60D96E4249518291C68EE01AEB5A5B05
+ 31: 609D0A3770F9BE06D7100E75FA0096F21FA498C28224A9406228534A43DEAD, C3148A9D60CD32EA378720262A3529EA
+ 32: 18BA4D72E61851F5878E07459A24BDFCB48C6A2AF719584CBE6A0B321078B967, 077031AE31FC5ED7B27ADABDBE699CDE
+
OCB-cast5 (8 byte key)
0: , 77E8002236021687
1: 52, D57DF1037B6A799D
diff --git a/notes/omac_tv.txt b/notes/omac_tv.txt
index 2f13110b0..4e86098a2 100644
--- a/notes/omac_tv.txt
+++ b/notes/omac_tv.txt
@@ -351,6 +351,41 @@ OMAC-3des (24 byte key)
15: F8637AEEFF10F470
16: 0EA19531D42706EA
+OMAC-sm4 (16 byte key)
+ 0: 4DCF78C73B13A3B9494DE1152E66E9EF
+ 1: D969654F96D6A5888051528E4B417231
+ 2: E90E3429E81886D1C31C5B0076DD6967
+ 3: 0200C38838F6AC2547B57CAFB68197FE
+ 4: 7675C0168A1E04AAD697D52B74F354ED
+ 5: F3235CD0A674C4BE3DE00A59C59047B9
+ 6: BDD7ACCEC9155E657BDBC36DF69AD7E7
+ 7: DB9B8E833FED5FC7D5FFE00526DDD6AA
+ 8: 83DBBE2766B9BDB59146CB3CEA4A0AD7
+ 9: 95D3B45E49B41F5EF0D661DC62054A8B
+ 10: CF88F8DFAB57F5966ADBE703EB12A3F1
+ 11: 7BC575121B4F37E7A7BFD22F1B78685C
+ 12: 43DD61A4A29D22970F51DF0573A92670
+ 13: 305B1ADF8D5E5787CE7DAF41A1D383B9
+ 14: B9CB5438AC40D45B15D438A97CA56C3C
+ 15: EA698BFB6B673D2DB6A44CCF45B359A2
+ 16: 6C5F95BFAC3303A1EC872B01453AB9FB
+ 17: C5E8C622028131DDC11EA51AD38CB12A
+ 18: 0D306885AA1E6AC9B4A1F257AFF4F8A2
+ 19: FB942DA9F2BF3A42A48D4F3704F9E4D9
+ 20: 164E103E28C7BF2897212C45465E6BE4
+ 21: 29A8D843431D5383BEF4618186244A88
+ 22: E5198A53CA4D07C652A43F5F9F1284CE
+ 23: 7D7EE2E1992B09A10BFBE257E0965426
+ 24: DB01EACF33255EEC1B91124D20C00EA8
+ 25: 5BC1F4E148752FEB8FFA54638EA34E2A
+ 26: 908D641E82579034EF63BC715E8340BB
+ 27: D22784953DA497AEB56D795FC1576D48
+ 28: 2C5332E28F42758FBC3B35B430B54CFC
+ 29: EAA87F4C87BE3AD58927E556502D68FA
+ 30: 130EE9E3DE2AA6EBC5FC5CF0247D0FDA
+ 31: 8B08E2E6106B8454F2180AEAB5034D65
+ 32: ADF8EE93CC8DC3FC007EA2C361FA995A
+
OMAC-cast5 (8 byte key)
0: 7413DCDB9F0C3100
1: 423799EDF1472B79
diff --git a/notes/pmac_tv.txt b/notes/pmac_tv.txt
index 8f835abfe..d99311a4e 100644
--- a/notes/pmac_tv.txt
+++ b/notes/pmac_tv.txt
@@ -351,6 +351,41 @@ PMAC-3des (24 byte key)
15: B13BFD07E7A202CB
16: 58A6931335B4B2C2
+PMAC-sm4 (16 byte key)
+ 0: 69265690FA00C7D1E2AD63F9C01C56CC
+ 1: E63B0AEECB1D62691A360E78ECAD7B73
+ 2: B8B95E753F7016CCCCB0BCC8536D2DE9
+ 3: 22F61907C271F6B779AFC0EEDE1E0AB3
+ 4: 8A31DAB6D0F493058328FFE0A6349CF1
+ 5: 6760AA80889EC043BD944A4F18FD2D0F
+ 6: F50F83052E6AF271787815E01DFB4E8A
+ 7: 4F552D36BC345805EC9FCFCE5E54153F
+ 8: AEF8016C64F27C79562C50C335AA3A10
+ 9: C306E855A485ECC4969F66218705CCEF
+ 10: 7FA21114DC4C11212AD1DEABB8BD8AD6
+ 11: 322BD6CE62C0608438C1715F5871308A
+ 12: 4F5A19156E62660043C12FFDE62D9755
+ 13: EA1D701763EA97753EF54C9AA45F8678
+ 14: 64CAFE7A3C93A469D3A37B3C86A75AF2
+ 15: C8E6F0C75A3829EDB9C3570DCA18126D
+ 16: 1140368A72A1EC6AF8A5449924A76AE1
+ 17: B8EAD770AF3070B3712040E35E297D45
+ 18: 3E862A7270F6EFBB54E56B38E226E349
+ 19: BC80B79D95E6B30F33A1FAC0311835BA
+ 20: 292696B550C240DCFEBA46CD94D6B368
+ 21: 22C4BAA7FC406BCD7C67DDB1D4628185
+ 22: E500A8794059239A63169E62E7F13F9A
+ 23: A756B798A6DD9219D57A488AC32C8682
+ 24: 83A8F58D0FFB75A007CB99CAFFE650BC
+ 25: 99827ACA1F014860E687D0EA721657ED
+ 26: 6AF8CC23F2E4B9777BD171812BBA688F
+ 27: 0F31384898582A32808A66D8E986278F
+ 28: 8C36AD9C3CDA4DE1386CDFD6864C5DBC
+ 29: 0CC3D0BFCA51A052D807396B7160A33B
+ 30: 4E6A494B9C6CF7E4C540AB1CB84F5BE3
+ 31: B0FE54FBC1FDFC468419079DF58C74AC
+ 32: AD881C8EE04209E68E31B66458377848
+
PMAC-cast5 (8 byte key)
0: 0654F2F4BC1F7470
1: 3F725B162A1C8E6B
diff --git a/sources.cmake b/sources.cmake
index ab62ea8cd..49f8b3ddc 100644
--- a/sources.cmake
+++ b/sources.cmake
@@ -22,6 +22,7 @@ src/ciphers/safer/safer_tab.c
src/ciphers/safer/saferp.c
src/ciphers/serpent.c
src/ciphers/skipjack.c
+src/ciphers/sm4.c
src/ciphers/tea.c
src/ciphers/twofish/twofish.c
src/ciphers/twofish/twofish_tab.c
diff --git a/src/ciphers/sm4.c b/src/ciphers/sm4.c
new file mode 100644
index 000000000..12d7daad7
--- /dev/null
+++ b/src/ciphers/sm4.c
@@ -0,0 +1,375 @@
+/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/**
+ @brief SM4 block cipher algorithm
+ @date Oct 2018
+ @author Chao Wei
+
+ SM4 (formerly SMS4) is a block cipher used in the Chinese National
+ Standard for Wireless LAN WAPI (Wired Authentication and Privacy
+ Infrastructure).
+ --from wikipedia:
+ https://en.wikipedia.org/wiki/SM4_(cipher)
+
+ This implimentation follows Chinese National Standard
+ GM/T 0002-2012
+ */
+#include "tomcrypt_private.h"
+
+#ifdef LTC_SM4
+
+/*porting to libtomcrypt*/
+/*char always 8bits long*/
+typedef unsigned char sm4_u8_t;
+typedef ulong32 sm4_u32_t;
+
+/*
+ * S-box defined in section 6.2
+ * (1) Nonlinear transformation
+ */
+static const sm4_u8_t sbox_table[16][16] = {
+ {0xd6, 0x90, 0xe9, 0xfe, 0xcc, 0xe1, 0x3d, 0xb7,
+ 0x16, 0xb6, 0x14, 0xc2, 0x28, 0xfb, 0x2c, 0x05},
+ {0x2b, 0x67, 0x9a, 0x76, 0x2a, 0xbe, 0x04, 0xc3,
+ 0xaa, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99},
+ {0x9c, 0x42, 0x50, 0xf4, 0x91, 0xef, 0x98, 0x7a,
+ 0x33, 0x54, 0x0b, 0x43, 0xed, 0xcf, 0xac, 0x62},
+ {0xe4, 0xb3, 0x1c, 0xa9, 0xc9, 0x08, 0xe8, 0x95,
+ 0x80, 0xdf, 0x94, 0xfa, 0x75, 0x8f, 0x3f, 0xa6},
+ {0x47, 0x07, 0xa7, 0xfc, 0xf3, 0x73, 0x17, 0xba,
+ 0x83, 0x59, 0x3c, 0x19, 0xe6, 0x85, 0x4f, 0xa8},
+ {0x68, 0x6b, 0x81, 0xb2, 0x71, 0x64, 0xda, 0x8b,
+ 0xf8, 0xeb, 0x0f, 0x4b, 0x70, 0x56, 0x9d, 0x35},
+ {0x1e, 0x24, 0x0e, 0x5e, 0x63, 0x58, 0xd1, 0xa2,
+ 0x25, 0x22, 0x7c, 0x3b, 0x01, 0x21, 0x78, 0x87},
+ {0xd4, 0x00, 0x46, 0x57, 0x9f, 0xd3, 0x27, 0x52,
+ 0x4c, 0x36, 0x02, 0xe7, 0xa0, 0xc4, 0xc8, 0x9e},
+ {0xea, 0xbf, 0x8a, 0xd2, 0x40, 0xc7, 0x38, 0xb5,
+ 0xa3, 0xf7, 0xf2, 0xce, 0xf9, 0x61, 0x15, 0xa1},
+ {0xe0, 0xae, 0x5d, 0xa4, 0x9b, 0x34, 0x1a, 0x55,
+ 0xad, 0x93, 0x32, 0x30, 0xf5, 0x8c, 0xb1, 0xe3},
+ {0x1d, 0xf6, 0xe2, 0x2e, 0x82, 0x66, 0xca, 0x60,
+ 0xc0, 0x29, 0x23, 0xab, 0x0d, 0x53, 0x4e, 0x6f},
+ {0xd5, 0xdb, 0x37, 0x45, 0xde, 0xfd, 0x8e, 0x2f,
+ 0x03, 0xff, 0x6a, 0x72, 0x6d, 0x6c, 0x5b, 0x51},
+ {0x8d, 0x1b, 0xaf, 0x92, 0xbb, 0xdd, 0xbc, 0x7f,
+ 0x11, 0xd9, 0x5c, 0x41, 0x1f, 0x10, 0x5a, 0xd8},
+ {0x0a, 0xc1, 0x31, 0x88, 0xa5, 0xcd, 0x7b, 0xbd,
+ 0x2d, 0x74, 0xd0, 0x12, 0xb8, 0xe5, 0xb4, 0xb0},
+ {0x89, 0x69, 0x97, 0x4a, 0x0c, 0x96, 0x77, 0x7e,
+ 0x65, 0xb9, 0xf1, 0x09, 0xc5, 0x6e, 0xc6, 0x84},
+ {0x18, 0xf0, 0x7d, 0xec, 0x3a, 0xdc, 0x4d, 0x20,
+ 0x79, 0xee, 0x5f, 0x3e, 0xd7, 0xcb, 0x39, 0x48},
+};
+
+/*
+ * S-box
+ * defined in section 2.6 S-box
+ */
+LTC_INLINE static sm4_u8_t sbox(sm4_u8_t a)
+{
+ return sbox_table[(a >> 4) & 0x0f][a & 0x0f];
+}
+
+/*
+ * Nonlinear transformation t
+ * defined in section 6.2 (1) Nonelinear transformation t
+ *
+ * Here should be big endian.
+ * But we just convert a 32bit word byte by byte.
+ * So it's OK if we don't convert the endian order
+ */
+LTC_INLINE static sm4_u32_t t(sm4_u32_t A)
+{
+ sm4_u8_t a[4];
+ sm4_u8_t b[4];
+ sm4_u32_t B;
+
+ STORE32H(A, a);
+ b[0] = sbox(a[0]);
+ b[1] = sbox(a[1]);
+ b[2] = sbox(a[2]);
+ b[3] = sbox(a[3]);
+ LOAD32H(B, b);
+ return B;
+}
+
+/*
+ * defined in section 6.2 (2) Linear transformation L
+ */
+LTC_INLINE static sm4_u32_t L(sm4_u32_t B)
+{
+ return B ^ ROLc(B, 2) ^ ROLc(B, 10) ^ ROLc(B, 18) ^ ROLc(B, 24);
+}
+
+/*
+ * defined in section 6.2 Permutation T
+ */
+LTC_INLINE static sm4_u32_t T(sm4_u32_t Z)
+{
+ return L(t(Z));
+}
+
+/*
+ * defined in section 7.3 (2) The system parameter FK
+ */
+static const sm4_u32_t FK[4] = {
+ 0xa3b1bac6, 0x56aa3350, 0x677d9197, 0xb27022dc
+};
+
+/*
+ * defined in section 7.3 (3) The fixed parameter CK
+ * The fixed parameter CK is used in the key expansion algorithm
+ */
+static const sm4_u32_t CK[32] =
+{
+ 0x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269,
+ 0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9,
+ 0xe0e7eef5, 0xfc030a11, 0x181f262d, 0x343b4249,
+ 0x50575e65, 0x6c737a81, 0x888f969d, 0xa4abb2b9,
+ 0xc0c7ced5, 0xdce3eaf1, 0xf8ff060d, 0x141b2229,
+ 0x30373e45, 0x4c535a61, 0x686f767d, 0x848b9299,
+ 0xa0a7aeb5, 0xbcc3cad1, 0xd8dfe6ed, 0xf4fb0209,
+ 0x10171e25, 0x2c333a41, 0x484f565d, 0x646b7279,
+};
+
+/*
+ * defined in section 7.3 (1) L'
+ */
+LTC_INLINE static sm4_u32_t _L(sm4_u32_t B)
+{
+ return B ^ ROLc(B, 13) ^ ROLc(B, 23);
+}
+
+/*
+ * defined in section 7.3 (1) T'
+ */
+LTC_INLINE static sm4_u32_t _T(sm4_u32_t Z)
+{
+ return _L(t(Z));
+}
+
+/*
+ * defined in section 7.3 Key Expansion
+ */
+LTC_INLINE static void mk2rk(sm4_u32_t rk[32], sm4_u8_t mk[16])
+{
+ sm4_u32_t MK[4] = { 0 };
+ sm4_u32_t K[4+32] = { 0 };
+ int i;
+
+ LOAD32H(MK[0], mk );
+ LOAD32H(MK[1], mk + 4);
+ LOAD32H(MK[2], mk + 8);
+ LOAD32H(MK[3], mk + 12);
+
+ for (i = 0; i < 4; ++i)
+ K[i] = MK[i] ^ FK[i];
+ for (i = 0; i < 32; ++i)
+ K[i+4] = K[i] ^ _T(K[i+1] ^ K[i+2] ^ K[i+3] ^ CK[i]);
+ for (i = 0; i < 32; ++i)
+ rk[i] = K[i+4];
+}
+
+/*
+ * defined in section 6 Round Function F
+ */
+LTC_INLINE static sm4_u32_t F(sm4_u32_t X[4], sm4_u32_t rk)
+{
+ return X[0] ^ T(X[1] ^ X[2] ^ X[3] ^ rk);
+}
+
+/*
+ * defined in section 7.1 (2) The reverse transformation
+ */
+LTC_INLINE static void R(sm4_u32_t Y[4], sm4_u32_t X[32+4])
+{
+ Y[0] = X[35];
+ Y[1] = X[34];
+ Y[2] = X[33];
+ Y[3] = X[32];
+}
+
+/*
+ * defined in section 7.1 (En)cryption
+ */
+LTC_INLINE static void sm4_crypt(sm4_u32_t Y[4], sm4_u32_t X[4+32], const sm4_u32_t rk[32])
+{
+ int i;
+
+ for (i = 0; i < 32; ++i)
+ X[i+4] = F(X+i, rk[i]);
+ R(Y, X);
+}
+
+LTC_INLINE static void sm4_setkey(struct sm4_key *sm4, const unsigned char *key)
+{
+ int i;
+
+ mk2rk(sm4->ek,(void*)key);
+ /*swap key sequence when decrypt cipher*/
+ for (i = 0; i < 32; ++i)
+ sm4->dk[i] = sm4->ek[32 - 1 - i];
+}
+
+int sm4_setup(const unsigned char *key, int keylen,
+ int num_rounds, symmetric_key *skey)
+{
+ LTC_ARGCHK(key != NULL);
+ LTC_ARGCHK(skey != NULL);
+ if (num_rounds != 0 && num_rounds != 32)
+ return CRYPT_INVALID_ROUNDS;
+ if (keylen != 16)
+ return CRYPT_INVALID_KEYSIZE;
+ sm4_setkey(&(skey->sm4), key);
+ return CRYPT_OK;
+}
+
+/*
+ * SM4 encryption.
+ */
+LTC_INLINE static void sm4_do(void *output, const void *input, const sm4_u32_t rk[32])
+{
+ sm4_u32_t Y[4];
+ sm4_u32_t X[32+4];
+
+ LOAD32H(X[0], (sm4_u8_t *)input );
+ LOAD32H(X[1], (sm4_u8_t *)input + 4);
+ LOAD32H(X[2], (sm4_u8_t *)input + 8);
+ LOAD32H(X[3], (sm4_u8_t *)input + 12);
+
+ sm4_crypt(Y, X, rk);
+
+ STORE32H(Y[0], (sm4_u8_t *)output );
+ STORE32H(Y[1], (sm4_u8_t *)output + 4);
+ STORE32H(Y[2], (sm4_u8_t *)output + 8);
+ STORE32H(Y[3], (sm4_u8_t *)output + 12);
+}
+
+int sm4_ecb_encrypt(const unsigned char *pt, unsigned char *ct,
+ const symmetric_key *skey)
+{
+ LTC_ARGCHK(pt != NULL);
+ LTC_ARGCHK(ct != NULL);
+ LTC_ARGCHK(skey != NULL);
+ sm4_do(ct, pt, skey->sm4.ek);
+ return CRYPT_OK;
+}
+int sm4_ecb_decrypt(const unsigned char *ct, unsigned char *pt,
+ const symmetric_key *skey)
+{
+ LTC_ARGCHK(pt != NULL);
+ LTC_ARGCHK(ct != NULL);
+ LTC_ARGCHK(skey != NULL);
+ sm4_do(pt, ct, skey->sm4.dk);
+ return CRYPT_OK;
+}
+
+void sm4_done(symmetric_key *skey)
+{
+ LTC_UNUSED_PARAM(skey);
+}
+int sm4_keysize(int *keysize)
+{
+ LTC_ARGCHK(keysize != NULL);
+ if(*keysize < 16) {
+ return CRYPT_INVALID_KEYSIZE;
+ }
+ *keysize = 16;
+ return CRYPT_OK;
+}
+
+/*
+ * libtomcrypt interface is used
+ */
+
+static int sm4_self_test_ltc(void)
+{
+ int result;
+ int i;
+ int keysize;
+ symmetric_key skey;
+
+ sm4_u8_t output[16];
+ sm4_u8_t plaintext[] = {
+ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
+ 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
+ };
+ sm4_u8_t key[] = {
+ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
+ 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
+ };
+ sm4_u8_t ciphertext[] = {
+ 0x68, 0x1E, 0xDF, 0x34, 0xD2, 0x06, 0x96, 0x5E,
+ 0x86, 0xB3, 0xE9, 0x4F, 0x53, 0x6E, 0x42, 0x46,
+ };
+ sm4_u8_t ciphertext_1000000t[] = {
+ 0x59, 0x52, 0x98, 0xC7, 0xC6, 0xFD, 0x27, 0x1F,
+ 0x04, 0x02, 0xF8, 0x04, 0xC3, 0x3D, 0x3F, 0x66,
+ };
+
+ result = CRYPT_OK; /* Assume the best */
+
+ sm4_setup(key, sizeof(key), 32, &skey);
+
+ /*A.1 example 1*/
+ sm4_ecb_encrypt(plaintext, output, &skey);
+ if (compare_testvector(output, 16, ciphertext, 16, "SM4 single encryption", 0) != 0)
+ result = CRYPT_ERROR;
+
+ sm4_ecb_decrypt(ciphertext, output, &skey);
+ if (compare_testvector(output, 16, plaintext, 16, "SM4 single decryption", 0) != 0)
+ result = CRYPT_ERROR;
+
+ /*A.2 example 2*/
+ XMEMCPY(output, plaintext, 16);
+ for (i = 0; i < 1000000; ++i)
+ sm4_ecb_encrypt(output, output, &skey);
+ if (compare_testvector(output, 16, ciphertext_1000000t, 16, "SM4 1000000 times encryption", 0) != 0)
+ result = CRYPT_ERROR;
+
+ XMEMCPY(output, ciphertext_1000000t, 16);
+ for (i = 0; i < 1000000; ++i)
+ sm4_ecb_decrypt(output, output, &skey);
+ if (compare_testvector(output, 16, plaintext, 16, "SM4 1000000 times encryption", 0) != 0)
+ result = CRYPT_ERROR;
+
+ keysize = 128;
+ if (sm4_keysize(&keysize) != CRYPT_OK) {
+ fprintf(stderr, "Getting the max SM4 keysize failed\n");
+ result = CRYPT_ERROR;
+ } else if (keysize != 16) {
+ fprintf(stderr, "SM4 maximum key size is faulty:\nSHOULD be 16\nIS %d\n", keysize);
+ result = CRYPT_ERROR;
+ }
+
+ sm4_done(&skey);
+
+ return result;
+}
+
+int sm4_test(void)
+{
+#ifndef LTC_TEST
+ return CRYPT_NOP;
+#else
+ return sm4_self_test_ltc();
+#endif
+}
+
+const struct ltc_cipher_descriptor sm4_desc = {
+ "sm4",
+ 28,
+ 16, 16, 16, 32, /* min_key_len, max_key_len, block_len, default_rounds */
+ &sm4_setup,
+ &sm4_ecb_encrypt,
+ &sm4_ecb_decrypt,
+ &sm4_test,
+ &sm4_done,
+ &sm4_keysize,
+ NULL, NULL, NULL, NULL, NULL, NULL, NULL,
+ NULL, NULL, NULL, NULL, NULL, NULL, NULL,
+};
+
+#endif /*LTC_SM4*/
diff --git a/src/headers/tomcrypt_cipher.h b/src/headers/tomcrypt_cipher.h
index 074e55900..ac7d87695 100644
--- a/src/headers/tomcrypt_cipher.h
+++ b/src/headers/tomcrypt_cipher.h
@@ -107,6 +107,12 @@ struct des3_key {
};
#endif
+#ifdef LTC_SM4
+struct sm4_key {
+ ulong32 ek[32], dk[32];
+};
+#endif
+
#ifdef LTC_CAST5
struct cast5_key {
ulong32 K[32], keylen;
@@ -184,6 +190,9 @@ typedef union Symmetric_key {
struct desx_key desx;
struct des3_key des3;
#endif
+#ifdef LTC_SM4
+ struct sm4_key sm4;
+#endif
#ifdef LTC_RC2
struct rc2_key rc2;
#endif
@@ -780,6 +789,16 @@ int des3_keysize(int *keysize);
extern const struct ltc_cipher_descriptor des_desc, desx_desc, des3_desc;
#endif
+#ifdef LTC_SM4
+int sm4_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
+int sm4_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey);
+int sm4_ecb_decrypt(const unsigned char *ct, unsigned char *pt, const symmetric_key *skey);
+int sm4_test(void);
+void sm4_done(symmetric_key *skey);
+int sm4_keysize(int *keysize);
+extern const struct ltc_cipher_descriptor sm4_desc;
+#endif
+
#ifdef LTC_CAST5
int cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey);
int cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, const symmetric_key *skey);
diff --git a/src/headers/tomcrypt_custom.h b/src/headers/tomcrypt_custom.h
index bea0ea501..8f85c1923 100644
--- a/src/headers/tomcrypt_custom.h
+++ b/src/headers/tomcrypt_custom.h
@@ -79,6 +79,7 @@
#define LTC_RIJNDAEL
#define LTC_BLOWFISH
#define LTC_DES
+ #define LTC_SM4
#define LTC_CAST5
#define LTC_NO_MODES
@@ -197,6 +198,7 @@
/* #define LTC_TWOFISH_SMALL */
/* LTC_DES includes EDE triple-DES */
#define LTC_DES
+#define LTC_SM4
#define LTC_CAST5
#define LTC_NOEKEON
#define LTC_SKIPJACK
diff --git a/src/misc/crypt/crypt.c b/src/misc/crypt/crypt.c
index a215b6a1f..c21d2e8c3 100644
--- a/src/misc/crypt/crypt.c
+++ b/src/misc/crypt/crypt.c
@@ -87,6 +87,9 @@ const char *crypt_build_settings =
" DES-X\n"
" 3DES\n"
#endif
+#if defined(LTC_SM4)
+ " SM4\n"
+#endif
#if defined(LTC_CAST5)
" CAST5\n"
#endif
diff --git a/src/misc/crypt/crypt_register_all_ciphers.c b/src/misc/crypt/crypt_register_all_ciphers.c
index da46051c8..5193ff906 100644
--- a/src/misc/crypt/crypt_register_all_ciphers.c
+++ b/src/misc/crypt/crypt_register_all_ciphers.c
@@ -61,6 +61,9 @@ int register_all_ciphers(void)
REGISTER_CIPHER(&desx_desc);
REGISTER_CIPHER(&des3_desc);
#endif
+#ifdef LTC_SM4
+ REGISTER_CIPHER(&sm4_desc);
+#endif
#ifdef LTC_CAST5
REGISTER_CIPHER(&cast5_desc);
#endif
diff --git a/src/misc/crypt/crypt_sizes.c b/src/misc/crypt/crypt_sizes.c
index 7545aa828..26f2d13fc 100644
--- a/src/misc/crypt/crypt_sizes.c
+++ b/src/misc/crypt/crypt_sizes.c
@@ -92,6 +92,9 @@ static const crypt_size s_crypt_sizes[] = {
SZ_STRINGIFY_S(des_key),
SZ_STRINGIFY_S(des3_key),
#endif
+#ifdef LTC_SM4
+ SZ_STRINGIFY_S(sm4_key),
+#endif
#ifdef LTC_IDEA
SZ_STRINGIFY_S(idea_key),
#endif
diff --git a/tests/test.c b/tests/test.c
index 25cacaf17..60e8c363c 100644
--- a/tests/test.c
+++ b/tests/test.c
@@ -152,6 +152,9 @@ static void s_unregister_all(void)
unregister_cipher(&desx_desc);
unregister_cipher(&des3_desc);
#endif
+#ifdef LTC_SM4
+ unregister_cipher(&sm4_desc);
+#endif
#ifdef LTC_CAST5
unregister_cipher(&cast5_desc);
#endif