aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlloyd <[email protected]>2006-08-15 01:35:46 +0000
committerlloyd <[email protected]>2006-08-15 01:35:46 +0000
commit2ef993044a5e3b972f910995cfbeea5775b7ce6a (patch)
tree51a5a222a46257c2322014c8ba14ef18bfe29088
parenteba970e70755d5192433267c47a320f3f395bc4a (diff)
Add the beginnings of an x96 assembler version of Serpent. Currently only
encryption is done in asm, the rest is still in C++
-rw-r--r--modules/alg_ia32/asm_macr.h3
-rw-r--r--modules/alg_ia32/modinfo.txt2
-rw-r--r--modules/alg_ia32/serp_asm.S294
-rw-r--r--modules/alg_ia32/serpent.cpp322
4 files changed, 621 insertions, 0 deletions
diff --git a/modules/alg_ia32/asm_macr.h b/modules/alg_ia32/asm_macr.h
index bbbb0239c..8643e0768 100644
--- a/modules/alg_ia32/asm_macr.h
+++ b/modules/alg_ia32/asm_macr.h
@@ -89,6 +89,9 @@ func_name:
#define ADD2_IMM(TO, FROM, NUM) leal NUM(FROM), TO
#define ADD3_IMM(TO, FROM, NUM) leal NUM(TO,FROM,1), TO
+#define SHL_IMM(REG, SHIFT) shll IMM(SHIFT), REG
+#define SHR_IMM(REG, SHIFT) shrl IMM(SHIFT), REG
+
#define XOR(TO, FROM) xorl FROM, TO
#define AND(TO, FROM) andl FROM, TO
#define OR(TO, FROM) orl FROM, TO
diff --git a/modules/alg_ia32/modinfo.txt b/modules/alg_ia32/modinfo.txt
index 5864c574c..02581578f 100644
--- a/modules/alg_ia32/modinfo.txt
+++ b/modules/alg_ia32/modinfo.txt
@@ -3,12 +3,14 @@ realname "Algorithm x86 Assembler"
replace_file md4.cpp
replace_file md5.cpp
replace_file sha160.cpp
+replace_file serpent.cpp
add_file asm_macr.h
add_file md4core.S
add_file md5core.S
add_file sha1core.S
+add_file serp_asm.S
<arch>
ia32
diff --git a/modules/alg_ia32/serp_asm.S b/modules/alg_ia32/serp_asm.S
new file mode 100644
index 000000000..049c2976b
--- /dev/null
+++ b/modules/alg_ia32/serp_asm.S
@@ -0,0 +1,294 @@
+/*************************************************
+* Serpent Source File *
+* (C) 1999-2006 The Botan Project *
+*************************************************/
+
+#include <botan/asm_macr.h>
+
+START_LISTING(serp_asm.S)
+
+#define SBOX1(A, B, C, D, T) \
+ XOR(D, A) ; \
+ ASSIGN(T, B) ; \
+ AND(B, D) ; \
+ XOR(T, C) ; \
+ XOR(B, A) ; \
+ OR(A, D) ; \
+ XOR(A, T) ; \
+ XOR(T, D) ; \
+ XOR(D, C) ; \
+ OR(C, B) ; \
+ XOR(C, T) ; \
+ NOT(T) ; \
+ OR(T, B) ; \
+ XOR(B, D) ; \
+ XOR(B, T) ; \
+ OR(D, A) ; \
+ XOR(B, D) ; \
+ XOR(T, D) ; \
+ ASSIGN(D, A) ; \
+ ASSIGN(A, B) ; \
+ ASSIGN(B, T) ;
+
+#define SBOX2(A, B, C, D, T) \
+ NOT(A) ; \
+ NOT(C) ; \
+ ASSIGN(T, A) ; \
+ AND(A, B) ; \
+ XOR(C, A) ; \
+ OR(A, D) ; \
+ XOR(D, C) ; \
+ XOR(B, A) ; \
+ XOR(A, T) ; \
+ OR(T, B) ; \
+ XOR(B, D) ; \
+ OR(C, A) ; \
+ AND(C, T) ; \
+ XOR(A, B) ; \
+ AND(B, C) ; \
+ XOR(B, A) ; \
+ AND(A, C) ; \
+ XOR(T, A) ; \
+ ASSIGN(A, C) ; \
+ ASSIGN(C, D) ; \
+ ASSIGN(D, B) ; \
+ ASSIGN(B, T) ;
+
+#define SBOX3(A, B, C, D, T) \
+ ASSIGN(T, A) ; \
+ AND(A, C) ; \
+ XOR(A, D) ; \
+ XOR(C, B) ; \
+ XOR(C, A) ; \
+ OR(D, T) ; \
+ XOR(D, B) ; \
+ XOR(T, C) ; \
+ ASSIGN(B, D) ; \
+ OR(D, T) ; \
+ XOR(D, A) ; \
+ AND(A, B) ; \
+ XOR(T, A) ; \
+ XOR(B, D) ; \
+ XOR(B, T) ; \
+ NOT(T) ; \
+ ASSIGN(A, C) ; \
+ ASSIGN(C, B) ; \
+ ASSIGN(B, D) ; \
+ ASSIGN(D, T) ;
+
+#define SBOX4(A, B, C, D, T) \
+ ASSIGN(T, A) ; \
+ OR(A, D) ; \
+ XOR(D, B) ; \
+ AND(B, T) ; \
+ XOR(T, C) ; \
+ XOR(C, D) ; \
+ AND(D, A) ; \
+ OR(T, B) ; \
+ XOR(D, T) ; \
+ XOR(A, B) ; \
+ AND(T, A) ; \
+ XOR(B, D) ; \
+ XOR(T, C) ; \
+ OR(B, A) ; \
+ XOR(B, C) ; \
+ XOR(A, D) ; \
+ ASSIGN(C, B) ; \
+ OR(B, D) ; \
+ XOR(B, A) ; \
+ ASSIGN(A, B) ; \
+ ASSIGN(B, C) ; \
+ ASSIGN(C, D) ; \
+ ASSIGN(D, T) ; \
+
+#define SBOX5(A, B, C, D, T) \
+ XOR(B, D) ; \
+ NOT(D) ; \
+ XOR(C, D) ; \
+ XOR(D, A) ; \
+ ASSIGN(T, B) ; \
+ AND(B, D) ; \
+ XOR(B, C) ; \
+ XOR(T, D) ; \
+ XOR(A, T) ; \
+ AND(C, T) ; \
+ XOR(C, A) ; \
+ AND(A, B) ; \
+ XOR(D, A) ; \
+ OR(T, B) ; \
+ XOR(T, A) ; \
+ OR(A, D) ; \
+ XOR(A, C) ; \
+ AND(C, D) ; \
+ NOT(A) ; \
+ XOR(T, C) ; \
+ ASSIGN(C, A) ; \
+ ASSIGN(A, B) ; \
+ ASSIGN(B, T) ;
+
+#define SBOX6(A, B, C, D, T) \
+ XOR(A, B) ; \
+ XOR(B, D) ; \
+ NOT(D) ; \
+ ASSIGN(T, B) ; \
+ AND(B, A) ; \
+ XOR(C, D) ; \
+ XOR(B, C) ; \
+ OR(C, T) ; \
+ XOR(T, D) ; \
+ AND(D, B) ; \
+ XOR(D, A) ; \
+ XOR(T, B) ; \
+ XOR(T, C) ; \
+ XOR(C, A) ; \
+ AND(A, D) ; \
+ NOT(C) ; \
+ XOR(A, T) ; \
+ OR(T, D) ; \
+ XOR(T, C) ; \
+ ASSIGN(C, A) ; \
+ ASSIGN(A, B) ; \
+ ASSIGN(B, D) ; \
+ ASSIGN(D, T) ; \
+
+#define SBOX7(A, B, C, D, T) \
+ NOT(C) ; \
+ ASSIGN(T, D) ; \
+ AND(D, A) ; \
+ XOR(A, T) ; \
+ XOR(D, C) ; \
+ OR(C, T) ; \
+ XOR(B, D) ; \
+ XOR(C, A) ; \
+ OR(A, B) ; \
+ XOR(C, B) ; \
+ XOR(T, A) ; \
+ OR(A, D) ; \
+ XOR(A, C) ; \
+ XOR(T, D) ; \
+ XOR(T, A) ; \
+ NOT(D) ; \
+ AND(C, T) ; \
+ XOR(C, D) ; \
+ ASSIGN(D, C) ; \
+ ASSIGN(C, T) ;
+
+#define SBOX8(A, B, C, D, T) \
+ ASSIGN(T, B) ; \
+ OR(B, C) ; \
+ XOR(B, D) ; \
+ XOR(T, C) ; \
+ XOR(C, B) ; \
+ OR(D, T) ; \
+ AND(D, A) ; \
+ XOR(T, C) ; \
+ XOR(D, B) ; \
+ OR(B, T) ; \
+ XOR(B, A) ; \
+ OR(A, T) ; \
+ XOR(A, C) ; \
+ XOR(B, T) ; \
+ XOR(C, B) ; \
+ AND(B, A) ; \
+ XOR(B, T) ; \
+ NOT(C) ; \
+ OR(C, A) ; \
+ XOR(T, C) ; \
+ ASSIGN(C, B) ; \
+ ASSIGN(B, D) ; \
+ ASSIGN(D, A) ; \
+ ASSIGN(A, T) ; \
+
+#define TRANSFORM(A, B, C, D, T) \
+ ROTL_IMM(A, 13) ; \
+ ROTL_IMM(C, 3) ; \
+ XOR(B, A) ; \
+ XOR(B, C) ; \
+ XOR(D, C) ; \
+ ASSIGN(T, A) ; \
+ SHL_IMM(T, 3) ; \
+ XOR(D, T) ; \
+ ROTL_IMM(B, 1) ; \
+ ROTL_IMM(D, 7) ; \
+ XOR(A, B) ; \
+ XOR(A, D) ; \
+ XOR(C, D) ; \
+ ASSIGN(T, B) ; \
+ SHL_IMM(T, 7) ; \
+ XOR(C, T) ; \
+ ROTL_IMM(A, 5) ; \
+ ROTL_IMM(C, 22) ;
+
+#define KEY_XOR(A, B, C, D, N) \
+ XOR(A, ARRAY4(EDI, (4*N ))) ; \
+ XOR(B, ARRAY4(EDI, (4*N+1))) ; \
+ XOR(C, ARRAY4(EDI, (4*N+2))) ; \
+ XOR(D, ARRAY4(EDI, (4*N+3))) ; \
+
+START_FUNCTION(serpent_encrypt)
+ SPILL_REGS()
+
+#define PUSHED 4
+
+ ASSIGN(EBP, ARG(1)) /* input block */
+ ASSIGN(EDI, ARG(3)) /* round keys */
+
+ ASSIGN(EAX, ARRAY4(EBP, 0))
+ ASSIGN(EBX, ARRAY4(EBP, 1))
+ ASSIGN(ECX, ARRAY4(EBP, 2))
+ ASSIGN(EDX, ARRAY4(EBP, 3))
+
+ ZEROIZE(EBP)
+
+#define ROUND(A, B, C, D, T, N, SBOX) \
+ KEY_XOR(A, B, C, D, N) \
+ SBOX(A, B, C, D, T) \
+ TRANSFORM(A, B, C, D, T)
+
+ ROUND(EAX, EBX, ECX, EDX, EBP, 0, SBOX1)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 1, SBOX2)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 2, SBOX3)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 3, SBOX4)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 4, SBOX5)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 5, SBOX6)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 6, SBOX7)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 7, SBOX8)
+
+ ROUND(EAX, EBX, ECX, EDX, EBP, 8, SBOX1)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 9, SBOX2)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 10, SBOX3)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 11, SBOX4)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 12, SBOX5)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 13, SBOX6)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 14, SBOX7)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 15, SBOX8)
+
+ ROUND(EAX, EBX, ECX, EDX, EBP, 16, SBOX1)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 17, SBOX2)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 18, SBOX3)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 19, SBOX4)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 20, SBOX5)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 21, SBOX6)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 22, SBOX7)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 23, SBOX8)
+
+ ROUND(EAX, EBX, ECX, EDX, EBP, 24, SBOX1)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 25, SBOX2)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 26, SBOX3)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 27, SBOX4)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 28, SBOX5)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 29, SBOX6)
+ ROUND(EAX, EBX, ECX, EDX, EBP, 30, SBOX7)
+
+ KEY_XOR(EAX, EBX, ECX, EDX, 31)
+ SBOX8(EAX, EBX, ECX, EDX, EBP)
+ KEY_XOR(EAX, EBX, ECX, EDX, 32)
+
+ ASSIGN(EBP, ARG(2)) /* output block */
+ ASSIGN(ARRAY4(EBP, 0), EAX)
+ ASSIGN(ARRAY4(EBP, 1), EBX)
+ ASSIGN(ARRAY4(EBP, 2), ECX)
+ ASSIGN(ARRAY4(EBP, 3), EDX)
+
+ RESTORE_REGS()
+END_FUNCTION(serpent_encrypt)
diff --git a/modules/alg_ia32/serpent.cpp b/modules/alg_ia32/serpent.cpp
new file mode 100644
index 000000000..aaa666ea7
--- /dev/null
+++ b/modules/alg_ia32/serpent.cpp
@@ -0,0 +1,322 @@
+/*************************************************
+* Serpent Source File *
+* (C) 1999-2006 The Botan Project *
+*************************************************/
+
+#include <botan/serpent.h>
+#include <botan/bit_ops.h>
+
+namespace Botan {
+
+namespace {
+
+/*************************************************
+* Serpent Encryption S-Box 1 *
+*************************************************/
+inline void SBoxE1(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3)
+ {
+ u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4;
+ T3 ^= T0; T4 = T1; T1 &= T3; T4 ^= T2; T1 ^= T0; T0 |= T3; T0 ^= T4;
+ T4 ^= T3; T3 ^= T2; T2 |= T1; T2 ^= T4; T4 = ~T4; T4 |= T1; T1 ^= T3;
+ T1 ^= T4; T3 |= T0; T1 ^= T3; T4 ^= T3;
+ B0 = T1; B1 = T4; B2 = T2; B3 = T0;
+ }
+
+/*************************************************
+* Serpent Encryption S-Box 2 *
+*************************************************/
+inline void SBoxE2(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3)
+ {
+ u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4;
+ T0 = ~T0; T2 = ~T2; T4 = T0; T0 &= T1; T2 ^= T0; T0 |= T3; T3 ^= T2;
+ T1 ^= T0; T0 ^= T4; T4 |= T1; T1 ^= T3; T2 |= T0; T2 &= T4; T0 ^= T1;
+ T1 &= T2; T1 ^= T0; T0 &= T2; T0 ^= T4;
+ B0 = T2; B1 = T0; B2 = T3; B3 = T1;
+ }
+
+/*************************************************
+* Serpent Encryption S-Box 3 *
+*************************************************/
+inline void SBoxE3(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3)
+ {
+ u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4;
+ T4 = T0; T0 &= T2; T0 ^= T3; T2 ^= T1; T2 ^= T0; T3 |= T4; T3 ^= T1;
+ T4 ^= T2; T1 = T3; T3 |= T4; T3 ^= T0; T0 &= T1; T4 ^= T0; T1 ^= T3;
+ T1 ^= T4; T4 = ~T4;
+ B0 = T2; B1 = T3; B2 = T1; B3 = T4;
+ }
+
+/*************************************************
+* Serpent Encryption S-Box 4 *
+*************************************************/
+inline void SBoxE4(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3)
+ {
+ u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4;
+ T4 = T0; T0 |= T3; T3 ^= T1; T1 &= T4; T4 ^= T2; T2 ^= T3; T3 &= T0;
+ T4 |= T1; T3 ^= T4; T0 ^= T1; T4 &= T0; T1 ^= T3; T4 ^= T2; T1 |= T0;
+ T1 ^= T2; T0 ^= T3; T2 = T1; T1 |= T3; T1 ^= T0;
+ B0 = T1; B1 = T2; B2 = T3; B3 = T4;
+ }
+
+/*************************************************
+* Serpent Encryption S-Box 5 *
+*************************************************/
+inline void SBoxE5(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3)
+ {
+ u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4;
+ T1 ^= T3; T3 = ~T3; T2 ^= T3; T3 ^= T0; T4 = T1; T1 &= T3; T1 ^= T2;
+ T4 ^= T3; T0 ^= T4; T2 &= T4; T2 ^= T0; T0 &= T1; T3 ^= T0; T4 |= T1;
+ T4 ^= T0; T0 |= T3; T0 ^= T2; T2 &= T3; T0 = ~T0; T4 ^= T2;
+ B0 = T1; B1 = T4; B2 = T0; B3 = T3;
+ }
+/*************************************************
+* Serpent Encryption S-Box 6 *
+*************************************************/
+inline void SBoxE6(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3)
+ {
+ u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4;
+ T0 ^= T1; T1 ^= T3; T3 = ~T3; T4 = T1; T1 &= T0; T2 ^= T3; T1 ^= T2;
+ T2 |= T4; T4 ^= T3; T3 &= T1; T3 ^= T0; T4 ^= T1; T4 ^= T2; T2 ^= T0;
+ T0 &= T3; T2 = ~T2; T0 ^= T4; T4 |= T3; T2 ^= T4;
+ B0 = T1; B1 = T3; B2 = T0; B3 = T2;
+ }
+
+/*************************************************
+* Serpent Encryption S-Box 7 *
+*************************************************/
+inline void SBoxE7(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3)
+ {
+ u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4;
+ T2 = ~T2; T4 = T3; T3 &= T0; T0 ^= T4; T3 ^= T2; T2 |= T4; T1 ^= T3;
+ T2 ^= T0; T0 |= T1; T2 ^= T1; T4 ^= T0; T0 |= T3; T0 ^= T2; T4 ^= T3;
+ T4 ^= T0; T3 = ~T3; T2 &= T4; T2 ^= T3;
+ B0 = T0; B1 = T1; B2 = T4; B3 = T2;
+ }
+
+/*************************************************
+* Serpent Encryption S-Box 8 *
+*************************************************/
+inline void SBoxE8(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3)
+ {
+ u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4;
+ T4 = T1; T1 |= T2; T1 ^= T3; T4 ^= T2; T2 ^= T1; T3 |= T4; T3 &= T0;
+ T4 ^= T2; T3 ^= T1; T1 |= T4; T1 ^= T0; T0 |= T4; T0 ^= T2; T1 ^= T4;
+ T2 ^= T1; T1 &= T0; T1 ^= T4; T2 = ~T2; T2 |= T0; T4 ^= T2;
+ B0 = T4; B1 = T3; B2 = T1; B3 = T0;
+ }
+
+/*************************************************
+* Serpent Decryption S-Box 1 *
+*************************************************/
+inline void SBoxD1(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3)
+ {
+ u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4;
+ T2 = ~T2; T4 = T1; T1 |= T0; T4 = ~T4; T1 ^= T2; T2 |= T4; T1 ^= T3;
+ T0 ^= T4; T2 ^= T0; T0 &= T3; T4 ^= T0; T0 |= T1; T0 ^= T2; T3 ^= T4;
+ T2 ^= T1; T3 ^= T0; T3 ^= T1; T2 &= T3; T4 ^= T2;
+ B0 = T0; B1 = T4; B2 = T1; B3 = T3;
+ }
+
+/*************************************************
+* Serpent Decryption S-Box 2 *
+*************************************************/
+inline void SBoxD2(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3)
+ {
+ u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4;
+ T4 = T1; T1 ^= T3; T3 &= T1; T4 ^= T2; T3 ^= T0; T0 |= T1; T2 ^= T3;
+ T0 ^= T4; T0 |= T2; T1 ^= T3; T0 ^= T1; T1 |= T3; T1 ^= T0; T4 = ~T4;
+ T4 ^= T1; T1 |= T0; T1 ^= T0; T1 |= T4; T3 ^= T1;
+ B0 = T4; B1 = T0; B2 = T3; B3 = T2;
+ }
+
+/*************************************************
+* Serpent Decryption S-Box 3 *
+*************************************************/
+inline void SBoxD3(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3)
+ {
+ u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4;
+ T2 ^= T3; T3 ^= T0; T4 = T3; T3 &= T2; T3 ^= T1; T1 |= T2; T1 ^= T4;
+ T4 &= T3; T2 ^= T3; T4 &= T0; T4 ^= T2; T2 &= T1; T2 |= T0; T3 = ~T3;
+ T2 ^= T3; T0 ^= T3; T0 &= T1; T3 ^= T4; T3 ^= T0;
+ B0 = T1; B1 = T4; B2 = T2; B3 = T3;
+ }
+
+/*************************************************
+* Serpent Decryption S-Box 4 *
+*************************************************/
+inline void SBoxD4(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3)
+ {
+ u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4;
+ T4 = T2; T2 ^= T1; T0 ^= T2; T4 &= T2; T4 ^= T0; T0 &= T1; T1 ^= T3;
+ T3 |= T4; T2 ^= T3; T0 ^= T3; T1 ^= T4; T3 &= T2; T3 ^= T1; T1 ^= T0;
+ T1 |= T2; T0 ^= T3; T1 ^= T4; T0 ^= T1;
+ B0 = T2; B1 = T1; B2 = T3; B3 = T0;
+ }
+
+/*************************************************
+* Serpent Decryption S-Box 5 *
+*************************************************/
+inline void SBoxD5(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3)
+ {
+ u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4;
+ T4 = T2; T2 &= T3; T2 ^= T1; T1 |= T3; T1 &= T0; T4 ^= T2; T4 ^= T1;
+ T1 &= T2; T0 = ~T0; T3 ^= T4; T1 ^= T3; T3 &= T0; T3 ^= T2; T0 ^= T1;
+ T2 &= T0; T3 ^= T0; T2 ^= T4; T2 |= T3; T3 ^= T0; T2 ^= T1;
+ B0 = T0; B1 = T3; B2 = T2; B3 = T4;
+ }
+
+/*************************************************
+* Serpent Decryption S-Box 6 *
+*************************************************/
+inline void SBoxD6(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3)
+ {
+ u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4;
+ T1 = ~T1; T4 = T3; T2 ^= T1; T3 |= T0; T3 ^= T2; T2 |= T1; T2 &= T0;
+ T4 ^= T3; T2 ^= T4; T4 |= T0; T4 ^= T1; T1 &= T2; T1 ^= T3; T4 ^= T2;
+ T3 &= T4; T4 ^= T1; T3 ^= T4; T4 = ~T4; T3 ^= T0;
+ B0 = T1; B1 = T4; B2 = T3; B3 = T2;
+ }
+
+/*************************************************
+* Serpent Decryption S-Box 7 *
+*************************************************/
+inline void SBoxD7(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3)
+ {
+ u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4;
+ T0 ^= T2; T4 = T2; T2 &= T0; T4 ^= T3; T2 = ~T2; T3 ^= T1; T2 ^= T3;
+ T4 |= T0; T0 ^= T2; T3 ^= T4; T4 ^= T1; T1 &= T3; T1 ^= T0; T0 ^= T3;
+ T0 |= T2; T3 ^= T1; T4 ^= T0;
+ B0 = T1; B1 = T2; B2 = T4; B3 = T3;
+ }
+
+/*************************************************
+* Serpent Decryption S-Box 8 *
+*************************************************/
+inline void SBoxD8(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3)
+ {
+ u32bit T0 = B0, T1 = B1, T2 = B2, T3 = B3, T4;
+ T4 = T2; T2 ^= T0; T0 &= T3; T4 |= T3; T2 = ~T2; T3 ^= T1; T1 |= T0;
+ T0 ^= T2; T2 &= T4; T3 &= T4; T1 ^= T2; T2 ^= T0; T0 |= T2; T4 ^= T1;
+ T0 ^= T3; T3 ^= T4; T4 |= T0; T3 ^= T2; T4 ^= T2;
+ B0 = T3; B1 = T0; B2 = T1; B3 = T4;
+ }
+
+/*************************************************
+* Serpent's Inverse Linear Transformation *
+*************************************************/
+inline void i_transform(u32bit& B0, u32bit& B1, u32bit& B2, u32bit& B3)
+ {
+ B2 = rotate_right(B2, 22); B0 = rotate_right(B0, 5);
+ B2 ^= B3 ^ (B1 << 7); B0 ^= B1 ^ B3;
+ B3 = rotate_right(B3, 7); B1 = rotate_right(B1, 1);
+ B3 ^= B2 ^ (B0 << 3); B1 ^= B0 ^ B2;
+ B2 = rotate_right(B2, 3); B0 = rotate_right(B0, 13);
+ }
+
+}
+
+/*************************************************
+* XOR a key block with a data block *
+*************************************************/
+inline void Serpent::key_xor(u32bit round, u32bit& B0, u32bit& B1,
+ u32bit& B2, u32bit& B3) const
+ {
+ B0 ^= round_key[4*round ]; B1 ^= round_key[4*round+1];
+ B2 ^= round_key[4*round+2]; B3 ^= round_key[4*round+3];
+ }
+
+extern "C" void serpent_encrypt(const byte[16], byte[16], const u32bit[132]);
+
+/*************************************************
+* Serpent Encryption *
+*************************************************/
+void Serpent::enc(const byte in[], byte out[]) const
+ {
+ serpent_encrypt(in, out, round_key);
+ }
+
+/*************************************************
+* Serpent Decryption *
+*************************************************/
+void Serpent::dec(const byte in[], byte out[]) const
+ {
+ u32bit B0 = make_u32bit(in[ 3], in[ 2], in[ 1], in[ 0]),
+ B1 = make_u32bit(in[ 7], in[ 6], in[ 5], in[ 4]),
+ B2 = make_u32bit(in[11], in[10], in[ 9], in[ 8]),
+ B3 = make_u32bit(in[15], in[14], in[13], in[12]);
+ key_xor(32,B0,B1,B2,B3); SBoxD8(B0,B1,B2,B3); key_xor(31,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD7(B0,B1,B2,B3); key_xor(30,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD6(B0,B1,B2,B3); key_xor(29,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD5(B0,B1,B2,B3); key_xor(28,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD4(B0,B1,B2,B3); key_xor(27,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD3(B0,B1,B2,B3); key_xor(26,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD2(B0,B1,B2,B3); key_xor(25,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD1(B0,B1,B2,B3); key_xor(24,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD8(B0,B1,B2,B3); key_xor(23,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD7(B0,B1,B2,B3); key_xor(22,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD6(B0,B1,B2,B3); key_xor(21,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD5(B0,B1,B2,B3); key_xor(20,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD4(B0,B1,B2,B3); key_xor(19,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD3(B0,B1,B2,B3); key_xor(18,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD2(B0,B1,B2,B3); key_xor(17,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD1(B0,B1,B2,B3); key_xor(16,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD8(B0,B1,B2,B3); key_xor(15,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD7(B0,B1,B2,B3); key_xor(14,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD6(B0,B1,B2,B3); key_xor(13,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD5(B0,B1,B2,B3); key_xor(12,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD4(B0,B1,B2,B3); key_xor(11,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD3(B0,B1,B2,B3); key_xor(10,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD2(B0,B1,B2,B3); key_xor( 9,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD1(B0,B1,B2,B3); key_xor( 8,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD8(B0,B1,B2,B3); key_xor( 7,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD7(B0,B1,B2,B3); key_xor( 6,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD6(B0,B1,B2,B3); key_xor( 5,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD5(B0,B1,B2,B3); key_xor( 4,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD4(B0,B1,B2,B3); key_xor( 3,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD3(B0,B1,B2,B3); key_xor( 2,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD2(B0,B1,B2,B3); key_xor( 1,B0,B1,B2,B3);
+ i_transform(B0,B1,B2,B3); SBoxD1(B0,B1,B2,B3); key_xor( 0,B0,B1,B2,B3);
+ out[ 0] = get_byte(3, B0); out[ 1] = get_byte(2, B0);
+ out[ 2] = get_byte(1, B0); out[ 3] = get_byte(0, B0);
+ out[ 4] = get_byte(3, B1); out[ 5] = get_byte(2, B1);
+ out[ 6] = get_byte(1, B1); out[ 7] = get_byte(0, B1);
+ out[ 8] = get_byte(3, B2); out[ 9] = get_byte(2, B2);
+ out[10] = get_byte(1, B2); out[11] = get_byte(0, B2);
+ out[12] = get_byte(3, B3); out[13] = get_byte(2, B3);
+ out[14] = get_byte(1, B3); out[15] = get_byte(0, B3);
+ }
+
+/*************************************************
+* Serpent Key Schedule *
+*************************************************/
+void Serpent::key(const byte key[], u32bit length)
+ {
+ const u32bit PHI = 0x9E3779B9;
+
+ SecureBuffer<u32bit, 140> W;
+ for(u32bit j = 0; j != length / 4; ++j)
+ W[j] = make_u32bit(key[4*j+3], key[4*j+2], key[4*j+1], key[4*j]);
+ W[length / 4] |= u32bit(1) << ((length%4)*8);
+ for(u32bit j = 8; j != 140; ++j)
+ W[j] = rotate_left(W[j-8] ^ W[j-5] ^ W[j-3] ^ W[j-1] ^ PHI ^ (j-8), 11);
+ SBoxE4(W[ 8],W[ 9],W[ 10],W[ 11]); SBoxE3(W[ 12],W[ 13],W[ 14],W[ 15]);
+ SBoxE2(W[ 16],W[ 17],W[ 18],W[ 19]); SBoxE1(W[ 20],W[ 21],W[ 22],W[ 23]);
+ SBoxE8(W[ 24],W[ 25],W[ 26],W[ 27]); SBoxE7(W[ 28],W[ 29],W[ 30],W[ 31]);
+ SBoxE6(W[ 32],W[ 33],W[ 34],W[ 35]); SBoxE5(W[ 36],W[ 37],W[ 38],W[ 39]);
+ SBoxE4(W[ 40],W[ 41],W[ 42],W[ 43]); SBoxE3(W[ 44],W[ 45],W[ 46],W[ 47]);
+ SBoxE2(W[ 48],W[ 49],W[ 50],W[ 51]); SBoxE1(W[ 52],W[ 53],W[ 54],W[ 55]);
+ SBoxE8(W[ 56],W[ 57],W[ 58],W[ 59]); SBoxE7(W[ 60],W[ 61],W[ 62],W[ 63]);
+ SBoxE6(W[ 64],W[ 65],W[ 66],W[ 67]); SBoxE5(W[ 68],W[ 69],W[ 70],W[ 71]);
+ SBoxE4(W[ 72],W[ 73],W[ 74],W[ 75]); SBoxE3(W[ 76],W[ 77],W[ 78],W[ 79]);
+ SBoxE2(W[ 80],W[ 81],W[ 82],W[ 83]); SBoxE1(W[ 84],W[ 85],W[ 86],W[ 87]);
+ SBoxE8(W[ 88],W[ 89],W[ 90],W[ 91]); SBoxE7(W[ 92],W[ 93],W[ 94],W[ 95]);
+ SBoxE6(W[ 96],W[ 97],W[ 98],W[ 99]); SBoxE5(W[100],W[101],W[102],W[103]);
+ SBoxE4(W[104],W[105],W[106],W[107]); SBoxE3(W[108],W[109],W[110],W[111]);
+ SBoxE2(W[112],W[113],W[114],W[115]); SBoxE1(W[116],W[117],W[118],W[119]);
+ SBoxE8(W[120],W[121],W[122],W[123]); SBoxE7(W[124],W[125],W[126],W[127]);
+ SBoxE6(W[128],W[129],W[130],W[131]); SBoxE5(W[132],W[133],W[134],W[135]);
+ SBoxE4(W[136],W[137],W[138],W[139]);
+ round_key.copy(W + 8, 132);
+ }
+
+}