VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/Library/VeraCryptLib/llmath.c
blob: 7d796576d400b4fb9cf3022f8be2e59fb7063f24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#include <uefi.h>
void __cdecl atexit() {}

int __cdecl _purecall() { return 0; }

#if defined(_M_IX86)
//////////////////////////////////////////////////////////////////////////
// _allmul
//////////////////////////////////////////////////////////////////////////
__declspec(naked) void __cdecl _allmul(void)
{
   _asm {
      mov  ebx, [esp + 4]              ; ebx <- M1[0..31]
      mov  edx, [esp + 12]             ; edx <- M2[0..31]
      mov  ecx, ebx
      mov  eax, edx
      imul ebx, [esp + 16]             ; ebx <- M1[0..31] * M2[32..63]
      imul edx, [esp + 8]              ; edx <- M1[32..63] * M2[0..31]
      add  ebx, edx                    ; carries are abandoned
      mul  ecx                         ; edx:eax <- M1[0..31] * M2[0..31]
      add  edx, ebx                    ; carries are abandoned
      ret 16
   }
}

//////////////////////////////////////////////////////////////////////////
// _aullmul
//////////////////////////////////////////////////////////////////////////
__declspec(naked) void __cdecl _aullmul()
{
   _asm {
      mov  ebx, [esp + 4]              ; ebx <- M1[0..31]
      mov  edx, [esp + 12]             ; edx <- M2[0..31]
      mov  ecx, ebx
      mov  eax, edx
      imul ebx, [esp + 16]             ; ebx <- M1[0..31] * M2[32..63]
      imul edx, [esp + 8]              ; edx <- M1[32..63] * M2[0..31]
      add  ebx, edx                    ; carries are abandoned
      mul  ecx                         ; edx:eax <- M1[0..31] * M2[0..31]
      add  edx, ebx                    ; carries are abandoned
      ret 16
   }
}

//////////////////////////////////////////////////////////////////////////
// _alldiv
//////////////////////////////////////////////////////////////////////////
__declspec(naked) void __cdecl _alldiv()
{
   _asm {
      ; Check sign of res
      mov     ebx, [esp + 8]    ; dividend msdw
      mov     ecx, [esp + 16]   ; divisor msdw
      xor     ebx, ecx
      shr     ebx, 31
      jz      _PosRes           ; if Result is positive
      push    1                 ; if is negative
      jmp _Preparing
      _PosRes:
      push    0

      ; Preparing operands
      ; Dividend
      _Preparing:
      mov     ecx, [esp + 12]
      shr     ecx, 31
      jz      _ChkDvsr                        ; Divident is positive
      mov     eax, [esp + 12]                 ; is negative
      mov     ecx, [esp + 8]
      xor     eax, 0xFFFFFFFF
      xor     ecx, 0xFFFFFFFF
      add     ecx, 1
      jnc     _DvntOK
      adc     eax, 0
      _DvntOK:
      mov     [esp + 12], eax
      mov     [esp + 8], ecx

      ; Divisor
      _ChkDvsr:
      mov     ecx, [esp + 20]
      shr     ecx, 31
      jz      _Divide                         ; Divisor is positive
      mov     eax, [esp + 20]                 ; is negative
      mov     ecx, [esp + 16]
      xor     eax, 0xFFFFFFFF
      xor     ecx, 0xFFFFFFFF
      add     ecx, 1
      jnc     _DvsrOK
      adc     eax, 0
      _DvsrOK:
      mov     [esp + 20], eax
      mov     [esp + 16], ecx
      
      _Divide:
      mov     ecx, [esp + 20]             ; ecx <- divisor[32..63]
      test    ecx, ecx
      jnz     __DivRemU64x64              ; call __DivRemU64x64 if Divisor > 2^32
      mov     ecx, [esp + 16]             ; ecx <- divisor
      mov     eax, [esp + 12]             ; eax <- dividend[32..63]
      xor     edx, edx
      div     ecx                         ; eax <- quotient[32..63], edx <- remainder
      push    eax
      mov     eax, [esp + 12]             ; eax <- dividend[0..31]
      div     ecx                         ; eax <- quotient[0..31]
      pop     edx                         ; edx <- quotient[32..63] - edx:eax
      jmp     _GetSign

      __DivRemU64x64:
      mov     edx, dword ptr [esp + 12]
      mov     eax, dword ptr [esp + 8]    ; edx:eax <- dividend
      mov     edi, edx
      mov     esi, eax                    ; edi:esi <- dividend
      mov     ebx, dword ptr [esp + 16]   ; ecx:ebx <- divisor
      _B:
      shr     edx, 1
      rcr     eax, 1
      shrd    ebx, ecx, 1
      shr     ecx, 1
      jnz     _B
      div     ebx
      mov     ebx, eax                    ; ebx <- quotient
      mov     ecx, [esp + 20]             ; ecx <- high dword of divisor
      mul     dword ptr [esp + 16]        ; edx:eax <- quotient * divisor[0..31]
      imul    ecx, ebx                    ; ecx <- quotient * divisor[32..63]
      add     edx, ecx                    ; edx <- (quotient * divisor)[32..63]
      ;mov     ecx, dword ptr [esp + 32]   ; ecx <- addr for Remainder
      jc      _TooLarge                   ; product > 2^64
      cmp     edi, edx                    ; compare high 32 bits
      ja      _Correct
      jb      _TooLarge                   ; product > dividend
      cmp     esi, eax
      jae     _Correct                    ; product <= dividend
      _TooLarge:
      dec     ebx                         ; adjust quotient by -1
      jecxz   _Return                     ; return if Remainder == NULL
      sub     eax, dword ptr [esp + 16]
      sbb     edx, dword ptr [esp + 20]   ; edx:eax <- (quotient - 1) * divisor
      _Correct:
      jecxz   _Return
      sub     esi, eax
      sbb     edi, edx                    ; edi:esi <- remainder
      ;mov     [ecx], esi
      ;mov     [ecx + 4], edi
      _Return:
      mov     eax, ebx                    ; eax <- quotient
      xor     edx, edx                    ; quotient is 32 bits long

      ; Get sign of result
      _GetSign:
      pop     ecx                         ; Sign of res
      jecxz   _Rtrn                       ; Result is positive
      xor     eax, 0xFFFFFFFF
      xor     edx, 0xFFFFFFFF
      add     eax, 1                      ; edx:eax
      jnc     _Rtrn
      adc     edx, 0

      _Rtrn:
      ret     16
   }
}

//////////////////////////////////////////////////////////////////////////
// _aulldiv
//////////////////////////////////////////////////////////////////////////
__declspec(naked) void __cdecl _aulldiv()
{
   _asm {
      mov     ecx, [esp + 16]             ; ecx <- divisor[32..63]
      test    ecx, ecx
      jnz     __DivRemU64x64              ; call __DivRemU64x64 if Divisor > 2^32
      mov     ecx, [esp + 12]             ; ecx <- divisor
      mov     eax, [esp + 8]              ; eax <- dividend[32..63]
      xor     edx, edx
      div     ecx                         ; eax <- quotient[32..63], edx <- remainder
      push    eax
      mov     eax, [esp + 8]              ; eax <- dividend[0..31]
      div     ecx                         ; eax <- quotient[0..31]
      pop     edx                         ; edx <- quotient[32..63]
      ret     16

      __DivRemU64x64:
      mov     edx, dword ptr [esp + 8]
      mov     eax, dword ptr [esp + 4]    ; edx:eax <- dividend
      mov     edi, edx
      mov     esi, eax                    ; edi:esi <- dividend
      mov     ebx, dword ptr [esp + 12]   ; ecx:ebx <- divisor
      _B:
      shr     edx, 1
      rcr     eax, 1
      shrd    ebx, ecx, 1
      shr     ecx, 1
      jnz     _B
      div     ebx
      mov     ebx, eax                    ; ebx <- quotient
      mov     ecx, [esp + 16]             ; ecx <- high dword of divisor
      mul     dword ptr [esp + 12]        ; edx:eax <- quotient * divisor[0..31]
      imul    ecx, ebx                    ; ecx <- quotient * divisor[32..63]
      add     edx, ecx                    ; edx <- (quotient * divisor)[32..63]
      ;mov     ecx, dword ptr [esp + 32]   ; ecx <- addr for Remainder
      jc      _TooLarge                   ; product > 2^64
      cmp     edi, edx                    ; compare high 32 bits
      ja      _Correct
      jb      _TooLarge                   ; product > dividend
      cmp     esi, eax
      jae     _Correct                    ; product <= dividend
      _TooLarge:
      dec     ebx                         ; adjust quotient by -1
      jecxz   _Return                     ; return if Remainder == NULL
      sub     eax, dword ptr [esp + 12]
      sbb     edx, dword ptr [esp + 16]   ; edx:eax <- (quotient - 1) * divisor
      _Correct:
      jecxz   _Return
      sub     esi, eax
      sbb     edi, edx                    ; edi:esi <- remainder
      ;mov     [ecx], esi
      ;mov     [ecx + 4], edi
      _Return:
      mov     eax, ebx                    ; eax <- quotient
      xor     edx, edx                    ; quotient is 32 bits long

      ret     16
   }
}

//////////////////////////////////////////////////////////////////////////
// Shifts
//////////////////////////////////////////////////////////////////////////
__declspec(naked) void __cdecl _aullshr1() {
   _asm {
    ;
    ; Checking: Only handle 64bit shifting or more
    ;
    cmp     cl, 64
    jae     _Exit

    ;
    ; Handle shifting between 0 and 31 bits
    ;
    cmp     cl, 32
    jae     More32
    shrd    eax, edx, cl
    shr     edx, cl
    ret

    ;
    ; Handle shifting of 32-63 bits
    ;
More32:
    mov     eax, edx
    xor     edx, edx
    and     cl, 31
    shr     eax, cl
    ret

    ;
    ; Invalid number (less then 32bits), return 0
    ;
_Exit:
    xor     eax, eax
    xor     edx, edx
    ret
  }
}

__declspec(naked) void __cdecl _allshl1() {
     _asm {
    ;
    ; Handle shifting of 64 or more bits (return 0)
    ;
    cmp     cl, 64
    jae     short ReturnZero

    ;
    ; Handle shifting of between 0 and 31 bits
    ;
    cmp     cl, 32
    jae     short More32
    shld    edx, eax, cl
    shl     eax, cl
    ret

    ;
    ; Handle shifting of between 32 and 63 bits
    ;
More32:
    mov     edx, eax
    xor     eax, eax
    and     cl, 31
    shl     edx, cl
    ret

ReturnZero:
    xor     eax,eax
    xor     edx,edx
    ret
  }
}

UINT64
EFIAPI
DivU64x64Remainder(
IN      UINT64                    Dividend,
IN      UINT64                    Divisor,
OUT     UINT64                    *Remainder  OPTIONAL
);
/*
 * Divides a 64-bit unsigned value by another 64-bit unsigned value and returns
 * the 64-bit unsigned remainder.
 */
__declspec(naked) void __cdecl _aullrem(void)
{
  //
  // Wrapper Implementation over EDKII DivU64x64Remainder() routine
  //    UINT64
  //    EFIAPI
  //    DivU64x64Remainder (
  //      IN      UINT64     Dividend,
  //      IN      UINT64     Divisor,
  //      OUT     UINT64     *Remainder  OPTIONAL
  //      )
  //
  _asm {
    ; Original local stack when calling _aullrem
    ;               -----------------
    ;               |               |
    ;               |---------------|
    ;               |               |
    ;               |--  Divisor  --|
    ;               |               |
    ;               |---------------|
    ;               |               |
    ;               |--  Dividend --|
    ;               |               |
    ;               |---------------|
    ;               |  ReturnAddr** |
    ;       ESP---->|---------------|
    ;

    ;
    ; Set up the local stack for Reminder pointer
    ;
    sub  esp, 8
    push esp

    ;
    ; Set up the local stack for Divisor parameter
    ;
    mov  eax, [esp + 28]
    push eax
    mov  eax, [esp + 28]
    push eax

    ;
    ; Set up the local stack for Dividend parameter
    ;
    mov  eax, [esp + 28]
    push eax
    mov  eax, [esp + 28]
    push eax

    ;
    ; Call native DivU64x64Remainder of BaseLib
    ;
    call DivU64x64Remainder

    ;
    ; Put the Reminder in EDX:EAX as return value
    ;
    mov  eax, [esp + 20]
    mov  edx, [esp + 24]

    ;
    ; Adjust stack
    ;
    add  esp, 28

    ret  16
  }
}

#endif