2002-10-04 17:31:41 +00:00
|
|
|
// panama.cpp - written and placed in the public domain by Wei Dai
|
|
|
|
|
2007-09-24 00:43:57 +00:00
|
|
|
// use "cl /EP /P /DCRYPTOPP_GENERATE_X64_MASM panama.cpp" to generate MASM code
|
|
|
|
|
2002-10-04 17:31:41 +00:00
|
|
|
#include "pch.h"
|
2007-09-24 00:43:57 +00:00
|
|
|
|
|
|
|
#ifndef CRYPTOPP_GENERATE_X64_MASM
|
|
|
|
|
2002-10-04 17:31:41 +00:00
|
|
|
#include "panama.h"
|
|
|
|
#include "misc.h"
|
2007-04-15 22:54:31 +00:00
|
|
|
#include "cpu.h"
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
|
|
|
template <class B>
|
|
|
|
void Panama<B>::Reset()
|
|
|
|
{
|
2007-04-15 22:54:31 +00:00
|
|
|
memset(m_state, 0, m_state.SizeInBytes());
|
|
|
|
#if CRYPTOPP_BOOL_SSSE3_ASM_AVAILABLE
|
|
|
|
m_state[17] = HasSSSE3();
|
|
|
|
#endif
|
2002-10-04 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
2007-09-24 00:43:57 +00:00
|
|
|
#endif // #ifndef CRYPTOPP_GENERATE_X64_MASM
|
2007-04-15 22:54:31 +00:00
|
|
|
|
2007-09-24 00:43:57 +00:00
|
|
|
#ifdef CRYPTOPP_X64_MASM_AVAILABLE
|
|
|
|
extern "C" {
|
|
|
|
void Panama_SSE2_Pull(size_t count, word32 *state, word32 *z, const word32 *y);
|
|
|
|
}
|
|
|
|
#elif CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE
|
|
|
|
|
|
|
|
#ifdef CRYPTOPP_GENERATE_X64_MASM
|
|
|
|
Panama_SSE2_Pull PROC FRAME
|
2009-03-29 21:41:30 +00:00
|
|
|
rex_push_reg rdi
|
|
|
|
alloc_stack(2*16)
|
2007-09-24 00:43:57 +00:00
|
|
|
save_xmm128 xmm6, 0h
|
|
|
|
save_xmm128 xmm7, 10h
|
|
|
|
.endprolog
|
|
|
|
#else
|
2007-04-15 22:54:31 +00:00
|
|
|
#pragma warning(disable: 4731) // frame pointer register 'ebp' modified by inline assembly code
|
2007-09-24 05:35:59 +00:00
|
|
|
void CRYPTOPP_NOINLINE Panama_SSE2_Pull(size_t count, word32 *state, word32 *z, const word32 *y)
|
2007-04-15 22:54:31 +00:00
|
|
|
{
|
2010-08-08 23:02:36 +00:00
|
|
|
#ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
|
|
|
|
asm __volatile__
|
2007-04-15 22:54:31 +00:00
|
|
|
(
|
|
|
|
".intel_syntax noprefix;"
|
2007-09-24 05:35:59 +00:00
|
|
|
AS_PUSH_IF86( bx)
|
2007-04-15 22:54:31 +00:00
|
|
|
#else
|
2007-09-24 00:43:57 +00:00
|
|
|
AS2( mov AS_REG_1, count)
|
|
|
|
AS2( mov AS_REG_2, state)
|
|
|
|
AS2( mov AS_REG_3, z)
|
|
|
|
AS2( mov AS_REG_4, y)
|
2007-04-15 22:54:31 +00:00
|
|
|
#endif
|
2007-09-24 00:43:57 +00:00
|
|
|
#endif // #ifdef CRYPTOPP_GENERATE_X64_MASM
|
2007-04-15 22:54:31 +00:00
|
|
|
|
2007-09-24 00:43:57 +00:00
|
|
|
#if CRYPTOPP_BOOL_X86
|
|
|
|
#define REG_loopEnd [esp]
|
|
|
|
#elif defined(CRYPTOPP_GENERATE_X64_MASM)
|
|
|
|
#define REG_loopEnd rdi
|
|
|
|
#else
|
|
|
|
#define REG_loopEnd r8
|
|
|
|
#endif
|
2007-04-15 22:54:31 +00:00
|
|
|
|
2007-09-24 00:43:57 +00:00
|
|
|
AS2( shl AS_REG_1, 5)
|
|
|
|
ASJ( jz, 5, f)
|
|
|
|
AS2( mov AS_REG_6d, [AS_REG_2+4*17])
|
|
|
|
AS2( add AS_REG_1, AS_REG_6)
|
|
|
|
|
|
|
|
#if CRYPTOPP_BOOL_X64
|
|
|
|
AS2( mov REG_loopEnd, AS_REG_1)
|
|
|
|
#else
|
|
|
|
AS1( push ebp)
|
|
|
|
AS1( push AS_REG_1)
|
|
|
|
#endif
|
|
|
|
|
2007-09-24 01:19:38 +00:00
|
|
|
AS2( movdqa xmm0, XMMWORD_PTR [AS_REG_2+0*16])
|
|
|
|
AS2( movdqa xmm1, XMMWORD_PTR [AS_REG_2+1*16])
|
|
|
|
AS2( movdqa xmm2, XMMWORD_PTR [AS_REG_2+2*16])
|
|
|
|
AS2( movdqa xmm3, XMMWORD_PTR [AS_REG_2+3*16])
|
2007-09-24 00:43:57 +00:00
|
|
|
AS2( mov eax, dword ptr [AS_REG_2+4*16])
|
2007-04-15 22:54:31 +00:00
|
|
|
|
|
|
|
ASL(4)
|
|
|
|
// gamma and pi
|
|
|
|
#if CRYPTOPP_BOOL_SSSE3_ASM_AVAILABLE
|
2007-09-24 00:43:57 +00:00
|
|
|
AS2( test AS_REG_6, 1)
|
2007-04-15 22:54:31 +00:00
|
|
|
ASJ( jnz, 6, f)
|
|
|
|
#endif
|
|
|
|
AS2( movdqa xmm6, xmm2)
|
|
|
|
AS2( movss xmm6, xmm3)
|
|
|
|
ASS( pshufd xmm5, xmm6, 0, 3, 2, 1)
|
|
|
|
AS2( movd xmm6, eax)
|
|
|
|
AS2( movdqa xmm7, xmm3)
|
|
|
|
AS2( movss xmm7, xmm6)
|
|
|
|
ASS( pshufd xmm6, xmm7, 0, 3, 2, 1)
|
|
|
|
#if CRYPTOPP_BOOL_SSSE3_ASM_AVAILABLE
|
|
|
|
ASJ( jmp, 7, f)
|
|
|
|
ASL(6)
|
|
|
|
AS2( movdqa xmm5, xmm3)
|
|
|
|
AS3( palignr xmm5, xmm2, 4)
|
|
|
|
AS2( movd xmm6, eax)
|
|
|
|
AS3( palignr xmm6, xmm3, 4)
|
|
|
|
ASL(7)
|
|
|
|
#endif
|
|
|
|
|
2007-09-24 00:43:57 +00:00
|
|
|
AS2( movd AS_REG_1d, xmm2)
|
|
|
|
AS1( not AS_REG_1d)
|
|
|
|
AS2( movd AS_REG_7d, xmm3)
|
|
|
|
AS2( or AS_REG_1d, AS_REG_7d)
|
|
|
|
AS2( xor eax, AS_REG_1d)
|
2007-04-15 22:54:31 +00:00
|
|
|
|
|
|
|
#define SSE2_Index(i) ASM_MOD(((i)*13+16), 17)
|
|
|
|
|
|
|
|
#define pi(i) \
|
2007-09-24 00:43:57 +00:00
|
|
|
AS2( movd AS_REG_1d, xmm7)\
|
|
|
|
AS2( rol AS_REG_1d, ASM_MOD((ASM_MOD(5*i,17)*(ASM_MOD(5*i,17)+1)/2), 32))\
|
|
|
|
AS2( mov [AS_REG_2+SSE2_Index(ASM_MOD(5*(i), 17))*4], AS_REG_1d)
|
2007-04-15 22:54:31 +00:00
|
|
|
|
|
|
|
#define pi4(x, y, z, a, b, c, d) \
|
|
|
|
AS2( pcmpeqb xmm7, xmm7)\
|
|
|
|
AS2( pxor xmm7, x)\
|
|
|
|
AS2( por xmm7, y)\
|
|
|
|
AS2( pxor xmm7, z)\
|
|
|
|
pi(a)\
|
|
|
|
ASS( pshuflw xmm7, xmm7, 1, 0, 3, 2)\
|
|
|
|
pi(b)\
|
|
|
|
AS2( punpckhqdq xmm7, xmm7)\
|
|
|
|
pi(c)\
|
|
|
|
ASS( pshuflw xmm7, xmm7, 1, 0, 3, 2)\
|
|
|
|
pi(d)
|
|
|
|
|
|
|
|
pi4(xmm1, xmm2, xmm3, 1, 5, 9, 13)
|
|
|
|
pi4(xmm0, xmm1, xmm2, 2, 6, 10, 14)
|
|
|
|
pi4(xmm6, xmm0, xmm1, 3, 7, 11, 15)
|
|
|
|
pi4(xmm5, xmm6, xmm0, 4, 8, 12, 16)
|
|
|
|
|
|
|
|
// output keystream and update buffer here to hide partial memory stalls between pi and theta
|
|
|
|
AS2( movdqa xmm4, xmm3)
|
|
|
|
AS2( punpcklqdq xmm3, xmm2) // 1 5 2 6
|
|
|
|
AS2( punpckhdq xmm4, xmm2) // 9 10 13 14
|
|
|
|
AS2( movdqa xmm2, xmm1)
|
|
|
|
AS2( punpcklqdq xmm1, xmm0) // 3 7 4 8
|
|
|
|
AS2( punpckhdq xmm2, xmm0) // 11 12 15 16
|
|
|
|
|
|
|
|
// keystream
|
2007-09-24 00:43:57 +00:00
|
|
|
AS2( test AS_REG_3, AS_REG_3)
|
2007-04-15 22:54:31 +00:00
|
|
|
ASJ( jz, 0, f)
|
|
|
|
AS2( movdqa xmm6, xmm4)
|
|
|
|
AS2( punpcklqdq xmm4, xmm2)
|
|
|
|
AS2( punpckhqdq xmm6, xmm2)
|
2007-09-24 00:43:57 +00:00
|
|
|
AS2( test AS_REG_4, 15)
|
2007-04-15 22:54:31 +00:00
|
|
|
ASJ( jnz, 2, f)
|
2007-09-24 00:43:57 +00:00
|
|
|
AS2( test AS_REG_4, AS_REG_4)
|
2007-04-15 22:54:31 +00:00
|
|
|
ASJ( jz, 1, f)
|
2007-09-24 00:43:57 +00:00
|
|
|
AS2( pxor xmm4, [AS_REG_4])
|
|
|
|
AS2( pxor xmm6, [AS_REG_4+16])
|
|
|
|
AS2( add AS_REG_4, 32)
|
2007-04-15 22:54:31 +00:00
|
|
|
ASJ( jmp, 1, f)
|
|
|
|
ASL(2)
|
2007-09-24 00:43:57 +00:00
|
|
|
AS2( movdqu xmm0, [AS_REG_4])
|
|
|
|
AS2( movdqu xmm2, [AS_REG_4+16])
|
2007-04-15 22:54:31 +00:00
|
|
|
AS2( pxor xmm4, xmm0)
|
|
|
|
AS2( pxor xmm6, xmm2)
|
2007-09-24 00:43:57 +00:00
|
|
|
AS2( add AS_REG_4, 32)
|
2007-04-15 22:54:31 +00:00
|
|
|
ASL(1)
|
2007-09-24 00:43:57 +00:00
|
|
|
AS2( test AS_REG_3, 15)
|
2007-04-15 22:54:31 +00:00
|
|
|
ASJ( jnz, 3, f)
|
2007-09-24 01:19:38 +00:00
|
|
|
AS2( movdqa XMMWORD_PTR [AS_REG_3], xmm4)
|
|
|
|
AS2( movdqa XMMWORD_PTR [AS_REG_3+16], xmm6)
|
2007-09-24 00:43:57 +00:00
|
|
|
AS2( add AS_REG_3, 32)
|
2007-04-15 22:54:31 +00:00
|
|
|
ASJ( jmp, 0, f)
|
|
|
|
ASL(3)
|
2007-09-24 01:19:38 +00:00
|
|
|
AS2( movdqu XMMWORD_PTR [AS_REG_3], xmm4)
|
|
|
|
AS2( movdqu XMMWORD_PTR [AS_REG_3+16], xmm6)
|
2007-09-24 00:43:57 +00:00
|
|
|
AS2( add AS_REG_3, 32)
|
2007-04-15 22:54:31 +00:00
|
|
|
ASL(0)
|
|
|
|
|
|
|
|
// buffer update
|
2007-09-24 00:43:57 +00:00
|
|
|
AS2( lea AS_REG_1, [AS_REG_6 + 32])
|
|
|
|
AS2( and AS_REG_1, 31*32)
|
|
|
|
AS2( lea AS_REG_7, [AS_REG_6 + (32-24)*32])
|
|
|
|
AS2( and AS_REG_7, 31*32)
|
2007-04-15 22:54:31 +00:00
|
|
|
|
2007-09-24 01:19:38 +00:00
|
|
|
AS2( movdqa xmm0, XMMWORD_PTR [AS_REG_2+20*4+AS_REG_1+0*8])
|
2007-04-15 22:54:31 +00:00
|
|
|
AS2( pxor xmm3, xmm0)
|
|
|
|
ASS( pshufd xmm0, xmm0, 2, 3, 0, 1)
|
2007-09-24 01:19:38 +00:00
|
|
|
AS2( movdqa XMMWORD_PTR [AS_REG_2+20*4+AS_REG_1+0*8], xmm3)
|
|
|
|
AS2( pxor xmm0, XMMWORD_PTR [AS_REG_2+20*4+AS_REG_7+2*8])
|
|
|
|
AS2( movdqa XMMWORD_PTR [AS_REG_2+20*4+AS_REG_7+2*8], xmm0)
|
2007-04-15 22:54:31 +00:00
|
|
|
|
2007-09-24 01:19:38 +00:00
|
|
|
AS2( movdqa xmm4, XMMWORD_PTR [AS_REG_2+20*4+AS_REG_1+2*8])
|
2007-04-15 22:54:31 +00:00
|
|
|
AS2( pxor xmm1, xmm4)
|
2007-09-24 01:19:38 +00:00
|
|
|
AS2( movdqa XMMWORD_PTR [AS_REG_2+20*4+AS_REG_1+2*8], xmm1)
|
|
|
|
AS2( pxor xmm4, XMMWORD_PTR [AS_REG_2+20*4+AS_REG_7+0*8])
|
|
|
|
AS2( movdqa XMMWORD_PTR [AS_REG_2+20*4+AS_REG_7+0*8], xmm4)
|
2007-04-15 22:54:31 +00:00
|
|
|
|
|
|
|
// theta
|
2007-09-24 01:19:38 +00:00
|
|
|
AS2( movdqa xmm3, XMMWORD_PTR [AS_REG_2+3*16])
|
|
|
|
AS2( movdqa xmm2, XMMWORD_PTR [AS_REG_2+2*16])
|
|
|
|
AS2( movdqa xmm1, XMMWORD_PTR [AS_REG_2+1*16])
|
|
|
|
AS2( movdqa xmm0, XMMWORD_PTR [AS_REG_2+0*16])
|
2007-04-15 22:54:31 +00:00
|
|
|
|
|
|
|
#if CRYPTOPP_BOOL_SSSE3_ASM_AVAILABLE
|
2007-09-24 00:43:57 +00:00
|
|
|
AS2( test AS_REG_6, 1)
|
2007-04-15 22:54:31 +00:00
|
|
|
ASJ( jnz, 8, f)
|
|
|
|
#endif
|
|
|
|
AS2( movd xmm6, eax)
|
|
|
|
AS2( movdqa xmm7, xmm3)
|
|
|
|
AS2( movss xmm7, xmm6)
|
|
|
|
AS2( movdqa xmm6, xmm2)
|
|
|
|
AS2( movss xmm6, xmm3)
|
|
|
|
AS2( movdqa xmm5, xmm1)
|
|
|
|
AS2( movss xmm5, xmm2)
|
|
|
|
AS2( movdqa xmm4, xmm0)
|
|
|
|
AS2( movss xmm4, xmm1)
|
|
|
|
ASS( pshufd xmm7, xmm7, 0, 3, 2, 1)
|
|
|
|
ASS( pshufd xmm6, xmm6, 0, 3, 2, 1)
|
|
|
|
ASS( pshufd xmm5, xmm5, 0, 3, 2, 1)
|
|
|
|
ASS( pshufd xmm4, xmm4, 0, 3, 2, 1)
|
|
|
|
#if CRYPTOPP_BOOL_SSSE3_ASM_AVAILABLE
|
|
|
|
ASJ( jmp, 9, f)
|
|
|
|
ASL(8)
|
|
|
|
AS2( movd xmm7, eax)
|
|
|
|
AS3( palignr xmm7, xmm3, 4)
|
|
|
|
AS2( movq xmm6, xmm3)
|
|
|
|
AS3( palignr xmm6, xmm2, 4)
|
|
|
|
AS2( movq xmm5, xmm2)
|
|
|
|
AS3( palignr xmm5, xmm1, 4)
|
|
|
|
AS2( movq xmm4, xmm1)
|
|
|
|
AS3( palignr xmm4, xmm0, 4)
|
|
|
|
ASL(9)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
AS2( xor eax, 1)
|
2007-09-24 00:43:57 +00:00
|
|
|
AS2( movd AS_REG_1d, xmm0)
|
|
|
|
AS2( xor eax, AS_REG_1d)
|
|
|
|
AS2( movd AS_REG_1d, xmm3)
|
|
|
|
AS2( xor eax, AS_REG_1d)
|
2007-04-15 22:54:31 +00:00
|
|
|
|
|
|
|
AS2( pxor xmm3, xmm2)
|
|
|
|
AS2( pxor xmm2, xmm1)
|
|
|
|
AS2( pxor xmm1, xmm0)
|
|
|
|
AS2( pxor xmm0, xmm7)
|
|
|
|
AS2( pxor xmm3, xmm7)
|
|
|
|
AS2( pxor xmm2, xmm6)
|
|
|
|
AS2( pxor xmm1, xmm5)
|
|
|
|
AS2( pxor xmm0, xmm4)
|
|
|
|
|
|
|
|
// sigma
|
2007-09-24 00:43:57 +00:00
|
|
|
AS2( lea AS_REG_1, [AS_REG_6 + (32-4)*32])
|
|
|
|
AS2( and AS_REG_1, 31*32)
|
|
|
|
AS2( lea AS_REG_7, [AS_REG_6 + 16*32])
|
|
|
|
AS2( and AS_REG_7, 31*32)
|
2007-04-15 22:54:31 +00:00
|
|
|
|
2007-09-24 01:19:38 +00:00
|
|
|
AS2( movdqa xmm4, XMMWORD_PTR [AS_REG_2+20*4+AS_REG_1+0*16])
|
|
|
|
AS2( movdqa xmm5, XMMWORD_PTR [AS_REG_2+20*4+AS_REG_7+0*16])
|
2007-04-15 22:54:31 +00:00
|
|
|
AS2( movdqa xmm6, xmm4)
|
|
|
|
AS2( punpcklqdq xmm4, xmm5)
|
|
|
|
AS2( punpckhqdq xmm6, xmm5)
|
|
|
|
AS2( pxor xmm3, xmm4)
|
|
|
|
AS2( pxor xmm2, xmm6)
|
|
|
|
|
2007-09-24 01:19:38 +00:00
|
|
|
AS2( movdqa xmm4, XMMWORD_PTR [AS_REG_2+20*4+AS_REG_1+1*16])
|
|
|
|
AS2( movdqa xmm5, XMMWORD_PTR [AS_REG_2+20*4+AS_REG_7+1*16])
|
2007-04-15 22:54:31 +00:00
|
|
|
AS2( movdqa xmm6, xmm4)
|
|
|
|
AS2( punpcklqdq xmm4, xmm5)
|
|
|
|
AS2( punpckhqdq xmm6, xmm5)
|
|
|
|
AS2( pxor xmm1, xmm4)
|
|
|
|
AS2( pxor xmm0, xmm6)
|
|
|
|
|
|
|
|
// loop
|
2007-09-24 00:43:57 +00:00
|
|
|
AS2( add AS_REG_6, 32)
|
|
|
|
AS2( cmp AS_REG_6, REG_loopEnd)
|
2007-04-15 22:54:31 +00:00
|
|
|
ASJ( jne, 4, b)
|
|
|
|
|
|
|
|
// save state
|
2007-09-24 00:43:57 +00:00
|
|
|
AS2( mov [AS_REG_2+4*16], eax)
|
2007-09-24 01:19:38 +00:00
|
|
|
AS2( movdqa XMMWORD_PTR [AS_REG_2+3*16], xmm3)
|
|
|
|
AS2( movdqa XMMWORD_PTR [AS_REG_2+2*16], xmm2)
|
|
|
|
AS2( movdqa XMMWORD_PTR [AS_REG_2+1*16], xmm1)
|
|
|
|
AS2( movdqa XMMWORD_PTR [AS_REG_2+0*16], xmm0)
|
2007-09-24 00:43:57 +00:00
|
|
|
|
|
|
|
#if CRYPTOPP_BOOL_X86
|
|
|
|
AS2( add esp, 4)
|
|
|
|
AS1( pop ebp)
|
|
|
|
#endif
|
2007-04-15 22:54:31 +00:00
|
|
|
ASL(5)
|
|
|
|
|
2010-08-08 23:02:36 +00:00
|
|
|
#ifdef CRYPTOPP_GNU_STYLE_INLINE_ASSEMBLY
|
2007-09-24 00:43:57 +00:00
|
|
|
AS_POP_IF86( bx)
|
|
|
|
".att_syntax prefix;"
|
|
|
|
:
|
|
|
|
#if CRYPTOPP_BOOL_X64
|
|
|
|
: "D" (count), "S" (state), "d" (z), "c" (y)
|
|
|
|
: "%r8", "%r9", "r10", "%eax", "memory", "cc", "%xmm0", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "%xmm5", "%xmm6", "%xmm7"
|
|
|
|
#else
|
|
|
|
: "c" (count), "d" (state), "S" (z), "D" (y)
|
2007-09-24 03:52:17 +00:00
|
|
|
: "%eax", "memory", "cc"
|
2007-09-24 00:43:57 +00:00
|
|
|
#endif
|
2007-04-15 22:54:31 +00:00
|
|
|
);
|
|
|
|
#endif
|
2007-09-24 00:43:57 +00:00
|
|
|
#ifdef CRYPTOPP_GENERATE_X64_MASM
|
|
|
|
movdqa xmm6, [rsp + 0h]
|
|
|
|
movdqa xmm7, [rsp + 10h]
|
2009-03-29 21:41:30 +00:00
|
|
|
add rsp, 2*16
|
|
|
|
pop rdi
|
2007-09-24 00:43:57 +00:00
|
|
|
ret
|
|
|
|
Panama_SSE2_Pull ENDP
|
|
|
|
#else
|
2007-04-15 22:54:31 +00:00
|
|
|
}
|
|
|
|
#endif
|
2007-09-24 00:43:57 +00:00
|
|
|
#endif // #ifdef CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE
|
|
|
|
|
|
|
|
#ifndef CRYPTOPP_GENERATE_X64_MASM
|
2007-04-15 22:54:31 +00:00
|
|
|
|
2002-10-04 17:31:41 +00:00
|
|
|
template <class B>
|
2005-07-12 04:23:32 +00:00
|
|
|
void Panama<B>::Iterate(size_t count, const word32 *p, word32 *z, const word32 *y)
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
2007-04-15 22:54:31 +00:00
|
|
|
word32 bstart = m_state[17];
|
|
|
|
word32 *const aPtr = m_state;
|
|
|
|
word32 cPtr[17];
|
|
|
|
|
|
|
|
#define bPtr ((byte *)(aPtr+20))
|
|
|
|
|
|
|
|
// reorder the state for SSE2
|
|
|
|
// a and c: 4 8 12 16 | 3 7 11 15 | 2 6 10 14 | 1 5 9 13 | 0
|
|
|
|
// xmm0 xmm1 xmm2 xmm3 eax
|
|
|
|
#define a(i) aPtr[((i)*13+16) % 17] // 13 is inverse of 4 mod 17
|
|
|
|
#define c(i) cPtr[((i)*13+16) % 17]
|
|
|
|
// b: 0 4 | 1 5 | 2 6 | 3 7
|
|
|
|
#define b(i, j) b##i[(j)*2%8 + (j)/4]
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
// output
|
2007-04-15 22:54:31 +00:00
|
|
|
#define OA(i) z[i] = ConditionalByteReverse(B::ToEnum(), a(i+9))
|
|
|
|
#define OX(i) z[i] = y[i] ^ ConditionalByteReverse(B::ToEnum(), a(i+9))
|
2002-10-04 17:31:41 +00:00
|
|
|
// buffer update
|
2007-04-15 22:54:31 +00:00
|
|
|
#define US(i) {word32 t=b(0,i); b(0,i)=ConditionalByteReverse(B::ToEnum(), p[i])^t; b(25,(i+6)%8)^=t;}
|
|
|
|
#define UL(i) {word32 t=b(0,i); b(0,i)=a(i+1)^t; b(25,(i+6)%8)^=t;}
|
2002-10-04 17:31:41 +00:00
|
|
|
// gamma and pi
|
2007-04-15 22:54:31 +00:00
|
|
|
#define GP(i) c(5*i%17) = rotlFixed(a(i) ^ (a((i+1)%17) | ~a((i+2)%17)), ((5*i%17)*((5*i%17)+1)/2)%32)
|
2002-10-04 17:31:41 +00:00
|
|
|
// theta and sigma
|
2007-04-15 22:54:31 +00:00
|
|
|
#define T(i,x) a(i) = c(i) ^ c((i+1)%17) ^ c((i+4)%17) ^ x
|
2002-10-04 17:31:41 +00:00
|
|
|
#define TS1S(i) T(i+1, ConditionalByteReverse(B::ToEnum(), p[i]))
|
2007-04-15 22:54:31 +00:00
|
|
|
#define TS1L(i) T(i+1, b(4,i))
|
|
|
|
#define TS2(i) T(i+9, b(16,i))
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
while (count--)
|
|
|
|
{
|
|
|
|
if (z)
|
|
|
|
{
|
|
|
|
if (y)
|
|
|
|
{
|
|
|
|
OX(0); OX(1); OX(2); OX(3); OX(4); OX(5); OX(6); OX(7);
|
|
|
|
y += 8;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
OA(0); OA(1); OA(2); OA(3); OA(4); OA(5); OA(6); OA(7);
|
|
|
|
}
|
|
|
|
z += 8;
|
|
|
|
}
|
|
|
|
|
2007-04-15 22:54:31 +00:00
|
|
|
word32 *const b16 = (word32 *)(bPtr+((bstart+16*32) & 31*32));
|
|
|
|
word32 *const b4 = (word32 *)(bPtr+((bstart+(32-4)*32) & 31*32));
|
|
|
|
bstart += 32;
|
|
|
|
word32 *const b0 = (word32 *)(bPtr+((bstart) & 31*32));
|
|
|
|
word32 *const b25 = (word32 *)(bPtr+((bstart+(32-25)*32) & 31*32));
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
US(0); US(1); US(2); US(3); US(4); US(5); US(6); US(7);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
UL(0); UL(1); UL(2); UL(3); UL(4); UL(5); UL(6); UL(7);
|
|
|
|
}
|
|
|
|
|
2007-04-15 22:54:31 +00:00
|
|
|
GP(0);
|
|
|
|
GP(1);
|
|
|
|
GP(2);
|
|
|
|
GP(3);
|
|
|
|
GP(4);
|
|
|
|
GP(5);
|
|
|
|
GP(6);
|
|
|
|
GP(7);
|
|
|
|
GP(8);
|
|
|
|
GP(9);
|
|
|
|
GP(10);
|
|
|
|
GP(11);
|
|
|
|
GP(12);
|
|
|
|
GP(13);
|
|
|
|
GP(14);
|
|
|
|
GP(15);
|
|
|
|
GP(16);
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
T(0,1);
|
|
|
|
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
TS1S(0); TS1S(1); TS1S(2); TS1S(3); TS1S(4); TS1S(5); TS1S(6); TS1S(7);
|
|
|
|
p += 8;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TS1L(0); TS1L(1); TS1L(2); TS1L(3); TS1L(4); TS1L(5); TS1L(6); TS1L(7);
|
|
|
|
}
|
|
|
|
|
|
|
|
TS2(0); TS2(1); TS2(2); TS2(3); TS2(4); TS2(5); TS2(6); TS2(7);
|
|
|
|
}
|
2007-04-15 22:54:31 +00:00
|
|
|
m_state[17] = bstart;
|
2002-10-04 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
2007-05-05 18:29:44 +00:00
|
|
|
namespace Weak {
|
2002-10-04 17:31:41 +00:00
|
|
|
template <class B>
|
2007-05-05 18:29:44 +00:00
|
|
|
size_t PanamaHash<B>::HashMultipleBlocks(const word32 *input, size_t length)
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
2004-06-19 08:28:09 +00:00
|
|
|
this->Iterate(length / this->BLOCKSIZE, input);
|
|
|
|
return length % this->BLOCKSIZE;
|
2002-10-04 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class B>
|
2007-05-05 18:29:44 +00:00
|
|
|
void PanamaHash<B>::TruncatedFinal(byte *hash, size_t size)
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
2004-06-19 08:28:09 +00:00
|
|
|
this->ThrowIfInvalidTruncatedSize(size);
|
2002-10-04 17:31:41 +00:00
|
|
|
|
2004-06-19 08:28:09 +00:00
|
|
|
PadLastBlock(this->BLOCKSIZE, 0x01);
|
2002-10-04 17:31:41 +00:00
|
|
|
|
2004-06-19 08:28:09 +00:00
|
|
|
HashEndianCorrectedBlock(this->m_data);
|
2002-10-04 17:31:41 +00:00
|
|
|
|
2004-06-19 08:28:09 +00:00
|
|
|
this->Iterate(32); // pull
|
2002-10-04 17:31:41 +00:00
|
|
|
|
2007-04-15 22:54:31 +00:00
|
|
|
FixedSizeSecBlock<word32, 8> buf;
|
|
|
|
this->Iterate(1, NULL, buf, NULL);
|
|
|
|
|
|
|
|
memcpy(hash, buf, size);
|
2002-10-04 17:31:41 +00:00
|
|
|
|
2004-06-19 08:28:09 +00:00
|
|
|
this->Restart(); // reinit for next use
|
2002-10-04 17:31:41 +00:00
|
|
|
}
|
2007-05-05 18:29:44 +00:00
|
|
|
}
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
template <class B>
|
2005-07-12 04:23:32 +00:00
|
|
|
void PanamaCipherPolicy<B>::CipherSetKey(const NameValuePairs ¶ms, const byte *key, size_t length)
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
2007-04-15 22:54:31 +00:00
|
|
|
assert(length==32);
|
|
|
|
memcpy(m_key, key, 32);
|
|
|
|
}
|
2002-10-04 17:31:41 +00:00
|
|
|
|
2007-04-15 22:54:31 +00:00
|
|
|
template <class B>
|
2009-03-02 02:39:17 +00:00
|
|
|
void PanamaCipherPolicy<B>::CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length)
|
2007-04-15 22:54:31 +00:00
|
|
|
{
|
2009-03-02 02:39:17 +00:00
|
|
|
assert(length==32);
|
2004-06-19 08:28:09 +00:00
|
|
|
this->Reset();
|
2007-04-15 22:54:31 +00:00
|
|
|
this->Iterate(1, m_key);
|
|
|
|
if (iv && IsAligned<word32>(iv))
|
|
|
|
this->Iterate(1, (const word32 *)iv);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FixedSizeSecBlock<word32, 8> buf;
|
|
|
|
if (iv)
|
|
|
|
memcpy(buf, iv, 32);
|
|
|
|
else
|
|
|
|
memset(buf, 0, 32);
|
|
|
|
this->Iterate(1, buf);
|
|
|
|
}
|
|
|
|
|
2007-09-24 00:43:57 +00:00
|
|
|
#if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE || defined(CRYPTOPP_X64_MASM_AVAILABLE)
|
2009-04-05 20:01:05 +00:00
|
|
|
if (B::ToEnum() == LITTLE_ENDIAN_ORDER && HasSSE2() && !IsP4()) // SSE2 code is slower on P4 Prescott
|
2007-04-15 22:54:31 +00:00
|
|
|
Panama_SSE2_Pull(32, this->m_state, NULL, NULL);
|
2002-10-04 17:31:41 +00:00
|
|
|
else
|
2007-04-15 22:54:31 +00:00
|
|
|
#endif
|
|
|
|
this->Iterate(32);
|
|
|
|
}
|
2002-10-04 17:31:41 +00:00
|
|
|
|
2007-04-15 22:54:31 +00:00
|
|
|
#if CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X64
|
|
|
|
template <class B>
|
|
|
|
unsigned int PanamaCipherPolicy<B>::GetAlignment() const
|
|
|
|
{
|
2007-09-24 00:43:57 +00:00
|
|
|
#if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE || defined(CRYPTOPP_X64_MASM_AVAILABLE)
|
2007-04-15 22:54:31 +00:00
|
|
|
if (B::ToEnum() == LITTLE_ENDIAN_ORDER && HasSSE2())
|
|
|
|
return 16;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
return 1;
|
2002-10-04 17:31:41 +00:00
|
|
|
}
|
2007-04-15 22:54:31 +00:00
|
|
|
#endif
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
template <class B>
|
2005-07-12 04:23:32 +00:00
|
|
|
void PanamaCipherPolicy<B>::OperateKeystream(KeystreamOperation operation, byte *output, const byte *input, size_t iterationCount)
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
2007-09-24 00:43:57 +00:00
|
|
|
#if CRYPTOPP_BOOL_SSE2_ASM_AVAILABLE || defined(CRYPTOPP_X64_MASM_AVAILABLE)
|
2007-04-15 22:54:31 +00:00
|
|
|
if (B::ToEnum() == LITTLE_ENDIAN_ORDER && HasSSE2())
|
|
|
|
Panama_SSE2_Pull(iterationCount, this->m_state, (word32 *)output, (const word32 *)input);
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
this->Iterate(iterationCount, NULL, (word32 *)output, (const word32 *)input);
|
2002-10-04 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template class Panama<BigEndian>;
|
|
|
|
template class Panama<LittleEndian>;
|
|
|
|
|
2007-04-15 22:54:31 +00:00
|
|
|
template class Weak::PanamaHash<BigEndian>;
|
|
|
|
template class Weak::PanamaHash<LittleEndian>;
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
template class PanamaCipherPolicy<BigEndian>;
|
|
|
|
template class PanamaCipherPolicy<LittleEndian>;
|
|
|
|
|
|
|
|
NAMESPACE_END
|
2007-09-24 00:43:57 +00:00
|
|
|
|
|
|
|
#endif // #ifndef CRYPTOPP_GENERATE_X64_MASM
|