xref: /netbsd/sys/crypto/des/arch/i386/des_cbc.S (revision c4a72b64)
1/* $NetBSD: des_cbc.S,v 1.2 2002/11/12 03:03:11 itohy Exp $ */
2
3/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
4 * All rights reserved.
5 *
6 * This package is an SSL implementation written
7 * by Eric Young (eay@cryptsoft.com).
8 * The implementation was written so as to conform with Netscapes SSL.
9 *
10 * This library is free for commercial and non-commercial use as long as
11 * the following conditions are aheared to.  The following conditions
12 * apply to all code found in this distribution, be it the RC4, RSA,
13 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
14 * included with this distribution is covered by the same copyright terms
15 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 *
17 * Copyright remains Eric Young's, and as such any Copyright notices in
18 * the code are not to be removed.
19 * If this package is used in a product, Eric Young should be given attribution
20 * as the author of the parts of the library used.
21 * This can be in the form of a textual message at program startup or
22 * in documentation (online or textual) provided with the package.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 * 1. Redistributions of source code must retain the copyright
28 *    notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright
30 *    notice, this list of conditions and the following disclaimer in the
31 *    documentation and/or other materials provided with the distribution.
32 * 3. All advertising materials mentioning features or use of this software
33 *    must display the following acknowledgement:
34 *    "This product includes cryptographic software written by
35 *     Eric Young (eay@cryptsoft.com)"
36 *    The word 'cryptographic' can be left out if the rouines from the library
37 *    being used are not cryptographic related :-).
38 * 4. If you include any Windows specific code (or a derivative thereof) from
39 *    the apps directory (application code) you must include an acknowledgement:
40 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 *
42 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * SUCH DAMAGE.
53 *
54 * The licence and distribution terms for any publically available version or
55 * derivative of this code cannot be changed.  i.e. this code cannot simply be
56 * copied and put under another distribution licence
57 * [including the GNU Public Licence.]
58 */
59
60/*
61 * Modified from the output of `perl des686.pl elf' by
62 * Thor Lancelot Simon <tls@netbsd.org>
63 */
64
65#include <i386/include/asm.h>
66
67ENTRY(des_ncbc_encrypt)
68
69	pushl	%ebp
70	pushl	%ebx
71	pushl	%esi
72	pushl	%edi
73	movl	28(%esp),	%ebp
74	# getting iv ptr from parameter 4
75	movl	36(%esp),	%ebx
76	movl	(%ebx),		%esi
77	movl	4(%ebx),	%edi
78	pushl	%edi
79	pushl	%esi
80	pushl	%edi
81	pushl	%esi
82	movl	%esp,		%ebx
83	movl	36(%esp),	%esi
84	movl	40(%esp),	%edi
85	# getting encrypt flag from parameter 5
86	movl	56(%esp),	%ecx
87	# get and push parameter 5
88	pushl	%ecx
89	# get and push parameter 3
90	movl	52(%esp),	%eax
91	pushl	%eax
92	pushl	%ebx
93	cmpl	$0,		%ecx
94	jz	.L004decrypt
95	andl	$4294967288,	%ebp
96	movl	12(%esp),	%eax
97	movl	16(%esp),	%ebx
98	jz	.L005encrypt_finish
99.L006encrypt_loop:
100	movl	(%esi),		%ecx
101	movl	4(%esi),	%edx
102	xorl	%ecx,		%eax
103	xorl	%edx,		%ebx
104	movl	%eax,		12(%esp)
105	movl	%ebx,		16(%esp)
106	call	_C_LABEL(des_encrypt1)
107	movl	12(%esp),	%eax
108	movl	16(%esp),	%ebx
109	movl	%eax,		(%edi)
110	movl	%ebx,		4(%edi)
111	addl	$8,		%esi
112	addl	$8,		%edi
113	subl	$8,		%ebp
114	jnz	.L006encrypt_loop
115.L005encrypt_finish:
116	movl	56(%esp),	%ebp
117	andl	$7,		%ebp
118	jz	.L007finish
119	xorl	%ecx,		%ecx
120	xorl	%edx,		%edx
121	movl	.L008cbc_enc_jmp_table(,%ebp,4),%ebp
122	jmp	*%ebp
123.L009ej7:
124	movb	6(%esi),	%dh
125	sall	$8,		%edx
126.L010ej6:
127	movb	5(%esi),	%dh
128.L011ej5:
129	movb	4(%esi),	%dl
130.L012ej4:
131	movl	(%esi),		%ecx
132	jmp	.L013ejend
133.L014ej3:
134	movb	2(%esi),	%ch
135	sall	$8,		%ecx
136.L015ej2:
137	movb	1(%esi),	%ch
138.L016ej1:
139	movb	(%esi),		%cl
140.L013ejend:
141	xorl	%ecx,		%eax
142	xorl	%edx,		%ebx
143	movl	%eax,		12(%esp)
144	movl	%ebx,		16(%esp)
145	call	_C_LABEL(des_encrypt1)
146	movl	12(%esp),	%eax
147	movl	16(%esp),	%ebx
148	movl	%eax,		(%edi)
149	movl	%ebx,		4(%edi)
150	jmp	.L007finish
151#ifdef __ELF__
152.align 16
153#else
154.align 4
155#endif
156.L004decrypt:
157	andl	$4294967288,	%ebp
158	movl	20(%esp),	%eax
159	movl	24(%esp),	%ebx
160	jz	.L017decrypt_finish
161.L018decrypt_loop:
162	movl	(%esi),		%eax
163	movl	4(%esi),	%ebx
164	movl	%eax,		12(%esp)
165	movl	%ebx,		16(%esp)
166	call	_C_LABEL(des_encrypt1)
167	movl	12(%esp),	%eax
168	movl	16(%esp),	%ebx
169	movl	20(%esp),	%ecx
170	movl	24(%esp),	%edx
171	xorl	%eax,		%ecx
172	xorl	%ebx,		%edx
173	movl	(%esi),		%eax
174	movl	4(%esi),	%ebx
175	movl	%ecx,		(%edi)
176	movl	%edx,		4(%edi)
177	movl	%eax,		20(%esp)
178	movl	%ebx,		24(%esp)
179	addl	$8,		%esi
180	addl	$8,		%edi
181	subl	$8,		%ebp
182	jnz	.L018decrypt_loop
183.L017decrypt_finish:
184	movl	56(%esp),	%ebp
185	andl	$7,		%ebp
186	jz	.L007finish
187	movl	(%esi),		%eax
188	movl	4(%esi),	%ebx
189	movl	%eax,		12(%esp)
190	movl	%ebx,		16(%esp)
191	call	_C_LABEL(des_encrypt1)
192	movl	12(%esp),	%eax
193	movl	16(%esp),	%ebx
194	movl	20(%esp),	%ecx
195	movl	24(%esp),	%edx
196	xorl	%eax,		%ecx
197	xorl	%ebx,		%edx
198	movl	(%esi),		%eax
199	movl	4(%esi),	%ebx
200.L019dj7:
201	rorl	$16,		%edx
202	movb	%dl,		6(%edi)
203	shrl	$16,		%edx
204.L020dj6:
205	movb	%dh,		5(%edi)
206.L021dj5:
207	movb	%dl,		4(%edi)
208.L022dj4:
209	movl	%ecx,		(%edi)
210	jmp	.L023djend
211.L024dj3:
212	rorl	$16,		%ecx
213	movb	%cl,		2(%edi)
214	sall	$16,		%ecx
215.L025dj2:
216	movb	%ch,		1(%esi)
217.L026dj1:
218	movb	%cl,		(%esi)
219.L023djend:
220	jmp	.L007finish
221#ifdef __ELF__
222.align 16
223#else
224.align 4
225#endif
226.L007finish:
227	movl	64(%esp),	%ecx
228	addl	$28,		%esp
229	movl	%eax,		(%ecx)
230	movl	%ebx,		4(%ecx)
231	popl	%edi
232	popl	%esi
233	popl	%ebx
234	popl	%ebp
235	ret
236#ifdef __ELF__
237.align 16
238#else
239.align 4
240#endif
241.L008cbc_enc_jmp_table:
242	.long 0
243	.long .L016ej1
244	.long .L015ej2
245	.long .L014ej3
246	.long .L012ej4
247	.long .L011ej5
248	.long .L010ej6
249	.long .L009ej7
250#ifdef __ELF__
251.align 16
252#else
253.align 4
254#endif
255.L027cbc_dec_jmp_table:
256	.long 0
257	.long .L026dj1
258	.long .L025dj2
259	.long .L024dj3
260	.long .L022dj4
261	.long .L021dj5
262	.long .L020dj6
263	.long .L019dj7
264.L_des_ncbc_encrypt_end:
265	.size	_C_LABEL(des_ncbc_encrypt),.L_des_ncbc_encrypt_end-_C_LABEL(des_ncbc_encrypt)
266
267ENTRY(des_ede3_cbc_encrypt)
268
269	pushl	%ebp
270	pushl	%ebx
271	pushl	%esi
272	pushl	%edi
273	movl	28(%esp),	%ebp
274	# getting iv ptr from parameter 6
275	movl	44(%esp),	%ebx
276	movl	(%ebx),		%esi
277	movl	4(%ebx),	%edi
278	pushl	%edi
279	pushl	%esi
280	pushl	%edi
281	pushl	%esi
282	movl	%esp,		%ebx
283	movl	36(%esp),	%esi
284	movl	40(%esp),	%edi
285	# getting encrypt flag from parameter 7
286	movl	64(%esp),	%ecx
287	# get and push parameter 5
288	movl	56(%esp),	%eax
289	pushl	%eax
290	# get and push parameter 4
291	movl	56(%esp),	%eax
292	pushl	%eax
293	# get and push parameter 3
294	movl	56(%esp),	%eax
295	pushl	%eax
296	pushl	%ebx
297	cmpl	$0,		%ecx
298	jz	.L028decrypt
299	andl	$4294967288,	%ebp
300	movl	16(%esp),	%eax
301	movl	20(%esp),	%ebx
302	jz	.L029encrypt_finish
303.L030encrypt_loop:
304	movl	(%esi),		%ecx
305	movl	4(%esi),	%edx
306	xorl	%ecx,		%eax
307	xorl	%edx,		%ebx
308	movl	%eax,		16(%esp)
309	movl	%ebx,		20(%esp)
310	call	_C_LABEL(des_encrypt3)
311	movl	16(%esp),	%eax
312	movl	20(%esp),	%ebx
313	movl	%eax,		(%edi)
314	movl	%ebx,		4(%edi)
315	addl	$8,		%esi
316	addl	$8,		%edi
317	subl	$8,		%ebp
318	jnz	.L030encrypt_loop
319.L029encrypt_finish:
320	movl	60(%esp),	%ebp
321	andl	$7,		%ebp
322	jz	.L031finish
323	xorl	%ecx,		%ecx
324	xorl	%edx,		%edx
325	movl	.L032cbc_enc_jmp_table(,%ebp,4),%ebp
326	jmp	*%ebp
327.L033ej7:
328	movb	6(%esi),	%dh
329	sall	$8,		%edx
330.L034ej6:
331	movb	5(%esi),	%dh
332.L035ej5:
333	movb	4(%esi),	%dl
334.L036ej4:
335	movl	(%esi),		%ecx
336	jmp	.L037ejend
337.L038ej3:
338	movb	2(%esi),	%ch
339	sall	$8,		%ecx
340.L039ej2:
341	movb	1(%esi),	%ch
342.L040ej1:
343	movb	(%esi),		%cl
344.L037ejend:
345	xorl	%ecx,		%eax
346	xorl	%edx,		%ebx
347	movl	%eax,		16(%esp)
348	movl	%ebx,		20(%esp)
349	call	_C_LABEL(des_encrypt3)
350	movl	16(%esp),	%eax
351	movl	20(%esp),	%ebx
352	movl	%eax,		(%edi)
353	movl	%ebx,		4(%edi)
354	jmp	.L031finish
355#ifdef __ELF__
356.align 16
357#else
358.align 4
359#endif
360.L028decrypt:
361	andl	$4294967288,	%ebp
362	movl	24(%esp),	%eax
363	movl	28(%esp),	%ebx
364	jz	.L041decrypt_finish
365.L042decrypt_loop:
366	movl	(%esi),		%eax
367	movl	4(%esi),	%ebx
368	movl	%eax,		16(%esp)
369	movl	%ebx,		20(%esp)
370	call	_C_LABEL(des_decrypt3)
371	movl	16(%esp),	%eax
372	movl	20(%esp),	%ebx
373	movl	24(%esp),	%ecx
374	movl	28(%esp),	%edx
375	xorl	%eax,		%ecx
376	xorl	%ebx,		%edx
377	movl	(%esi),		%eax
378	movl	4(%esi),	%ebx
379	movl	%ecx,		(%edi)
380	movl	%edx,		4(%edi)
381	movl	%eax,		24(%esp)
382	movl	%ebx,		28(%esp)
383	addl	$8,		%esi
384	addl	$8,		%edi
385	subl	$8,		%ebp
386	jnz	.L042decrypt_loop
387.L041decrypt_finish:
388	movl	60(%esp),	%ebp
389	andl	$7,		%ebp
390	jz	.L031finish
391	movl	(%esi),		%eax
392	movl	4(%esi),	%ebx
393	movl	%eax,		16(%esp)
394	movl	%ebx,		20(%esp)
395	call	_C_LABEL(des_decrypt3)
396	movl	16(%esp),	%eax
397	movl	20(%esp),	%ebx
398	movl	24(%esp),	%ecx
399	movl	28(%esp),	%edx
400	xorl	%eax,		%ecx
401	xorl	%ebx,		%edx
402	movl	(%esi),		%eax
403	movl	4(%esi),	%ebx
404.L043dj7:
405	rorl	$16,		%edx
406	movb	%dl,		6(%edi)
407	shrl	$16,		%edx
408.L044dj6:
409	movb	%dh,		5(%edi)
410.L045dj5:
411	movb	%dl,		4(%edi)
412.L046dj4:
413	movl	%ecx,		(%edi)
414	jmp	.L047djend
415.L048dj3:
416	rorl	$16,		%ecx
417	movb	%cl,		2(%edi)
418	sall	$16,		%ecx
419.L049dj2:
420	movb	%ch,		1(%esi)
421.L050dj1:
422	movb	%cl,		(%esi)
423.L047djend:
424	jmp	.L031finish
425#ifdef __ELF__
426.align 16
427#else
428.align 4
429#endif
430.L031finish:
431	movl	76(%esp),	%ecx
432	addl	$32,		%esp
433	movl	%eax,		(%ecx)
434	movl	%ebx,		4(%ecx)
435	popl	%edi
436	popl	%esi
437	popl	%ebx
438	popl	%ebp
439	ret
440#ifdef __ELF__
441.align 16
442#else
443.align 4
444#endif
445.L032cbc_enc_jmp_table:
446	.long 0
447	.long .L040ej1
448	.long .L039ej2
449	.long .L038ej3
450	.long .L036ej4
451	.long .L035ej5
452	.long .L034ej6
453	.long .L033ej7
454#ifdef __ELF__
455.align 16
456#else
457.align 4
458#endif
459.L051cbc_dec_jmp_table:
460	.long 0
461	.long .L050dj1
462	.long .L049dj2
463	.long .L048dj3
464	.long .L046dj4
465	.long .L045dj5
466	.long .L044dj6
467	.long .L043dj7
468.L_des_ede3_cbc_encrypt_end:
469	.size	_C_LABEL(des_ede3_cbc_encrypt),.L_des_ede3_cbc_encrypt_end-_C_LABEL(des_ede3_cbc_encrypt)
470