⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 phelix86.s

📁 phelix加密算法源代码,是一个开源的加密算法
💻 S
📖 第 1 页 / 共 4 页
字号:
        movl    $_ret_MAC2,exitTab+_SO_(%esp,%edi)
        leal    4*4-1(%ebp),%edi            #FUNKY wrap logic requires -1
        movl    %edi,loopByteCnt+_SO_(%esp)
        jmp     *Encrypt_jmpTab(%ebp)
        ##
        ## here with the MAC computed. eax..esi now can be trashed
_ret_MAC2:
        leal    callerParms+_SO_(%esp),%esi
        movl    ctxt_Ptr_LCL(%esi),%edi
        movl    macSize(%edi),%ecx          #ecx = # bits in MAC
        movl    dstPtr0_LCL(%esi),%edi
        leal    tmpBuf+8*4+_SO_(%esp),%esi
        testl   $31,%ecx                    #can we do it one word at a time?
        _if z
          shrl  $5,%ecx                     #if so, it is faster
          rep   movsl
        _else
          addl  $7,%ecx                     #round up to byte boundary
          shrl  $3,%ecx                     #non-word sizes get the slow treatment
          rep   movsb
        _endif
        #################
        ## tear down the stack and return
        addl    $_Phelix_LocalSize,%esp
        popal                               #restore all of callers regs
        ret                                 #and return to caller
##
##----------------------------------------------------------------
## Common subroutine (for use in Phelix_Main) to init subkeys
##----------------------------------------------------------------
## In:  ebp     --> pCtxt (const)   
##      edx     --> nonce (const)
##      edi     =   X value for I
##      esi     =   value of I (0..3)
## Out: esi incremented.  ebp, edx unmodified
##      edi     = oldZ[I] = 0
##      X_i_0, X_i_1 set on stack for both i=I and i=I+4
##      edi
.set _SO_,12                                        #two words on stack before call
SetTwoKeys:
        movl    X_0+4*0(%ebp,%esi,4),%eax           #load two key values
        movl    X_0+4*4(%ebp,%esi,4),%ebx
        movl    %eax,X_i_0+4*0+_SO_(%esp,%esi,4)    #store the X_i_0 values
        movl    %ebx,X_i_0+4*4+_SO_(%esp,%esi,4)
        movl    (%edx,%esi,4),%ecx                  #get ecx = N_i
        addl    %edi,%eax                           #add in 4*L(U), for esi == 1
        addl    %edi,%ebx
        addl    %ecx,%ebx                           #add/sub the nonce value
        subl    %ecx,%eax
        addl    %esi,%eax
        xorl    %edi,%edi                           #set edi = 0
        movl    %ebx,X_i_1+4*0+_SO_(%esp,%esi,4)    #store the X_i_1 values
        movl    %eax,X_i_1+4*4+_SO_(%esp,%esi,4)
        movl    %edi,oldZ+_SO_(%esp,%esi,4)         #zero out the oldZ values
        .set _NN_,0
    .rept UNROLL_CNT/4                              #init the "block exit" jump table: all zeroes
        movl    %edi,exitTab+_NN_+_SO_(%esp,%esi,4)
        .set _NN_,_NN_ + 16
    .endr
        incl    %esi                        #bump the counter for next call
        ret
##
.set _SO_,0                         #back to no offset
##
##----------------------------------------------------------------
## Encryption routines
##----------------------------------------------------------------
##
        .align  4
C_global PhelixEncryptPacket,ECRYPT_AE_encrypt_packet
        PhelixAlgo  Encrypt                 #instantiate the algorithm ocde
        ##
        ## the main block processing loop
        ##
    _rept
      .irp _blkNum_,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
       .if  \_blkNum_ < UNROLL_CNT
strCat  EncryptBlk_,\_blkNum_,":"           #make a label for re-entry points
        .set _bb_,\_blkNum_ & 7             #support UNROLL_CNT > 8

        _o_ "addl %edx,%eax","roll $ROT_3b,%edx","movl X_i_0+4*_bb_+_SO_(%esp),%ebp"
        _o_ "addl %esi,%ebx","roll $ROT_4b,%esi"
        _o_ "xorl %eax,%ecx","roll $ROT_0a,%eax","movl srcPtr+_SO_(%esp),%edi"
        _o_ "xorl %ebx,%edx","roll $ROT_1a,%ebx","addl %edx,%ebp"   #does LEA opcode help here?
        _o_ "addl %ecx,%esi","roll $ROT_2a,%ecx"

        _o_ "xorl %ebp,%eax","roll $ROT_3a,%edx","movl 4*_bb_(%edi),%ebp"   #ebp = plaintext
        _o_ "xorl %esi,%ebx","roll $ROT_4a,%esi","movl oldZ+4*(_bb_&3)+_SO_(%esp),%edi"
        _o_ "addl %eax,%ecx","roll $ROT_0b,%eax"
        _o_ "addl %ebx,%edx","roll $ROT_1b,%ebx","xorl %edx,%ebp"
        _o_ "xorl %ecx,%esi","roll $ROT_2b,%ecx"

        addl %esi,%edi                      #now edi = keystream
        xorl %edx,%edi                      #set up to compute edi = ciphertext below

        _o_ "addl %ebp,%eax","roll $ROT_3b,%edx","xorl %ebp,%edi"           #now edi = ciphertext
        _o_ "addl %esi,%ebx","roll $ROT_4b,%esi","movl X_i_1+4*_bb_+_SO_(%esp),%ebp"
        _o_ "xorl %eax,%ecx","roll $ROT_0a,%eax"
        _o_ "xorl %ebx,%edx","roll $ROT_1a,%ebx","addl _i_+_SO_(%esp),%ebp"
        _o_ "addl %ecx,%esi","roll $ROT_2a,%ecx","leal _bb_(%ebp,%edx),%ebp"

        _o_ "xorl %ebp,%eax","roll $ROT_3a,%edx","movl dstPtr+_SO_(%esp),%ebp"
        _o_ "xorl %esi,%ebx","roll $ROT_4a,%esi"
        _o_ "addl %eax,%ecx","roll $ROT_0b,%eax","movl %edi,4*_bb_(%ebp)"   #save ciphertext
        _o_ "addl %ebx,%edx","roll $ROT_1b,%ebx","movl exitTab+4*\_blkNum_+_SO_(%esp),%edi"
        _o_ "xorl %ecx,%esi","roll $ROT_2b,%ecx"
        
        PhelixEarlyExit edi,\_blkNum_       #do we need to do an early exit? If so, do it
       .endif
      .endr
        PhelixEndLoop   UNROLL_CNT          #set condition code for _until below
    _until b
        jmp *exitTab+4*(UNROLL_CNT-1)+_SO_(%esp)    #"return" to do more
##
##----------------------------------------------------------------
## Decryption routine
##----------------------------------------------------------------
##
        .align  4
C_global PhelixDecryptPacket,ECRYPT_AE_decrypt_packet
        PhelixAlgo  Decrypt             #instantiate the algorithm ocde
        ##
        ## the main block processing loop
        ##
    _rept
      .irp _blkNum_,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
       .if  \_blkNum_ < UNROLL_CNT
strCat  DecryptBlk_,\_blkNum_,":"       #make a label for re-entry points
        .set _bb_,\_blkNum_ & 7         #support UNROLL_CNT > 8 (but not really!)
        _o_ "addl %edx,%eax","roll $ROT_3b,%edx","movl X_i_0+4*_bb_+_SO_(%esp),%ebp"
        _o_ "addl %esi,%ebx","roll $ROT_4b,%esi"
        _o_ "xorl %eax,%ecx","roll $ROT_0a,%eax","movl srcPtr+_SO_(%esp),%edi"
        _o_ "xorl %ebx,%edx","roll $ROT_1a,%ebx","addl %edx,%ebp"
        _o_ "addl %ecx,%esi","roll $ROT_2a,%ecx"

        _o_ "xorl %ebp,%eax","roll $ROT_3a,%edx","movl 4*_bb_(%edi),%ebp"   #ebp = ciphertext
        _o_ "xorl %esi,%ebx","roll $ROT_4a,%esi","movl oldZ+4*(_bb_&3)+_SO_(%esp),%edi"
        _o_ "addl %eax,%ecx","roll $ROT_0b,%eax"
        _o_ "addl %ebx,%edx","roll $ROT_1b,%ebx"
        _o_ "xorl %ecx,%esi","roll $ROT_2b,%ecx"

        addl %esi,%edi                      #set edi = keystream
        xorl %ebp,%edi                      #now edi = plaintext
        movl %edx,%ebp
        xorl %edi,%ebp                      #now ebp = plaintext ^ edx

        _o_ "addl %ebp,%eax","roll $ROT_3b,%edx"
        _o_ "addl %esi,%ebx","roll $ROT_4b,%esi","movl X_i_1+4*_bb_+_SO_(%esp),%ebp"
        _o_ "xorl %eax,%ecx","roll $ROT_0a,%eax"
        _o_ "xorl %ebx,%edx","roll $ROT_1a,%ebx","addl _i_+_SO_(%esp),%ebp"
        _o_ "addl %ecx,%esi","roll $ROT_2a,%ecx","leal _bb_(%ebp,%edx),%ebp"

        _o_ "xorl %ebp,%eax","roll $ROT_3a,%edx","movl dstPtr+_SO_(%esp),%ebp"
        _o_ "xorl %esi,%ebx","roll $ROT_4a,%esi"
        _o_ "addl %eax,%ecx","roll $ROT_0b,%eax","movl %edi,4*_bb_(%ebp)"   #save plaintext computed above
        _o_ "addl %ebx,%edx","roll $ROT_1b,%ebx","movl exitTab+4*\_blkNum_+_SO_(%esp),%edi"
        _o_ "xorl %ecx,%esi","roll $ROT_2b,%ecx"

        PhelixEarlyExit edi,\_blkNum_       #do we need to do an early exit? If so, do it
       .endif
      .endr
        PhelixEndLoop   UNROLL_CNT          #set condition code for _until below
    _until b
        jmp     *exitTab+4*(UNROLL_CNT-1)+_SO_(%esp)    #"return" to do more
##
_PhelixCodeEnd_:

##
##----------------------------------------------------------------
## "Incremental" function: SetupNonce
##----------------------------------------------------------------
##  use same stack as EncryptPacket!
##
C_global PhelixSetupNonce,ECRYPT_AE_ivsetup
        pushal
        lea     callerParms-_Phelix_LocalSize(%esp),%esi
        subl    $_Phelix_LocalSize,%esp
_SO_            =   0
        call    InitNonce
        movl    $_ret_SetupNonceDone,exitTab+4*(ZERO_INIT_CNT-1)+_SO_(%esp)
        jmp     EncryptBlk_0
_ret_SetupNonceDone:
    .if UNROLL_CNT > ZERO_INIT_CNT      #do we need to clear out the return point?
        .err    "Replicate code here from _ret_InitZeroDone"
    .endif
        movl    ctxt_Ptr+_SO_(%esp),%ebp        #save our context
        #
        movl    $MAGIC_AAD_XOR,%edi
        xorl    %edi,%ebx
        movl    %edi,aadXor(%ebp)
        #
        movl    %eax,4*0+_Z_(%ebp)
        movl    %ebx,4*1+_Z_(%ebp)
        movl    %ecx,4*2+_Z_(%ebp)
        movl    %edx,4*3+_Z_(%ebp)
        movl    %esi,4*4+_Z_(%ebp)

        .irp    _nn_,0,1,2,3
          movl  X_i_1+8*\_nn_  +_SO_(%esp),%eax
          movl  X_i_1+8*\_nn_+4+_SO_(%esp),%ebx
          movl  oldZ +4*\_nn_  +_SO_(%esp),%ecx
          movl  %eax,X_1+  8*\_nn_(%ebp)
          movl  %ebx,X_1+4+8*\_nn_(%ebp)
          movl  %ecx,old_Z+4*\_nn_(%ebp)
        .endr
        #
        xorl    %edi,%edi
        movl    %edi,msgLen  (%ebp)
        movl    %edi,aadLen  (%ebp)
        movl    %edi,aadLen+4(%ebp)
        movl    _i_+_SO_(%esp),%edi
        movl    %edi,blkNum(%ebp)
        #
        addl    $_Phelix_LocalSize,%esp
        popal
        ret
##
##----------------------------------------------------------------
## "Incremental" function: EncryptBytes/DecryptBytes
##----------------------------------------------------------------
##  use same locals stack as EncryptPacket
##
_pOfs_  =       _cpOfs_
        _newParm 1,ctxt_Ptr
        _newParm 1,src_Ptr
        _newParm 1,dst_Ptr
        _newParm 1,bCnt
##
C_global PhelixEncryptBytes,ECRYPT_AE_encrypt_bytes
        pushal
        leal    Encrypt_jmpTab,%ebp
PhelixBytes:
        leal    callerParms-_Phelix_LocalSize(%esp),%esi
        subl    $_Phelix_LocalSize,%esp
    .set _SO_,0
        movl    %ebp,jmpTabPtr+_SO_(%esp)
        ## copy context to local on stack
        movl    ctxt_Ptr_LCL(%esi),%ebp
        _push   esi
        leal    X_0(%ebp),%esi
        leal    X_i_0+_SO_(%esp),%edi
        movl    $8+8+4,%ecx                 #X_0, X_1, and oldZ
        cld
        rep     movsl                       #copy the context
        xorl    %eax,%eax   
        movl    $UNROLL_CNT,%ecx            #zero out exitTab
        rep     stosl
        _pop    esi
        leal    _ret_PhelixBytes,%ebp
        movl    %ebp,retAddr_LCL(%esi)      #set up return address
        movl    src_Ptr_LCL(%esi),%ebp      #copy srcPtr and dstPtr
        movl    %ebp,srcPtr+_SO_(%esp)
        movl    dst_Ptr_LCL(%esi),%ebp
        movl    %ebp,dstPtr+_SO_(%esp)
        movl    ctxt_Ptr_LCL(%esi),%ebp
        movl    blkNum(%ebp),%edi           #convert blkNum from pCtxt to locals
        andl    $~(UNROLL_CNT-1),%edi
        movl    %edi,_i_+_SO_(%esp)
        movl    blkNum(%ebp),%edi
        shll    $2,%edi                     #convert blkNum to a word count
        movl    %edi,loopByteCnt+_SO_(%esp) #and save it as the "phase"
        movl    _Z_+4*0(%ebp),%eax          #load the Z values
        movl    _Z_+4*1(%ebp),%ebx
        movl    _Z_+4*2(%ebp),%ecx
        movl    _Z_+4*3(%ebp),%edx
        movl    _Z_+4*4(%ebp),%esi
        xorl    aadXor(%ebp),%ebx
        movl    $0,aadXor(%ebp)

        _push   esi
        leal    callerParms+_SO_(%esp),%esi
        movl    src_Ptr_LCL(%esi),%ebp
        movl    %ebp,srcPtr+_SO_(%esp)
        movl    bCnt_LCL(%esi),%ebp
        movl    dst_Ptr_LCL(%esi),%edi
        jmp     processUserData
        _pop    esi
_ret_PhelixBytes:

        ## copy modified value back to context
        movl    ctxt_Ptr+_SO_(%esp),%ebp
        movl    %eax,_Z_+4*0(%ebp)          #store the values Z0..Z4
        movl    %ebx,_Z_+4*1(%ebp)
        movl    %ecx,_Z_+4*2(%ebp)
        movl    %edx,_Z_+4*3(%ebp)
        movl    %esi,_Z_+4*4(%ebp)

        movl    msgLen0+_SO_(%esp),%edi     #update pCtxt.blkNum
        movl    %edi,%esi
        addl    $3,%edi

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -