1e1051a39Sopenharmony_ci#! /usr/bin/env perl 2e1051a39Sopenharmony_ci# Copyright 2010-2020 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci# 4e1051a39Sopenharmony_ci# Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci# this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci# in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci# https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci 9e1051a39Sopenharmony_ci# 10e1051a39Sopenharmony_ci# ==================================================================== 11e1051a39Sopenharmony_ci# Written by Andy Polyakov <appro@openssl.org> for the OpenSSL 12e1051a39Sopenharmony_ci# project. The module is, however, dual licensed under OpenSSL and 13e1051a39Sopenharmony_ci# CRYPTOGAMS licenses depending on where you obtain it. For further 14e1051a39Sopenharmony_ci# details see http://www.openssl.org/~appro/cryptogams/. 15e1051a39Sopenharmony_ci# ==================================================================== 16e1051a39Sopenharmony_ci# 17e1051a39Sopenharmony_ci# March, May, June 2010 18e1051a39Sopenharmony_ci# 19e1051a39Sopenharmony_ci# The module implements "4-bit" GCM GHASH function and underlying 20e1051a39Sopenharmony_ci# single multiplication operation in GF(2^128). "4-bit" means that it 21e1051a39Sopenharmony_ci# uses 256 bytes per-key table [+64/128 bytes fixed table]. It has two 22e1051a39Sopenharmony_ci# code paths: vanilla x86 and vanilla SSE. Former will be executed on 23e1051a39Sopenharmony_ci# 486 and Pentium, latter on all others. SSE GHASH features so called 24e1051a39Sopenharmony_ci# "528B" variant of "4-bit" method utilizing additional 256+16 bytes 25e1051a39Sopenharmony_ci# of per-key storage [+512 bytes shared table]. Performance results 26e1051a39Sopenharmony_ci# are for streamed GHASH subroutine and are expressed in cycles per 27e1051a39Sopenharmony_ci# processed byte, less is better: 28e1051a39Sopenharmony_ci# 29e1051a39Sopenharmony_ci# gcc 2.95.3(*) SSE assembler x86 assembler 30e1051a39Sopenharmony_ci# 31e1051a39Sopenharmony_ci# Pentium 105/111(**) - 50 32e1051a39Sopenharmony_ci# PIII 68 /75 12.2 24 33e1051a39Sopenharmony_ci# P4 125/125 17.8 84(***) 34e1051a39Sopenharmony_ci# Opteron 66 /70 10.1 30 35e1051a39Sopenharmony_ci# Core2 54 /67 8.4 18 36e1051a39Sopenharmony_ci# Atom 105/105 16.8 53 37e1051a39Sopenharmony_ci# VIA Nano 69 /71 13.0 27 38e1051a39Sopenharmony_ci# 39e1051a39Sopenharmony_ci# (*) gcc 3.4.x was observed to generate few percent slower code, 40e1051a39Sopenharmony_ci# which is one of reasons why 2.95.3 results were chosen, 41e1051a39Sopenharmony_ci# another reason is lack of 3.4.x results for older CPUs; 42e1051a39Sopenharmony_ci# comparison with SSE results is not completely fair, because C 43e1051a39Sopenharmony_ci# results are for vanilla "256B" implementation, while 44e1051a39Sopenharmony_ci# assembler results are for "528B";-) 45e1051a39Sopenharmony_ci# (**) second number is result for code compiled with -fPIC flag, 46e1051a39Sopenharmony_ci# which is actually more relevant, because assembler code is 47e1051a39Sopenharmony_ci# position-independent; 48e1051a39Sopenharmony_ci# (***) see comment in non-MMX routine for further details; 49e1051a39Sopenharmony_ci# 50e1051a39Sopenharmony_ci# To summarize, it's >2-5 times faster than gcc-generated code. To 51e1051a39Sopenharmony_ci# anchor it to something else SHA1 assembler processes one byte in 52e1051a39Sopenharmony_ci# ~7 cycles on contemporary x86 cores. As for choice of MMX/SSE 53e1051a39Sopenharmony_ci# in particular, see comment at the end of the file... 54e1051a39Sopenharmony_ci 55e1051a39Sopenharmony_ci# May 2010 56e1051a39Sopenharmony_ci# 57e1051a39Sopenharmony_ci# Add PCLMULQDQ version performing at 2.10 cycles per processed byte. 58e1051a39Sopenharmony_ci# The question is how close is it to theoretical limit? The pclmulqdq 59e1051a39Sopenharmony_ci# instruction latency appears to be 14 cycles and there can't be more 60e1051a39Sopenharmony_ci# than 2 of them executing at any given time. This means that single 61e1051a39Sopenharmony_ci# Karatsuba multiplication would take 28 cycles *plus* few cycles for 62e1051a39Sopenharmony_ci# pre- and post-processing. Then multiplication has to be followed by 63e1051a39Sopenharmony_ci# modulo-reduction. Given that aggregated reduction method [see 64e1051a39Sopenharmony_ci# "Carry-less Multiplication and Its Usage for Computing the GCM Mode" 65e1051a39Sopenharmony_ci# white paper by Intel] allows you to perform reduction only once in 66e1051a39Sopenharmony_ci# a while we can assume that asymptotic performance can be estimated 67e1051a39Sopenharmony_ci# as (28+Tmod/Naggr)/16, where Tmod is time to perform reduction 68e1051a39Sopenharmony_ci# and Naggr is the aggregation factor. 69e1051a39Sopenharmony_ci# 70e1051a39Sopenharmony_ci# Before we proceed to this implementation let's have closer look at 71e1051a39Sopenharmony_ci# the best-performing code suggested by Intel in their white paper. 72e1051a39Sopenharmony_ci# By tracing inter-register dependencies Tmod is estimated as ~19 73e1051a39Sopenharmony_ci# cycles and Naggr chosen by Intel is 4, resulting in 2.05 cycles per 74e1051a39Sopenharmony_ci# processed byte. As implied, this is quite optimistic estimate, 75e1051a39Sopenharmony_ci# because it does not account for Karatsuba pre- and post-processing, 76e1051a39Sopenharmony_ci# which for a single multiplication is ~5 cycles. Unfortunately Intel 77e1051a39Sopenharmony_ci# does not provide performance data for GHASH alone. But benchmarking 78e1051a39Sopenharmony_ci# AES_GCM_encrypt ripped out of Fig. 15 of the white paper with aadt 79e1051a39Sopenharmony_ci# alone resulted in 2.46 cycles per byte of out 16KB buffer. Note that 80e1051a39Sopenharmony_ci# the result accounts even for pre-computing of degrees of the hash 81e1051a39Sopenharmony_ci# key H, but its portion is negligible at 16KB buffer size. 82e1051a39Sopenharmony_ci# 83e1051a39Sopenharmony_ci# Moving on to the implementation in question. Tmod is estimated as 84e1051a39Sopenharmony_ci# ~13 cycles and Naggr is 2, giving asymptotic performance of ... 85e1051a39Sopenharmony_ci# 2.16. How is it possible that measured performance is better than 86e1051a39Sopenharmony_ci# optimistic theoretical estimate? There is one thing Intel failed 87e1051a39Sopenharmony_ci# to recognize. By serializing GHASH with CTR in same subroutine 88e1051a39Sopenharmony_ci# former's performance is really limited to above (Tmul + Tmod/Naggr) 89e1051a39Sopenharmony_ci# equation. But if GHASH procedure is detached, the modulo-reduction 90e1051a39Sopenharmony_ci# can be interleaved with Naggr-1 multiplications at instruction level 91e1051a39Sopenharmony_ci# and under ideal conditions even disappear from the equation. So that 92e1051a39Sopenharmony_ci# optimistic theoretical estimate for this implementation is ... 93e1051a39Sopenharmony_ci# 28/16=1.75, and not 2.16. Well, it's probably way too optimistic, 94e1051a39Sopenharmony_ci# at least for such small Naggr. I'd argue that (28+Tproc/Naggr), 95e1051a39Sopenharmony_ci# where Tproc is time required for Karatsuba pre- and post-processing, 96e1051a39Sopenharmony_ci# is more realistic estimate. In this case it gives ... 1.91 cycles. 97e1051a39Sopenharmony_ci# Or in other words, depending on how well we can interleave reduction 98e1051a39Sopenharmony_ci# and one of the two multiplications the performance should be between 99e1051a39Sopenharmony_ci# 1.91 and 2.16. As already mentioned, this implementation processes 100e1051a39Sopenharmony_ci# one byte out of 8KB buffer in 2.10 cycles, while x86_64 counterpart 101e1051a39Sopenharmony_ci# - in 2.02. x86_64 performance is better, because larger register 102e1051a39Sopenharmony_ci# bank allows to interleave reduction and multiplication better. 103e1051a39Sopenharmony_ci# 104e1051a39Sopenharmony_ci# Does it make sense to increase Naggr? To start with it's virtually 105e1051a39Sopenharmony_ci# impossible in 32-bit mode, because of limited register bank 106e1051a39Sopenharmony_ci# capacity. Otherwise improvement has to be weighed against slower 107e1051a39Sopenharmony_ci# setup, as well as code size and complexity increase. As even 108e1051a39Sopenharmony_ci# optimistic estimate doesn't promise 30% performance improvement, 109e1051a39Sopenharmony_ci# there are currently no plans to increase Naggr. 110e1051a39Sopenharmony_ci# 111e1051a39Sopenharmony_ci# Special thanks to David Woodhouse for providing access to a 112e1051a39Sopenharmony_ci# Westmere-based system on behalf of Intel Open Source Technology Centre. 113e1051a39Sopenharmony_ci 114e1051a39Sopenharmony_ci# January 2010 115e1051a39Sopenharmony_ci# 116e1051a39Sopenharmony_ci# Tweaked to optimize transitions between integer and FP operations 117e1051a39Sopenharmony_ci# on same XMM register, PCLMULQDQ subroutine was measured to process 118e1051a39Sopenharmony_ci# one byte in 2.07 cycles on Sandy Bridge, and in 2.12 - on Westmere. 119e1051a39Sopenharmony_ci# The minor regression on Westmere is outweighed by ~15% improvement 120e1051a39Sopenharmony_ci# on Sandy Bridge. Strangely enough attempt to modify 64-bit code in 121e1051a39Sopenharmony_ci# similar manner resulted in almost 20% degradation on Sandy Bridge, 122e1051a39Sopenharmony_ci# where original 64-bit code processes one byte in 1.95 cycles. 123e1051a39Sopenharmony_ci 124e1051a39Sopenharmony_ci##################################################################### 125e1051a39Sopenharmony_ci# For reference, AMD Bulldozer processes one byte in 1.98 cycles in 126e1051a39Sopenharmony_ci# 32-bit mode and 1.89 in 64-bit. 127e1051a39Sopenharmony_ci 128e1051a39Sopenharmony_ci# February 2013 129e1051a39Sopenharmony_ci# 130e1051a39Sopenharmony_ci# Overhaul: aggregate Karatsuba post-processing, improve ILP in 131e1051a39Sopenharmony_ci# reduction_alg9. Resulting performance is 1.96 cycles per byte on 132e1051a39Sopenharmony_ci# Westmere, 1.95 - on Sandy/Ivy Bridge, 1.76 - on Bulldozer. 133e1051a39Sopenharmony_ci 134e1051a39Sopenharmony_ci$0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1; 135e1051a39Sopenharmony_cipush(@INC,"${dir}","${dir}../../perlasm"); 136e1051a39Sopenharmony_cirequire "x86asm.pl"; 137e1051a39Sopenharmony_ci 138e1051a39Sopenharmony_ci$output=pop and open STDOUT,">$output"; 139e1051a39Sopenharmony_ci 140e1051a39Sopenharmony_ci&asm_init($ARGV[0],$x86only = $ARGV[$#ARGV] eq "386"); 141e1051a39Sopenharmony_ci 142e1051a39Sopenharmony_ci$sse2=0; 143e1051a39Sopenharmony_cifor (@ARGV) { $sse2=1 if (/-DOPENSSL_IA32_SSE2/); } 144e1051a39Sopenharmony_ci 145e1051a39Sopenharmony_ci($Zhh,$Zhl,$Zlh,$Zll) = ("ebp","edx","ecx","ebx"); 146e1051a39Sopenharmony_ci$inp = "edi"; 147e1051a39Sopenharmony_ci$Htbl = "esi"; 148e1051a39Sopenharmony_ci 149e1051a39Sopenharmony_ci$unroll = 0; # Affects x86 loop. Folded loop performs ~7% worse 150e1051a39Sopenharmony_ci # than unrolled, which has to be weighted against 151e1051a39Sopenharmony_ci # 2.5x x86-specific code size reduction. 152e1051a39Sopenharmony_ci 153e1051a39Sopenharmony_cisub x86_loop { 154e1051a39Sopenharmony_ci my $off = shift; 155e1051a39Sopenharmony_ci my $rem = "eax"; 156e1051a39Sopenharmony_ci 157e1051a39Sopenharmony_ci &mov ($Zhh,&DWP(4,$Htbl,$Zll)); 158e1051a39Sopenharmony_ci &mov ($Zhl,&DWP(0,$Htbl,$Zll)); 159e1051a39Sopenharmony_ci &mov ($Zlh,&DWP(12,$Htbl,$Zll)); 160e1051a39Sopenharmony_ci &mov ($Zll,&DWP(8,$Htbl,$Zll)); 161e1051a39Sopenharmony_ci &xor ($rem,$rem); # avoid partial register stalls on PIII 162e1051a39Sopenharmony_ci 163e1051a39Sopenharmony_ci # shrd practically kills P4, 2.5x deterioration, but P4 has 164e1051a39Sopenharmony_ci # MMX code-path to execute. shrd runs tad faster [than twice 165e1051a39Sopenharmony_ci # the shifts, move's and or's] on pre-MMX Pentium (as well as 166e1051a39Sopenharmony_ci # PIII and Core2), *but* minimizes code size, spares register 167e1051a39Sopenharmony_ci # and thus allows to fold the loop... 168e1051a39Sopenharmony_ci if (!$unroll) { 169e1051a39Sopenharmony_ci my $cnt = $inp; 170e1051a39Sopenharmony_ci &mov ($cnt,15); 171e1051a39Sopenharmony_ci &jmp (&label("x86_loop")); 172e1051a39Sopenharmony_ci &set_label("x86_loop",16); 173e1051a39Sopenharmony_ci for($i=1;$i<=2;$i++) { 174e1051a39Sopenharmony_ci &mov (&LB($rem),&LB($Zll)); 175e1051a39Sopenharmony_ci &shrd ($Zll,$Zlh,4); 176e1051a39Sopenharmony_ci &and (&LB($rem),0xf); 177e1051a39Sopenharmony_ci &shrd ($Zlh,$Zhl,4); 178e1051a39Sopenharmony_ci &shrd ($Zhl,$Zhh,4); 179e1051a39Sopenharmony_ci &shr ($Zhh,4); 180e1051a39Sopenharmony_ci &xor ($Zhh,&DWP($off+16,"esp",$rem,4)); 181e1051a39Sopenharmony_ci 182e1051a39Sopenharmony_ci &mov (&LB($rem),&BP($off,"esp",$cnt)); 183e1051a39Sopenharmony_ci if ($i&1) { 184e1051a39Sopenharmony_ci &and (&LB($rem),0xf0); 185e1051a39Sopenharmony_ci } else { 186e1051a39Sopenharmony_ci &shl (&LB($rem),4); 187e1051a39Sopenharmony_ci } 188e1051a39Sopenharmony_ci 189e1051a39Sopenharmony_ci &xor ($Zll,&DWP(8,$Htbl,$rem)); 190e1051a39Sopenharmony_ci &xor ($Zlh,&DWP(12,$Htbl,$rem)); 191e1051a39Sopenharmony_ci &xor ($Zhl,&DWP(0,$Htbl,$rem)); 192e1051a39Sopenharmony_ci &xor ($Zhh,&DWP(4,$Htbl,$rem)); 193e1051a39Sopenharmony_ci 194e1051a39Sopenharmony_ci if ($i&1) { 195e1051a39Sopenharmony_ci &dec ($cnt); 196e1051a39Sopenharmony_ci &js (&label("x86_break")); 197e1051a39Sopenharmony_ci } else { 198e1051a39Sopenharmony_ci &jmp (&label("x86_loop")); 199e1051a39Sopenharmony_ci } 200e1051a39Sopenharmony_ci } 201e1051a39Sopenharmony_ci &set_label("x86_break",16); 202e1051a39Sopenharmony_ci } else { 203e1051a39Sopenharmony_ci for($i=1;$i<32;$i++) { 204e1051a39Sopenharmony_ci &comment($i); 205e1051a39Sopenharmony_ci &mov (&LB($rem),&LB($Zll)); 206e1051a39Sopenharmony_ci &shrd ($Zll,$Zlh,4); 207e1051a39Sopenharmony_ci &and (&LB($rem),0xf); 208e1051a39Sopenharmony_ci &shrd ($Zlh,$Zhl,4); 209e1051a39Sopenharmony_ci &shrd ($Zhl,$Zhh,4); 210e1051a39Sopenharmony_ci &shr ($Zhh,4); 211e1051a39Sopenharmony_ci &xor ($Zhh,&DWP($off+16,"esp",$rem,4)); 212e1051a39Sopenharmony_ci 213e1051a39Sopenharmony_ci if ($i&1) { 214e1051a39Sopenharmony_ci &mov (&LB($rem),&BP($off+15-($i>>1),"esp")); 215e1051a39Sopenharmony_ci &and (&LB($rem),0xf0); 216e1051a39Sopenharmony_ci } else { 217e1051a39Sopenharmony_ci &mov (&LB($rem),&BP($off+15-($i>>1),"esp")); 218e1051a39Sopenharmony_ci &shl (&LB($rem),4); 219e1051a39Sopenharmony_ci } 220e1051a39Sopenharmony_ci 221e1051a39Sopenharmony_ci &xor ($Zll,&DWP(8,$Htbl,$rem)); 222e1051a39Sopenharmony_ci &xor ($Zlh,&DWP(12,$Htbl,$rem)); 223e1051a39Sopenharmony_ci &xor ($Zhl,&DWP(0,$Htbl,$rem)); 224e1051a39Sopenharmony_ci &xor ($Zhh,&DWP(4,$Htbl,$rem)); 225e1051a39Sopenharmony_ci } 226e1051a39Sopenharmony_ci } 227e1051a39Sopenharmony_ci &bswap ($Zll); 228e1051a39Sopenharmony_ci &bswap ($Zlh); 229e1051a39Sopenharmony_ci &bswap ($Zhl); 230e1051a39Sopenharmony_ci if (!$x86only) { 231e1051a39Sopenharmony_ci &bswap ($Zhh); 232e1051a39Sopenharmony_ci } else { 233e1051a39Sopenharmony_ci &mov ("eax",$Zhh); 234e1051a39Sopenharmony_ci &bswap ("eax"); 235e1051a39Sopenharmony_ci &mov ($Zhh,"eax"); 236e1051a39Sopenharmony_ci } 237e1051a39Sopenharmony_ci} 238e1051a39Sopenharmony_ci 239e1051a39Sopenharmony_ciif ($unroll) { 240e1051a39Sopenharmony_ci &function_begin_B("_x86_gmult_4bit_inner"); 241e1051a39Sopenharmony_ci &x86_loop(4); 242e1051a39Sopenharmony_ci &ret (); 243e1051a39Sopenharmony_ci &function_end_B("_x86_gmult_4bit_inner"); 244e1051a39Sopenharmony_ci} 245e1051a39Sopenharmony_ci 246e1051a39Sopenharmony_cisub deposit_rem_4bit { 247e1051a39Sopenharmony_ci my $bias = shift; 248e1051a39Sopenharmony_ci 249e1051a39Sopenharmony_ci &mov (&DWP($bias+0, "esp"),0x0000<<16); 250e1051a39Sopenharmony_ci &mov (&DWP($bias+4, "esp"),0x1C20<<16); 251e1051a39Sopenharmony_ci &mov (&DWP($bias+8, "esp"),0x3840<<16); 252e1051a39Sopenharmony_ci &mov (&DWP($bias+12,"esp"),0x2460<<16); 253e1051a39Sopenharmony_ci &mov (&DWP($bias+16,"esp"),0x7080<<16); 254e1051a39Sopenharmony_ci &mov (&DWP($bias+20,"esp"),0x6CA0<<16); 255e1051a39Sopenharmony_ci &mov (&DWP($bias+24,"esp"),0x48C0<<16); 256e1051a39Sopenharmony_ci &mov (&DWP($bias+28,"esp"),0x54E0<<16); 257e1051a39Sopenharmony_ci &mov (&DWP($bias+32,"esp"),0xE100<<16); 258e1051a39Sopenharmony_ci &mov (&DWP($bias+36,"esp"),0xFD20<<16); 259e1051a39Sopenharmony_ci &mov (&DWP($bias+40,"esp"),0xD940<<16); 260e1051a39Sopenharmony_ci &mov (&DWP($bias+44,"esp"),0xC560<<16); 261e1051a39Sopenharmony_ci &mov (&DWP($bias+48,"esp"),0x9180<<16); 262e1051a39Sopenharmony_ci &mov (&DWP($bias+52,"esp"),0x8DA0<<16); 263e1051a39Sopenharmony_ci &mov (&DWP($bias+56,"esp"),0xA9C0<<16); 264e1051a39Sopenharmony_ci &mov (&DWP($bias+60,"esp"),0xB5E0<<16); 265e1051a39Sopenharmony_ci} 266e1051a39Sopenharmony_ci 267e1051a39Sopenharmony_ci$suffix = $x86only ? "" : "_x86"; 268e1051a39Sopenharmony_ci 269e1051a39Sopenharmony_ci&function_begin("gcm_gmult_4bit".$suffix); 270e1051a39Sopenharmony_ci &stack_push(16+4+1); # +1 for stack alignment 271e1051a39Sopenharmony_ci &mov ($inp,&wparam(0)); # load Xi 272e1051a39Sopenharmony_ci &mov ($Htbl,&wparam(1)); # load Htable 273e1051a39Sopenharmony_ci 274e1051a39Sopenharmony_ci &mov ($Zhh,&DWP(0,$inp)); # load Xi[16] 275e1051a39Sopenharmony_ci &mov ($Zhl,&DWP(4,$inp)); 276e1051a39Sopenharmony_ci &mov ($Zlh,&DWP(8,$inp)); 277e1051a39Sopenharmony_ci &mov ($Zll,&DWP(12,$inp)); 278e1051a39Sopenharmony_ci 279e1051a39Sopenharmony_ci &deposit_rem_4bit(16); 280e1051a39Sopenharmony_ci 281e1051a39Sopenharmony_ci &mov (&DWP(0,"esp"),$Zhh); # copy Xi[16] on stack 282e1051a39Sopenharmony_ci &mov (&DWP(4,"esp"),$Zhl); 283e1051a39Sopenharmony_ci &mov (&DWP(8,"esp"),$Zlh); 284e1051a39Sopenharmony_ci &mov (&DWP(12,"esp"),$Zll); 285e1051a39Sopenharmony_ci &shr ($Zll,20); 286e1051a39Sopenharmony_ci &and ($Zll,0xf0); 287e1051a39Sopenharmony_ci 288e1051a39Sopenharmony_ci if ($unroll) { 289e1051a39Sopenharmony_ci &call ("_x86_gmult_4bit_inner"); 290e1051a39Sopenharmony_ci } else { 291e1051a39Sopenharmony_ci &x86_loop(0); 292e1051a39Sopenharmony_ci &mov ($inp,&wparam(0)); 293e1051a39Sopenharmony_ci } 294e1051a39Sopenharmony_ci 295e1051a39Sopenharmony_ci &mov (&DWP(12,$inp),$Zll); 296e1051a39Sopenharmony_ci &mov (&DWP(8,$inp),$Zlh); 297e1051a39Sopenharmony_ci &mov (&DWP(4,$inp),$Zhl); 298e1051a39Sopenharmony_ci &mov (&DWP(0,$inp),$Zhh); 299e1051a39Sopenharmony_ci &stack_pop(16+4+1); 300e1051a39Sopenharmony_ci&function_end("gcm_gmult_4bit".$suffix); 301e1051a39Sopenharmony_ci 302e1051a39Sopenharmony_ci&function_begin("gcm_ghash_4bit".$suffix); 303e1051a39Sopenharmony_ci &stack_push(16+4+1); # +1 for 64-bit alignment 304e1051a39Sopenharmony_ci &mov ($Zll,&wparam(0)); # load Xi 305e1051a39Sopenharmony_ci &mov ($Htbl,&wparam(1)); # load Htable 306e1051a39Sopenharmony_ci &mov ($inp,&wparam(2)); # load in 307e1051a39Sopenharmony_ci &mov ("ecx",&wparam(3)); # load len 308e1051a39Sopenharmony_ci &add ("ecx",$inp); 309e1051a39Sopenharmony_ci &mov (&wparam(3),"ecx"); 310e1051a39Sopenharmony_ci 311e1051a39Sopenharmony_ci &mov ($Zhh,&DWP(0,$Zll)); # load Xi[16] 312e1051a39Sopenharmony_ci &mov ($Zhl,&DWP(4,$Zll)); 313e1051a39Sopenharmony_ci &mov ($Zlh,&DWP(8,$Zll)); 314e1051a39Sopenharmony_ci &mov ($Zll,&DWP(12,$Zll)); 315e1051a39Sopenharmony_ci 316e1051a39Sopenharmony_ci &deposit_rem_4bit(16); 317e1051a39Sopenharmony_ci 318e1051a39Sopenharmony_ci &set_label("x86_outer_loop",16); 319e1051a39Sopenharmony_ci &xor ($Zll,&DWP(12,$inp)); # xor with input 320e1051a39Sopenharmony_ci &xor ($Zlh,&DWP(8,$inp)); 321e1051a39Sopenharmony_ci &xor ($Zhl,&DWP(4,$inp)); 322e1051a39Sopenharmony_ci &xor ($Zhh,&DWP(0,$inp)); 323e1051a39Sopenharmony_ci &mov (&DWP(12,"esp"),$Zll); # dump it on stack 324e1051a39Sopenharmony_ci &mov (&DWP(8,"esp"),$Zlh); 325e1051a39Sopenharmony_ci &mov (&DWP(4,"esp"),$Zhl); 326e1051a39Sopenharmony_ci &mov (&DWP(0,"esp"),$Zhh); 327e1051a39Sopenharmony_ci 328e1051a39Sopenharmony_ci &shr ($Zll,20); 329e1051a39Sopenharmony_ci &and ($Zll,0xf0); 330e1051a39Sopenharmony_ci 331e1051a39Sopenharmony_ci if ($unroll) { 332e1051a39Sopenharmony_ci &call ("_x86_gmult_4bit_inner"); 333e1051a39Sopenharmony_ci } else { 334e1051a39Sopenharmony_ci &x86_loop(0); 335e1051a39Sopenharmony_ci &mov ($inp,&wparam(2)); 336e1051a39Sopenharmony_ci } 337e1051a39Sopenharmony_ci &lea ($inp,&DWP(16,$inp)); 338e1051a39Sopenharmony_ci &cmp ($inp,&wparam(3)); 339e1051a39Sopenharmony_ci &mov (&wparam(2),$inp) if (!$unroll); 340e1051a39Sopenharmony_ci &jb (&label("x86_outer_loop")); 341e1051a39Sopenharmony_ci 342e1051a39Sopenharmony_ci &mov ($inp,&wparam(0)); # load Xi 343e1051a39Sopenharmony_ci &mov (&DWP(12,$inp),$Zll); 344e1051a39Sopenharmony_ci &mov (&DWP(8,$inp),$Zlh); 345e1051a39Sopenharmony_ci &mov (&DWP(4,$inp),$Zhl); 346e1051a39Sopenharmony_ci &mov (&DWP(0,$inp),$Zhh); 347e1051a39Sopenharmony_ci &stack_pop(16+4+1); 348e1051a39Sopenharmony_ci&function_end("gcm_ghash_4bit".$suffix); 349e1051a39Sopenharmony_ci 350e1051a39Sopenharmony_ciif (!$x86only) {{{ 351e1051a39Sopenharmony_ci 352e1051a39Sopenharmony_ci&static_label("rem_4bit"); 353e1051a39Sopenharmony_ci 354e1051a39Sopenharmony_ciif (!$sse2) {{ # pure-MMX "May" version... 355e1051a39Sopenharmony_ci 356e1051a39Sopenharmony_ci$S=12; # shift factor for rem_4bit 357e1051a39Sopenharmony_ci 358e1051a39Sopenharmony_ci&function_begin_B("_mmx_gmult_4bit_inner"); 359e1051a39Sopenharmony_ci# MMX version performs 3.5 times better on P4 (see comment in non-MMX 360e1051a39Sopenharmony_ci# routine for further details), 100% better on Opteron, ~70% better 361e1051a39Sopenharmony_ci# on Core2 and PIII... In other words effort is considered to be well 362e1051a39Sopenharmony_ci# spent... Since initial release the loop was unrolled in order to 363e1051a39Sopenharmony_ci# "liberate" register previously used as loop counter. Instead it's 364e1051a39Sopenharmony_ci# used to optimize critical path in 'Z.hi ^= rem_4bit[Z.lo&0xf]'. 365e1051a39Sopenharmony_ci# The path involves move of Z.lo from MMX to integer register, 366e1051a39Sopenharmony_ci# effective address calculation and finally merge of value to Z.hi. 367e1051a39Sopenharmony_ci# Reference to rem_4bit is scheduled so late that I had to >>4 368e1051a39Sopenharmony_ci# rem_4bit elements. This resulted in 20-45% procent improvement 369e1051a39Sopenharmony_ci# on contemporary µ-archs. 370e1051a39Sopenharmony_ci{ 371e1051a39Sopenharmony_ci my $cnt; 372e1051a39Sopenharmony_ci my $rem_4bit = "eax"; 373e1051a39Sopenharmony_ci my @rem = ($Zhh,$Zll); 374e1051a39Sopenharmony_ci my $nhi = $Zhl; 375e1051a39Sopenharmony_ci my $nlo = $Zlh; 376e1051a39Sopenharmony_ci 377e1051a39Sopenharmony_ci my ($Zlo,$Zhi) = ("mm0","mm1"); 378e1051a39Sopenharmony_ci my $tmp = "mm2"; 379e1051a39Sopenharmony_ci 380e1051a39Sopenharmony_ci &xor ($nlo,$nlo); # avoid partial register stalls on PIII 381e1051a39Sopenharmony_ci &mov ($nhi,$Zll); 382e1051a39Sopenharmony_ci &mov (&LB($nlo),&LB($nhi)); 383e1051a39Sopenharmony_ci &shl (&LB($nlo),4); 384e1051a39Sopenharmony_ci &and ($nhi,0xf0); 385e1051a39Sopenharmony_ci &movq ($Zlo,&QWP(8,$Htbl,$nlo)); 386e1051a39Sopenharmony_ci &movq ($Zhi,&QWP(0,$Htbl,$nlo)); 387e1051a39Sopenharmony_ci &movd ($rem[0],$Zlo); 388e1051a39Sopenharmony_ci 389e1051a39Sopenharmony_ci for ($cnt=28;$cnt>=-2;$cnt--) { 390e1051a39Sopenharmony_ci my $odd = $cnt&1; 391e1051a39Sopenharmony_ci my $nix = $odd ? $nlo : $nhi; 392e1051a39Sopenharmony_ci 393e1051a39Sopenharmony_ci &shl (&LB($nlo),4) if ($odd); 394e1051a39Sopenharmony_ci &psrlq ($Zlo,4); 395e1051a39Sopenharmony_ci &movq ($tmp,$Zhi); 396e1051a39Sopenharmony_ci &psrlq ($Zhi,4); 397e1051a39Sopenharmony_ci &pxor ($Zlo,&QWP(8,$Htbl,$nix)); 398e1051a39Sopenharmony_ci &mov (&LB($nlo),&BP($cnt/2,$inp)) if (!$odd && $cnt>=0); 399e1051a39Sopenharmony_ci &psllq ($tmp,60); 400e1051a39Sopenharmony_ci &and ($nhi,0xf0) if ($odd); 401e1051a39Sopenharmony_ci &pxor ($Zhi,&QWP(0,$rem_4bit,$rem[1],8)) if ($cnt<28); 402e1051a39Sopenharmony_ci &and ($rem[0],0xf); 403e1051a39Sopenharmony_ci &pxor ($Zhi,&QWP(0,$Htbl,$nix)); 404e1051a39Sopenharmony_ci &mov ($nhi,$nlo) if (!$odd && $cnt>=0); 405e1051a39Sopenharmony_ci &movd ($rem[1],$Zlo); 406e1051a39Sopenharmony_ci &pxor ($Zlo,$tmp); 407e1051a39Sopenharmony_ci 408e1051a39Sopenharmony_ci push (@rem,shift(@rem)); # "rotate" registers 409e1051a39Sopenharmony_ci } 410e1051a39Sopenharmony_ci 411e1051a39Sopenharmony_ci &mov ($inp,&DWP(4,$rem_4bit,$rem[1],8)); # last rem_4bit[rem] 412e1051a39Sopenharmony_ci 413e1051a39Sopenharmony_ci &psrlq ($Zlo,32); # lower part of Zlo is already there 414e1051a39Sopenharmony_ci &movd ($Zhl,$Zhi); 415e1051a39Sopenharmony_ci &psrlq ($Zhi,32); 416e1051a39Sopenharmony_ci &movd ($Zlh,$Zlo); 417e1051a39Sopenharmony_ci &movd ($Zhh,$Zhi); 418e1051a39Sopenharmony_ci &shl ($inp,4); # compensate for rem_4bit[i] being >>4 419e1051a39Sopenharmony_ci 420e1051a39Sopenharmony_ci &bswap ($Zll); 421e1051a39Sopenharmony_ci &bswap ($Zhl); 422e1051a39Sopenharmony_ci &bswap ($Zlh); 423e1051a39Sopenharmony_ci &xor ($Zhh,$inp); 424e1051a39Sopenharmony_ci &bswap ($Zhh); 425e1051a39Sopenharmony_ci 426e1051a39Sopenharmony_ci &ret (); 427e1051a39Sopenharmony_ci} 428e1051a39Sopenharmony_ci&function_end_B("_mmx_gmult_4bit_inner"); 429e1051a39Sopenharmony_ci 430e1051a39Sopenharmony_ci&function_begin("gcm_gmult_4bit_mmx"); 431e1051a39Sopenharmony_ci &mov ($inp,&wparam(0)); # load Xi 432e1051a39Sopenharmony_ci &mov ($Htbl,&wparam(1)); # load Htable 433e1051a39Sopenharmony_ci 434e1051a39Sopenharmony_ci &call (&label("pic_point")); 435e1051a39Sopenharmony_ci &set_label("pic_point"); 436e1051a39Sopenharmony_ci &blindpop("eax"); 437e1051a39Sopenharmony_ci &lea ("eax",&DWP(&label("rem_4bit")."-".&label("pic_point"),"eax")); 438e1051a39Sopenharmony_ci 439e1051a39Sopenharmony_ci &movz ($Zll,&BP(15,$inp)); 440e1051a39Sopenharmony_ci 441e1051a39Sopenharmony_ci &call ("_mmx_gmult_4bit_inner"); 442e1051a39Sopenharmony_ci 443e1051a39Sopenharmony_ci &mov ($inp,&wparam(0)); # load Xi 444e1051a39Sopenharmony_ci &emms (); 445e1051a39Sopenharmony_ci &mov (&DWP(12,$inp),$Zll); 446e1051a39Sopenharmony_ci &mov (&DWP(4,$inp),$Zhl); 447e1051a39Sopenharmony_ci &mov (&DWP(8,$inp),$Zlh); 448e1051a39Sopenharmony_ci &mov (&DWP(0,$inp),$Zhh); 449e1051a39Sopenharmony_ci&function_end("gcm_gmult_4bit_mmx"); 450e1051a39Sopenharmony_ci 451e1051a39Sopenharmony_ci# Streamed version performs 20% better on P4, 7% on Opteron, 452e1051a39Sopenharmony_ci# 10% on Core2 and PIII... 453e1051a39Sopenharmony_ci&function_begin("gcm_ghash_4bit_mmx"); 454e1051a39Sopenharmony_ci &mov ($Zhh,&wparam(0)); # load Xi 455e1051a39Sopenharmony_ci &mov ($Htbl,&wparam(1)); # load Htable 456e1051a39Sopenharmony_ci &mov ($inp,&wparam(2)); # load in 457e1051a39Sopenharmony_ci &mov ($Zlh,&wparam(3)); # load len 458e1051a39Sopenharmony_ci 459e1051a39Sopenharmony_ci &call (&label("pic_point")); 460e1051a39Sopenharmony_ci &set_label("pic_point"); 461e1051a39Sopenharmony_ci &blindpop("eax"); 462e1051a39Sopenharmony_ci &lea ("eax",&DWP(&label("rem_4bit")."-".&label("pic_point"),"eax")); 463e1051a39Sopenharmony_ci 464e1051a39Sopenharmony_ci &add ($Zlh,$inp); 465e1051a39Sopenharmony_ci &mov (&wparam(3),$Zlh); # len to point at the end of input 466e1051a39Sopenharmony_ci &stack_push(4+1); # +1 for stack alignment 467e1051a39Sopenharmony_ci 468e1051a39Sopenharmony_ci &mov ($Zll,&DWP(12,$Zhh)); # load Xi[16] 469e1051a39Sopenharmony_ci &mov ($Zhl,&DWP(4,$Zhh)); 470e1051a39Sopenharmony_ci &mov ($Zlh,&DWP(8,$Zhh)); 471e1051a39Sopenharmony_ci &mov ($Zhh,&DWP(0,$Zhh)); 472e1051a39Sopenharmony_ci &jmp (&label("mmx_outer_loop")); 473e1051a39Sopenharmony_ci 474e1051a39Sopenharmony_ci &set_label("mmx_outer_loop",16); 475e1051a39Sopenharmony_ci &xor ($Zll,&DWP(12,$inp)); 476e1051a39Sopenharmony_ci &xor ($Zhl,&DWP(4,$inp)); 477e1051a39Sopenharmony_ci &xor ($Zlh,&DWP(8,$inp)); 478e1051a39Sopenharmony_ci &xor ($Zhh,&DWP(0,$inp)); 479e1051a39Sopenharmony_ci &mov (&wparam(2),$inp); 480e1051a39Sopenharmony_ci &mov (&DWP(12,"esp"),$Zll); 481e1051a39Sopenharmony_ci &mov (&DWP(4,"esp"),$Zhl); 482e1051a39Sopenharmony_ci &mov (&DWP(8,"esp"),$Zlh); 483e1051a39Sopenharmony_ci &mov (&DWP(0,"esp"),$Zhh); 484e1051a39Sopenharmony_ci 485e1051a39Sopenharmony_ci &mov ($inp,"esp"); 486e1051a39Sopenharmony_ci &shr ($Zll,24); 487e1051a39Sopenharmony_ci 488e1051a39Sopenharmony_ci &call ("_mmx_gmult_4bit_inner"); 489e1051a39Sopenharmony_ci 490e1051a39Sopenharmony_ci &mov ($inp,&wparam(2)); 491e1051a39Sopenharmony_ci &lea ($inp,&DWP(16,$inp)); 492e1051a39Sopenharmony_ci &cmp ($inp,&wparam(3)); 493e1051a39Sopenharmony_ci &jb (&label("mmx_outer_loop")); 494e1051a39Sopenharmony_ci 495e1051a39Sopenharmony_ci &mov ($inp,&wparam(0)); # load Xi 496e1051a39Sopenharmony_ci &emms (); 497e1051a39Sopenharmony_ci &mov (&DWP(12,$inp),$Zll); 498e1051a39Sopenharmony_ci &mov (&DWP(4,$inp),$Zhl); 499e1051a39Sopenharmony_ci &mov (&DWP(8,$inp),$Zlh); 500e1051a39Sopenharmony_ci &mov (&DWP(0,$inp),$Zhh); 501e1051a39Sopenharmony_ci 502e1051a39Sopenharmony_ci &stack_pop(4+1); 503e1051a39Sopenharmony_ci&function_end("gcm_ghash_4bit_mmx"); 504e1051a39Sopenharmony_ci 505e1051a39Sopenharmony_ci}} else {{ # "June" MMX version... 506e1051a39Sopenharmony_ci # ... has slower "April" gcm_gmult_4bit_mmx with folded 507e1051a39Sopenharmony_ci # loop. This is done to conserve code size... 508e1051a39Sopenharmony_ci$S=16; # shift factor for rem_4bit 509e1051a39Sopenharmony_ci 510e1051a39Sopenharmony_cisub mmx_loop() { 511e1051a39Sopenharmony_ci# MMX version performs 2.8 times better on P4 (see comment in non-MMX 512e1051a39Sopenharmony_ci# routine for further details), 40% better on Opteron and Core2, 50% 513e1051a39Sopenharmony_ci# better on PIII... In other words effort is considered to be well 514e1051a39Sopenharmony_ci# spent... 515e1051a39Sopenharmony_ci my $inp = shift; 516e1051a39Sopenharmony_ci my $rem_4bit = shift; 517e1051a39Sopenharmony_ci my $cnt = $Zhh; 518e1051a39Sopenharmony_ci my $nhi = $Zhl; 519e1051a39Sopenharmony_ci my $nlo = $Zlh; 520e1051a39Sopenharmony_ci my $rem = $Zll; 521e1051a39Sopenharmony_ci 522e1051a39Sopenharmony_ci my ($Zlo,$Zhi) = ("mm0","mm1"); 523e1051a39Sopenharmony_ci my $tmp = "mm2"; 524e1051a39Sopenharmony_ci 525e1051a39Sopenharmony_ci &xor ($nlo,$nlo); # avoid partial register stalls on PIII 526e1051a39Sopenharmony_ci &mov ($nhi,$Zll); 527e1051a39Sopenharmony_ci &mov (&LB($nlo),&LB($nhi)); 528e1051a39Sopenharmony_ci &mov ($cnt,14); 529e1051a39Sopenharmony_ci &shl (&LB($nlo),4); 530e1051a39Sopenharmony_ci &and ($nhi,0xf0); 531e1051a39Sopenharmony_ci &movq ($Zlo,&QWP(8,$Htbl,$nlo)); 532e1051a39Sopenharmony_ci &movq ($Zhi,&QWP(0,$Htbl,$nlo)); 533e1051a39Sopenharmony_ci &movd ($rem,$Zlo); 534e1051a39Sopenharmony_ci &jmp (&label("mmx_loop")); 535e1051a39Sopenharmony_ci 536e1051a39Sopenharmony_ci &set_label("mmx_loop",16); 537e1051a39Sopenharmony_ci &psrlq ($Zlo,4); 538e1051a39Sopenharmony_ci &and ($rem,0xf); 539e1051a39Sopenharmony_ci &movq ($tmp,$Zhi); 540e1051a39Sopenharmony_ci &psrlq ($Zhi,4); 541e1051a39Sopenharmony_ci &pxor ($Zlo,&QWP(8,$Htbl,$nhi)); 542e1051a39Sopenharmony_ci &mov (&LB($nlo),&BP(0,$inp,$cnt)); 543e1051a39Sopenharmony_ci &psllq ($tmp,60); 544e1051a39Sopenharmony_ci &pxor ($Zhi,&QWP(0,$rem_4bit,$rem,8)); 545e1051a39Sopenharmony_ci &dec ($cnt); 546e1051a39Sopenharmony_ci &movd ($rem,$Zlo); 547e1051a39Sopenharmony_ci &pxor ($Zhi,&QWP(0,$Htbl,$nhi)); 548e1051a39Sopenharmony_ci &mov ($nhi,$nlo); 549e1051a39Sopenharmony_ci &pxor ($Zlo,$tmp); 550e1051a39Sopenharmony_ci &js (&label("mmx_break")); 551e1051a39Sopenharmony_ci 552e1051a39Sopenharmony_ci &shl (&LB($nlo),4); 553e1051a39Sopenharmony_ci &and ($rem,0xf); 554e1051a39Sopenharmony_ci &psrlq ($Zlo,4); 555e1051a39Sopenharmony_ci &and ($nhi,0xf0); 556e1051a39Sopenharmony_ci &movq ($tmp,$Zhi); 557e1051a39Sopenharmony_ci &psrlq ($Zhi,4); 558e1051a39Sopenharmony_ci &pxor ($Zlo,&QWP(8,$Htbl,$nlo)); 559e1051a39Sopenharmony_ci &psllq ($tmp,60); 560e1051a39Sopenharmony_ci &pxor ($Zhi,&QWP(0,$rem_4bit,$rem,8)); 561e1051a39Sopenharmony_ci &movd ($rem,$Zlo); 562e1051a39Sopenharmony_ci &pxor ($Zhi,&QWP(0,$Htbl,$nlo)); 563e1051a39Sopenharmony_ci &pxor ($Zlo,$tmp); 564e1051a39Sopenharmony_ci &jmp (&label("mmx_loop")); 565e1051a39Sopenharmony_ci 566e1051a39Sopenharmony_ci &set_label("mmx_break",16); 567e1051a39Sopenharmony_ci &shl (&LB($nlo),4); 568e1051a39Sopenharmony_ci &and ($rem,0xf); 569e1051a39Sopenharmony_ci &psrlq ($Zlo,4); 570e1051a39Sopenharmony_ci &and ($nhi,0xf0); 571e1051a39Sopenharmony_ci &movq ($tmp,$Zhi); 572e1051a39Sopenharmony_ci &psrlq ($Zhi,4); 573e1051a39Sopenharmony_ci &pxor ($Zlo,&QWP(8,$Htbl,$nlo)); 574e1051a39Sopenharmony_ci &psllq ($tmp,60); 575e1051a39Sopenharmony_ci &pxor ($Zhi,&QWP(0,$rem_4bit,$rem,8)); 576e1051a39Sopenharmony_ci &movd ($rem,$Zlo); 577e1051a39Sopenharmony_ci &pxor ($Zhi,&QWP(0,$Htbl,$nlo)); 578e1051a39Sopenharmony_ci &pxor ($Zlo,$tmp); 579e1051a39Sopenharmony_ci 580e1051a39Sopenharmony_ci &psrlq ($Zlo,4); 581e1051a39Sopenharmony_ci &and ($rem,0xf); 582e1051a39Sopenharmony_ci &movq ($tmp,$Zhi); 583e1051a39Sopenharmony_ci &psrlq ($Zhi,4); 584e1051a39Sopenharmony_ci &pxor ($Zlo,&QWP(8,$Htbl,$nhi)); 585e1051a39Sopenharmony_ci &psllq ($tmp,60); 586e1051a39Sopenharmony_ci &pxor ($Zhi,&QWP(0,$rem_4bit,$rem,8)); 587e1051a39Sopenharmony_ci &movd ($rem,$Zlo); 588e1051a39Sopenharmony_ci &pxor ($Zhi,&QWP(0,$Htbl,$nhi)); 589e1051a39Sopenharmony_ci &pxor ($Zlo,$tmp); 590e1051a39Sopenharmony_ci 591e1051a39Sopenharmony_ci &psrlq ($Zlo,32); # lower part of Zlo is already there 592e1051a39Sopenharmony_ci &movd ($Zhl,$Zhi); 593e1051a39Sopenharmony_ci &psrlq ($Zhi,32); 594e1051a39Sopenharmony_ci &movd ($Zlh,$Zlo); 595e1051a39Sopenharmony_ci &movd ($Zhh,$Zhi); 596e1051a39Sopenharmony_ci 597e1051a39Sopenharmony_ci &bswap ($Zll); 598e1051a39Sopenharmony_ci &bswap ($Zhl); 599e1051a39Sopenharmony_ci &bswap ($Zlh); 600e1051a39Sopenharmony_ci &bswap ($Zhh); 601e1051a39Sopenharmony_ci} 602e1051a39Sopenharmony_ci 603e1051a39Sopenharmony_ci&function_begin("gcm_gmult_4bit_mmx"); 604e1051a39Sopenharmony_ci &mov ($inp,&wparam(0)); # load Xi 605e1051a39Sopenharmony_ci &mov ($Htbl,&wparam(1)); # load Htable 606e1051a39Sopenharmony_ci 607e1051a39Sopenharmony_ci &call (&label("pic_point")); 608e1051a39Sopenharmony_ci &set_label("pic_point"); 609e1051a39Sopenharmony_ci &blindpop("eax"); 610e1051a39Sopenharmony_ci &lea ("eax",&DWP(&label("rem_4bit")."-".&label("pic_point"),"eax")); 611e1051a39Sopenharmony_ci 612e1051a39Sopenharmony_ci &movz ($Zll,&BP(15,$inp)); 613e1051a39Sopenharmony_ci 614e1051a39Sopenharmony_ci &mmx_loop($inp,"eax"); 615e1051a39Sopenharmony_ci 616e1051a39Sopenharmony_ci &emms (); 617e1051a39Sopenharmony_ci &mov (&DWP(12,$inp),$Zll); 618e1051a39Sopenharmony_ci &mov (&DWP(4,$inp),$Zhl); 619e1051a39Sopenharmony_ci &mov (&DWP(8,$inp),$Zlh); 620e1051a39Sopenharmony_ci &mov (&DWP(0,$inp),$Zhh); 621e1051a39Sopenharmony_ci&function_end("gcm_gmult_4bit_mmx"); 622e1051a39Sopenharmony_ci 623e1051a39Sopenharmony_ci###################################################################### 624e1051a39Sopenharmony_ci# Below subroutine is "528B" variant of "4-bit" GCM GHASH function 625e1051a39Sopenharmony_ci# (see gcm128.c for details). It provides further 20-40% performance 626e1051a39Sopenharmony_ci# improvement over above mentioned "May" version. 627e1051a39Sopenharmony_ci 628e1051a39Sopenharmony_ci&static_label("rem_8bit"); 629e1051a39Sopenharmony_ci 630e1051a39Sopenharmony_ci&function_begin("gcm_ghash_4bit_mmx"); 631e1051a39Sopenharmony_ci{ my ($Zlo,$Zhi) = ("mm7","mm6"); 632e1051a39Sopenharmony_ci my $rem_8bit = "esi"; 633e1051a39Sopenharmony_ci my $Htbl = "ebx"; 634e1051a39Sopenharmony_ci 635e1051a39Sopenharmony_ci # parameter block 636e1051a39Sopenharmony_ci &mov ("eax",&wparam(0)); # Xi 637e1051a39Sopenharmony_ci &mov ("ebx",&wparam(1)); # Htable 638e1051a39Sopenharmony_ci &mov ("ecx",&wparam(2)); # inp 639e1051a39Sopenharmony_ci &mov ("edx",&wparam(3)); # len 640e1051a39Sopenharmony_ci &mov ("ebp","esp"); # original %esp 641e1051a39Sopenharmony_ci &call (&label("pic_point")); 642e1051a39Sopenharmony_ci &set_label ("pic_point"); 643e1051a39Sopenharmony_ci &blindpop ($rem_8bit); 644e1051a39Sopenharmony_ci &lea ($rem_8bit,&DWP(&label("rem_8bit")."-".&label("pic_point"),$rem_8bit)); 645e1051a39Sopenharmony_ci 646e1051a39Sopenharmony_ci &sub ("esp",512+16+16); # allocate stack frame... 647e1051a39Sopenharmony_ci &and ("esp",-64); # ...and align it 648e1051a39Sopenharmony_ci &sub ("esp",16); # place for (u8)(H[]<<4) 649e1051a39Sopenharmony_ci 650e1051a39Sopenharmony_ci &add ("edx","ecx"); # pointer to the end of input 651e1051a39Sopenharmony_ci &mov (&DWP(528+16+0,"esp"),"eax"); # save Xi 652e1051a39Sopenharmony_ci &mov (&DWP(528+16+8,"esp"),"edx"); # save inp+len 653e1051a39Sopenharmony_ci &mov (&DWP(528+16+12,"esp"),"ebp"); # save original %esp 654e1051a39Sopenharmony_ci 655e1051a39Sopenharmony_ci { my @lo = ("mm0","mm1","mm2"); 656e1051a39Sopenharmony_ci my @hi = ("mm3","mm4","mm5"); 657e1051a39Sopenharmony_ci my @tmp = ("mm6","mm7"); 658e1051a39Sopenharmony_ci my ($off1,$off2,$i) = (0,0,); 659e1051a39Sopenharmony_ci 660e1051a39Sopenharmony_ci &add ($Htbl,128); # optimize for size 661e1051a39Sopenharmony_ci &lea ("edi",&DWP(16+128,"esp")); 662e1051a39Sopenharmony_ci &lea ("ebp",&DWP(16+256+128,"esp")); 663e1051a39Sopenharmony_ci 664e1051a39Sopenharmony_ci # decompose Htable (low and high parts are kept separately), 665e1051a39Sopenharmony_ci # generate Htable[]>>4, (u8)(Htable[]<<4), save to stack... 666e1051a39Sopenharmony_ci for ($i=0;$i<18;$i++) { 667e1051a39Sopenharmony_ci 668e1051a39Sopenharmony_ci &mov ("edx",&DWP(16*$i+8-128,$Htbl)) if ($i<16); 669e1051a39Sopenharmony_ci &movq ($lo[0],&QWP(16*$i+8-128,$Htbl)) if ($i<16); 670e1051a39Sopenharmony_ci &psllq ($tmp[1],60) if ($i>1); 671e1051a39Sopenharmony_ci &movq ($hi[0],&QWP(16*$i+0-128,$Htbl)) if ($i<16); 672e1051a39Sopenharmony_ci &por ($lo[2],$tmp[1]) if ($i>1); 673e1051a39Sopenharmony_ci &movq (&QWP($off1-128,"edi"),$lo[1]) if ($i>0 && $i<17); 674e1051a39Sopenharmony_ci &psrlq ($lo[1],4) if ($i>0 && $i<17); 675e1051a39Sopenharmony_ci &movq (&QWP($off1,"edi"),$hi[1]) if ($i>0 && $i<17); 676e1051a39Sopenharmony_ci &movq ($tmp[0],$hi[1]) if ($i>0 && $i<17); 677e1051a39Sopenharmony_ci &movq (&QWP($off2-128,"ebp"),$lo[2]) if ($i>1); 678e1051a39Sopenharmony_ci &psrlq ($hi[1],4) if ($i>0 && $i<17); 679e1051a39Sopenharmony_ci &movq (&QWP($off2,"ebp"),$hi[2]) if ($i>1); 680e1051a39Sopenharmony_ci &shl ("edx",4) if ($i<16); 681e1051a39Sopenharmony_ci &mov (&BP($i,"esp"),&LB("edx")) if ($i<16); 682e1051a39Sopenharmony_ci 683e1051a39Sopenharmony_ci unshift (@lo,pop(@lo)); # "rotate" registers 684e1051a39Sopenharmony_ci unshift (@hi,pop(@hi)); 685e1051a39Sopenharmony_ci unshift (@tmp,pop(@tmp)); 686e1051a39Sopenharmony_ci $off1 += 8 if ($i>0); 687e1051a39Sopenharmony_ci $off2 += 8 if ($i>1); 688e1051a39Sopenharmony_ci } 689e1051a39Sopenharmony_ci } 690e1051a39Sopenharmony_ci 691e1051a39Sopenharmony_ci &movq ($Zhi,&QWP(0,"eax")); 692e1051a39Sopenharmony_ci &mov ("ebx",&DWP(8,"eax")); 693e1051a39Sopenharmony_ci &mov ("edx",&DWP(12,"eax")); # load Xi 694e1051a39Sopenharmony_ci 695e1051a39Sopenharmony_ci&set_label("outer",16); 696e1051a39Sopenharmony_ci { my $nlo = "eax"; 697e1051a39Sopenharmony_ci my $dat = "edx"; 698e1051a39Sopenharmony_ci my @nhi = ("edi","ebp"); 699e1051a39Sopenharmony_ci my @rem = ("ebx","ecx"); 700e1051a39Sopenharmony_ci my @red = ("mm0","mm1","mm2"); 701e1051a39Sopenharmony_ci my $tmp = "mm3"; 702e1051a39Sopenharmony_ci 703e1051a39Sopenharmony_ci &xor ($dat,&DWP(12,"ecx")); # merge input data 704e1051a39Sopenharmony_ci &xor ("ebx",&DWP(8,"ecx")); 705e1051a39Sopenharmony_ci &pxor ($Zhi,&QWP(0,"ecx")); 706e1051a39Sopenharmony_ci &lea ("ecx",&DWP(16,"ecx")); # inp+=16 707e1051a39Sopenharmony_ci #&mov (&DWP(528+12,"esp"),$dat); # save inp^Xi 708e1051a39Sopenharmony_ci &mov (&DWP(528+8,"esp"),"ebx"); 709e1051a39Sopenharmony_ci &movq (&QWP(528+0,"esp"),$Zhi); 710e1051a39Sopenharmony_ci &mov (&DWP(528+16+4,"esp"),"ecx"); # save inp 711e1051a39Sopenharmony_ci 712e1051a39Sopenharmony_ci &xor ($nlo,$nlo); 713e1051a39Sopenharmony_ci &rol ($dat,8); 714e1051a39Sopenharmony_ci &mov (&LB($nlo),&LB($dat)); 715e1051a39Sopenharmony_ci &mov ($nhi[1],$nlo); 716e1051a39Sopenharmony_ci &and (&LB($nlo),0x0f); 717e1051a39Sopenharmony_ci &shr ($nhi[1],4); 718e1051a39Sopenharmony_ci &pxor ($red[0],$red[0]); 719e1051a39Sopenharmony_ci &rol ($dat,8); # next byte 720e1051a39Sopenharmony_ci &pxor ($red[1],$red[1]); 721e1051a39Sopenharmony_ci &pxor ($red[2],$red[2]); 722e1051a39Sopenharmony_ci 723e1051a39Sopenharmony_ci # Just like in "May" version modulo-schedule for critical path in 724e1051a39Sopenharmony_ci # 'Z.hi ^= rem_8bit[Z.lo&0xff^((u8)H[nhi]<<4)]<<48'. Final 'pxor' 725e1051a39Sopenharmony_ci # is scheduled so late that rem_8bit[] has to be shifted *right* 726e1051a39Sopenharmony_ci # by 16, which is why last argument to pinsrw is 2, which 727e1051a39Sopenharmony_ci # corresponds to <<32=<<48>>16... 728e1051a39Sopenharmony_ci for ($j=11,$i=0;$i<15;$i++) { 729e1051a39Sopenharmony_ci 730e1051a39Sopenharmony_ci if ($i>0) { 731e1051a39Sopenharmony_ci &pxor ($Zlo,&QWP(16,"esp",$nlo,8)); # Z^=H[nlo] 732e1051a39Sopenharmony_ci &rol ($dat,8); # next byte 733e1051a39Sopenharmony_ci &pxor ($Zhi,&QWP(16+128,"esp",$nlo,8)); 734e1051a39Sopenharmony_ci 735e1051a39Sopenharmony_ci &pxor ($Zlo,$tmp); 736e1051a39Sopenharmony_ci &pxor ($Zhi,&QWP(16+256+128,"esp",$nhi[0],8)); 737e1051a39Sopenharmony_ci &xor (&LB($rem[1]),&BP(0,"esp",$nhi[0])); # rem^(H[nhi]<<4) 738e1051a39Sopenharmony_ci } else { 739e1051a39Sopenharmony_ci &movq ($Zlo,&QWP(16,"esp",$nlo,8)); 740e1051a39Sopenharmony_ci &movq ($Zhi,&QWP(16+128,"esp",$nlo,8)); 741e1051a39Sopenharmony_ci } 742e1051a39Sopenharmony_ci 743e1051a39Sopenharmony_ci &mov (&LB($nlo),&LB($dat)); 744e1051a39Sopenharmony_ci &mov ($dat,&DWP(528+$j,"esp")) if (--$j%4==0); 745e1051a39Sopenharmony_ci 746e1051a39Sopenharmony_ci &movd ($rem[0],$Zlo); 747e1051a39Sopenharmony_ci &movz ($rem[1],&LB($rem[1])) if ($i>0); 748e1051a39Sopenharmony_ci &psrlq ($Zlo,8); # Z>>=8 749e1051a39Sopenharmony_ci 750e1051a39Sopenharmony_ci &movq ($tmp,$Zhi); 751e1051a39Sopenharmony_ci &mov ($nhi[0],$nlo); 752e1051a39Sopenharmony_ci &psrlq ($Zhi,8); 753e1051a39Sopenharmony_ci 754e1051a39Sopenharmony_ci &pxor ($Zlo,&QWP(16+256+0,"esp",$nhi[1],8)); # Z^=H[nhi]>>4 755e1051a39Sopenharmony_ci &and (&LB($nlo),0x0f); 756e1051a39Sopenharmony_ci &psllq ($tmp,56); 757e1051a39Sopenharmony_ci 758e1051a39Sopenharmony_ci &pxor ($Zhi,$red[1]) if ($i>1); 759e1051a39Sopenharmony_ci &shr ($nhi[0],4); 760e1051a39Sopenharmony_ci &pinsrw ($red[0],&WP(0,$rem_8bit,$rem[1],2),2) if ($i>0); 761e1051a39Sopenharmony_ci 762e1051a39Sopenharmony_ci unshift (@red,pop(@red)); # "rotate" registers 763e1051a39Sopenharmony_ci unshift (@rem,pop(@rem)); 764e1051a39Sopenharmony_ci unshift (@nhi,pop(@nhi)); 765e1051a39Sopenharmony_ci } 766e1051a39Sopenharmony_ci 767e1051a39Sopenharmony_ci &pxor ($Zlo,&QWP(16,"esp",$nlo,8)); # Z^=H[nlo] 768e1051a39Sopenharmony_ci &pxor ($Zhi,&QWP(16+128,"esp",$nlo,8)); 769e1051a39Sopenharmony_ci &xor (&LB($rem[1]),&BP(0,"esp",$nhi[0])); # rem^(H[nhi]<<4) 770e1051a39Sopenharmony_ci 771e1051a39Sopenharmony_ci &pxor ($Zlo,$tmp); 772e1051a39Sopenharmony_ci &pxor ($Zhi,&QWP(16+256+128,"esp",$nhi[0],8)); 773e1051a39Sopenharmony_ci &movz ($rem[1],&LB($rem[1])); 774e1051a39Sopenharmony_ci 775e1051a39Sopenharmony_ci &pxor ($red[2],$red[2]); # clear 2nd word 776e1051a39Sopenharmony_ci &psllq ($red[1],4); 777e1051a39Sopenharmony_ci 778e1051a39Sopenharmony_ci &movd ($rem[0],$Zlo); 779e1051a39Sopenharmony_ci &psrlq ($Zlo,4); # Z>>=4 780e1051a39Sopenharmony_ci 781e1051a39Sopenharmony_ci &movq ($tmp,$Zhi); 782e1051a39Sopenharmony_ci &psrlq ($Zhi,4); 783e1051a39Sopenharmony_ci &shl ($rem[0],4); # rem<<4 784e1051a39Sopenharmony_ci 785e1051a39Sopenharmony_ci &pxor ($Zlo,&QWP(16,"esp",$nhi[1],8)); # Z^=H[nhi] 786e1051a39Sopenharmony_ci &psllq ($tmp,60); 787e1051a39Sopenharmony_ci &movz ($rem[0],&LB($rem[0])); 788e1051a39Sopenharmony_ci 789e1051a39Sopenharmony_ci &pxor ($Zlo,$tmp); 790e1051a39Sopenharmony_ci &pxor ($Zhi,&QWP(16+128,"esp",$nhi[1],8)); 791e1051a39Sopenharmony_ci 792e1051a39Sopenharmony_ci &pinsrw ($red[0],&WP(0,$rem_8bit,$rem[1],2),2); 793e1051a39Sopenharmony_ci &pxor ($Zhi,$red[1]); 794e1051a39Sopenharmony_ci 795e1051a39Sopenharmony_ci &movd ($dat,$Zlo); 796e1051a39Sopenharmony_ci &pinsrw ($red[2],&WP(0,$rem_8bit,$rem[0],2),3); # last is <<48 797e1051a39Sopenharmony_ci 798e1051a39Sopenharmony_ci &psllq ($red[0],12); # correct by <<16>>4 799e1051a39Sopenharmony_ci &pxor ($Zhi,$red[0]); 800e1051a39Sopenharmony_ci &psrlq ($Zlo,32); 801e1051a39Sopenharmony_ci &pxor ($Zhi,$red[2]); 802e1051a39Sopenharmony_ci 803e1051a39Sopenharmony_ci &mov ("ecx",&DWP(528+16+4,"esp")); # restore inp 804e1051a39Sopenharmony_ci &movd ("ebx",$Zlo); 805e1051a39Sopenharmony_ci &movq ($tmp,$Zhi); # 01234567 806e1051a39Sopenharmony_ci &psllw ($Zhi,8); # 1.3.5.7. 807e1051a39Sopenharmony_ci &psrlw ($tmp,8); # .0.2.4.6 808e1051a39Sopenharmony_ci &por ($Zhi,$tmp); # 10325476 809e1051a39Sopenharmony_ci &bswap ($dat); 810e1051a39Sopenharmony_ci &pshufw ($Zhi,$Zhi,0b00011011); # 76543210 811e1051a39Sopenharmony_ci &bswap ("ebx"); 812e1051a39Sopenharmony_ci 813e1051a39Sopenharmony_ci &cmp ("ecx",&DWP(528+16+8,"esp")); # are we done? 814e1051a39Sopenharmony_ci &jne (&label("outer")); 815e1051a39Sopenharmony_ci } 816e1051a39Sopenharmony_ci 817e1051a39Sopenharmony_ci &mov ("eax",&DWP(528+16+0,"esp")); # restore Xi 818e1051a39Sopenharmony_ci &mov (&DWP(12,"eax"),"edx"); 819e1051a39Sopenharmony_ci &mov (&DWP(8,"eax"),"ebx"); 820e1051a39Sopenharmony_ci &movq (&QWP(0,"eax"),$Zhi); 821e1051a39Sopenharmony_ci 822e1051a39Sopenharmony_ci &mov ("esp",&DWP(528+16+12,"esp")); # restore original %esp 823e1051a39Sopenharmony_ci &emms (); 824e1051a39Sopenharmony_ci} 825e1051a39Sopenharmony_ci&function_end("gcm_ghash_4bit_mmx"); 826e1051a39Sopenharmony_ci}} 827e1051a39Sopenharmony_ci 828e1051a39Sopenharmony_ciif ($sse2) {{ 829e1051a39Sopenharmony_ci###################################################################### 830e1051a39Sopenharmony_ci# PCLMULQDQ version. 831e1051a39Sopenharmony_ci 832e1051a39Sopenharmony_ci$Xip="eax"; 833e1051a39Sopenharmony_ci$Htbl="edx"; 834e1051a39Sopenharmony_ci$const="ecx"; 835e1051a39Sopenharmony_ci$inp="esi"; 836e1051a39Sopenharmony_ci$len="ebx"; 837e1051a39Sopenharmony_ci 838e1051a39Sopenharmony_ci($Xi,$Xhi)=("xmm0","xmm1"); $Hkey="xmm2"; 839e1051a39Sopenharmony_ci($T1,$T2,$T3)=("xmm3","xmm4","xmm5"); 840e1051a39Sopenharmony_ci($Xn,$Xhn)=("xmm6","xmm7"); 841e1051a39Sopenharmony_ci 842e1051a39Sopenharmony_ci&static_label("bswap"); 843e1051a39Sopenharmony_ci 844e1051a39Sopenharmony_cisub clmul64x64_T2 { # minimal "register" pressure 845e1051a39Sopenharmony_cimy ($Xhi,$Xi,$Hkey,$HK)=@_; 846e1051a39Sopenharmony_ci 847e1051a39Sopenharmony_ci &movdqa ($Xhi,$Xi); # 848e1051a39Sopenharmony_ci &pshufd ($T1,$Xi,0b01001110); 849e1051a39Sopenharmony_ci &pshufd ($T2,$Hkey,0b01001110) if (!defined($HK)); 850e1051a39Sopenharmony_ci &pxor ($T1,$Xi); # 851e1051a39Sopenharmony_ci &pxor ($T2,$Hkey) if (!defined($HK)); 852e1051a39Sopenharmony_ci $HK=$T2 if (!defined($HK)); 853e1051a39Sopenharmony_ci 854e1051a39Sopenharmony_ci &pclmulqdq ($Xi,$Hkey,0x00); ####### 855e1051a39Sopenharmony_ci &pclmulqdq ($Xhi,$Hkey,0x11); ####### 856e1051a39Sopenharmony_ci &pclmulqdq ($T1,$HK,0x00); ####### 857e1051a39Sopenharmony_ci &xorps ($T1,$Xi); # 858e1051a39Sopenharmony_ci &xorps ($T1,$Xhi); # 859e1051a39Sopenharmony_ci 860e1051a39Sopenharmony_ci &movdqa ($T2,$T1); # 861e1051a39Sopenharmony_ci &psrldq ($T1,8); 862e1051a39Sopenharmony_ci &pslldq ($T2,8); # 863e1051a39Sopenharmony_ci &pxor ($Xhi,$T1); 864e1051a39Sopenharmony_ci &pxor ($Xi,$T2); # 865e1051a39Sopenharmony_ci} 866e1051a39Sopenharmony_ci 867e1051a39Sopenharmony_cisub clmul64x64_T3 { 868e1051a39Sopenharmony_ci# Even though this subroutine offers visually better ILP, it 869e1051a39Sopenharmony_ci# was empirically found to be a tad slower than above version. 870e1051a39Sopenharmony_ci# At least in gcm_ghash_clmul context. But it's just as well, 871e1051a39Sopenharmony_ci# because loop modulo-scheduling is possible only thanks to 872e1051a39Sopenharmony_ci# minimized "register" pressure... 873e1051a39Sopenharmony_cimy ($Xhi,$Xi,$Hkey)=@_; 874e1051a39Sopenharmony_ci 875e1051a39Sopenharmony_ci &movdqa ($T1,$Xi); # 876e1051a39Sopenharmony_ci &movdqa ($Xhi,$Xi); 877e1051a39Sopenharmony_ci &pclmulqdq ($Xi,$Hkey,0x00); ####### 878e1051a39Sopenharmony_ci &pclmulqdq ($Xhi,$Hkey,0x11); ####### 879e1051a39Sopenharmony_ci &pshufd ($T2,$T1,0b01001110); # 880e1051a39Sopenharmony_ci &pshufd ($T3,$Hkey,0b01001110); 881e1051a39Sopenharmony_ci &pxor ($T2,$T1); # 882e1051a39Sopenharmony_ci &pxor ($T3,$Hkey); 883e1051a39Sopenharmony_ci &pclmulqdq ($T2,$T3,0x00); ####### 884e1051a39Sopenharmony_ci &pxor ($T2,$Xi); # 885e1051a39Sopenharmony_ci &pxor ($T2,$Xhi); # 886e1051a39Sopenharmony_ci 887e1051a39Sopenharmony_ci &movdqa ($T3,$T2); # 888e1051a39Sopenharmony_ci &psrldq ($T2,8); 889e1051a39Sopenharmony_ci &pslldq ($T3,8); # 890e1051a39Sopenharmony_ci &pxor ($Xhi,$T2); 891e1051a39Sopenharmony_ci &pxor ($Xi,$T3); # 892e1051a39Sopenharmony_ci} 893e1051a39Sopenharmony_ci 894e1051a39Sopenharmony_ciif (1) { # Algorithm 9 with <<1 twist. 895e1051a39Sopenharmony_ci # Reduction is shorter and uses only two 896e1051a39Sopenharmony_ci # temporary registers, which makes it better 897e1051a39Sopenharmony_ci # candidate for interleaving with 64x64 898e1051a39Sopenharmony_ci # multiplication. Pre-modulo-scheduled loop 899e1051a39Sopenharmony_ci # was found to be ~20% faster than Algorithm 5 900e1051a39Sopenharmony_ci # below. Algorithm 9 was therefore chosen for 901e1051a39Sopenharmony_ci # further optimization... 902e1051a39Sopenharmony_ci 903e1051a39Sopenharmony_cisub reduction_alg9 { # 17/11 times faster than Intel version 904e1051a39Sopenharmony_cimy ($Xhi,$Xi) = @_; 905e1051a39Sopenharmony_ci 906e1051a39Sopenharmony_ci # 1st phase 907e1051a39Sopenharmony_ci &movdqa ($T2,$Xi); # 908e1051a39Sopenharmony_ci &movdqa ($T1,$Xi); 909e1051a39Sopenharmony_ci &psllq ($Xi,5); 910e1051a39Sopenharmony_ci &pxor ($T1,$Xi); # 911e1051a39Sopenharmony_ci &psllq ($Xi,1); 912e1051a39Sopenharmony_ci &pxor ($Xi,$T1); # 913e1051a39Sopenharmony_ci &psllq ($Xi,57); # 914e1051a39Sopenharmony_ci &movdqa ($T1,$Xi); # 915e1051a39Sopenharmony_ci &pslldq ($Xi,8); 916e1051a39Sopenharmony_ci &psrldq ($T1,8); # 917e1051a39Sopenharmony_ci &pxor ($Xi,$T2); 918e1051a39Sopenharmony_ci &pxor ($Xhi,$T1); # 919e1051a39Sopenharmony_ci 920e1051a39Sopenharmony_ci # 2nd phase 921e1051a39Sopenharmony_ci &movdqa ($T2,$Xi); 922e1051a39Sopenharmony_ci &psrlq ($Xi,1); 923e1051a39Sopenharmony_ci &pxor ($Xhi,$T2); # 924e1051a39Sopenharmony_ci &pxor ($T2,$Xi); 925e1051a39Sopenharmony_ci &psrlq ($Xi,5); 926e1051a39Sopenharmony_ci &pxor ($Xi,$T2); # 927e1051a39Sopenharmony_ci &psrlq ($Xi,1); # 928e1051a39Sopenharmony_ci &pxor ($Xi,$Xhi) # 929e1051a39Sopenharmony_ci} 930e1051a39Sopenharmony_ci 931e1051a39Sopenharmony_ci&function_begin_B("gcm_init_clmul"); 932e1051a39Sopenharmony_ci &mov ($Htbl,&wparam(0)); 933e1051a39Sopenharmony_ci &mov ($Xip,&wparam(1)); 934e1051a39Sopenharmony_ci 935e1051a39Sopenharmony_ci &call (&label("pic")); 936e1051a39Sopenharmony_ci&set_label("pic"); 937e1051a39Sopenharmony_ci &blindpop ($const); 938e1051a39Sopenharmony_ci &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const)); 939e1051a39Sopenharmony_ci 940e1051a39Sopenharmony_ci &movdqu ($Hkey,&QWP(0,$Xip)); 941e1051a39Sopenharmony_ci &pshufd ($Hkey,$Hkey,0b01001110);# dword swap 942e1051a39Sopenharmony_ci 943e1051a39Sopenharmony_ci # <<1 twist 944e1051a39Sopenharmony_ci &pshufd ($T2,$Hkey,0b11111111); # broadcast uppermost dword 945e1051a39Sopenharmony_ci &movdqa ($T1,$Hkey); 946e1051a39Sopenharmony_ci &psllq ($Hkey,1); 947e1051a39Sopenharmony_ci &pxor ($T3,$T3); # 948e1051a39Sopenharmony_ci &psrlq ($T1,63); 949e1051a39Sopenharmony_ci &pcmpgtd ($T3,$T2); # broadcast carry bit 950e1051a39Sopenharmony_ci &pslldq ($T1,8); 951e1051a39Sopenharmony_ci &por ($Hkey,$T1); # H<<=1 952e1051a39Sopenharmony_ci 953e1051a39Sopenharmony_ci # magic reduction 954e1051a39Sopenharmony_ci &pand ($T3,&QWP(16,$const)); # 0x1c2_polynomial 955e1051a39Sopenharmony_ci &pxor ($Hkey,$T3); # if(carry) H^=0x1c2_polynomial 956e1051a39Sopenharmony_ci 957e1051a39Sopenharmony_ci # calculate H^2 958e1051a39Sopenharmony_ci &movdqa ($Xi,$Hkey); 959e1051a39Sopenharmony_ci &clmul64x64_T2 ($Xhi,$Xi,$Hkey); 960e1051a39Sopenharmony_ci &reduction_alg9 ($Xhi,$Xi); 961e1051a39Sopenharmony_ci 962e1051a39Sopenharmony_ci &pshufd ($T1,$Hkey,0b01001110); 963e1051a39Sopenharmony_ci &pshufd ($T2,$Xi,0b01001110); 964e1051a39Sopenharmony_ci &pxor ($T1,$Hkey); # Karatsuba pre-processing 965e1051a39Sopenharmony_ci &movdqu (&QWP(0,$Htbl),$Hkey); # save H 966e1051a39Sopenharmony_ci &pxor ($T2,$Xi); # Karatsuba pre-processing 967e1051a39Sopenharmony_ci &movdqu (&QWP(16,$Htbl),$Xi); # save H^2 968e1051a39Sopenharmony_ci &palignr ($T2,$T1,8); # low part is H.lo^H.hi 969e1051a39Sopenharmony_ci &movdqu (&QWP(32,$Htbl),$T2); # save Karatsuba "salt" 970e1051a39Sopenharmony_ci 971e1051a39Sopenharmony_ci &ret (); 972e1051a39Sopenharmony_ci&function_end_B("gcm_init_clmul"); 973e1051a39Sopenharmony_ci 974e1051a39Sopenharmony_ci&function_begin_B("gcm_gmult_clmul"); 975e1051a39Sopenharmony_ci &mov ($Xip,&wparam(0)); 976e1051a39Sopenharmony_ci &mov ($Htbl,&wparam(1)); 977e1051a39Sopenharmony_ci 978e1051a39Sopenharmony_ci &call (&label("pic")); 979e1051a39Sopenharmony_ci&set_label("pic"); 980e1051a39Sopenharmony_ci &blindpop ($const); 981e1051a39Sopenharmony_ci &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const)); 982e1051a39Sopenharmony_ci 983e1051a39Sopenharmony_ci &movdqu ($Xi,&QWP(0,$Xip)); 984e1051a39Sopenharmony_ci &movdqa ($T3,&QWP(0,$const)); 985e1051a39Sopenharmony_ci &movups ($Hkey,&QWP(0,$Htbl)); 986e1051a39Sopenharmony_ci &pshufb ($Xi,$T3); 987e1051a39Sopenharmony_ci &movups ($T2,&QWP(32,$Htbl)); 988e1051a39Sopenharmony_ci 989e1051a39Sopenharmony_ci &clmul64x64_T2 ($Xhi,$Xi,$Hkey,$T2); 990e1051a39Sopenharmony_ci &reduction_alg9 ($Xhi,$Xi); 991e1051a39Sopenharmony_ci 992e1051a39Sopenharmony_ci &pshufb ($Xi,$T3); 993e1051a39Sopenharmony_ci &movdqu (&QWP(0,$Xip),$Xi); 994e1051a39Sopenharmony_ci 995e1051a39Sopenharmony_ci &ret (); 996e1051a39Sopenharmony_ci&function_end_B("gcm_gmult_clmul"); 997e1051a39Sopenharmony_ci 998e1051a39Sopenharmony_ci&function_begin("gcm_ghash_clmul"); 999e1051a39Sopenharmony_ci &mov ($Xip,&wparam(0)); 1000e1051a39Sopenharmony_ci &mov ($Htbl,&wparam(1)); 1001e1051a39Sopenharmony_ci &mov ($inp,&wparam(2)); 1002e1051a39Sopenharmony_ci &mov ($len,&wparam(3)); 1003e1051a39Sopenharmony_ci 1004e1051a39Sopenharmony_ci &call (&label("pic")); 1005e1051a39Sopenharmony_ci&set_label("pic"); 1006e1051a39Sopenharmony_ci &blindpop ($const); 1007e1051a39Sopenharmony_ci &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const)); 1008e1051a39Sopenharmony_ci 1009e1051a39Sopenharmony_ci &movdqu ($Xi,&QWP(0,$Xip)); 1010e1051a39Sopenharmony_ci &movdqa ($T3,&QWP(0,$const)); 1011e1051a39Sopenharmony_ci &movdqu ($Hkey,&QWP(0,$Htbl)); 1012e1051a39Sopenharmony_ci &pshufb ($Xi,$T3); 1013e1051a39Sopenharmony_ci 1014e1051a39Sopenharmony_ci &sub ($len,0x10); 1015e1051a39Sopenharmony_ci &jz (&label("odd_tail")); 1016e1051a39Sopenharmony_ci 1017e1051a39Sopenharmony_ci ####### 1018e1051a39Sopenharmony_ci # Xi+2 =[H*(Ii+1 + Xi+1)] mod P = 1019e1051a39Sopenharmony_ci # [(H*Ii+1) + (H*Xi+1)] mod P = 1020e1051a39Sopenharmony_ci # [(H*Ii+1) + H^2*(Ii+Xi)] mod P 1021e1051a39Sopenharmony_ci # 1022e1051a39Sopenharmony_ci &movdqu ($T1,&QWP(0,$inp)); # Ii 1023e1051a39Sopenharmony_ci &movdqu ($Xn,&QWP(16,$inp)); # Ii+1 1024e1051a39Sopenharmony_ci &pshufb ($T1,$T3); 1025e1051a39Sopenharmony_ci &pshufb ($Xn,$T3); 1026e1051a39Sopenharmony_ci &movdqu ($T3,&QWP(32,$Htbl)); 1027e1051a39Sopenharmony_ci &pxor ($Xi,$T1); # Ii+Xi 1028e1051a39Sopenharmony_ci 1029e1051a39Sopenharmony_ci &pshufd ($T1,$Xn,0b01001110); # H*Ii+1 1030e1051a39Sopenharmony_ci &movdqa ($Xhn,$Xn); 1031e1051a39Sopenharmony_ci &pxor ($T1,$Xn); # 1032e1051a39Sopenharmony_ci &lea ($inp,&DWP(32,$inp)); # i+=2 1033e1051a39Sopenharmony_ci 1034e1051a39Sopenharmony_ci &pclmulqdq ($Xn,$Hkey,0x00); ####### 1035e1051a39Sopenharmony_ci &pclmulqdq ($Xhn,$Hkey,0x11); ####### 1036e1051a39Sopenharmony_ci &pclmulqdq ($T1,$T3,0x00); ####### 1037e1051a39Sopenharmony_ci &movups ($Hkey,&QWP(16,$Htbl)); # load H^2 1038e1051a39Sopenharmony_ci &nop (); 1039e1051a39Sopenharmony_ci 1040e1051a39Sopenharmony_ci &sub ($len,0x20); 1041e1051a39Sopenharmony_ci &jbe (&label("even_tail")); 1042e1051a39Sopenharmony_ci &jmp (&label("mod_loop")); 1043e1051a39Sopenharmony_ci 1044e1051a39Sopenharmony_ci&set_label("mod_loop",32); 1045e1051a39Sopenharmony_ci &pshufd ($T2,$Xi,0b01001110); # H^2*(Ii+Xi) 1046e1051a39Sopenharmony_ci &movdqa ($Xhi,$Xi); 1047e1051a39Sopenharmony_ci &pxor ($T2,$Xi); # 1048e1051a39Sopenharmony_ci &nop (); 1049e1051a39Sopenharmony_ci 1050e1051a39Sopenharmony_ci &pclmulqdq ($Xi,$Hkey,0x00); ####### 1051e1051a39Sopenharmony_ci &pclmulqdq ($Xhi,$Hkey,0x11); ####### 1052e1051a39Sopenharmony_ci &pclmulqdq ($T2,$T3,0x10); ####### 1053e1051a39Sopenharmony_ci &movups ($Hkey,&QWP(0,$Htbl)); # load H 1054e1051a39Sopenharmony_ci 1055e1051a39Sopenharmony_ci &xorps ($Xi,$Xn); # (H*Ii+1) + H^2*(Ii+Xi) 1056e1051a39Sopenharmony_ci &movdqa ($T3,&QWP(0,$const)); 1057e1051a39Sopenharmony_ci &xorps ($Xhi,$Xhn); 1058e1051a39Sopenharmony_ci &movdqu ($Xhn,&QWP(0,$inp)); # Ii 1059e1051a39Sopenharmony_ci &pxor ($T1,$Xi); # aggregated Karatsuba post-processing 1060e1051a39Sopenharmony_ci &movdqu ($Xn,&QWP(16,$inp)); # Ii+1 1061e1051a39Sopenharmony_ci &pxor ($T1,$Xhi); # 1062e1051a39Sopenharmony_ci 1063e1051a39Sopenharmony_ci &pshufb ($Xhn,$T3); 1064e1051a39Sopenharmony_ci &pxor ($T2,$T1); # 1065e1051a39Sopenharmony_ci 1066e1051a39Sopenharmony_ci &movdqa ($T1,$T2); # 1067e1051a39Sopenharmony_ci &psrldq ($T2,8); 1068e1051a39Sopenharmony_ci &pslldq ($T1,8); # 1069e1051a39Sopenharmony_ci &pxor ($Xhi,$T2); 1070e1051a39Sopenharmony_ci &pxor ($Xi,$T1); # 1071e1051a39Sopenharmony_ci &pshufb ($Xn,$T3); 1072e1051a39Sopenharmony_ci &pxor ($Xhi,$Xhn); # "Ii+Xi", consume early 1073e1051a39Sopenharmony_ci 1074e1051a39Sopenharmony_ci &movdqa ($Xhn,$Xn); #&clmul64x64_TX ($Xhn,$Xn,$Hkey); H*Ii+1 1075e1051a39Sopenharmony_ci &movdqa ($T2,$Xi); #&reduction_alg9($Xhi,$Xi); 1st phase 1076e1051a39Sopenharmony_ci &movdqa ($T1,$Xi); 1077e1051a39Sopenharmony_ci &psllq ($Xi,5); 1078e1051a39Sopenharmony_ci &pxor ($T1,$Xi); # 1079e1051a39Sopenharmony_ci &psllq ($Xi,1); 1080e1051a39Sopenharmony_ci &pxor ($Xi,$T1); # 1081e1051a39Sopenharmony_ci &pclmulqdq ($Xn,$Hkey,0x00); ####### 1082e1051a39Sopenharmony_ci &movups ($T3,&QWP(32,$Htbl)); 1083e1051a39Sopenharmony_ci &psllq ($Xi,57); # 1084e1051a39Sopenharmony_ci &movdqa ($T1,$Xi); # 1085e1051a39Sopenharmony_ci &pslldq ($Xi,8); 1086e1051a39Sopenharmony_ci &psrldq ($T1,8); # 1087e1051a39Sopenharmony_ci &pxor ($Xi,$T2); 1088e1051a39Sopenharmony_ci &pxor ($Xhi,$T1); # 1089e1051a39Sopenharmony_ci &pshufd ($T1,$Xhn,0b01001110); 1090e1051a39Sopenharmony_ci &movdqa ($T2,$Xi); # 2nd phase 1091e1051a39Sopenharmony_ci &psrlq ($Xi,1); 1092e1051a39Sopenharmony_ci &pxor ($T1,$Xhn); 1093e1051a39Sopenharmony_ci &pxor ($Xhi,$T2); # 1094e1051a39Sopenharmony_ci &pclmulqdq ($Xhn,$Hkey,0x11); ####### 1095e1051a39Sopenharmony_ci &movups ($Hkey,&QWP(16,$Htbl)); # load H^2 1096e1051a39Sopenharmony_ci &pxor ($T2,$Xi); 1097e1051a39Sopenharmony_ci &psrlq ($Xi,5); 1098e1051a39Sopenharmony_ci &pxor ($Xi,$T2); # 1099e1051a39Sopenharmony_ci &psrlq ($Xi,1); # 1100e1051a39Sopenharmony_ci &pxor ($Xi,$Xhi) # 1101e1051a39Sopenharmony_ci &pclmulqdq ($T1,$T3,0x00); ####### 1102e1051a39Sopenharmony_ci 1103e1051a39Sopenharmony_ci &lea ($inp,&DWP(32,$inp)); 1104e1051a39Sopenharmony_ci &sub ($len,0x20); 1105e1051a39Sopenharmony_ci &ja (&label("mod_loop")); 1106e1051a39Sopenharmony_ci 1107e1051a39Sopenharmony_ci&set_label("even_tail"); 1108e1051a39Sopenharmony_ci &pshufd ($T2,$Xi,0b01001110); # H^2*(Ii+Xi) 1109e1051a39Sopenharmony_ci &movdqa ($Xhi,$Xi); 1110e1051a39Sopenharmony_ci &pxor ($T2,$Xi); # 1111e1051a39Sopenharmony_ci 1112e1051a39Sopenharmony_ci &pclmulqdq ($Xi,$Hkey,0x00); ####### 1113e1051a39Sopenharmony_ci &pclmulqdq ($Xhi,$Hkey,0x11); ####### 1114e1051a39Sopenharmony_ci &pclmulqdq ($T2,$T3,0x10); ####### 1115e1051a39Sopenharmony_ci &movdqa ($T3,&QWP(0,$const)); 1116e1051a39Sopenharmony_ci 1117e1051a39Sopenharmony_ci &xorps ($Xi,$Xn); # (H*Ii+1) + H^2*(Ii+Xi) 1118e1051a39Sopenharmony_ci &xorps ($Xhi,$Xhn); 1119e1051a39Sopenharmony_ci &pxor ($T1,$Xi); # aggregated Karatsuba post-processing 1120e1051a39Sopenharmony_ci &pxor ($T1,$Xhi); # 1121e1051a39Sopenharmony_ci 1122e1051a39Sopenharmony_ci &pxor ($T2,$T1); # 1123e1051a39Sopenharmony_ci 1124e1051a39Sopenharmony_ci &movdqa ($T1,$T2); # 1125e1051a39Sopenharmony_ci &psrldq ($T2,8); 1126e1051a39Sopenharmony_ci &pslldq ($T1,8); # 1127e1051a39Sopenharmony_ci &pxor ($Xhi,$T2); 1128e1051a39Sopenharmony_ci &pxor ($Xi,$T1); # 1129e1051a39Sopenharmony_ci 1130e1051a39Sopenharmony_ci &reduction_alg9 ($Xhi,$Xi); 1131e1051a39Sopenharmony_ci 1132e1051a39Sopenharmony_ci &test ($len,$len); 1133e1051a39Sopenharmony_ci &jnz (&label("done")); 1134e1051a39Sopenharmony_ci 1135e1051a39Sopenharmony_ci &movups ($Hkey,&QWP(0,$Htbl)); # load H 1136e1051a39Sopenharmony_ci&set_label("odd_tail"); 1137e1051a39Sopenharmony_ci &movdqu ($T1,&QWP(0,$inp)); # Ii 1138e1051a39Sopenharmony_ci &pshufb ($T1,$T3); 1139e1051a39Sopenharmony_ci &pxor ($Xi,$T1); # Ii+Xi 1140e1051a39Sopenharmony_ci 1141e1051a39Sopenharmony_ci &clmul64x64_T2 ($Xhi,$Xi,$Hkey); # H*(Ii+Xi) 1142e1051a39Sopenharmony_ci &reduction_alg9 ($Xhi,$Xi); 1143e1051a39Sopenharmony_ci 1144e1051a39Sopenharmony_ci&set_label("done"); 1145e1051a39Sopenharmony_ci &pshufb ($Xi,$T3); 1146e1051a39Sopenharmony_ci &movdqu (&QWP(0,$Xip),$Xi); 1147e1051a39Sopenharmony_ci&function_end("gcm_ghash_clmul"); 1148e1051a39Sopenharmony_ci 1149e1051a39Sopenharmony_ci} else { # Algorithm 5. Kept for reference purposes. 1150e1051a39Sopenharmony_ci 1151e1051a39Sopenharmony_cisub reduction_alg5 { # 19/16 times faster than Intel version 1152e1051a39Sopenharmony_cimy ($Xhi,$Xi)=@_; 1153e1051a39Sopenharmony_ci 1154e1051a39Sopenharmony_ci # <<1 1155e1051a39Sopenharmony_ci &movdqa ($T1,$Xi); # 1156e1051a39Sopenharmony_ci &movdqa ($T2,$Xhi); 1157e1051a39Sopenharmony_ci &pslld ($Xi,1); 1158e1051a39Sopenharmony_ci &pslld ($Xhi,1); # 1159e1051a39Sopenharmony_ci &psrld ($T1,31); 1160e1051a39Sopenharmony_ci &psrld ($T2,31); # 1161e1051a39Sopenharmony_ci &movdqa ($T3,$T1); 1162e1051a39Sopenharmony_ci &pslldq ($T1,4); 1163e1051a39Sopenharmony_ci &psrldq ($T3,12); # 1164e1051a39Sopenharmony_ci &pslldq ($T2,4); 1165e1051a39Sopenharmony_ci &por ($Xhi,$T3); # 1166e1051a39Sopenharmony_ci &por ($Xi,$T1); 1167e1051a39Sopenharmony_ci &por ($Xhi,$T2); # 1168e1051a39Sopenharmony_ci 1169e1051a39Sopenharmony_ci # 1st phase 1170e1051a39Sopenharmony_ci &movdqa ($T1,$Xi); 1171e1051a39Sopenharmony_ci &movdqa ($T2,$Xi); 1172e1051a39Sopenharmony_ci &movdqa ($T3,$Xi); # 1173e1051a39Sopenharmony_ci &pslld ($T1,31); 1174e1051a39Sopenharmony_ci &pslld ($T2,30); 1175e1051a39Sopenharmony_ci &pslld ($Xi,25); # 1176e1051a39Sopenharmony_ci &pxor ($T1,$T2); 1177e1051a39Sopenharmony_ci &pxor ($T1,$Xi); # 1178e1051a39Sopenharmony_ci &movdqa ($T2,$T1); # 1179e1051a39Sopenharmony_ci &pslldq ($T1,12); 1180e1051a39Sopenharmony_ci &psrldq ($T2,4); # 1181e1051a39Sopenharmony_ci &pxor ($T3,$T1); 1182e1051a39Sopenharmony_ci 1183e1051a39Sopenharmony_ci # 2nd phase 1184e1051a39Sopenharmony_ci &pxor ($Xhi,$T3); # 1185e1051a39Sopenharmony_ci &movdqa ($Xi,$T3); 1186e1051a39Sopenharmony_ci &movdqa ($T1,$T3); 1187e1051a39Sopenharmony_ci &psrld ($Xi,1); # 1188e1051a39Sopenharmony_ci &psrld ($T1,2); 1189e1051a39Sopenharmony_ci &psrld ($T3,7); # 1190e1051a39Sopenharmony_ci &pxor ($Xi,$T1); 1191e1051a39Sopenharmony_ci &pxor ($Xhi,$T2); 1192e1051a39Sopenharmony_ci &pxor ($Xi,$T3); # 1193e1051a39Sopenharmony_ci &pxor ($Xi,$Xhi); # 1194e1051a39Sopenharmony_ci} 1195e1051a39Sopenharmony_ci 1196e1051a39Sopenharmony_ci&function_begin_B("gcm_init_clmul"); 1197e1051a39Sopenharmony_ci &mov ($Htbl,&wparam(0)); 1198e1051a39Sopenharmony_ci &mov ($Xip,&wparam(1)); 1199e1051a39Sopenharmony_ci 1200e1051a39Sopenharmony_ci &call (&label("pic")); 1201e1051a39Sopenharmony_ci&set_label("pic"); 1202e1051a39Sopenharmony_ci &blindpop ($const); 1203e1051a39Sopenharmony_ci &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const)); 1204e1051a39Sopenharmony_ci 1205e1051a39Sopenharmony_ci &movdqu ($Hkey,&QWP(0,$Xip)); 1206e1051a39Sopenharmony_ci &pshufd ($Hkey,$Hkey,0b01001110);# dword swap 1207e1051a39Sopenharmony_ci 1208e1051a39Sopenharmony_ci # calculate H^2 1209e1051a39Sopenharmony_ci &movdqa ($Xi,$Hkey); 1210e1051a39Sopenharmony_ci &clmul64x64_T3 ($Xhi,$Xi,$Hkey); 1211e1051a39Sopenharmony_ci &reduction_alg5 ($Xhi,$Xi); 1212e1051a39Sopenharmony_ci 1213e1051a39Sopenharmony_ci &movdqu (&QWP(0,$Htbl),$Hkey); # save H 1214e1051a39Sopenharmony_ci &movdqu (&QWP(16,$Htbl),$Xi); # save H^2 1215e1051a39Sopenharmony_ci 1216e1051a39Sopenharmony_ci &ret (); 1217e1051a39Sopenharmony_ci&function_end_B("gcm_init_clmul"); 1218e1051a39Sopenharmony_ci 1219e1051a39Sopenharmony_ci&function_begin_B("gcm_gmult_clmul"); 1220e1051a39Sopenharmony_ci &mov ($Xip,&wparam(0)); 1221e1051a39Sopenharmony_ci &mov ($Htbl,&wparam(1)); 1222e1051a39Sopenharmony_ci 1223e1051a39Sopenharmony_ci &call (&label("pic")); 1224e1051a39Sopenharmony_ci&set_label("pic"); 1225e1051a39Sopenharmony_ci &blindpop ($const); 1226e1051a39Sopenharmony_ci &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const)); 1227e1051a39Sopenharmony_ci 1228e1051a39Sopenharmony_ci &movdqu ($Xi,&QWP(0,$Xip)); 1229e1051a39Sopenharmony_ci &movdqa ($Xn,&QWP(0,$const)); 1230e1051a39Sopenharmony_ci &movdqu ($Hkey,&QWP(0,$Htbl)); 1231e1051a39Sopenharmony_ci &pshufb ($Xi,$Xn); 1232e1051a39Sopenharmony_ci 1233e1051a39Sopenharmony_ci &clmul64x64_T3 ($Xhi,$Xi,$Hkey); 1234e1051a39Sopenharmony_ci &reduction_alg5 ($Xhi,$Xi); 1235e1051a39Sopenharmony_ci 1236e1051a39Sopenharmony_ci &pshufb ($Xi,$Xn); 1237e1051a39Sopenharmony_ci &movdqu (&QWP(0,$Xip),$Xi); 1238e1051a39Sopenharmony_ci 1239e1051a39Sopenharmony_ci &ret (); 1240e1051a39Sopenharmony_ci&function_end_B("gcm_gmult_clmul"); 1241e1051a39Sopenharmony_ci 1242e1051a39Sopenharmony_ci&function_begin("gcm_ghash_clmul"); 1243e1051a39Sopenharmony_ci &mov ($Xip,&wparam(0)); 1244e1051a39Sopenharmony_ci &mov ($Htbl,&wparam(1)); 1245e1051a39Sopenharmony_ci &mov ($inp,&wparam(2)); 1246e1051a39Sopenharmony_ci &mov ($len,&wparam(3)); 1247e1051a39Sopenharmony_ci 1248e1051a39Sopenharmony_ci &call (&label("pic")); 1249e1051a39Sopenharmony_ci&set_label("pic"); 1250e1051a39Sopenharmony_ci &blindpop ($const); 1251e1051a39Sopenharmony_ci &lea ($const,&DWP(&label("bswap")."-".&label("pic"),$const)); 1252e1051a39Sopenharmony_ci 1253e1051a39Sopenharmony_ci &movdqu ($Xi,&QWP(0,$Xip)); 1254e1051a39Sopenharmony_ci &movdqa ($T3,&QWP(0,$const)); 1255e1051a39Sopenharmony_ci &movdqu ($Hkey,&QWP(0,$Htbl)); 1256e1051a39Sopenharmony_ci &pshufb ($Xi,$T3); 1257e1051a39Sopenharmony_ci 1258e1051a39Sopenharmony_ci &sub ($len,0x10); 1259e1051a39Sopenharmony_ci &jz (&label("odd_tail")); 1260e1051a39Sopenharmony_ci 1261e1051a39Sopenharmony_ci ####### 1262e1051a39Sopenharmony_ci # Xi+2 =[H*(Ii+1 + Xi+1)] mod P = 1263e1051a39Sopenharmony_ci # [(H*Ii+1) + (H*Xi+1)] mod P = 1264e1051a39Sopenharmony_ci # [(H*Ii+1) + H^2*(Ii+Xi)] mod P 1265e1051a39Sopenharmony_ci # 1266e1051a39Sopenharmony_ci &movdqu ($T1,&QWP(0,$inp)); # Ii 1267e1051a39Sopenharmony_ci &movdqu ($Xn,&QWP(16,$inp)); # Ii+1 1268e1051a39Sopenharmony_ci &pshufb ($T1,$T3); 1269e1051a39Sopenharmony_ci &pshufb ($Xn,$T3); 1270e1051a39Sopenharmony_ci &pxor ($Xi,$T1); # Ii+Xi 1271e1051a39Sopenharmony_ci 1272e1051a39Sopenharmony_ci &clmul64x64_T3 ($Xhn,$Xn,$Hkey); # H*Ii+1 1273e1051a39Sopenharmony_ci &movdqu ($Hkey,&QWP(16,$Htbl)); # load H^2 1274e1051a39Sopenharmony_ci 1275e1051a39Sopenharmony_ci &sub ($len,0x20); 1276e1051a39Sopenharmony_ci &lea ($inp,&DWP(32,$inp)); # i+=2 1277e1051a39Sopenharmony_ci &jbe (&label("even_tail")); 1278e1051a39Sopenharmony_ci 1279e1051a39Sopenharmony_ci&set_label("mod_loop"); 1280e1051a39Sopenharmony_ci &clmul64x64_T3 ($Xhi,$Xi,$Hkey); # H^2*(Ii+Xi) 1281e1051a39Sopenharmony_ci &movdqu ($Hkey,&QWP(0,$Htbl)); # load H 1282e1051a39Sopenharmony_ci 1283e1051a39Sopenharmony_ci &pxor ($Xi,$Xn); # (H*Ii+1) + H^2*(Ii+Xi) 1284e1051a39Sopenharmony_ci &pxor ($Xhi,$Xhn); 1285e1051a39Sopenharmony_ci 1286e1051a39Sopenharmony_ci &reduction_alg5 ($Xhi,$Xi); 1287e1051a39Sopenharmony_ci 1288e1051a39Sopenharmony_ci ####### 1289e1051a39Sopenharmony_ci &movdqa ($T3,&QWP(0,$const)); 1290e1051a39Sopenharmony_ci &movdqu ($T1,&QWP(0,$inp)); # Ii 1291e1051a39Sopenharmony_ci &movdqu ($Xn,&QWP(16,$inp)); # Ii+1 1292e1051a39Sopenharmony_ci &pshufb ($T1,$T3); 1293e1051a39Sopenharmony_ci &pshufb ($Xn,$T3); 1294e1051a39Sopenharmony_ci &pxor ($Xi,$T1); # Ii+Xi 1295e1051a39Sopenharmony_ci 1296e1051a39Sopenharmony_ci &clmul64x64_T3 ($Xhn,$Xn,$Hkey); # H*Ii+1 1297e1051a39Sopenharmony_ci &movdqu ($Hkey,&QWP(16,$Htbl)); # load H^2 1298e1051a39Sopenharmony_ci 1299e1051a39Sopenharmony_ci &sub ($len,0x20); 1300e1051a39Sopenharmony_ci &lea ($inp,&DWP(32,$inp)); 1301e1051a39Sopenharmony_ci &ja (&label("mod_loop")); 1302e1051a39Sopenharmony_ci 1303e1051a39Sopenharmony_ci&set_label("even_tail"); 1304e1051a39Sopenharmony_ci &clmul64x64_T3 ($Xhi,$Xi,$Hkey); # H^2*(Ii+Xi) 1305e1051a39Sopenharmony_ci 1306e1051a39Sopenharmony_ci &pxor ($Xi,$Xn); # (H*Ii+1) + H^2*(Ii+Xi) 1307e1051a39Sopenharmony_ci &pxor ($Xhi,$Xhn); 1308e1051a39Sopenharmony_ci 1309e1051a39Sopenharmony_ci &reduction_alg5 ($Xhi,$Xi); 1310e1051a39Sopenharmony_ci 1311e1051a39Sopenharmony_ci &movdqa ($T3,&QWP(0,$const)); 1312e1051a39Sopenharmony_ci &test ($len,$len); 1313e1051a39Sopenharmony_ci &jnz (&label("done")); 1314e1051a39Sopenharmony_ci 1315e1051a39Sopenharmony_ci &movdqu ($Hkey,&QWP(0,$Htbl)); # load H 1316e1051a39Sopenharmony_ci&set_label("odd_tail"); 1317e1051a39Sopenharmony_ci &movdqu ($T1,&QWP(0,$inp)); # Ii 1318e1051a39Sopenharmony_ci &pshufb ($T1,$T3); 1319e1051a39Sopenharmony_ci &pxor ($Xi,$T1); # Ii+Xi 1320e1051a39Sopenharmony_ci 1321e1051a39Sopenharmony_ci &clmul64x64_T3 ($Xhi,$Xi,$Hkey); # H*(Ii+Xi) 1322e1051a39Sopenharmony_ci &reduction_alg5 ($Xhi,$Xi); 1323e1051a39Sopenharmony_ci 1324e1051a39Sopenharmony_ci &movdqa ($T3,&QWP(0,$const)); 1325e1051a39Sopenharmony_ci&set_label("done"); 1326e1051a39Sopenharmony_ci &pshufb ($Xi,$T3); 1327e1051a39Sopenharmony_ci &movdqu (&QWP(0,$Xip),$Xi); 1328e1051a39Sopenharmony_ci&function_end("gcm_ghash_clmul"); 1329e1051a39Sopenharmony_ci 1330e1051a39Sopenharmony_ci} 1331e1051a39Sopenharmony_ci 1332e1051a39Sopenharmony_ci&set_label("bswap",64); 1333e1051a39Sopenharmony_ci &data_byte(15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0); 1334e1051a39Sopenharmony_ci &data_byte(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0xc2); # 0x1c2_polynomial 1335e1051a39Sopenharmony_ci&set_label("rem_8bit",64); 1336e1051a39Sopenharmony_ci &data_short(0x0000,0x01C2,0x0384,0x0246,0x0708,0x06CA,0x048C,0x054E); 1337e1051a39Sopenharmony_ci &data_short(0x0E10,0x0FD2,0x0D94,0x0C56,0x0918,0x08DA,0x0A9C,0x0B5E); 1338e1051a39Sopenharmony_ci &data_short(0x1C20,0x1DE2,0x1FA4,0x1E66,0x1B28,0x1AEA,0x18AC,0x196E); 1339e1051a39Sopenharmony_ci &data_short(0x1230,0x13F2,0x11B4,0x1076,0x1538,0x14FA,0x16BC,0x177E); 1340e1051a39Sopenharmony_ci &data_short(0x3840,0x3982,0x3BC4,0x3A06,0x3F48,0x3E8A,0x3CCC,0x3D0E); 1341e1051a39Sopenharmony_ci &data_short(0x3650,0x3792,0x35D4,0x3416,0x3158,0x309A,0x32DC,0x331E); 1342e1051a39Sopenharmony_ci &data_short(0x2460,0x25A2,0x27E4,0x2626,0x2368,0x22AA,0x20EC,0x212E); 1343e1051a39Sopenharmony_ci &data_short(0x2A70,0x2BB2,0x29F4,0x2836,0x2D78,0x2CBA,0x2EFC,0x2F3E); 1344e1051a39Sopenharmony_ci &data_short(0x7080,0x7142,0x7304,0x72C6,0x7788,0x764A,0x740C,0x75CE); 1345e1051a39Sopenharmony_ci &data_short(0x7E90,0x7F52,0x7D14,0x7CD6,0x7998,0x785A,0x7A1C,0x7BDE); 1346e1051a39Sopenharmony_ci &data_short(0x6CA0,0x6D62,0x6F24,0x6EE6,0x6BA8,0x6A6A,0x682C,0x69EE); 1347e1051a39Sopenharmony_ci &data_short(0x62B0,0x6372,0x6134,0x60F6,0x65B8,0x647A,0x663C,0x67FE); 1348e1051a39Sopenharmony_ci &data_short(0x48C0,0x4902,0x4B44,0x4A86,0x4FC8,0x4E0A,0x4C4C,0x4D8E); 1349e1051a39Sopenharmony_ci &data_short(0x46D0,0x4712,0x4554,0x4496,0x41D8,0x401A,0x425C,0x439E); 1350e1051a39Sopenharmony_ci &data_short(0x54E0,0x5522,0x5764,0x56A6,0x53E8,0x522A,0x506C,0x51AE); 1351e1051a39Sopenharmony_ci &data_short(0x5AF0,0x5B32,0x5974,0x58B6,0x5DF8,0x5C3A,0x5E7C,0x5FBE); 1352e1051a39Sopenharmony_ci &data_short(0xE100,0xE0C2,0xE284,0xE346,0xE608,0xE7CA,0xE58C,0xE44E); 1353e1051a39Sopenharmony_ci &data_short(0xEF10,0xEED2,0xEC94,0xED56,0xE818,0xE9DA,0xEB9C,0xEA5E); 1354e1051a39Sopenharmony_ci &data_short(0xFD20,0xFCE2,0xFEA4,0xFF66,0xFA28,0xFBEA,0xF9AC,0xF86E); 1355e1051a39Sopenharmony_ci &data_short(0xF330,0xF2F2,0xF0B4,0xF176,0xF438,0xF5FA,0xF7BC,0xF67E); 1356e1051a39Sopenharmony_ci &data_short(0xD940,0xD882,0xDAC4,0xDB06,0xDE48,0xDF8A,0xDDCC,0xDC0E); 1357e1051a39Sopenharmony_ci &data_short(0xD750,0xD692,0xD4D4,0xD516,0xD058,0xD19A,0xD3DC,0xD21E); 1358e1051a39Sopenharmony_ci &data_short(0xC560,0xC4A2,0xC6E4,0xC726,0xC268,0xC3AA,0xC1EC,0xC02E); 1359e1051a39Sopenharmony_ci &data_short(0xCB70,0xCAB2,0xC8F4,0xC936,0xCC78,0xCDBA,0xCFFC,0xCE3E); 1360e1051a39Sopenharmony_ci &data_short(0x9180,0x9042,0x9204,0x93C6,0x9688,0x974A,0x950C,0x94CE); 1361e1051a39Sopenharmony_ci &data_short(0x9F90,0x9E52,0x9C14,0x9DD6,0x9898,0x995A,0x9B1C,0x9ADE); 1362e1051a39Sopenharmony_ci &data_short(0x8DA0,0x8C62,0x8E24,0x8FE6,0x8AA8,0x8B6A,0x892C,0x88EE); 1363e1051a39Sopenharmony_ci &data_short(0x83B0,0x8272,0x8034,0x81F6,0x84B8,0x857A,0x873C,0x86FE); 1364e1051a39Sopenharmony_ci &data_short(0xA9C0,0xA802,0xAA44,0xAB86,0xAEC8,0xAF0A,0xAD4C,0xAC8E); 1365e1051a39Sopenharmony_ci &data_short(0xA7D0,0xA612,0xA454,0xA596,0xA0D8,0xA11A,0xA35C,0xA29E); 1366e1051a39Sopenharmony_ci &data_short(0xB5E0,0xB422,0xB664,0xB7A6,0xB2E8,0xB32A,0xB16C,0xB0AE); 1367e1051a39Sopenharmony_ci &data_short(0xBBF0,0xBA32,0xB874,0xB9B6,0xBCF8,0xBD3A,0xBF7C,0xBEBE); 1368e1051a39Sopenharmony_ci}} # $sse2 1369e1051a39Sopenharmony_ci 1370e1051a39Sopenharmony_ci&set_label("rem_4bit",64); 1371e1051a39Sopenharmony_ci &data_word(0,0x0000<<$S,0,0x1C20<<$S,0,0x3840<<$S,0,0x2460<<$S); 1372e1051a39Sopenharmony_ci &data_word(0,0x7080<<$S,0,0x6CA0<<$S,0,0x48C0<<$S,0,0x54E0<<$S); 1373e1051a39Sopenharmony_ci &data_word(0,0xE100<<$S,0,0xFD20<<$S,0,0xD940<<$S,0,0xC560<<$S); 1374e1051a39Sopenharmony_ci &data_word(0,0x9180<<$S,0,0x8DA0<<$S,0,0xA9C0<<$S,0,0xB5E0<<$S); 1375e1051a39Sopenharmony_ci}}} # !$x86only 1376e1051a39Sopenharmony_ci 1377e1051a39Sopenharmony_ci&asciz("GHASH for x86, CRYPTOGAMS by <appro\@openssl.org>"); 1378e1051a39Sopenharmony_ci&asm_finish(); 1379e1051a39Sopenharmony_ci 1380e1051a39Sopenharmony_ciclose STDOUT or die "error closing STDOUT: $!"; 1381e1051a39Sopenharmony_ci 1382e1051a39Sopenharmony_ci# A question was risen about choice of vanilla MMX. Or rather why wasn't 1383e1051a39Sopenharmony_ci# SSE2 chosen instead? In addition to the fact that MMX runs on legacy 1384e1051a39Sopenharmony_ci# CPUs such as PIII, "4-bit" MMX version was observed to provide better 1385e1051a39Sopenharmony_ci# performance than *corresponding* SSE2 one even on contemporary CPUs. 1386e1051a39Sopenharmony_ci# SSE2 results were provided by Peter-Michael Hager. He maintains SSE2 1387e1051a39Sopenharmony_ci# implementation featuring full range of lookup-table sizes, but with 1388e1051a39Sopenharmony_ci# per-invocation lookup table setup. Latter means that table size is 1389e1051a39Sopenharmony_ci# chosen depending on how much data is to be hashed in every given call, 1390e1051a39Sopenharmony_ci# more data - larger table. Best reported result for Core2 is ~4 cycles 1391e1051a39Sopenharmony_ci# per processed byte out of 64KB block. This number accounts even for 1392e1051a39Sopenharmony_ci# 64KB table setup overhead. As discussed in gcm128.c we choose to be 1393e1051a39Sopenharmony_ci# more conservative in respect to lookup table sizes, but how do the 1394e1051a39Sopenharmony_ci# results compare? Minimalistic "256B" MMX version delivers ~11 cycles 1395e1051a39Sopenharmony_ci# on same platform. As also discussed in gcm128.c, next in line "8-bit 1396e1051a39Sopenharmony_ci# Shoup's" or "4KB" method should deliver twice the performance of 1397e1051a39Sopenharmony_ci# "256B" one, in other words not worse than ~6 cycles per byte. It 1398e1051a39Sopenharmony_ci# should be also be noted that in SSE2 case improvement can be "super- 1399e1051a39Sopenharmony_ci# linear," i.e. more than twice, mostly because >>8 maps to single 1400e1051a39Sopenharmony_ci# instruction on SSE2 register. This is unlike "4-bit" case when >>4 1401e1051a39Sopenharmony_ci# maps to same amount of instructions in both MMX and SSE2 cases. 1402e1051a39Sopenharmony_ci# Bottom line is that switch to SSE2 is considered to be justifiable 1403e1051a39Sopenharmony_ci# only in case we choose to implement "8-bit" method... 1404