aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--modules/alg_ia32/asm_macr.h8
-rw-r--r--modules/alg_ia32/serp_asm.S90
-rw-r--r--modules/alg_ia32/serpent.cpp122
3 files changed, 98 insertions, 122 deletions
diff --git a/modules/alg_ia32/asm_macr.h b/modules/alg_ia32/asm_macr.h
index 8643e0768..8e930ef96 100644
--- a/modules/alg_ia32/asm_macr.h
+++ b/modules/alg_ia32/asm_macr.h
@@ -55,10 +55,10 @@ func_name:
/*************************************************
* Memory Access Operations *
*************************************************/
-#define ARRAY1(REG, NUM) NUM(REG)
-#define ARRAY4(REG, NUM) 4*NUM(REG)
-#define ARRAY4_INDIRECT(BASE, OFFSET, NUM) 4*NUM(BASE,OFFSET,4)
-#define ARG(NUM) 4*PUSHED + ARRAY4(ESP, NUM)
+#define ARRAY1(REG, NUM) (NUM)(REG)
+#define ARRAY4(REG, NUM) 4*(NUM)(REG)
+#define ARRAY4_INDIRECT(BASE, OFFSET, NUM) 4*(NUM)(BASE,OFFSET,4)
+#define ARG(NUM) 4*(PUSHED) + ARRAY4(ESP, NUM)
#define ASSIGN(TO, FROM) movl FROM, TO
#define ASSIGN_BYTE(TO, FROM) movzbl FROM, TO
diff --git a/modules/alg_ia32/serp_asm.S b/modules/alg_ia32/serp_asm.S
index ec84ffbc5..e6c8c3cb3 100644
--- a/modules/alg_ia32/serp_asm.S
+++ b/modules/alg_ia32/serp_asm.S
@@ -438,6 +438,9 @@ START_LISTING(serp_asm.S)
XOR(C, ARRAY4(EDI, (4*N+2))) ; \
XOR(D, ARRAY4(EDI, (4*N+3))) ; \
+/*************************************************
+* Serpent Encryption *
+*************************************************/
START_FUNCTION(serpent_encrypt)
SPILL_REGS()
@@ -506,6 +509,9 @@ START_FUNCTION(serpent_encrypt)
RESTORE_REGS()
END_FUNCTION(serpent_encrypt)
+/*************************************************
+* Serpent Decryption *
+*************************************************/
START_FUNCTION(serpent_decrypt)
SPILL_REGS()
@@ -572,3 +578,87 @@ START_FUNCTION(serpent_decrypt)
RESTORE_REGS()
END_FUNCTION(serpent_decrypt)
+/*************************************************
+* Serpent Key Schedule *
+*************************************************/
+START_FUNCTION(serpent_key_schedule)
+ SPILL_REGS()
+
+ ASSIGN(EDI, ARG(1)) /* round keys */
+ ASSIGN(ESI, IMM(8))
+ ADD_IMM(EDI, 32)
+
+START_LOOP(.EXPANSION)
+
+ ASSIGN(EAX, ESI)
+ SUB_IMM(EAX, 8)
+
+ XOR(EAX, ARRAY4(EDI, -1))
+ XOR(EAX, ARRAY4(EDI, -3))
+ XOR(EAX, ARRAY4(EDI, -5))
+ XOR(EAX, ARRAY4(EDI, -8))
+ XOR(EAX, IMM(0x9E3779B9))
+
+ ROTL_IMM(EAX, 11)
+
+ ASSIGN(ARRAY4(EDI, 0), EAX)
+
+ ADD_IMM(EDI, 4)
+ ADD_IMM(ESI, 1)
+LOOP_UNTIL(ESI, IMM(140), .EXPANSION)
+
+
+ ASSIGN(EDI, ARG(1)) /* round keys */
+
+#define LOAD_AND_SBOX(MSG, SBOX) \
+ ASSIGN(EAX, ARRAY4(EDI, (4*MSG+ 8))) ; \
+ ASSIGN(EBX, ARRAY4(EDI, (4*MSG+ 9))) ; \
+ ASSIGN(ECX, ARRAY4(EDI, (4*MSG+10))) ; \
+ ASSIGN(EDX, ARRAY4(EDI, (4*MSG+11))) ; \
+ SBOX(EAX, EBX, ECX, EDX, EBP) ; \
+ ASSIGN(ARRAY4(EDI, (4*MSG+ 8)), EAX) ; \
+ ASSIGN(ARRAY4(EDI, (4*MSG+ 9)), EBX) ; \
+ ASSIGN(ARRAY4(EDI, (4*MSG+10)), ECX) ; \
+ ASSIGN(ARRAY4(EDI, (4*MSG+11)), EDX)
+
+ LOAD_AND_SBOX( 0, SBOX_E4)
+ LOAD_AND_SBOX( 1, SBOX_E3)
+ LOAD_AND_SBOX( 2, SBOX_E2)
+ LOAD_AND_SBOX( 3, SBOX_E1)
+
+ LOAD_AND_SBOX( 4, SBOX_E8)
+ LOAD_AND_SBOX( 5, SBOX_E7)
+ LOAD_AND_SBOX( 6, SBOX_E6)
+ LOAD_AND_SBOX( 7, SBOX_E5)
+ LOAD_AND_SBOX( 8, SBOX_E4)
+ LOAD_AND_SBOX( 9, SBOX_E3)
+ LOAD_AND_SBOX(10, SBOX_E2)
+ LOAD_AND_SBOX(11, SBOX_E1)
+
+ LOAD_AND_SBOX(12, SBOX_E8)
+ LOAD_AND_SBOX(13, SBOX_E7)
+ LOAD_AND_SBOX(14, SBOX_E6)
+ LOAD_AND_SBOX(15, SBOX_E5)
+ LOAD_AND_SBOX(16, SBOX_E4)
+ LOAD_AND_SBOX(17, SBOX_E3)
+ LOAD_AND_SBOX(18, SBOX_E2)
+ LOAD_AND_SBOX(19, SBOX_E1)
+
+ LOAD_AND_SBOX(20, SBOX_E8)
+ LOAD_AND_SBOX(21, SBOX_E7)
+ LOAD_AND_SBOX(22, SBOX_E6)
+ LOAD_AND_SBOX(23, SBOX_E5)
+ LOAD_AND_SBOX(24, SBOX_E4)
+ LOAD_AND_SBOX(25, SBOX_E3)
+ LOAD_AND_SBOX(26, SBOX_E2)
+ LOAD_AND_SBOX(27, SBOX_E1)
+
+ LOAD_AND_SBOX(28, SBOX_E8)
+ LOAD_AND_SBOX(29, SBOX_E7)
+ LOAD_AND_SBOX(30, SBOX_E6)
+ LOAD_AND_SBOX(31, SBOX_E5)
+ LOAD_AND_SBOX(32, SBOX_E4)
+
+ RESTORE_REGS()
+END_FUNCTION(serpent_key_schedule)
+
diff --git a/modules/alg_ia32/serpent.cpp b/modules/alg_ia32/serpent.cpp
index c6db285c1..4bae0a79c 100644
--- a/modules/alg_ia32/serpent.cpp
+++ b/modules/alg_ia32/serpent.cpp
@@ -8,107 +8,9 @@
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;
- }
-
-}
-
extern "C" void serpent_encrypt(const byte[16], byte[16], const u32bit[132]);
extern "C" void serpent_decrypt(const byte[16], byte[16], const u32bit[132]);
+extern "C" void serpent_key_schedule(u32bit[140]);
/*************************************************
* Serpent Encryption *
@@ -137,25 +39,9 @@ void Serpent::key(const byte key[], u32bit length)
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]);
+
+ serpent_key_schedule(W);
+
round_key.copy(W + 8, 132);
}