Gribley
05-20-2015, 01:56 PM
Maybe I went wrong way with your original comment, If you are looking pure speed then AES does rock with a very low key prep time and encrypt and is totally easy to implement in hardware:
#IF 0
Please be sure TO READ the HEADER OF RIJNDAEL2.BAS FOR information
about the Rijndael algorithm AND the PowerBASIC implementation
provided IN THIS package.
=====================================================================
Rijndael (AES) Encryption v.2
32-BIT (non-MMX) CODE
=====================================================================
#ENDIF
DECLARE FUNCTION EncryptBlock32(Ctx AS ENCRYPTION_CONTEXT) AS LONG
DECLARE FUNCTION DecryptBlock32(Ctx AS ENCRYPTION_CONTEXT) AS LONG
'--------------------
MACRO putBlock32(memPtr,value)
MACROTEMP retval
LOCAL retval&
retval=value
! mov edx, retval
#IF %RETURN_LITTLE_ENDIAN
! bswap edx
#ENDIF
! mov eax, memPtr
! mov [eax], edx
! add eax, 4
! mov memPtr,eax
END MACRO
'--------------------
MACRO encode32(tt,ptable,aa,bb,cc,DDD)
! push ebx
! push esi
! mov eax, aa
! mov ebx, bb
! mov ecx, cc
! mov edx, ddd
! mov esi, ptable 'esi --> Ctx.TE0/Ctx.TD0
! shr eax, 24
! shr ebx, 16
! shr ecx, 8
! and ebx, &hff
! and ecx, &hff
! and edx, &hff
! mov eax, [esi+eax*4]
! xor eax, [esi+ebx*4 + %TABLE_SIZE ]
! xor eax, [esi+ecx*4 + %TABLE_SIZE2]
! xor eax, [esi+edx*4 + %TABLE_SIZE3]
! mov tt, eax
! pop esi
! pop ebx
END MACRO
'--------------------
MACRO encodeLast32(xx,ptable,aa,bb,cc,DDD,uu,vv,kk)
! push esi
! mov esi, ptable 'esi --> pTable
! mov ecx, kk 'save ecx = kk
'uu = @se[byte3(aa)]
'vv = shiftrc(kk,24)
'a0 = uu XOR vv
! mov eax, aa
! mov edx, ecx 'edx = kk
! shr eax, 24
! shr edx, 24 'shiftrc(kk,24)
! mov al, byte ptr [esi+eax] 'al = @se[byte3(aa)]
! xor eax, edx
! mov a0, eax
'uu = @se[byte2(bb)]
'vv = shiftrc(kk,16)
'a1 = (uu XOR vv) AND &hff
! mov eax, bb
! mov edx, ecx 'edx = kk
! shr eax, 16
! shr edx, 16 'shiftrc(kk,16)
! and eax, &hff
! mov al, byte ptr [esi+eax] 'al = @se[byte2(bb)]
! xor eax, edx
! and eax, &hff
! mov a1, eax
'uu = @se[byte1(cc)]
'vv = shiftrc(kk,8)
'a2 = (uu XOR vv) AND &hff
! mov eax, cc
! mov edx, ecx 'edx = kk
! shr eax, 8
! shr edx, 8 'shiftrc(kk,8)
! and eax, &hff
! mov al, byte ptr [esi+eax] 'al = @se[byte2(bb)]
! xor eax, edx
! and eax, &hff
! mov a2, eax
'uu = @se[dd AND &hff]
'vv = kk
'a3 = (uu XOR vv) AND &hff
! mov eax, ddd
! mov edx, ecx 'edx = kk
! and eax, &hff
! mov al, byte ptr [esi+eax] 'al = @se[ddd AND &hff]
! xor eax, edx
! and eax, &hff
! mov a3, eax
'packbytes(xx,a0,a1,a2,a3)
! mov eax, a0
! mov esi, a1
! mov ecx, a2
! mov edx, a3
! shl eax, 24
! shl esi, 16
! shl ecx, 8
! or eax, esi
! or eax, ecx
! or eax, edx
! mov xx, eax
! pop esi
END MACRO
'====================
FUNCTION EncryptBlock32(Ctx AS ENCRYPTION_CONTEXT) AS LONG '%BLOCKSIZE = 16
#REGISTER NONE
LOCAL r, t, u, v, w, pblock, retval AS LONG
LOCAL a0, a1, a2, a3, t0, t1, t2, t3, s0, s1, s2, s3 AS LONG
LOCAL k, ptable AS LONG PTR, se AS BYTE PTR
'? "EncryptBlock32"
k = Ctx.KE : ptable = Ctx.Te0
pblock = Ctx.InBlock
t0 = getBlock32(pblock) : t1 = getBlock32(pblock)
t2 = getBlock32(pblock) : t3 = getBlock32(pblock)
t0 = t0 XOR @k[0] : t1 = t1 XOR @k[1]
t2 = t2 XOR @k[2] : t3 = t3 XOR @k[3]
FOR r = 1 TO Ctx.Rounds-1
k = k + %BLOCKSIZE
encode32(t,ptable,t0,t1,t2,t3) : a0 = t XOR @k[0] ': ? hex8(t);
encode32(t,ptable,t1,t2,t3,t0) : a1 = t XOR @k[1] ': ? hex8(t);
encode32(t,ptable,t2,t3,t0,t1) : a2 = t XOR @k[2] ': ? hex8(t);
encode32(t,ptable,t3,t0,t1,t2) : a3 = t XOR @k[3] ': ? hex8(t)
t0 = a0 : t1 = a1 : t2 = a2 : t3 = a3
NEXT r
ptable = Ctx.SE
k = k + %BLOCKSIZE
w = @k[0] : encodeLast32(s0,ptable,t0,t1,t2,t3,u,v,w)
w = @k[1] : encodeLast32(s1,ptable,t1,t2,t3,t0,u,v,w)
w = @k[2] : encodeLast32(s2,ptable,t2,t3,t0,t1,u,v,w)
w = @k[3] : encodeLast32(s3,ptable,t3,t0,t1,t2,u,v,w)
pblock = Ctx.OutBlock
putBlock32(pblock,s0) : putBlock32(pblock,s1)
putBlock32(pblock,s2) : putBlock32(pblock,s3)
END FUNCTION
'====================
FUNCTION DecryptBlock32(Ctx AS ENCRYPTION_CONTEXT) AS LONG
#REGISTER NONE
LOCAL r, t, u, v, w, pblock, retval AS LONG
LOCAL a0, a1, a2, a3, t0, t1, t2, t3, s0, s1, s2, s3 AS LONG
LOCAL ptable, k AS LONG PTR
ptable = Ctx.TD0 : pblock = Ctx.InBlock
t0 = getBlock32(pblock) : t1 = getBlock32(pblock)
t2 = getBlock32(pblock) : t3 = getBlock32(pblock)
k = Ctx.KD
t0 = t0 XOR @k[0] : t1 = t1 XOR @k[1]
t2 = t2 XOR @k[2] : t3 = t3 XOR @k[3]
FOR r = 1 TO Ctx.Rounds-1
k = k + %BLOCKSIZE
encode32(t,ptable,t0,t3,t2,t1) : a0 = t XOR @k[0]
encode32(t,ptable,t1,t0,t3,t2) : a1 = t XOR @k[1]
encode32(t,ptable,t2,t1,t0,t3) : a2 = t XOR @k[2]
encode32(t,ptable,t3,t2,t1,t0) : a3 = t XOR @k[3]
t0 = a0 : t1 = a1 : t2 = a2 : t3 = a3
NEXT r
k = k + %BLOCKSIZE
ptable = Ctx.SD
w = @k[0] : encodeLast32(s0,ptable,t0,t3,t2,t1,u,v,w)
w = @k[1] : encodeLast32(s1,ptable,t1,t0,t3,t2,u,v,w)
w = @k[2] : encodeLast32(s2,ptable,t2,t1,t0,t3,u,v,w)
w = @k[3] : encodeLast32(s3,ptable,t3,t2,t1,t0,u,v,w)
pblock = Ctx.OutBlock
putBlock32(pblock,s0) : putBlock32(pblock,s1)
putBlock32(pblock,s2) : putBlock32(pblock,s3)
END FUNCTION
'-- end RIJNDAEL32.BAS
I really didn`t like the look up table scan in 3DES.... this algo does a much better pbox to sbox.... but then so does blowfish, twofish and a loads more :)
Powered by vBulletin® Version 4.2.3 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.