?? sosemanuk.cpp
字號:
(
".intel_syntax noprefix;"
AS_PUSH_IF86( bx)
#else
word32 *state = m_state;
AS2( mov WORD_REG(ax), state)
AS2( mov WORD_REG(di), output)
AS2( mov WORD_REG(dx), input)
AS2( mov WORD_REG(cx), iterationCount)
#endif
#endif // #ifdef CRYPTOPP_GENERATE_X64_MASM
#if defined(__GNUC__) && CRYPTOPP_BOOL_X64
#define SSE2_workspace %5
#else
#define SSE2_workspace WORD_REG(sp)
#endif
#define SSE2_output WORD_PTR [SSE2_workspace+1*WORD_SZ]
#define SSE2_input WORD_PTR [SSE2_workspace+2*WORD_SZ]
#define SSE2_wordsLeft WORD_PTR [SSE2_workspace+3*WORD_SZ]
#define SSE2_diEnd WORD_PTR [SSE2_workspace+4*WORD_SZ]
#define SSE2_pMulTables WORD_PTR [SSE2_workspace+5*WORD_SZ]
#define SSE2_state WORD_PTR [SSE2_workspace+6*WORD_SZ]
#define SSE2_wordsLeft2 WORD_PTR [SSE2_workspace+7*WORD_SZ]
#define SSE2_stateCopy SSE2_workspace + 8*WORD_SZ
#define SSE2_uvStart SSE2_stateCopy + 12*4
#if CRYPTOPP_BOOL_X86
AS_PUSH_IF86( bp)
AS2( mov AS_REG_6, esp)
AS2( and esp, -16)
AS2( sub esp, 80*4*2+12*4+8*WORD_SZ) // 80 v's, 80 u's, 12 state, 8 locals
AS2( mov [esp], AS_REG_6)
#endif
AS2( mov SSE2_output, WORD_REG(di))
AS2( mov SSE2_input, WORD_REG(dx))
AS2( mov SSE2_state, WORD_REG(ax))
#ifndef _MSC_VER
AS2( mov SSE2_pMulTables, WORD_REG(si))
#endif
AS2( lea WORD_REG(cx), [4*WORD_REG(cx)+WORD_REG(cx)])
AS2( lea WORD_REG(si), [4*WORD_REG(cx)])
AS2( mov SSE2_wordsLeft, WORD_REG(si))
AS2( movdqa xmm0, [WORD_REG(ax)+0*16]) // copy state to stack to save a register
AS2( movdqa [SSE2_stateCopy+0*16], xmm0)
AS2( movdqa xmm0, [WORD_REG(ax)+1*16])
AS2( movdqa [SSE2_stateCopy+1*16], xmm0)
AS2( movq xmm0, QWORD PTR [WORD_REG(ax)+2*16])
AS2( movq QWORD PTR [SSE2_stateCopy+2*16], xmm0)
AS2( psrlq xmm0, 32)
AS2( movd AS_REG_6d, xmm0) // s(9)
AS2( mov ecx, [WORD_REG(ax)+10*4])
AS2( mov edx, [WORD_REG(ax)+11*4])
AS2( pcmpeqb xmm7, xmm7) // all ones
#define s(i) SSE2_stateCopy + ASM_MOD(i,10)*4
#define u(j) WORD_REG(di) + (ASM_MOD(j,4)*20 + (j/4)) * 4
#define v(j) WORD_REG(di) + (ASM_MOD(j,4)*20 + (j/4)) * 4 + 80*4
#define R10 ecx
#define R11 edx
#define R20 edx
#define R21 ecx
#define SSE2_STEP(i, j) \
AS2( mov eax, [s(i+0)])\
AS2( mov [v(i)], eax)\
AS2( rol eax, 8)\
AS2( lea AS_REG_7d, [AS_REG_6d + R2##j])\
AS2( xor AS_REG_7d, R1##j)\
AS2( mov [u(i)], AS_REG_7d)\
AS2( mov AS_REG_7d, 1)\
AS2( and AS_REG_7d, R2##j)\
AS1( neg AS_REG_7d)\
AS2( and AS_REG_7d, AS_REG_6d)\
AS2( xor AS_REG_6d, eax)\
AS2( movzx eax, al)\
AS2( xor AS_REG_6d, [WORD_REG(si)+WORD_REG(ax)*4])\
AS2( mov eax, [s(i+3)])\
AS2( xor AS_REG_7d, [s(i+2)])\
AS2( add R1##j, AS_REG_7d)\
AS2( movzx AS_REG_7d, al)\
AS2( shr eax, 8)\
AS2( xor AS_REG_6d, [WORD_REG(si)+1024+AS_REG_7*4])\
AS2( xor AS_REG_6d, eax)\
AS2( imul R2##j, AS_HEX(54655307))\
AS2( rol R2##j, 7)\
AS2( mov [s(i+0)], AS_REG_6d)\
ASL(2) // outer loop, each iteration of this processes 80 words
AS2( lea WORD_REG(di), [SSE2_uvStart]) // start of v and u
AS2( mov WORD_REG(ax), 80)
AS2( cmp WORD_REG(si), 80)
AS2( cmovg WORD_REG(si), WORD_REG(ax))
AS2( mov SSE2_wordsLeft2, WORD_REG(si))
AS2( lea WORD_REG(si), [WORD_REG(di)+WORD_REG(si)]) // use to end first inner loop
AS2( mov SSE2_diEnd, WORD_REG(si))
#ifdef _MSC_VER
AS2( lea WORD_REG(si), s_sosemanukMulTables)
#else
AS2( mov WORD_REG(si), SSE2_pMulTables)
#endif
ASL(0) // first inner loop, 20 words each, 4 iterations
SSE2_STEP(0, 0)
SSE2_STEP(1, 1)
SSE2_STEP(2, 0)
SSE2_STEP(3, 1)
SSE2_STEP(4, 0)
SSE2_STEP(5, 1)
SSE2_STEP(6, 0)
SSE2_STEP(7, 1)
SSE2_STEP(8, 0)
SSE2_STEP(9, 1)
SSE2_STEP(10, 0)
SSE2_STEP(11, 1)
SSE2_STEP(12, 0)
SSE2_STEP(13, 1)
SSE2_STEP(14, 0)
SSE2_STEP(15, 1)
SSE2_STEP(16, 0)
SSE2_STEP(17, 1)
SSE2_STEP(18, 0)
SSE2_STEP(19, 1)
// loop
AS2( add WORD_REG(di), 5*4)
AS2( cmp WORD_REG(di), SSE2_diEnd)
ASJ( jne, 0, b)
AS2( mov WORD_REG(ax), SSE2_input)
AS2( mov AS_REG_7, SSE2_output)
AS2( lea WORD_REG(di), [SSE2_uvStart]) // start of v and u
AS2( mov WORD_REG(si), SSE2_wordsLeft2)
ASL(1) // second inner loop, 16 words each, 5 iterations
AS2( movdqa xmm0, [WORD_REG(di)+0*20*4])
AS2( movdqa xmm2, [WORD_REG(di)+2*20*4])
AS2( movdqa xmm3, [WORD_REG(di)+3*20*4])
AS2( movdqa xmm1, [WORD_REG(di)+1*20*4])
// S2
AS2( movdqa xmm4, xmm0)
AS2( pand xmm0, xmm2)
AS2( pxor xmm0, xmm3)
AS2( pxor xmm2, xmm1)
AS2( pxor xmm2, xmm0)
AS2( por xmm3, xmm4)
AS2( pxor xmm3, xmm1)
AS2( pxor xmm4, xmm2)
AS2( movdqa xmm1, xmm3)
AS2( por xmm3, xmm4)
AS2( pxor xmm3, xmm0)
AS2( pand xmm0, xmm1)
AS2( pxor xmm4, xmm0)
AS2( pxor xmm1, xmm3)
AS2( pxor xmm1, xmm4)
AS2( pxor xmm4, xmm7)
// xor with v
AS2( pxor xmm2, [WORD_REG(di)+80*4])
AS2( pxor xmm3, [WORD_REG(di)+80*5])
AS2( pxor xmm1, [WORD_REG(di)+80*6])
AS2( pxor xmm4, [WORD_REG(di)+80*7])
// exit loop early if less than 16 words left to output
// this is necessary because block size is 20 words, and we output 16 words in each iteration of this loop
AS2( cmp WORD_REG(si), 16)
ASJ( jl, 4, f)
// unpack
AS2( movdqa xmm6, xmm2)
AS2( punpckldq xmm2, xmm3)
AS2( movdqa xmm5, xmm1)
AS2( punpckldq xmm1, xmm4)
AS2( movdqa xmm0, xmm2)
AS2( punpcklqdq xmm2, xmm1)
AS2( punpckhqdq xmm0, xmm1)
AS2( punpckhdq xmm6, xmm3)
AS2( punpckhdq xmm5, xmm4)
AS2( movdqa xmm3, xmm6)
AS2( punpcklqdq xmm6, xmm5)
AS2( punpckhqdq xmm3, xmm5)
// output keystream
AS_XMM_OUTPUT4(SSE2_Sosemanuk_Output, WORD_REG(ax), AS_REG_7, 2,0,6,3, 1, 0,1,2,3, 4)
// loop
AS2( add WORD_REG(di), 4*4)
AS2( sub WORD_REG(si), 16)
ASJ( jnz, 1, b)
// outer loop
AS2( mov WORD_REG(si), SSE2_wordsLeft)
AS2( sub WORD_REG(si), 80)
ASJ( jz, 6, f)
AS2( mov SSE2_wordsLeft, WORD_REG(si))
AS2( mov SSE2_input, WORD_REG(ax))
AS2( mov SSE2_output, AS_REG_7)
ASJ( jmp, 2, b)
ASL(4) // final output of less than 16 words
AS2( test WORD_REG(ax), WORD_REG(ax))
ASJ( jz, 5, f)
AS2( movd xmm0, dword ptr [WORD_REG(ax)+0*4])
AS2( pxor xmm2, xmm0)
AS2( movd xmm0, dword ptr [WORD_REG(ax)+1*4])
AS2( pxor xmm3, xmm0)
AS2( movd xmm0, dword ptr [WORD_REG(ax)+2*4])
AS2( pxor xmm1, xmm0)
AS2( movd xmm0, dword ptr [WORD_REG(ax)+3*4])
AS2( pxor xmm4, xmm0)
AS2( add WORD_REG(ax), 16)
ASL(5)
AS2( movd dword ptr [AS_REG_7+0*4], xmm2)
AS2( movd dword ptr [AS_REG_7+1*4], xmm3)
AS2( movd dword ptr [AS_REG_7+2*4], xmm1)
AS2( movd dword ptr [AS_REG_7+3*4], xmm4)
AS2( sub WORD_REG(si), 4)
ASJ( jz, 6, f)
AS2( add AS_REG_7, 16)
AS2( psrldq xmm2, 4)
AS2( psrldq xmm3, 4)
AS2( psrldq xmm1, 4)
AS2( psrldq xmm4, 4)
ASJ( jmp, 4, b)
ASL(6) // save state
AS2( mov AS_REG_6, SSE2_state)
AS2( movdqa xmm0, [SSE2_stateCopy+0*16])
AS2( movdqa [AS_REG_6+0*16], xmm0)
AS2( movdqa xmm0, [SSE2_stateCopy+1*16])
AS2( movdqa [AS_REG_6+1*16], xmm0)
AS2( movq xmm0, QWORD PTR [SSE2_stateCopy+2*16])
AS2( movq QWORD PTR [AS_REG_6+2*16], xmm0)
AS2( mov [AS_REG_6+10*4], ecx)
AS2( mov [AS_REG_6+11*4], edx)
AS_POP_IF86( sp)
AS_POP_IF86( bp)
#ifdef __GNUC__
AS_POP_IF86( bx)
".att_syntax prefix;"
:
: "a" (m_state.m_ptr), "c" (iterationCount), "S" (s_sosemanukMulTables), "D" (output), "d" (input)
#if CRYPTOPP_BOOL_X64
, "r" (workspace)
: "memory", "cc", "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7"
#else
: "memory", "cc"
#endif
);
#endif
#ifdef CRYPTOPP_GENERATE_X64_MASM
movdqa xmm6, [rsp + 02f0h]
movdqa xmm7, [rsp + 0300h]
add rsp, 80*4*2+12*4+8*WORD_SZ + 2*16+8
pop rdi
pop rsi
ret
Sosemanuk_OperateKeystream ENDP
#else
}
else
#endif
#endif
#ifndef CRYPTOPP_GENERATE_X64_MASM
{
#if CRYPTOPP_BOOL_X86 | CRYPTOPP_BOOL_X64
#define MUL_A(x) (x = rotlFixed(x, 8), x ^ s_sosemanukMulTables[byte(x)])
#else
#define MUL_A(x) (((x) << 8) ^ s_sosemanukMulTables[(x) >> 24])
#endif
#define DIV_A(x) (((x) >> 8) ^ s_sosemanukMulTables[256 + byte(x)])
#define r1(i) ((i%2) ? reg2 : reg1)
#define r2(i) ((i%2) ? reg1 : reg2)
#define STEP(x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, v, u) \
u = (s##x9 + r2(x0)) ^ r1(x0);\
v = s##x0;\
s##x0 = MUL_A(s##x0) ^ DIV_A(s##x3) ^ s##x9;\
r1(x0) += XMUX(r2(x0), s##x2, s##x9);\
r2(x0) = rotlFixed(r2(x0) * 0x54655307, 7);\
#define SOSEMANUK_OUTPUT(x) \
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 0, u2 ^ v0);\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 1, u3 ^ v1);\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 2, u1 ^ v2);\
CRYPTOPP_KEYSTREAM_OUTPUT_WORD(x, LITTLE_ENDIAN_ORDER, 3, u4 ^ v3);
#define OUTPUT4 \
S2(0, u0, u1, u2, u3, u4);\
CRYPTOPP_KEYSTREAM_OUTPUT_SWITCH(SOSEMANUK_OUTPUT, 4*4);
word32 s0 = m_state[0];
word32 s1 = m_state[1];
word32 s2 = m_state[2];
word32 s3 = m_state[3];
word32 s4 = m_state[4];
word32 s5 = m_state[5];
word32 s6 = m_state[6];
word32 s7 = m_state[7];
word32 s8 = m_state[8];
word32 s9 = m_state[9];
word32 reg1 = m_state[10];
word32 reg2 = m_state[11];
word32 u0, u1, u2, u3, u4, v0, v1, v2, v3;
do
{
STEP(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, v0, u0)
STEP(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, v1, u1)
STEP(2, 3, 4, 5, 6, 7, 8, 9, 0, 1, v2, u2)
STEP(3, 4, 5, 6, 7, 8, 9, 0, 1, 2, v3, u3)
OUTPUT4
STEP(4, 5, 6, 7, 8, 9, 0, 1, 2, 3, v0, u0)
STEP(5, 6, 7, 8, 9, 0, 1, 2, 3, 4, v1, u1)
STEP(6, 7, 8, 9, 0, 1, 2, 3, 4, 5, v2, u2)
STEP(7, 8, 9, 0, 1, 2, 3, 4, 5, 6, v3, u3)
OUTPUT4
STEP(8, 9, 0, 1, 2, 3, 4, 5, 6, 7, v0, u0)
STEP(9, 0, 1, 2, 3, 4, 5, 6, 7, 8, v1, u1)
STEP(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, v2, u2)
STEP(1, 2, 3, 4, 5, 6, 7, 8, 9, 0, v3, u3)
OUTPUT4
STEP(2, 3, 4, 5, 6, 7, 8, 9, 0, 1, v0, u0)
STEP(3, 4, 5, 6, 7, 8, 9, 0, 1, 2, v1, u1)
STEP(4, 5, 6, 7, 8, 9, 0, 1, 2, 3, v2, u2)
STEP(5, 6, 7, 8, 9, 0, 1, 2, 3, 4, v3, u3)
OUTPUT4
STEP(6, 7, 8, 9, 0, 1, 2, 3, 4, 5, v0, u0)
STEP(7, 8, 9, 0, 1, 2, 3, 4, 5, 6, v1, u1)
STEP(8, 9, 0, 1, 2, 3, 4, 5, 6, 7, v2, u2)
STEP(9, 0, 1, 2, 3, 4, 5, 6, 7, 8, v3, u3)
OUTPUT4
}
while (--iterationCount);
m_state[0] = s0;
m_state[1] = s1;
m_state[2] = s2;
m_state[3] = s3;
m_state[4] = s4;
m_state[5] = s5;
m_state[6] = s6;
m_state[7] = s7;
m_state[8] = s8;
m_state[9] = s9;
m_state[10] = reg1;
m_state[11] = reg2;
}
}
NAMESPACE_END
#endif // #ifndef CRYPTOPP_GENERATE_X64_MASM
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -