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

📄 ace_modes.asm

📁 AES高级加密标准的C代码
💻 ASM
📖 第 1 页 / 共 3 页
字号:
    mov     ecx,buf_blocks
    cmp     eax,ecx             ; remaining blocks >= buffer Blocks
    jae     .2
    mov     ecx,eax             ; no - process only remaining
.2: mov     eax,ecx             ; save block count in eax
    mov     esi,[ebp+ibuf]      ; source data address
    mov     edi,[ebp-4]         ; destination buffer address
.3: movups  xmm0,[esi]          ; unaligned move to xmm0
    movaps  [edi],xmm0          ; aligned move into buffer
    add     esi,16              ; addjust source and
    add     edi,16              ; destination addresses
    sub     ecx,1               ; reduce block count
    jne     short .3            ; until all blocks have been moved
    mov     [ebp+ibuf],esi      ; update input buffer position
    mov     ebx,[ebp+ecb_ctx]   ; get key address -> ebx
    movzx   ecx,byte[ebx+4*KS_LENGTH]
    shr     ecx,1               ; get control word address ->edx

    movzx   ecx,byte[ebx+4*KS_LENGTH]
    shr     ecx,1
%if (KEY_TYPE = NEH_LOAD)
    lea     edx,[_dec_load_table-80+ecx]
%elif (KEY_TYPE = NEH_GENERATE)
    lea     edx,[_dec_gen_table-80+ecx]
    lea     ebx,[ebx+ecx+ecx]
%else
    lea     edx,[_dec_hybrid_table-80+ecx]
    cmp     ecx,byte 80
    jne     .4
    lea     ebx,[ebx+ecx+ecx]
%endif
.4: mov     esi,[ebp-4]         ; source address (our local buffer)
    mov     edi,esi             ; destination address (our buffer)
    mov     ecx,eax             ; restore block count into ecx
    neh_ecb                     ; execute VIA ACE operation
    mov     eax,[ebp+len]       ; recover number of blocks remaining
    mov     ecx,buf_blocks      ; blocks in buffer
    cmp     eax,ecx             ; remaining blocks >= blocks in buffer
    jae     .5
    mov     ecx,eax             ; no - move only remaining blocks
.5: sub     eax,ecx             ; compute new remaining block count
    mov     esi,[ebp-4]         ; load local buffer address
    mov     edi,[ebp+obuf]      ; load output address
.6: movaps  xmm0,[esi]          ; aligned move to xmm0
    movups  [edi],xmm0          ; unaligned move to output buffer
    add     esi,16              ; adjust source and destination
    add     edi,16              ; addresses
    sub     ecx,1               ; decrement block count
    jne     .6                  ; and continue if some remain
    mov     [ebp+obuf],edi      ; update output buffer position
    and     eax,eax             ; test remaining block count
    jne     .1                  ; and continue if some remain
.7: mov     esp,ebp
    pop     edi
    pop     esi
    pop     ebx
    pop     ebp
    do_exit  16

; aes_rval aes_cbc_encrypt(const unsigned char *in, unsigned char *out,
;                     int len, unsigned char *iv, const aes_encrypt_ctx cx[1]);

    do_name _aes_cbc_encrypt

    push    ebp
    push    ebx
    push    esi
    push    edi
    mov     ebp,esp

;   claim stack space for 'buf_blocks' 16 byte blocks and
;   extra space to allow for adjusting the base address
;   for a 16 byte alignment (4 byte alignment is assumed
;   so a maximum of 12 bytes extra is needed). The top 4
;   bytes (at [ebp-4]) are hence used to store the aligned
;   buffer address

    sub     esp,16 * buf_blocks + 32
    mov     edx,esp
    add     edx,12              ; move up 20 bytes, align the
    and     edx,~0x0000000f     ; resulting address and store
    mov     [ebp-4],edx         ; the resulting value
    mov     eax,[ebp+len]       ; get the data length
    and     eax,eax
    je      .6                  ; exit if zero
    shr     eax,4               ; convert to blocks
    mov     ebx,[ebp+iv]
    movups  xmm0,[ebx]          ; move the IV into the local
    movaps  [edx],xmm0          ; buffer
    neh_rekey
.1: mov     [ebp+len],eax       ; store remaining blocks
    mov     ecx,buf_blocks
    cmp     eax,ecx             ; remaining blocks >= buffer Blocks
    jae     .2
    mov     ecx,eax             ; no - process only remaining
.2: mov     eax,ecx             ; save block count in eax
    mov     esi,[ebp+ibuf]      ; source data address
    mov     edi,[ebp-4]         ; destination buffer address
.3: add     edi,16
    movups  xmm0,[esi]          ; unaligned move to xmm0
    movaps  [edi],xmm0          ; aligned move into buffer
    add     esi,16              ; addjust source and
    sub     ecx,1               ; reduce block count
    jne     short .3            ; until all blocks have been moved
    mov     [ebp+ibuf],esi      ; update input buffer position
    mov     ebx,[ebp+ctx]       ; get key address -> ebx
    movzx   ecx,byte[ebx+4*KS_LENGTH]
    shr     ecx,1               ; get control word address ->edx
%if (KEY_TYPE = NEH_LOAD)
    lea     edx,[_enc_load_table-80+ecx]
%elif (KEY_TYPE = NEH_GENERATE)
    lea     edx,[_enc_gen_table-80+ecx]
%else
    lea     edx,[_enc_hybrid_table-80+ecx]
%endif
    mov     ecx,eax             ; restore block count into ecx
    mov     eax,[ebp-4]         ; the local IV
    mov     esi,eax             ; source address (our local buffer)
    add     esi,16              ; source address (our local buffer)
    mov     edi,esi             ; destination address (our buffer)
    neh_cbc                     ; execute VIA ACE operation
    mov     esi,eax             ; restore the IV into the local
    mov     edi,[ebp-4]         ; buffer
    movsd
    movsd
    movsd
    movsd
    mov     eax,[ebp+len]       ; recover number of blocks remaining
    mov     ecx,buf_blocks      ; blocks in buffer
    cmp     eax,ecx             ; remaining blocks >= blocks in buffer
    jae     .4
    mov     ecx,eax             ; no - move only remaining blocks
.4: sub     eax,ecx             ; compute new remaining block count
    mov     esi,[ebp-4]         ; load local buffer address
    mov     edi,[ebp+obuf]      ; load output address
.5: add     esi,16
    movaps  xmm0,[esi]          ; aligned move to xmm0
    movups  [edi],xmm0          ; unaligned move to output buffer
    add     edi,16              ; addresses
    sub     ecx,1               ; decrement block count
    jne     .5                  ; and continue if some remain
    mov     [ebp+obuf],edi      ; update output buffer position
    mov     esi,[ebp-4]         ; store the IV
    mov     edi,[ebp+iv]
    movaps  xmm0,[esi]
    movups  [edi],xmm0
    and     eax,eax             ; test remaining block count
    jne     .1                  ; and continue if some remain
.6: mov     esp,ebp
    pop     edi
    pop     esi
    pop     ebx
    pop     ebp
    do_exit

; aes_rval aes_cbc_decrypt(const unsigned char *in, unsigned char *out,
;                     int len, unsigned char *iv, const aes_decrypt_ctx cx[1]);

    do_name _aes_cbc_decrypt

    push    ebp
    push    ebx
    push    esi
    push    edi
    mov     ebp,esp

;   claim stack space for 'buf_blocks' 16 byte blocks and
;   extra space to allow for adjusting the base address
;   for a 16 byte alignment (4 byte alignment is assumed
;   so a maximum of 12 bytes extra is needed). The top 4
;   bytes (at [ebp-4]) are hence used to store the aligned
;   buffer address

    sub     esp,16 * buf_blocks + 32
    mov     edx,esp
    add     edx,12              ; move up 20 bytes, align the
    and     edx,~0x0000000f     ; resulting address and store
    mov     [ebp-4],edx         ; the resulting value
    mov     eax,[ebp+len]       ; get the data length
    and     eax,eax
    je      .7                  ; exit if zero
    shr     eax,4               ; convert to blocks
    mov     ebx,[ebp+iv]
    movups  xmm0,[ebx]          ; move the IV into the local
    movaps  [edx],xmm0          ; buffer
    neh_rekey
.1: mov     [ebp+len],eax       ; store remaining blocks
    mov     ecx,buf_blocks
    cmp     eax,ecx             ; remaining blocks >= buffer Blocks
    jae     .2
    mov     ecx,eax             ; no - process only remaining
.2: mov     eax,ecx             ; save block count in eax
    mov     esi,[ebp+ibuf]      ; source data address
    mov     edi,[ebp-4]         ; destination buffer address
.3: add     edi,16
    movups  xmm0,[esi]          ; unaligned move to xmm0
    movaps  [edi],xmm0          ; aligned move into buffer
    add     esi,16              ; addjust source and
    sub     ecx,1               ; reduce block count
    jne     short .3            ; until all blocks have been moved
    mov     [ebp+ibuf],esi      ; update input buffer position
    mov     ebx,[ebp+ctx]       ; get key address -> ebx
    movzx   ecx,byte[ebx+4*KS_LENGTH]
    shr     ecx,1
%if (KEY_TYPE = NEH_LOAD)
    lea     edx,[_dec_load_table-80+ecx]
%elif (KEY_TYPE = NEH_GENERATE)
    lea     edx,[_dec_gen_table-80+ecx]
    lea     ebx,[ebx+ecx+ecx]
%else
    lea     edx,[_dec_hybrid_table-80+ecx]
    cmp     ecx,byte 80
    jne     .4
    lea     ebx,[ebx+ecx+ecx]
%endif
.4: mov     ecx,eax             ; restore block count into ecx
    mov     eax,[ebp-4]         ; the local IV
    mov     esi,eax             ; source address (our local buffer)
    add     esi,16              ; source address (our local buffer)
    mov     edi,esi             ; destination address (our buffer)
    neh_cbc                     ; execute VIA ACE operation
    mov     eax,[ebp+len]       ; recover number of blocks remaining
    mov     ecx,buf_blocks      ; blocks in buffer
    cmp     eax,ecx             ; remaining blocks >= blocks in buffer
    jae     .5
    mov     ecx,eax             ; no - move only remaining blocks
.5: sub     eax,ecx             ; compute new remaining block count
    mov     esi,[ebp-4]         ; load local buffer address
    mov     edi,[ebp+obuf]      ; load output address
.6: add     esi,16
    movaps  xmm0,[esi]          ; aligned move to xmm0
    movups  [edi],xmm0          ; unaligned move to output buffer
    add     edi,16              ; addresses
    sub     ecx,1               ; decrement block count
    jne     .6                  ; and continue if some remain
    mov     [ebp+obuf],edi      ; update output buffer position
    mov     esi,[ebp-4]         ; store the IV
    mov     edi,[ebp+iv]
    movaps  xmm0,[esi]
    movups  [edi],xmm0
    and     eax,eax             ; test remaining block count
    jne     .1                  ; and continue if some remain
.7: mov     esp,ebp
    pop     edi
    pop     esi
    pop     ebx
    pop     ebp
    do_exit

; aes_rval aes_cfb_encrypt(const unsigned char *in, unsigned char *out,
;                     int len, unsigned char *iv, const aes_encrypt_ctx cx[1]);

    do_name _aes_cfb_encrypt

    push    ebp
    push    ebx
    push    esi
    push    edi
    mov     ebp,esp

;   claim stack space for 'buf_blocks' 16 byte blocks and
;   extra space to allow for adjusting the base address
;   for a 16 byte alignment (4 byte alignment is assumed
;   so a maximum of 12 bytes extra is needed). The top 4
;   bytes (at [ebp-4]) are hence used to store the aligned
;   buffer address

    sub     esp,16 * buf_blocks + 32
    mov     edx,esp
    add     edx,12              ; move up 20 bytes, align the
    and     edx,~0x0000000f     ; resulting address and store
    mov     [ebp-4],edx         ; the resulting value
    mov     eax,[ebp+len]       ; get the data length
    and     eax,eax
    je      .6                  ; exit if zero
    shr     eax,4               ; convert to blocks
    mov     ebx,[ebp+iv]
    movups  xmm0,[ebx]          ; move the IV into the local
    movaps  [edx],xmm0          ; buffer
    neh_rekey
.1: mov     [ebp+len],eax       ; store remaining blocks
    mov     ecx,buf_blocks
    cmp     eax,ecx             ; remaining blocks >= buffer Blocks
    jae     .2
    mov     ecx,eax             ; no - process only remaining
.2: mov     eax,ecx             ; save block count in eax
    mov     esi,[ebp+ibuf]      ; source data address
    mov     edi,[ebp-4]         ; destination buffer address
.3: add     edi,16
    movups  xmm0,[esi]          ; unaligned move to xmm0
    movaps  [edi],xmm0          ; aligned move into buffer
    add     esi,16              ; addjust source and
    sub     ecx,1               ; reduce block count
    jne     short .3            ; until all blocks have been moved
    mov     [ebp+ibuf],esi      ; update input buffer position
    mov     ebx,[ebp+ctx]       ; get key address -> ebx
    movzx   ecx,byte[ebx+4*KS_LENGTH]
    shr     ecx,1               ; get control word address ->edx
%if (KEY_TYPE = NEH_LOAD)
    lea     edx,[_enc_load_table-80+ecx]
%elif (KEY_TYPE = NEH_GENERATE)
    lea     edx,[_enc_gen_table-80+ecx]
%else
    lea     edx,[_enc_hybrid_table-80+ecx]
%endif
    mov     ecx,eax             ; restore block count into ecx
    mov     eax,[ebp-4]         ; the local IV
    mov     esi,eax             ; source address (our local buffer)
    add     esi,16              ; source address (our local buffer)

⌨️ 快捷键说明

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