"Fossies" - the Fresh Open Source Software Archive

Member "src/Crypto/gost89_x64.asm" (10 Oct 2018, 11066 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Generic Assembler source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "gost89_x64.asm": 1.18_Source_vs_1.19_Source.

    1 ;
    2 ; GOST89 implementation x64
    3 ; 
    4 ; Copyright (c) 2016. Disk Cryptography Services for EFI (DCS), Alex Kolotnikov
    5 ;
    6 ; This program and the accompanying materials
    7 ; are licensed and made available under the terms and conditions
    8 ; of the Apache License, Version 2.0.  
    9 ;
   10 ; The full text of the license may be found at
   11 ; https://opensource.org/licenses/Apache-2.0
   12 ;
   13 ; Some ideas from article https://xakep.ru/2013/10/19/shifrovanie-gost-28147-89/
   14 ;
   15 
   16 [section .bss align=16]
   17 
   18 ;///////////////////////////////////////////////////////////////////
   19 ;// Win64 registers to save
   20 ;///////////////////////////////////////////////////////////////////
   21 %macro SaveRegs 0
   22    sub rsp,8*8+10*16
   23    mov [rsp], rbx
   24    mov [rsp+8], rbp
   25    mov [rsp+8*2], rdi
   26    mov [rsp+8*3], rsi
   27    mov [rsp+8*4], r12
   28    mov [rsp+8*5], r13
   29    mov [rsp+8*6], r14
   30    mov [rsp+8*7], r15
   31 %endmacro
   32 
   33 %macro RestoreRegs 0
   34    mov rbx, [rsp]
   35    mov rbp, [rsp+8]
   36    mov rdi, [rsp+8*2]
   37    mov rsi, [rsp+8*3]
   38    mov r12, [rsp+8*4]
   39    mov r13, [rsp+8*5]
   40    mov r14, [rsp+8*6]
   41    mov r15, [rsp+8*7]
   42    add rsp,8*8+10*16
   43 %endmacro
   44 
   45 [section .text align=16]
   46 ;///////////////////////////////////////////////////////////////////
   47 ;// Crypting 2 blocks
   48 ;///////////////////////////////////////////////////////////////////
   49 %macro gost_round2 2                               ; 1 - pos1, 2 - pos2
   50    ; 1st
   51    ; 1-2 byte
   52    add   ecx, r13d                                 ; add key
   53    movzx edi, cl
   54    movzx esi, ch
   55    xor   r14d, dword [r8 + 32 + 256*3*4 + rdi*4]
   56    xor   r14d, dword [r8 + 32 + 256*2*4 + rsi*4]
   57    shr   ecx, 16
   58    ; 3-4 байт
   59    movzx edi, cl
   60    xor   r14d, dword [r8 + 32 + 256*4 + rdi*4]
   61    movzx esi, ch
   62    xor   r14d, dword [r8 + 32 + rsi*4]
   63    mov   edx, [r8 + %1*4]                          ; read key for second step
   64    
   65    ; 2nd
   66    ; 1-2 byte
   67    add   eax, r10d                                 ; read key
   68    movzx r15d, al
   69    movzx ebp, ah
   70    xor   r11d, dword [r8 + 32 + 256*3*4 + r15*4]
   71    xor   r11d, dword [r8 + 32 + 256*2*4 + rbp*4]
   72    shr   eax, 16
   73    ; 3-4 байт
   74    movzx r15d, al
   75    xor   r11d, dword [r8 + 32 + 256*4 + r15*4]
   76    movzx ebp, ah
   77    xor   r11d, dword [r8 + 32 + rbp*4]
   78    mov   ebx, [r8 + %1*4]                          ; read key for second step
   79    
   80    ; second step
   81    ; 1st
   82    ; 1-2 byte
   83    add   edx, r14d                                 ; add key
   84    movzx edi, dl
   85    movzx esi, dh
   86    xor   r13d, dword [r8 + 32 + 256*3*4 + rdi*4]
   87    xor   r13d, dword [r8 + 32 + 256*2*4 + rsi*4]
   88    shr   edx, 16
   89    ; 3-4 байт
   90    movzx edi, dl
   91    xor   r13d, dword [r8 + 32 + 256*4 + rdi*4]
   92    movzx esi, dh
   93    xor   r13d, dword [r8 + 32 + rsi*4]
   94    mov   ecx, [r8 + %2*4]                          ; read key
   95    
   96    ; 2nd
   97    ; 1-2 byte
   98    add   ebx, r11d;                                ; add key
   99    movzx r15d, bl;
  100    movzx ebp, bh;
  101    xor   r10d, dword [r8 + 32 + 256*3*4 + r15*4]
  102    xor   r10d, dword [r8 + 32 + 256*2*4 + rbp*4]
  103    shr   ebx, 16
  104    ; 3-4 байт
  105    movzx r15d, bl
  106    xor   r10d, dword [r8 + 32 + 256*4 + r15*4]
  107    movzx ebp, bh
  108    xor   r10d, dword [r8 + 32 + rbp*4]
  109    mov   eax, [r8 + %2*4]                          ; read key
  110 %endmacro
  111 
  112 ; input: r8 - &key, rcx - &IN
  113 ; returns: (r13) & (r10)
  114 GostEncrypt2x64:
  115    ; 1st
  116    mov   r13d, [rcx]
  117    mov   r14,  [rcx]
  118    shr   r14, 32
  119 
  120    ; 2nd
  121    mov   r10d, [rcx + 16]
  122    mov   r11,  [rcx + 16]
  123    shr   r11, 32
  124 
  125    mov   ecx, [r8]
  126    mov   eax, ecx
  127 
  128    gost_round2 1, 2
  129    gost_round2 3, 4
  130    gost_round2 5, 6
  131    gost_round2 7, 0
  132 
  133    gost_round2 1, 2
  134    gost_round2 3, 4
  135    gost_round2 5, 6
  136    gost_round2 7, 0
  137 
  138    gost_round2 1, 2
  139    gost_round2 3, 4
  140    gost_round2 5, 6
  141    gost_round2 7, 7
  142 
  143    gost_round2 6, 5
  144    gost_round2 4, 3
  145    gost_round2 2, 1
  146    gost_round2 0, 0
  147 
  148    shl r13, 32                             ; combine
  149    or  r13, r14
  150 
  151    shl r10, 32                             ; combine
  152    or  r10, r11
  153    ret
  154 
  155 ; input: r8 - &key, rcx - &IN
  156 ; returns: (r13) & (r10)
  157 GostDecrypt2x64:
  158    ; 1st
  159    mov   r13d, [rcx]
  160    mov   r14,  [rcx]
  161    shr   r14, 32
  162 
  163    ; 2nd
  164    mov   r10d, [rcx + 16]
  165    mov   r11,  [rcx + 16]
  166    shr   r11, 32
  167 
  168    mov   ecx, [r8]
  169    mov   eax, ecx
  170 
  171    gost_round2 1, 2
  172    gost_round2 3, 4
  173    gost_round2 5, 6
  174    gost_round2 7, 7
  175 
  176    gost_round2 6, 5
  177    gost_round2 4, 3
  178    gost_round2 2, 1
  179    gost_round2 0, 7
  180 
  181    gost_round2 6, 5
  182    gost_round2 4, 3
  183    gost_round2 2, 1
  184    gost_round2 0, 7
  185 
  186    gost_round2 6, 5
  187    gost_round2 4, 3
  188    gost_round2 2, 1
  189    gost_round2 0, 0
  190 
  191    shl r13, 32                             ; combine
  192    or  r13, r14
  193 
  194    shl r10, 32                             ; combine
  195    or  r10, r11
  196 ret
  197 
  198 ;///////////////////////////////////////////////////////////////////
  199 ;// Crypting 1 block
  200 ;///////////////////////////////////////////////////////////////////
  201 %macro gost_round1 2                                     ; 1 - pos1, 2 - pos2
  202    ; 1-2 byte
  203    add   ecx, r13d                                 ; add key
  204    movzx edi, cl
  205    movzx esi, ch
  206    xor   r14d, dword [r8 + 32 + 256*3*4 + rdi*4]
  207    xor   r14d, dword [r8 + 32 + 256*2*4 + rsi*4]
  208    shr   ecx, 16
  209    ; 3-4 байт
  210    movzx edi, cl
  211    xor   r14d, dword [r8 + 32 + 256*4 + rdi*4]
  212    movzx esi, ch
  213    xor   r14d, dword [r8 + 32 + rsi*4]
  214    mov   edx, [r8 + %1*4]                          ; read key for second step
  215    
  216    ; second step
  217    ; 1-2 byte
  218    add   edx, r14d                                 ; add key
  219    movzx edi, dl
  220    movzx esi, dh
  221    xor   r13d, dword [r8 + 32 + 256*3*4 + rdi*4]
  222    xor   r13d, dword [r8 + 32 + 256*2*4 + rsi*4]
  223    shr   edx, 16
  224    ; 3-4 байт
  225    movzx edi, dl
  226    xor   r13d, dword [r8 + 32 + 256*4 + rdi*4]
  227    movzx esi, dh
  228    xor   r13d, dword [r8 + 32 + rsi*4]
  229    mov   ecx, [r8 + %2*4]                          ; read key
  230 %endmacro
  231 
  232 ; input: r8 - &gost_kds rcx - &IN
  233 ; returns: r13
  234 GostEncrypt1x64:
  235    mov   r13d, [rcx]
  236    mov   r14,  [rcx]
  237    shr   r14, 32
  238    mov   ecx, [r8]
  239 
  240    gost_round1 1, 2
  241    gost_round1 3, 4
  242    gost_round1 5, 6
  243    gost_round1 7, 0
  244    
  245    gost_round1 1, 2
  246    gost_round1 3, 4
  247    gost_round1 5, 6
  248    gost_round1 7, 0
  249    
  250    gost_round1 1, 2
  251    gost_round1 3, 4
  252    gost_round1 5, 6
  253    gost_round1 7, 7
  254    
  255    gost_round1 6, 5
  256    gost_round1 4, 3
  257    gost_round1 2, 1
  258    gost_round1 0, 0
  259 
  260    shl r13, 32                             ; combine
  261    or  r13, r14
  262 ret
  263 
  264 ; input: r8 - &gost_kds rcx - IN
  265 ; returns: r13
  266 GostDecrypt1x64:
  267    mov   r13d, [rcx]
  268    mov   r14, [rcx]
  269    shr   r14, 32
  270    mov   ecx, [r8]
  271    
  272    gost_round1 1, 2
  273    gost_round1 3, 4
  274    gost_round1 5, 6
  275    gost_round1 7, 7
  276    
  277    gost_round1 6, 5
  278    gost_round1 4, 3
  279    gost_round1 2, 1
  280    gost_round1 0, 7
  281    
  282    gost_round1 6, 5
  283    gost_round1 4, 3
  284    gost_round1 2, 1
  285    gost_round1 0, 7
  286    
  287    gost_round1 6, 5
  288    gost_round1 4, 3
  289    gost_round1 2, 1
  290    gost_round1 0, 0
  291 
  292    shl r13, 32                             ; combine
  293    or  r13, r14
  294 ret
  295 
  296 global gost_encrypt_128_CBC_asm                     ; gost_encrypt_128_CBC_asm(uint64* in, uint64* out, gost_kds* kds, uint64 count);
  297 ; rcx - &in
  298 ; rdx - &out
  299 ; r8  - &gost_kds
  300 ; r9  - count
  301 gost_encrypt_128_CBC_asm:
  302    SaveRegs                                 ; Saving
  303    
  304    sub rsp, 32
  305    mov [rsp], rdx                             ; Save out addr
  306    mov [rsp + 8], rcx                         ; Save in addr
  307    mov [rsp + 16], r8                         ; key addr
  308 
  309 .do:
  310    mov [rsp + 24], r9                      ; Save count
  311    cmp r9, 2
  312    jge .blk2
  313    cmp r9, 1
  314    jge .blk1
  315    jmp .end
  316 
  317 ; One 128 block encryption
  318 .blk1:
  319    mov  rcx, [rsp + 8]                         ; set in addr
  320    call GostEncrypt1x64
  321 
  322    mov rdx, [rsp]                              ; Restore out
  323    mov rcx, [rsp + 8]                          ; restore in
  324 
  325    mov [rdx], r13
  326    mov rax, [rcx + 8]
  327    xor rax, r13                              ; CBC
  328 
  329    add rdx, 8                                ;next 8 bytes
  330    mov [rdx], rax
  331 
  332    mov rcx, rdx
  333    call GostEncrypt1x64
  334 
  335    mov rdx, [rsp]                             ; Restore out addr
  336    mov rcx, [rsp+8]                           ; Restore in addr
  337 
  338    mov [rdx + 8], r13
  339 
  340    add rdx,16
  341    mov [rsp], rdx
  342 
  343    add rcx, 16
  344    mov [rsp+8], rcx
  345 
  346    mov r9, [rsp + 24]
  347    dec r9
  348 
  349    jmp .do
  350 
  351 .blk2:
  352    mov  rcx, [rsp + 8]                         ; set in addr
  353    call GostEncrypt2x64
  354 
  355    mov rdx, [rsp]                              ; Restore out
  356    mov rcx, [rsp + 8]                          ; restore in
  357 
  358    mov [rdx], r13
  359 
  360    mov rax, [rcx + 8]
  361    xor rax, r13                              ; CBC
  362 
  363    mov [rdx + 16], r10
  364 
  365    mov rbx, [rcx + 24]
  366    xor rbx, r10                              ; CBC
  367 
  368    mov [rdx + 8], rax
  369    mov [rdx + 24], rbx
  370 
  371    add rdx, 8                                ;next 8 bytes
  372 
  373    mov rcx, rdx
  374    call GostEncrypt2x64
  375 
  376    mov rdx, [rsp]                             ; Restore out addr
  377    mov rcx, [rsp+8]                           ; Restore in addr
  378 
  379    mov [rdx + 8], r13
  380    mov [rdx + 24], r10
  381 
  382    add rdx,32
  383    mov [rsp], rdx
  384 
  385    add rcx, 32
  386    mov [rsp+8], rcx
  387 
  388    mov r9, [rsp + 24]
  389    sub r9, 2
  390 
  391    jmp .do
  392 
  393 .end:
  394    add rsp, 32                              ; Load out addr
  395    RestoreRegs                              ; Load
  396 ret
  397 
  398 global gost_decrypt_128_CBC_asm                     ; gost_decrypt_128_CBC_asm(uint64* in, uint64* out, const gost_kds* kds, uint64 count);
  399 ; rcx - &in
  400 ; rdx - &out
  401 ; r8  - &gost_kds
  402 ; r9  - count
  403 gost_decrypt_128_CBC_asm:
  404    SaveRegs                                 ; Saving
  405    
  406    sub rsp, 32
  407    mov [rsp], rdx                           ; Save out addr
  408    mov [rsp+8], rcx                         ; Save in addr
  409    mov [rsp+16], r8                         ; key addr
  410 
  411 .do:
  412    mov [rsp + 24], r9                      ; Save count
  413    cmp r9, 2
  414    jge .blk2
  415    cmp r9, 1
  416    jge .blk1
  417    jmp .end
  418 
  419 ; One 128 block decryption
  420 .blk1:
  421    add  rcx, 8
  422    call GostDecrypt1x64
  423    mov rdx, [rsp]                              ; Restore out
  424    mov rcx, [rsp + 8]                          ; Restore in
  425    mov rax, [rcx]
  426    xor rax, r13                                ; CBC
  427    mov [rdx + 8], rax
  428 
  429    call GostDecrypt1x64
  430 
  431    mov rdx, [rsp]                             ; Restore out addr
  432    mov rcx, [rsp+8]                           ; Restore in addr
  433 
  434    mov [rdx], r13
  435 
  436    add rdx,16
  437    mov [rsp], rdx
  438 
  439    add rcx, 16
  440    mov [rsp+8], rcx
  441 
  442    mov r9, [rsp + 24]
  443    dec r9
  444 
  445    jmp .do
  446 
  447 .blk2:
  448    add  rcx, 8
  449    call GostDecrypt2x64
  450    mov rdx, [rsp]                              ; Restore out
  451    mov rcx, [rsp + 8]                          ; Restore in
  452 
  453    mov rax, [rcx]
  454    xor rax, r13                                ; CBC
  455    mov [rdx + 8], rax
  456 
  457    mov rbx, [rcx+16]
  458    xor rbx, r10                                ; CBC
  459    mov [rdx + 24], rbx
  460 
  461    call GostDecrypt2x64
  462 
  463    mov rdx, [rsp]                             ; Restore out addr
  464    mov rcx, [rsp+8]                           ; Restore in addr
  465 
  466    mov [rdx], r13
  467    mov [rdx+16], r10
  468 
  469    add rdx,32
  470    mov [rsp], rdx
  471 
  472    add rcx,32
  473    mov [rsp+8], rcx
  474 
  475    mov r9, [rsp + 24]
  476    sub r9, 2
  477 
  478    jmp .do
  479 
  480 .end:
  481    add rsp, 32                              ; Load out addr
  482    RestoreRegs                              ; Load
  483 ret