df3fe86923b7dab3df920bcfdb04ae1b0205f5f4
[platform/upstream/nettle.git] / x86 / arcfour-crypt.asm
1 C x86/arcfour-crypt.asm
2
3 ifelse(<
4    Copyright (C) 2004, Niels Möller
5
6    This file is part of GNU Nettle.
7
8    GNU Nettle is free software: you can redistribute it and/or
9    modify it under the terms of either:
10
11      * the GNU Lesser General Public License as published by the Free
12        Software Foundation; either version 3 of the License, or (at your
13        option) any later version.
14
15    or
16
17      * the GNU General Public License as published by the Free
18        Software Foundation; either version 2 of the License, or (at your
19        option) any later version.
20
21    or both in parallel, as here.
22
23    GNU Nettle is distributed in the hope that it will be useful,
24    but WITHOUT ANY WARRANTY; without even the implied warranty of
25    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26    General Public License for more details.
27
28    You should have received copies of the GNU General Public License and
29    the GNU Lesser General Public License along with this program.  If
30    not, see http://www.gnu.org/licenses/.
31 >)
32
33         .file "arcfour-crypt.asm"
34
35         C arcfour_crypt(struct arcfour_ctx *ctx,
36         C               size_t length, uint8_t *dst,
37         C               const uint8_t *src)
38         .text
39         ALIGN(16)
40 PROLOGUE(nettle_arcfour_crypt)
41         C save all registers that need to be saved
42         pushl   %ebx            C  12(%esp)
43         pushl   %ebp            C  8(%esp)
44         pushl   %esi            C  4(%esp)
45         pushl   %edi            C  0(%esp)
46
47 C Input arguments:
48         C ctx = 20(%esp)
49         C length = 24(%esp)
50         C dst = 28(%esp)
51         C src = 32(%esp)
52 C Register usage:
53         C %ebp = ctx
54         C %esi = src
55         C %edi = dst
56         C %edx = loop counter
57         C %eax = i
58         C %ebx = j
59         C %cl  = si
60         C %ch  = sj
61
62         movl    24(%esp), %edx          C  length
63         movl    20(%esp), %ebp          C  ctx
64         movl    28(%esp), %edi          C  dst
65         movl    32(%esp), %esi          C  src
66
67         lea     (%edx, %edi), %edi
68         lea     (%edx, %esi), %esi
69         negl    %edx
70         jnc     .Lend
71         
72         movzbl  ARCFOUR_I (%ebp), %eax  C  i
73         movzbl  ARCFOUR_J (%ebp), %ebx  C  j
74
75         incb    %al
76         sarl    $1, %edx
77         jc      .Lloop_odd
78         
79         ALIGN(16)
80 .Lloop:
81         movb    (%ebp, %eax), %cl       C  si.
82         addb    %cl, %bl
83         movb    (%ebp, %ebx), %ch       C  sj
84         movb    %ch, (%ebp, %eax)       C  S[i] = sj
85         incl    %eax
86         movzbl  %al, %eax
87         movb    %cl, (%ebp, %ebx)       C  S[j] = si
88         addb    %ch, %cl
89         movzbl  %cl, %ecx               C  Clear, so it can be used
90                                         C  for indexing.
91         movb    (%ebp, %ecx), %cl
92         xorb    (%esi, %edx, 2), %cl
93         movb    %cl, (%edi, %edx, 2)
94
95         C FIXME: Could exchange cl and ch in the second half
96         C and try to interleave instructions better.
97 .Lloop_odd:
98         movb    (%ebp, %eax), %cl       C  si.
99         addb    %cl, %bl
100         movb    (%ebp, %ebx), %ch       C  sj
101         movb    %ch, (%ebp, %eax)       C  S[i] = sj
102         incl    %eax
103         movzbl  %al, %eax
104         movb    %cl, (%ebp, %ebx)       C  S[j] = si
105         addb    %ch, %cl
106         movzbl  %cl, %ecx               C  Clear, so it can be used
107                                         C  for indexing.
108         movb    (%ebp, %ecx), %cl
109         xorb    1(%esi, %edx, 2), %cl
110         incl    %edx
111         movb    %cl, -1(%edi, %edx, 2)
112
113         jnz     .Lloop
114
115 C .Lloop_done:
116         decb    %al
117         movb    %al, ARCFOUR_I (%ebp)           C  Store the new i and j.
118         movb    %bl, ARCFOUR_J (%ebp)
119 .Lend:
120         popl    %edi
121         popl    %esi
122         popl    %ebp
123         popl    %ebx
124         ret
125 EPILOGUE(nettle_arcfour_crypt)