aboutsummaryrefslogtreecommitdiff
path: root/src/libsec/386/sha1block.spp
blob: 386b2e6c3995be96c68f20e633f727d65480f400 (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
.text

.p2align 2,0x90
#ifdef __Darwin__
.globl __sha1block
__sha1block:
#else
.globl _sha1block
_sha1block:
#endif

/* x = (wp[off-f] ^ wp[off-8] ^ wp[off-14] ^ wp[off-16]) <<< 1;
 * wp[off] = x;
 * x += A <<< 5;
 * E += 0xca62c1d6 + x;
 * x = FN(B,C,D);
 * E += x;
 * B >>> 2
 */
#define BSWAPDI	BYTE $0x0f; BYTE $0xcf;

#define BODY(off,FN,V,A,B,C,D,E)\
	movl (off-64)(%ebp), %edi;\
	xorl (off-56)(%ebp), %edi;\
	xorl (off-32)(%ebp), %edi;\
	xorl (off-12)(%ebp), %edi;\
	roll $1, %edi;\
	movl %edi, off(%ebp);\
	leal V(%edi, E, 1), E;\
	movl A, %edi;\
	roll $5, %edi;\
	addl %edi, E;\
	FN(B,C,D)\
	addl %edi, E;\
	rorl $2, B;\

#define BODY0(off,FN,V,A,B,C,D,E)\
	movl off(%ebx), %edi;\
	bswap %edi;\
	movl %edi, off(%ebp);\
	leal V(%edi,E,1), E;\
	movl A, %edi;\
	roll $5,%edi;\
	addl %edi,E;\
	FN(B,C,D)\
	addl %edi,E;\
	rorl $2,B;\

/*
 * fn1 = (((C^D)&B)^D);
 */
#define FN1(B,C,D)\
	movl C, %edi;\
	xorl D, %edi;\
	andl B, %edi;\
	xorl D, %edi;\

/*
 * fn24 = B ^ C ^ D
 */
#define FN24(B,C,D)\
	movl B, %edi;\
	xorl C, %edi;\
	xorl D, %edi;\

/*
 * fn3 = ((B ^ C) & (D ^= B)) ^ B
 * D ^= B to restore D
 */
#define FN3(B,C,D)\
	movl B, %edi;\
	xorl C, %edi;\
	xorl B, D;\
	andl D, %edi;\
	xorl B, %edi;\
	xorl B, D;\

/*
 * stack offsets
 * void sha1block(uchar *DATA, int LEN, ulong *STATE)
 */
#define STACKSIZE (48+80*4)
#define	DATA	(STACKSIZE+8)
#define	LEN	(STACKSIZE+12)
#define	STATE	(STACKSIZE+16)

/*
 * stack offsets for locals
 * ulong w[80];
 * uchar *edata;
 * ulong *w15, *w40, *w60, *w80;
 * register local
 * ulong *wp = %ebp
 * ulong a = eax, b = ebx, c = ecx, d = edx, e = esi
 * ulong tmp = edi
 */
#define WARRAY	(STACKSIZE-4-(80*4))
#define TMP1	(STACKSIZE-8-(80*4))
#define TMP2	(STACKSIZE-12-(80*4))
#define W15	(STACKSIZE-16-(80*4))
#define W40	(STACKSIZE-20-(80*4))
#define W60	(STACKSIZE-24-(80*4))
#define W80	(STACKSIZE-28-(80*4))
#define EDATA	(STACKSIZE-32-(80*4))
#define OLDEBX	(STACKSIZE-36-(80*4))
#define OLDESI	(STACKSIZE-40-(80*4))
#define OLDEDI	(STACKSIZE-44-(80*4))

	/* Prelude */
	pushl %ebp
	subl $(STACKSIZE), %esp

	mov %ebx, OLDEBX(%esp)
	mov %esi, OLDESI(%esp)
	mov %edi, OLDEDI(%esp)

	movl DATA(%esp), %eax
	addl LEN(%esp), %eax
	movl %eax, EDATA(%esp)

	leal (WARRAY+15*4)(%esp), %edi	/* aw15 */
	movl %edi, W15(%esp)
	leal (WARRAY+40*4)(%esp), %edx	/* aw40 */
	movl %edx, W40(%esp)
	leal (WARRAY+60*4)(%esp), %ecx	/* aw60 */
	movl %ecx, W60(%esp)
	leal (WARRAY+80*4)(%esp), %edi	/* aw80 */
	movl %edi, W80(%esp)

0:
	leal WARRAY(%esp), %ebp		/* warray */

	movl STATE(%esp), %edi		/* state */
	movl (%edi),%eax
	movl 4(%edi),%ebx
	movl %ebx, TMP1(%esp)		/* tmp1 */
	movl 8(%edi), %ecx
	movl 12(%edi), %edx
	movl 16(%edi), %esi

	movl DATA(%esp), %ebx		/* data */

1:
	BODY0(0,FN1,0x5a827999,%eax,TMP1(%esp),%ecx,%edx,%esi)
	movl %esi,TMP2(%esp)
	BODY0(4,FN1,0x5a827999,%esi,%eax,TMP1(%esp),%ecx,%edx)
	movl TMP1(%esp),%esi
	BODY0(8,FN1,0x5a827999,%edx,TMP2(%esp),%eax,%esi,%ecx)
	BODY0(12,FN1,0x5a827999,%ecx,%edx,TMP2(%esp),%eax,%esi)
	movl %esi,TMP1(%esp)
	BODY0(16,FN1,0x5a827999,%esi,%ecx,%edx,TMP2(%esp),%eax)
	movl TMP2(%esp),%esi

	addl $20, %ebx
	addl $20, %ebp
	cmpl W15(%esp), %ebp	/* w15 */
	jb 1b

	BODY0(0,FN1,0x5a827999,%eax,TMP1(%esp),%ecx,%edx,%esi)
	addl $4, %ebx
	movl %ebx, DATA(%esp)	/* data */
	movl TMP1(%esp),%ebx

	BODY(4,FN1,0x5a827999,%esi,%eax,%ebx,%ecx,%edx)
	BODY(8,FN1,0x5a827999,%edx,%esi,%eax,%ebx,%ecx)
	BODY(12,FN1,0x5a827999,%ecx,%edx,%esi,%eax,%ebx)
	BODY(16,FN1,0x5a827999,%ebx,%ecx,%edx,%esi,%eax)

	addl $20, %ebp

2:
	BODY(0,FN24,0x6ed9eba1,%eax,%ebx,%ecx,%edx,%esi)
	BODY(4,FN24,0x6ed9eba1,%esi,%eax,%ebx,%ecx,%edx)
	BODY(8,FN24,0x6ed9eba1,%edx,%esi,%eax,%ebx,%ecx)
	BODY(12,FN24,0x6ed9eba1,%ecx,%edx,%esi,%eax,%ebx)
	BODY(16,FN24,0x6ed9eba1,%ebx,%ecx,%edx,%esi,%eax)

	addl $20,%ebp
	cmpl W40(%esp), %ebp
	jb 2b

3:
	BODY(0,FN3,0x8f1bbcdc,%eax,%ebx,%ecx,%edx,%esi)
	BODY(4,FN3,0x8f1bbcdc,%esi,%eax,%ebx,%ecx,%edx)
	BODY(8,FN3,0x8f1bbcdc,%edx,%esi,%eax,%ebx,%ecx)
	BODY(12,FN3,0x8f1bbcdc,%ecx,%edx,%esi,%eax,%ebx)
	BODY(16,FN3,0x8f1bbcdc,%ebx,%ecx,%edx,%esi,%eax)

	addl $20, %ebp
	cmpl W60(%esp), %ebp 	/* w60 */
	jb 3b

4:
	BODY(0,FN24,0xca62c1d6,%eax,%ebx,%ecx,%edx,%esi)
	BODY(4,FN24,0xca62c1d6,%esi,%eax,%ebx,%ecx,%edx)
	BODY(8,FN24,0xca62c1d6,%edx,%esi,%eax,%ebx,%ecx)
	BODY(12,FN24,0xca62c1d6,%ecx,%edx,%esi,%eax,%ebx)
	BODY(16,FN24,0xca62c1d6,%ebx,%ecx,%edx,%esi,%eax)

	addl $20, %ebp
	cmpl W80(%esp), %ebp 	/* w80 */
	jb 4b

	movl STATE(%esp), %edi	/* state */
	addl %eax, 0(%edi)
	addl %ebx, 4(%edi)
	addl %ecx, 8(%edi)
	addl %edx, 12(%edi)
	addl %esi, 16(%edi)

	movl EDATA(%esp), %edi	/* edata */
	cmpl %edi, DATA(%esp)	/* data */
	jb 0b

	/* Postlude */
	mov OLDEBX(%esp), %ebx
	mov OLDESI(%esp), %esi
	mov OLDEDI(%esp), %edi
	addl $(STACKSIZE), %esp
	popl %ebp
	ret