aboutsummaryrefslogtreecommitdiffstats
path: root/modules/alg_ia32/sha1core.S
blob: 191686a7d4989f9c320850d83e2c78c6e08f010d (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
/*************************************************
* SHA-160 Source File                            *
* (C) 1999-2006 The Botan Project                *
*************************************************/

#include <botan/asm_macr.h>

START_LISTING(sha1core.S)

START_FUNCTION(sha160_core)
   SPILL_REGS()

#define PUSHED 4

   ASSIGN(EBP, ARG(2))
   ASSIGN(EDI, ARG(3))

   ZEROIZE(ESI)

START_LOOP(.LOAD_INPUT)
   ADD_IMM(ESI, 4)

   ASSIGN(EAX, ARRAY4(EBP, 0))
   ASSIGN(EBX, ARRAY4(EBP, 1))
   ASSIGN(ECX, ARRAY4(EBP, 2))
   ASSIGN(EDX, ARRAY4(EBP, 3))

   ADD_IMM(EBP, 16)

   BSWAP(EAX)
   BSWAP(EBX)
   BSWAP(ECX)
   BSWAP(EDX)

   ASSIGN(ARRAY4_INDIRECT(EDI,ESI,-4), EAX)
   ASSIGN(ARRAY4_INDIRECT(EDI,ESI,-3), EBX)
   ASSIGN(ARRAY4_INDIRECT(EDI,ESI,-2), ECX)
   ASSIGN(ARRAY4_INDIRECT(EDI,ESI,-1), EDX)
LOOP_UNTIL(ESI, IMM(16), .LOAD_INPUT)

   ADD2_IMM(EBP, EDI, 64)

START_LOOP(.EXPANSION)
   ADD_IMM(ESI, 4)

   ZEROIZE(EAX)
   ASSIGN(EBX, ARRAY4(EBP, -1))
   ASSIGN(ECX, ARRAY4(EBP, -2))
   ASSIGN(EDX, ARRAY4(EBP, -3))

   XOR(EAX, ARRAY4(EBP, -5))
   XOR(EBX, ARRAY4(EBP, -6))
   XOR(ECX, ARRAY4(EBP, -7))
   XOR(EDX, ARRAY4(EBP, -8))

   XOR(EAX, ARRAY4(EBP, -11))
   XOR(EBX, ARRAY4(EBP, -12))
   XOR(ECX, ARRAY4(EBP, -13))
   XOR(EDX, ARRAY4(EBP, -14))

   XOR(EAX, ARRAY4(EBP, -13))
   XOR(EBX, ARRAY4(EBP, -14))
   XOR(ECX, ARRAY4(EBP, -15))
   XOR(EDX, ARRAY4(EBP, -16))

   ROTL_IMM(EDX, 1)
   ROTL_IMM(ECX, 1)
   ROTL_IMM(EBX, 1)
   XOR(EAX, EDX)
   ROTL_IMM(EAX, 1)

   ASSIGN(ARRAY4(EBP, 0), EDX)
   ASSIGN(ARRAY4(EBP, 1), ECX)
   ASSIGN(ARRAY4(EBP, 2), EBX)
   ASSIGN(ARRAY4(EBP, 3), EAX)

   ADD_IMM(EBP, 16)
LOOP_UNTIL(ESI, IMM(80), .EXPANSION)

   ASSIGN(EBP, ARG(1))
   ASSIGN(EAX, ARRAY4(EBP, 0))
   ASSIGN(EBX, ARRAY4(EBP, 1))
   ASSIGN(ECX, ARRAY4(EBP, 2))
   ASSIGN(EDX, ARRAY4(EBP, 3))
   ASSIGN(ESI, ARRAY4(EBP, 4))

#define MAGIC1 0x5A827999
#define MAGIC2 0x6ED9EBA1
#define MAGIC3 0x8F1BBCDC
#define MAGIC4 0xCA62C1D6

#define F1(A, B, C, D, E, T, MSG) \
   ROTL_IMM(A, 5)               ; \
   ADD(E, ARRAY4(EDI, MSG))     ; \
   ASSIGN(T, C)                 ; \
   XOR(T, D)                    ; \
   AND(T, B)                    ; \
   XOR(T, D)                    ; \
   ROTR_IMM(B, 2)               ; \
   ADD(E, A)                    ; \
   ADD3_IMM(E, T, MAGIC1)       ; \
   ROTR_IMM(A, 5)               ;

#define F2_OR_F4(A, B, C, D, E, T, MSG, MAGIC) \
   ROTL_IMM(A, 5)               ; \
   ADD(E, ARRAY4(EDI, MSG))     ; \
   ASSIGN(T, D)                 ; \
   XOR(T, C)                    ; \
   XOR(T, B)                    ; \
   ROTR_IMM(B, 2)               ; \
   ADD(E, A)                    ; \
   ADD3_IMM(E, T, MAGIC)        ; \
   ROTR_IMM(A, 5)               ;

#define F3(A, B, C, D, E, T, MSG) \
   ROTL_IMM(A, 5)               ; \
   ADD(E, ARRAY4(EDI, MSG))     ; \
   ASSIGN(T, B)                 ; \
   OR(T, C)                     ; \
   AND(T, D)                    ; \
   ASSIGN(ARRAY4(EDI, MSG), B)  ; \
   AND(ARRAY4(EDI, MSG), C)     ; \
   OR(T, ARRAY4(EDI, MSG))      ; \
   ROTR_IMM(B, 2)               ; \
   ADD(E, A)                    ; \
   ADD3_IMM(E, T, MAGIC3)       ; \
   ROTR_IMM(A, 5)               ;

#define F2(A, B, C, D, E, T, MSG) \
   F2_OR_F4(A, B, C, D, E, T, MSG, MAGIC2)

#define F4(A, B, C, D, E, T, MSG) \
   F2_OR_F4(A, B, C, D, E, T, MSG, MAGIC4)

#define F_BLOCK(F, MSG) \
   F(EAX, EBX, ECX, EDX, ESI, EBP, (MSG+0)) \
   F(ESI, EAX, EBX, ECX, EDX, EBP, (MSG+1)) \
   F(EDX, ESI, EAX, EBX, ECX, EBP, (MSG+2)) \
   F(ECX, EDX, ESI, EAX, EBX, EBP, (MSG+3)) \
   F(EBX, ECX, EDX, ESI, EAX, EBP, (MSG+4))

   F_BLOCK(F1, 0)
   F_BLOCK(F1, 5)
   F_BLOCK(F1, 10)
   F_BLOCK(F1, 15)

   F_BLOCK(F2, 20)
   F_BLOCK(F2, 25)
   F_BLOCK(F2, 30)
   F_BLOCK(F2, 35)

   F_BLOCK(F3, 40)
   F_BLOCK(F3, 45)
   F_BLOCK(F3, 50)
   F_BLOCK(F3, 55)

   F_BLOCK(F4, 60)
   F_BLOCK(F4, 65)
   F_BLOCK(F4, 70)
   F_BLOCK(F4, 75)

   ASSIGN(EBP, ARG(1))
   ADD(ARRAY4(EBP, 0), EAX)
   ADD(ARRAY4(EBP, 1), EBX)
   ADD(ARRAY4(EBP, 2), ECX)
   ADD(ARRAY4(EBP, 3), EDX)
   ADD(ARRAY4(EBP, 4), ESI)

   RESTORE_REGS()
END_FUNCTION(sha160_core)