arcfour-crypt.asm 2.76 KB
Newer Older
1
2
C nettle, low-level cryptographics library
C 
Niels Möller's avatar
Niels Möller committed
3
C Copyright (C) 2004, Niels Möller
4
5
6
7
8
9
10
11
12
13
14
15
16
C  
C The nettle library is free software; you can redistribute it and/or modify
C it under the terms of the GNU Lesser General Public License as published by
C the Free Software Foundation; either version 2.1 of the License, or (at your
C option) any later version.
C 
C The nettle library is distributed in the hope that it will be useful, but
C WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
C or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
C License for more details.
C 
C You should have received a copy of the GNU Lesser General Public License
C along with the nettle library; see the file COPYING.LIB.  If not, write to
17
18
C the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
C MA 02111-1301, USA.
19

Niels Möller's avatar
Niels Möller committed
20
	.file "arcfour-crypt.asm"
21
22
23
24
25

	C arcfour_crypt(struct arcfour_ctx *ctx,
	C               unsigned length, uint8_t *dst,
	C               const uint8_t *src)
	.text
26
	ALIGN(16)
27
PROLOGUE(nettle_arcfour_crypt)
28
	C save all registers that need to be saved
Niels Möller's avatar
Niels Möller committed
29
30
31
32
	pushl	%ebx		C  12(%esp)
	pushl	%ebp		C  8(%esp)
	pushl	%esi		C  4(%esp)
	pushl	%edi		C  0(%esp)
33

34
C Input arguments:
35
36
37
38
	C ctx = 20(%esp)
	C length = 24(%esp)
	C dst = 28(%esp)
	C src = 32(%esp)
39
40
C Register usage:
	C %ebp = ctx
41
42
43
	C %esi = src
	C %edi = dst
	C %edx = loop counter
44
45
46
47
	C %eax = i
	C %ebx = j
	C %cl  = si
	C %ch  = sj
48

49
50
	movl	24(%esp), %edx		C  length
	movl	20(%esp), %ebp		C  ctx
51
52
53
54
55
56
57
	movl	28(%esp), %edi		C  dst
	movl	32(%esp), %esi		C  src

	lea	(%edx, %edi), %edi
	lea	(%edx, %esi), %esi
	negl	%edx
	jnc	.Lend
58
	
59
60
	movzbl  ARCFOUR_I (%ebp), %eax	C  i
	movzbl  ARCFOUR_J (%ebp), %ebx	C  j
61
62
63
64
65

	incb	%al
	sarl	$1, %edx
	jc	.Lloop_odd
	
66
	ALIGN(16)
67
.Lloop:
68
69
70
71
	movb	(%ebp, %eax), %cl	C  si.
	addb    %cl, %bl
	movb    (%ebp, %ebx), %ch	C  sj
	movb    %ch, (%ebp, %eax)	C  S[i] = sj
72
	incl	%eax
73
74
75
76
77
78
79
80
81
82
83
84
85
	movzbl	%al, %eax
	movb	%cl, (%ebp, %ebx)	C  S[j] = si
	addb    %ch, %cl
	movzbl  %cl, %ecx		C  Clear, so it can be used
					C  for indexing.
	movb    (%ebp, %ecx), %cl
	xorb    (%esi, %edx, 2), %cl
	movb    %cl, (%edi, %edx, 2)

	C FIXME: Could exchange cl and ch in the second half
	C and try to interleave instructions better.
.Lloop_odd:
	movb	(%ebp, %eax), %cl	C  si.
86
	addb    %cl, %bl
87
	movb    (%ebp, %ebx), %ch	C  sj
88
	movb    %ch, (%ebp, %eax)	C  S[i] = sj
89
90
	incl	%eax
	movzbl	%al, %eax
91
	movb	%cl, (%ebp, %ebx)	C  S[j] = si
92
	addb    %ch, %cl
93
	movzbl  %cl, %ecx		C  Clear, so it can be used
94
95
					C  for indexing.
	movb    (%ebp, %ecx), %cl
96
97
98
99
100
	xorb    1(%esi, %edx, 2), %cl
	incl    %edx
	movb    %cl, -1(%edi, %edx, 2)

	jnz	.Lloop
101

102
103
C .Lloop_done:
	decb	%al
104
105
	movb	%al, ARCFOUR_I (%ebp)		C  Store the new i and j.
	movb	%bl, ARCFOUR_J (%ebp)
106
107
108
109
110
111
.Lend:
	popl	%edi
	popl	%esi
	popl	%ebp
	popl	%ebx
	ret
112
EPILOGUE(nettle_arcfour_crypt)