2016-05-21 12:23:39 +00:00
|
|
|
#! /usr/bin/env perl
|
|
|
|
# Copyright 2010-2016 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
|
|
|
|
|
2010-09-10 14:50:17 +00:00
|
|
|
|
|
|
|
# ====================================================================
|
|
|
|
# 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/.
|
|
|
|
# ====================================================================
|
|
|
|
|
|
|
|
# September 2010.
|
2010-09-21 11:37:00 +00:00
|
|
|
#
|
|
|
|
# The module implements "4-bit" GCM GHASH function and underlying
|
|
|
|
# single multiplication operation in GF(2^128). "4-bit" means that it
|
|
|
|
# uses 256 bytes per-key table [+128 bytes shared table]. Performance
|
|
|
|
# was measured to be ~18 cycles per processed byte on z10, which is
|
|
|
|
# almost 40% better than gcc-generated code. It should be noted that
|
|
|
|
# 18 cycles is worse result than expected: loop is scheduled for 12
|
|
|
|
# and the result should be close to 12. In the lack of instruction-
|
|
|
|
# level profiling data it's impossible to tell why...
|
2010-09-10 14:50:17 +00:00
|
|
|
|
2010-11-29 20:52:43 +00:00
|
|
|
# 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
|
|
|
|
# 2.8x better than 32-bit code generated by gcc 4.3.
|
|
|
|
|
2011-03-04 13:09:16 +00:00
|
|
|
# March 2011.
|
|
|
|
#
|
|
|
|
# Support for hardware KIMD-GHASH is verified to produce correct
|
|
|
|
# result and therefore is engaged. On z196 it was measured to process
|
|
|
|
# 8KB buffer ~7 faster than software implementation. It's not as
|
|
|
|
# impressive for smaller buffer sizes and for smallest 16-bytes buffer
|
|
|
|
# it's actually almost 2 times slower. Which is the reason why
|
|
|
|
# KIMD-GHASH is not used in gcm_gmult_4bit.
|
|
|
|
|
2010-11-29 20:52:43 +00:00
|
|
|
$flavour = shift;
|
|
|
|
|
|
|
|
if ($flavour =~ /3[12]/) {
|
|
|
|
$SIZE_T=4;
|
|
|
|
$g="";
|
|
|
|
} else {
|
|
|
|
$SIZE_T=8;
|
|
|
|
$g="g";
|
|
|
|
}
|
|
|
|
|
2016-03-10 23:49:47 +00:00
|
|
|
while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
|
2010-09-10 14:50:17 +00:00
|
|
|
open STDOUT,">$output";
|
|
|
|
|
2011-03-04 13:09:16 +00:00
|
|
|
$softonly=0;
|
2010-09-21 11:37:00 +00:00
|
|
|
|
2010-09-10 14:50:17 +00:00
|
|
|
$Zhi="%r0";
|
|
|
|
$Zlo="%r1";
|
|
|
|
|
|
|
|
$Xi="%r2"; # argument block
|
|
|
|
$Htbl="%r3";
|
|
|
|
$inp="%r4";
|
|
|
|
$len="%r5";
|
|
|
|
|
|
|
|
$rem0="%r6"; # variables
|
|
|
|
$rem1="%r7";
|
|
|
|
$nlo="%r8";
|
|
|
|
$nhi="%r9";
|
|
|
|
$xi="%r10";
|
|
|
|
$cnt="%r11";
|
|
|
|
$tmp="%r12";
|
|
|
|
$x78="%r13";
|
|
|
|
$rem_4bit="%r14";
|
|
|
|
|
|
|
|
$sp="%r15";
|
|
|
|
|
|
|
|
$code.=<<___;
|
2017-10-02 09:24:02 +00:00
|
|
|
#include "s390x_arch.h"
|
|
|
|
|
2010-09-10 14:50:17 +00:00
|
|
|
.text
|
|
|
|
|
|
|
|
.globl gcm_gmult_4bit
|
|
|
|
.align 32
|
|
|
|
gcm_gmult_4bit:
|
2010-09-21 11:37:00 +00:00
|
|
|
___
|
2011-03-04 13:09:16 +00:00
|
|
|
$code.=<<___ if(!$softonly && 0); # hardware is slow for single block...
|
2010-09-21 11:37:00 +00:00
|
|
|
larl %r1,OPENSSL_s390xcap_P
|
|
|
|
lghi %r0,0
|
2017-10-02 09:24:02 +00:00
|
|
|
lg %r1,S390X_KIMD+8(%r1) # load second word of kimd capabilities
|
|
|
|
# vector
|
2010-09-21 11:37:00 +00:00
|
|
|
tmhh %r1,0x4000 # check for function 65
|
|
|
|
jz .Lsoft_gmult
|
|
|
|
stg %r0,16($sp) # arrange 16 bytes of zero input
|
|
|
|
stg %r0,24($sp)
|
2017-10-02 09:24:02 +00:00
|
|
|
lghi %r0,S390X_GHASH # function 65
|
2010-09-21 11:37:00 +00:00
|
|
|
la %r1,0($Xi) # H lies right after Xi in gcm128_context
|
|
|
|
la $inp,16($sp)
|
|
|
|
lghi $len,16
|
|
|
|
.long 0xb93e0004 # kimd %r0,$inp
|
|
|
|
brc 1,.-4 # pay attention to "partial completion"
|
|
|
|
br %r14
|
|
|
|
.align 32
|
|
|
|
.Lsoft_gmult:
|
|
|
|
___
|
|
|
|
$code.=<<___;
|
2010-11-29 20:52:43 +00:00
|
|
|
stm${g} %r6,%r14,6*$SIZE_T($sp)
|
2010-09-10 14:50:17 +00:00
|
|
|
|
|
|
|
aghi $Xi,-1
|
|
|
|
lghi $len,1
|
|
|
|
lghi $x78,`0xf<<3`
|
|
|
|
larl $rem_4bit,rem_4bit
|
|
|
|
|
|
|
|
lg $Zlo,8+1($Xi) # Xi
|
|
|
|
j .Lgmult_shortcut
|
|
|
|
.type gcm_gmult_4bit,\@function
|
|
|
|
.size gcm_gmult_4bit,(.-gcm_gmult_4bit)
|
|
|
|
|
|
|
|
.globl gcm_ghash_4bit
|
|
|
|
.align 32
|
|
|
|
gcm_ghash_4bit:
|
2010-09-21 11:37:00 +00:00
|
|
|
___
|
|
|
|
$code.=<<___ if(!$softonly);
|
|
|
|
larl %r1,OPENSSL_s390xcap_P
|
2017-10-02 09:24:02 +00:00
|
|
|
lg %r0,S390X_KIMD+8(%r1) # load second word of kimd capabilities
|
|
|
|
# vector
|
2017-01-30 11:50:54 +00:00
|
|
|
tmhh %r0,0x4000 # check for function 65
|
2010-09-21 11:37:00 +00:00
|
|
|
jz .Lsoft_ghash
|
2017-10-02 09:24:02 +00:00
|
|
|
lghi %r0,S390X_GHASH # function 65
|
2010-09-21 11:37:00 +00:00
|
|
|
la %r1,0($Xi) # H lies right after Xi in gcm128_context
|
|
|
|
.long 0xb93e0004 # kimd %r0,$inp
|
|
|
|
brc 1,.-4 # pay attention to "partial completion"
|
|
|
|
br %r14
|
|
|
|
.align 32
|
|
|
|
.Lsoft_ghash:
|
|
|
|
___
|
2012-04-12 06:44:34 +00:00
|
|
|
$code.=<<___ if ($flavour =~ /3[12]/);
|
2010-11-29 20:52:43 +00:00
|
|
|
llgfr $len,$len
|
|
|
|
___
|
2010-09-21 11:37:00 +00:00
|
|
|
$code.=<<___;
|
2010-11-29 20:52:43 +00:00
|
|
|
stm${g} %r6,%r14,6*$SIZE_T($sp)
|
2010-09-10 14:50:17 +00:00
|
|
|
|
|
|
|
aghi $Xi,-1
|
|
|
|
srlg $len,$len,4
|
|
|
|
lghi $x78,`0xf<<3`
|
|
|
|
larl $rem_4bit,rem_4bit
|
|
|
|
|
|
|
|
lg $Zlo,8+1($Xi) # Xi
|
|
|
|
lg $Zhi,0+1($Xi)
|
2010-09-21 11:37:00 +00:00
|
|
|
lghi $tmp,0
|
2010-09-10 14:50:17 +00:00
|
|
|
.Louter:
|
2016-10-10 16:01:24 +00:00
|
|
|
xg $Zhi,0($inp) # Xi ^= inp
|
2010-09-21 11:37:00 +00:00
|
|
|
xg $Zlo,8($inp)
|
|
|
|
xgr $Zhi,$tmp
|
2010-09-10 14:50:17 +00:00
|
|
|
stg $Zlo,8+1($Xi)
|
|
|
|
stg $Zhi,0+1($Xi)
|
|
|
|
|
|
|
|
.Lgmult_shortcut:
|
2010-09-21 11:37:00 +00:00
|
|
|
lghi $tmp,0xf0
|
|
|
|
sllg $nlo,$Zlo,4
|
|
|
|
srlg $xi,$Zlo,8 # extract second byte
|
|
|
|
ngr $nlo,$tmp
|
2010-09-10 14:50:17 +00:00
|
|
|
lgr $nhi,$Zlo
|
|
|
|
lghi $cnt,14
|
2010-09-21 11:37:00 +00:00
|
|
|
ngr $nhi,$tmp
|
2010-09-10 14:50:17 +00:00
|
|
|
|
|
|
|
lg $Zlo,8($nlo,$Htbl)
|
|
|
|
lg $Zhi,0($nlo,$Htbl)
|
|
|
|
|
|
|
|
sllg $nlo,$xi,4
|
|
|
|
sllg $rem0,$Zlo,3
|
2010-09-21 11:37:00 +00:00
|
|
|
ngr $nlo,$tmp
|
2010-09-10 14:50:17 +00:00
|
|
|
ngr $rem0,$x78
|
2010-09-21 11:37:00 +00:00
|
|
|
ngr $xi,$tmp
|
|
|
|
|
2010-09-10 14:50:17 +00:00
|
|
|
sllg $tmp,$Zhi,60
|
2010-09-21 11:37:00 +00:00
|
|
|
srlg $Zlo,$Zlo,4
|
2010-09-10 14:50:17 +00:00
|
|
|
srlg $Zhi,$Zhi,4
|
2010-09-21 11:37:00 +00:00
|
|
|
xg $Zlo,8($nhi,$Htbl)
|
2010-09-10 14:50:17 +00:00
|
|
|
xg $Zhi,0($nhi,$Htbl)
|
|
|
|
lgr $nhi,$xi
|
|
|
|
sllg $rem1,$Zlo,3
|
2010-09-21 11:37:00 +00:00
|
|
|
xgr $Zlo,$tmp
|
|
|
|
ngr $rem1,$x78
|
2014-02-01 23:09:17 +00:00
|
|
|
sllg $tmp,$Zhi,60
|
2010-09-21 11:37:00 +00:00
|
|
|
j .Lghash_inner
|
|
|
|
.align 16
|
2010-09-10 14:50:17 +00:00
|
|
|
.Lghash_inner:
|
|
|
|
srlg $Zlo,$Zlo,4
|
|
|
|
srlg $Zhi,$Zhi,4
|
2014-02-01 23:09:17 +00:00
|
|
|
xg $Zlo,8($nlo,$Htbl)
|
2010-09-10 14:50:17 +00:00
|
|
|
llgc $xi,0($cnt,$Xi)
|
|
|
|
xg $Zhi,0($nlo,$Htbl)
|
|
|
|
sllg $nlo,$xi,4
|
2010-09-21 11:37:00 +00:00
|
|
|
xg $Zhi,0($rem0,$rem_4bit)
|
2010-09-10 14:50:17 +00:00
|
|
|
nill $nlo,0xf0
|
2010-09-21 11:37:00 +00:00
|
|
|
sllg $rem0,$Zlo,3
|
|
|
|
xgr $Zlo,$tmp
|
2010-09-10 14:50:17 +00:00
|
|
|
ngr $rem0,$x78
|
2010-09-21 11:37:00 +00:00
|
|
|
nill $xi,0xf0
|
|
|
|
|
2010-09-10 14:50:17 +00:00
|
|
|
sllg $tmp,$Zhi,60
|
2010-09-21 11:37:00 +00:00
|
|
|
srlg $Zlo,$Zlo,4
|
2010-09-10 14:50:17 +00:00
|
|
|
srlg $Zhi,$Zhi,4
|
2010-09-21 11:37:00 +00:00
|
|
|
xg $Zlo,8($nhi,$Htbl)
|
2010-09-10 14:50:17 +00:00
|
|
|
xg $Zhi,0($nhi,$Htbl)
|
|
|
|
lgr $nhi,$xi
|
2010-09-21 11:37:00 +00:00
|
|
|
xg $Zhi,0($rem1,$rem_4bit)
|
|
|
|
sllg $rem1,$Zlo,3
|
|
|
|
xgr $Zlo,$tmp
|
|
|
|
ngr $rem1,$x78
|
2014-02-01 23:09:17 +00:00
|
|
|
sllg $tmp,$Zhi,60
|
2010-09-10 14:50:17 +00:00
|
|
|
brct $cnt,.Lghash_inner
|
|
|
|
|
|
|
|
srlg $Zlo,$Zlo,4
|
2010-09-21 11:37:00 +00:00
|
|
|
srlg $Zhi,$Zhi,4
|
2010-09-10 14:50:17 +00:00
|
|
|
xg $Zlo,8($nlo,$Htbl)
|
2010-09-21 11:37:00 +00:00
|
|
|
xg $Zhi,0($nlo,$Htbl)
|
|
|
|
sllg $xi,$Zlo,3
|
2010-09-10 14:50:17 +00:00
|
|
|
xg $Zhi,0($rem0,$rem_4bit)
|
|
|
|
xgr $Zlo,$tmp
|
2010-09-21 11:37:00 +00:00
|
|
|
ngr $xi,$x78
|
2010-09-10 14:50:17 +00:00
|
|
|
|
|
|
|
sllg $tmp,$Zhi,60
|
2010-09-21 11:37:00 +00:00
|
|
|
srlg $Zlo,$Zlo,4
|
2010-09-10 14:50:17 +00:00
|
|
|
srlg $Zhi,$Zhi,4
|
2010-09-21 11:37:00 +00:00
|
|
|
xg $Zlo,8($nhi,$Htbl)
|
2010-09-10 14:50:17 +00:00
|
|
|
xg $Zhi,0($nhi,$Htbl)
|
2010-09-21 11:37:00 +00:00
|
|
|
xgr $Zlo,$tmp
|
|
|
|
xg $Zhi,0($rem1,$rem_4bit)
|
2010-09-10 14:50:17 +00:00
|
|
|
|
2010-09-21 11:37:00 +00:00
|
|
|
lg $tmp,0($xi,$rem_4bit)
|
2010-09-10 14:50:17 +00:00
|
|
|
la $inp,16($inp)
|
2010-09-21 11:37:00 +00:00
|
|
|
sllg $tmp,$tmp,4 # correct last rem_4bit[rem]
|
2010-09-10 14:50:17 +00:00
|
|
|
brctg $len,.Louter
|
|
|
|
|
2010-09-21 11:37:00 +00:00
|
|
|
xgr $Zhi,$tmp
|
2010-09-10 14:50:17 +00:00
|
|
|
stg $Zlo,8+1($Xi)
|
|
|
|
stg $Zhi,0+1($Xi)
|
2010-11-29 20:52:43 +00:00
|
|
|
lm${g} %r6,%r14,6*$SIZE_T($sp)
|
2010-09-10 14:50:17 +00:00
|
|
|
br %r14
|
|
|
|
.type gcm_ghash_4bit,\@function
|
|
|
|
.size gcm_ghash_4bit,(.-gcm_ghash_4bit)
|
|
|
|
|
|
|
|
.align 64
|
|
|
|
rem_4bit:
|
2010-09-21 11:37:00 +00:00
|
|
|
.long `0x0000<<12`,0,`0x1C20<<12`,0,`0x3840<<12`,0,`0x2460<<12`,0
|
|
|
|
.long `0x7080<<12`,0,`0x6CA0<<12`,0,`0x48C0<<12`,0,`0x54E0<<12`,0
|
|
|
|
.long `0xE100<<12`,0,`0xFD20<<12`,0,`0xD940<<12`,0,`0xC560<<12`,0
|
|
|
|
.long `0x9180<<12`,0,`0x8DA0<<12`,0,`0xA9C0<<12`,0,`0xB5E0<<12`,0
|
2010-09-10 14:50:17 +00:00
|
|
|
.type rem_4bit,\@object
|
|
|
|
.size rem_4bit,(.-rem_4bit)
|
|
|
|
.string "GHASH for s390x, CRYPTOGAMS by <appro\@openssl.org>"
|
|
|
|
___
|
|
|
|
|
|
|
|
$code =~ s/\`([^\`]*)\`/eval $1/gem;
|
|
|
|
print $code;
|
|
|
|
close STDOUT;
|