hc
2024-03-22 a0752693d998599af469473b8dc239ef973a012f
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#! /usr/bin/env perl
# Copyright 2009-2020 The OpenSSL Project Authors. All Rights Reserved.
#
# Licensed under the OpenSSL license (the "License").  You may not use
# this file except in compliance with the License.  You can obtain a copy
# in the file LICENSE in the source distribution or at
# https://www.openssl.org/source/license.html
 
#
# ====================================================================
# Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
# project. The module is, however, dual licensed under OpenSSL and
# CRYPTOGAMS licenses depending on where you obtain it. For further
# details see http://www.openssl.org/~appro/cryptogams/.
# ====================================================================
#
# February 2009
#
# Performance is 2x of gcc 3.4.6 on z10. Coding "secret" is to
# "cluster" Address Generation Interlocks, so that one pipeline stall
# resolves several dependencies.
 
# November 2010.
#
# Adapt for -m31 build. If kernel supports what's called "highgprs"
# feature on Linux [see /proc/cpuinfo], it's possible to use 64-bit
# instructions and achieve "64-bit" performance even in 31-bit legacy
# application context. The feature is not specific to any particular
# processor, as long as it's "z-CPU". Latter implies that the code
# remains z/Architecture specific. On z990 it was measured to perform
# 50% better than code generated by gcc 4.3.
 
$flavour = shift;
 
if ($flavour =~ /3[12]/) {
   $SIZE_T=4;
   $g="";
} else {
   $SIZE_T=8;
   $g="g";
}
 
while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
open STDOUT,">$output";
 
$rp="%r14";
$sp="%r15";
$code=<<___;
.text
 
___
 
# void RC4(RC4_KEY *key,size_t len,const void *inp,void *out)
{
$acc="%r0";
$cnt="%r1";
$key="%r2";
$len="%r3";
$inp="%r4";
$out="%r5";
 
@XX=("%r6","%r7");
@TX=("%r8","%r9");
$YY="%r10";
$TY="%r11";
 
$code.=<<___;
.globl    RC4
.type    RC4,\@function
.align    64
RC4:
   stm${g}    %r6,%r11,6*$SIZE_T($sp)
___
$code.=<<___ if ($flavour =~ /3[12]/);
   llgfr    $len,$len
___
$code.=<<___;
   llgc    $XX[0],0($key)
   llgc    $YY,1($key)
   la    $XX[0],1($XX[0])
   nill    $XX[0],0xff
   srlg    $cnt,$len,3
   ltgr    $cnt,$cnt
   llgc    $TX[0],2($XX[0],$key)
   jz    .Lshort
   j    .Loop8
 
.align    64
.Loop8:
___
for ($i=0;$i<8;$i++) {
$code.=<<___;
   la    $YY,0($YY,$TX[0])    # $i
   nill    $YY,255
   la    $XX[1],1($XX[0])
   nill    $XX[1],255
___
$code.=<<___ if ($i==1);
   llgc    $acc,2($TY,$key)
___
$code.=<<___ if ($i>1);
   sllg    $acc,$acc,8
   ic    $acc,2($TY,$key)
___
$code.=<<___;
   llgc    $TY,2($YY,$key)
   stc    $TX[0],2($YY,$key)
   llgc    $TX[1],2($XX[1],$key)
   stc    $TY,2($XX[0],$key)
   cr    $XX[1],$YY
   jne    .Lcmov$i
   la    $TX[1],0($TX[0])
.Lcmov$i:
   la    $TY,0($TY,$TX[0])
   nill    $TY,255
___
push(@TX,shift(@TX)); push(@XX,shift(@XX));     # "rotate" registers
}
 
$code.=<<___;
   lg    $TX[1],0($inp)
   sllg    $acc,$acc,8
   la    $inp,8($inp)
   ic    $acc,2($TY,$key)
   xgr    $acc,$TX[1]
   stg    $acc,0($out)
   la    $out,8($out)
   brctg    $cnt,.Loop8
 
.Lshort:
   lghi    $acc,7
   ngr    $len,$acc
   jz    .Lexit
   j    .Loop1
 
.align    16
.Loop1:
   la    $YY,0($YY,$TX[0])
   nill    $YY,255
   llgc    $TY,2($YY,$key)
   stc    $TX[0],2($YY,$key)
   stc    $TY,2($XX[0],$key)
   ar    $TY,$TX[0]
   ahi    $XX[0],1
   nill    $TY,255
   nill    $XX[0],255
   llgc    $acc,0($inp)
   la    $inp,1($inp)
   llgc    $TY,2($TY,$key)
   llgc    $TX[0],2($XX[0],$key)
   xr    $acc,$TY
   stc    $acc,0($out)
   la    $out,1($out)
   brct    $len,.Loop1
 
.Lexit:
   ahi    $XX[0],-1
   stc    $XX[0],0($key)
   stc    $YY,1($key)
   lm${g}    %r6,%r11,6*$SIZE_T($sp)
   br    $rp
.size    RC4,.-RC4
.string    "RC4 for s390x, CRYPTOGAMS by <appro\@openssl.org>"
 
___
}
 
# void RC4_set_key(RC4_KEY *key,unsigned int len,const void *inp)
{
$cnt="%r0";
$idx="%r1";
$key="%r2";
$len="%r3";
$inp="%r4";
$acc="%r5";
$dat="%r6";
$ikey="%r7";
$iinp="%r8";
 
$code.=<<___;
.globl    RC4_set_key
.type    RC4_set_key,\@function
.align    64
RC4_set_key:
   stm${g}    %r6,%r8,6*$SIZE_T($sp)
   lhi    $cnt,256
   la    $idx,0
   sth    $idx,0($key)
.align    4
.L1stloop:
   stc    $idx,2($idx,$key)
   la    $idx,1($idx)
   brct    $cnt,.L1stloop
 
   lghi    $ikey,-256
   lr    $cnt,$len
   la    $iinp,0
   la    $idx,0
.align    16
.L2ndloop:
   llgc    $acc,2+256($ikey,$key)
   llgc    $dat,0($iinp,$inp)
   la    $idx,0($idx,$acc)
   la    $ikey,1($ikey)
   la    $idx,0($idx,$dat)
   nill    $idx,255
   la    $iinp,1($iinp)
   tml    $ikey,255
   llgc    $dat,2($idx,$key)
   stc    $dat,2+256-1($ikey,$key)
   stc    $acc,2($idx,$key)
   jz    .Ldone
   brct    $cnt,.L2ndloop
   lr    $cnt,$len
   la    $iinp,0
   j    .L2ndloop
.Ldone:
   lm${g}    %r6,%r8,6*$SIZE_T($sp)
   br    $rp
.size    RC4_set_key,.-RC4_set_key
 
___
}
 
# const char *RC4_options()
$code.=<<___;
.globl    RC4_options
.type    RC4_options,\@function
.align    16
RC4_options:
   larl    %r2,.Loptions
   br    %r14
.size    RC4_options,.-RC4_options
.section    .rodata
.Loptions:
.align    8
.string    "rc4(8x,char)"
___
 
print $code;
close STDOUT or die "error closing STDOUT: $!";    # force flush