"Fossies" - the Fresh Open Source Software Archive

Member "src/Crypto/Aes_hw_cpu.asm" (10 Oct 2018, 5559 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.

    1 ;
    2 ; Copyright (c) 2010 TrueCrypt Developers Association. All rights reserved.
    3 ;
    4 ; Governed by the TrueCrypt License 3.0 the full text of which is contained in
    5 ; the file License.txt included in TrueCrypt binary and source code distribution
    6 ; packages.
    7 ;
    8 
    9 
   10 %ifidn __BITS__, 16
   11     %define R(x) e %+ x
   12 %elifidn __BITS__, 32
   13     %define R(x) e %+ x
   14 %elifidn __BITS__, 64
   15     %define R(x) r %+ x
   16 %endif
   17 
   18 
   19 %macro export_function 1-2 0
   20 
   21     %ifdef MS_STDCALL
   22         global %1@%2
   23         export _%1@%2
   24     %1@%2:
   25     %elifidn __BITS__, 16
   26         global _%1
   27     _%1:
   28     %else
   29         global %1
   30     %1:
   31     %endif
   32 
   33 %endmacro
   34 
   35 
   36 %macro aes_function_entry 1
   37 
   38     ; void (const byte *ks, byte *data);
   39 
   40     export_function %1, 8
   41 
   42     %ifidn __BITS__, 32
   43         mov ecx, [esp + 4 + 4 * 0]
   44         mov edx, [esp + 4 + 4 * 1]
   45     %elifidn __BITS__, 64
   46         %ifnidn __OUTPUT_FORMAT__, win64
   47             mov rcx, rdi
   48             mov rdx, rsi
   49         %endif
   50     %endif
   51 
   52     ; ecx/rcx = ks
   53     ; edx/rdx = data
   54 
   55 %endmacro
   56 
   57 
   58 %macro aes_function_exit 0
   59 
   60     ; void (const byte *, byte *);
   61 
   62     %ifdef MS_STDCALL
   63         ret 8
   64     %else
   65         ret
   66     %endif
   67 
   68 %endmacro
   69 
   70 
   71 %macro push_xmm 2
   72     sub rsp, 16 * (%2 - %1 + 1)
   73 
   74     %assign stackoffset 0
   75     %assign regnumber %1
   76 
   77     %rep (%2 - %1 + 1)
   78         movdqu [rsp + 16 * stackoffset], xmm%[regnumber]
   79 
   80         %assign stackoffset stackoffset+1
   81         %assign regnumber regnumber+1
   82     %endrep
   83 %endmacro
   84 
   85 
   86 %macro pop_xmm 2
   87     %assign stackoffset 0
   88     %assign regnumber %1
   89 
   90     %rep (%2 - %1 + 1)
   91         movdqu xmm%[regnumber], [rsp + 16 * stackoffset]
   92 
   93         %assign stackoffset stackoffset+1
   94         %assign regnumber regnumber+1
   95     %endrep
   96 
   97     add rsp, 16 * (%2 - %1 + 1)
   98 %endmacro
   99 
  100 
  101 %macro aes_hw_cpu 2
  102     %define OPERATION %1
  103     %define BLOCK_COUNT %2
  104 
  105     ; Load data blocks
  106     %assign block 1
  107     %rep BLOCK_COUNT
  108         movdqu xmm %+ block, [R(dx) + 16 * (block - 1)]
  109         %assign block block+1
  110     %endrep
  111 
  112     ; Encrypt/decrypt data blocks
  113     %assign round 0
  114     %rep 15
  115         movdqu xmm0, [R(cx) + 16 * round]
  116 
  117         %assign block 1
  118         %rep BLOCK_COUNT
  119 
  120             %if round = 0
  121                 pxor xmm %+ block, xmm0
  122             %else
  123                 %if round < 14
  124                     aes %+ OPERATION xmm %+ block, xmm0
  125                 %else
  126                     aes %+ OPERATION %+ last xmm %+ block, xmm0
  127                 %endif
  128             %endif
  129 
  130             %assign block block+1
  131         %endrep
  132 
  133         %assign round round+1
  134     %endrep
  135 
  136     ; Store data blocks
  137     %assign block 1
  138     %rep BLOCK_COUNT
  139         movdqu [R(dx) + 16 * (block - 1)], xmm %+ block
  140         %assign block block+1
  141     %endrep
  142 
  143     %undef OPERATION
  144     %undef BLOCK_COUNT
  145 %endmacro
  146 
  147 
  148 %macro aes_hw_cpu_32_blocks 1
  149     %define OPERATION_32_BLOCKS %1
  150 
  151     %ifidn __BITS__, 64
  152         %define MAX_REG_BLOCK_COUNT 15
  153     %else
  154         %define MAX_REG_BLOCK_COUNT 7
  155     %endif
  156 
  157     %ifidn __OUTPUT_FORMAT__, win64
  158         %if MAX_REG_BLOCK_COUNT > 5
  159             push_xmm 6, MAX_REG_BLOCK_COUNT
  160         %endif
  161     %endif
  162 
  163         mov eax, 32 / MAX_REG_BLOCK_COUNT
  164     .1:
  165         aes_hw_cpu OPERATION_32_BLOCKS, MAX_REG_BLOCK_COUNT
  166 
  167         add R(dx), 16 * MAX_REG_BLOCK_COUNT
  168         dec eax
  169         jnz .1
  170 
  171     %if (32 % MAX_REG_BLOCK_COUNT) != 0
  172         aes_hw_cpu OPERATION_32_BLOCKS, (32 % MAX_REG_BLOCK_COUNT)
  173     %endif
  174 
  175     %ifidn __OUTPUT_FORMAT__, win64
  176         %if MAX_REG_BLOCK_COUNT > 5
  177             pop_xmm 6, MAX_REG_BLOCK_COUNT
  178         %endif
  179     %endif
  180 
  181     %undef OPERATION_32_BLOCKS
  182     %undef MAX_REG_BLOCK_COUNT
  183 %endmacro
  184 
  185 
  186 %ifidn __BITS__, 16
  187 
  188     USE16
  189     SEGMENT _TEXT PUBLIC CLASS=CODE USE16
  190     SEGMENT _DATA PUBLIC CLASS=DATA USE16
  191     GROUP DGROUP _TEXT _DATA
  192     SECTION _TEXT
  193 
  194 %else
  195 
  196     SECTION .text
  197 
  198 %endif
  199 
  200 
  201 ; void aes_hw_cpu_enable_sse ();
  202 
  203     export_function aes_hw_cpu_enable_sse
  204         mov R(ax), cr4
  205         or ax, 1 << 9
  206         mov cr4, R(ax)
  207     ret
  208 
  209 
  210 %ifidn __BITS__, 16
  211 
  212 
  213 ; byte is_aes_hw_cpu_supported ();
  214 
  215     export_function is_aes_hw_cpu_supported
  216         mov eax, 1
  217         cpuid
  218         mov eax, ecx
  219         shr eax, 25
  220         and al, 1
  221     ret
  222 
  223 
  224 ; void aes_hw_cpu_decrypt (const byte *ks, byte *data);
  225 
  226     export_function aes_hw_cpu_decrypt
  227         mov ax, -16
  228         jmp aes_hw_cpu_encrypt_decrypt
  229 
  230 ; void aes_hw_cpu_encrypt (const byte *ks, byte *data);
  231 
  232     export_function aes_hw_cpu_encrypt
  233         mov ax, 16
  234 
  235     aes_hw_cpu_encrypt_decrypt:
  236         push bp
  237         mov bp, sp
  238         push di
  239         push si
  240 
  241         mov si, [bp + 4]            ; ks
  242         mov di, [bp + 4 + 2]        ; data
  243 
  244         movdqu xmm0, [si]
  245         movdqu xmm1, [di]
  246 
  247         pxor xmm1, xmm0
  248 
  249         mov cx, 13
  250 
  251     .round1_13:
  252         add si, ax
  253         movdqu xmm0, [si]
  254 
  255         cmp ax, 0
  256         jl .decrypt
  257 
  258         aesenc xmm1, xmm0
  259         jmp .2
  260     .decrypt:
  261         aesdec xmm1, xmm0
  262     .2:
  263         loop .round1_13
  264 
  265         add si, ax
  266         movdqu xmm0, [si]
  267 
  268         cmp ax, 0
  269         jl .decrypt_last
  270 
  271         aesenclast xmm1, xmm0
  272         jmp .3
  273     .decrypt_last:
  274         aesdeclast xmm1, xmm0
  275     .3:
  276         movdqu [di], xmm1
  277 
  278         pop si
  279         pop di
  280         pop bp
  281     ret
  282 
  283 
  284 %else   ; __BITS__ != 16
  285 
  286 
  287 ; byte is_aes_hw_cpu_supported ();
  288 
  289 ; We comment this since we have an alternative C implementation
  290 ; that supports Hyper-V detection workaround
  291 ;
  292 ;   export_function is_aes_hw_cpu_supported
  293 ;       push R(bx)
  294 ;
  295 ;       mov eax, 1
  296 ;       cpuid
  297 ;       mov eax, ecx
  298 ;       shr eax, 25
  299 ;       and eax, 1
  300 ;
  301 ;       pop R(bx)
  302 ;   ret
  303 
  304 
  305 ; void aes_hw_cpu_decrypt (const byte *ks, byte *data);
  306 
  307     aes_function_entry aes_hw_cpu_decrypt
  308         aes_hw_cpu dec, 1
  309     aes_function_exit
  310 
  311 
  312 ; void aes_hw_cpu_decrypt_32_blocks (const byte *ks, byte *data);
  313 
  314     aes_function_entry aes_hw_cpu_decrypt_32_blocks
  315         aes_hw_cpu_32_blocks dec
  316     aes_function_exit
  317 
  318 
  319 ; void aes_hw_cpu_encrypt (const byte *ks, byte *data);
  320 
  321     aes_function_entry aes_hw_cpu_encrypt
  322         aes_hw_cpu enc, 1
  323     aes_function_exit
  324 
  325 
  326 ; void aes_hw_cpu_encrypt_32_blocks (const byte *ks, byte *data);
  327 
  328     aes_function_entry aes_hw_cpu_encrypt_32_blocks
  329         aes_hw_cpu_32_blocks enc
  330     aes_function_exit
  331 
  332 
  333 %endif  ; __BITS__ != 16
  334 
  335 %ifidn __OUTPUT_FORMAT__,elf
  336 section .note.GNU-stack noalloc noexec nowrite progbits
  337 %endif
  338 %ifidn __OUTPUT_FORMAT__,elf32
  339 section .note.GNU-stack noalloc noexec nowrite progbits
  340 %endif
  341 %ifidn __OUTPUT_FORMAT__,elf64
  342 section .note.GNU-stack noalloc noexec nowrite progbits
  343 %endif
  344