Do not silently truncate files on perlasm errors
[openssl.git] / crypto / aes / asm / bsaes-armv7.pl
index 9b9ba339cbb857b969546ac8d6d4b964bdb4c992..601b6b394204d283342020e475cfadafd9d70a14 100644 (file)
@@ -1,7 +1,7 @@
 #! /usr/bin/env perl
-# Copyright 2012-2016 The OpenSSL Project Authors. All Rights Reserved.
+# Copyright 2012-2018 The OpenSSL Project Authors. All Rights Reserved.
 #
-# Licensed under the OpenSSL license (the "License").  You may not use
+# Licensed under the Apache License 2.0 (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
@@ -14,8 +14,7 @@
 # details see http://www.openssl.org/~appro/cryptogams/.
 #
 # Specific modes and adaptation for Linux kernel by Ard Biesheuvel
-# <ard.biesheuvel@linaro.org>. Permission to use under GPL terms is
-# granted.
+# of Linaro. Permission to use under GPL terms is granted.
 # ====================================================================
 
 # Bit-sliced AES for ARM NEON
 #                                              <appro@openssl.org>
 
 # April-August 2013
-#
-# Add CBC, CTR and XTS subroutines, adapt for kernel use.
-#
-#                                      <ard.biesheuvel@linaro.org>
+# Add CBC, CTR and XTS subroutines and adapt for kernel use; courtesy of Ard.
 
-$flavour = shift;
-if ($flavour=~/\w[\w\-]*\.\w+$/) { $output=$flavour; undef $flavour; }
-else { while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {} }
+# $output is the last argument if it looks like a file (it has an extension)
+# $flavour is the first argument if it doesn't look like a file
+$output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
+$flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
 
 if ($flavour && $flavour ne "void") {
     $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
@@ -64,9 +61,10 @@ if ($flavour && $flavour ne "void") {
     ( $xlate="${dir}../../perlasm/arm-xlate.pl" and -f $xlate) or
     die "can't locate arm-xlate.pl";
 
-    open STDOUT,"| \"$^X\" $xlate $flavour $output";
+    open STDOUT,"| \"$^X\" $xlate $flavour \"$output\""
+        or die "can't call $xlate: $!";
 } else {
-    open STDOUT,">$output";
+    $output and open STDOUT,">$output";
 }
 
 my ($inp,$out,$len,$key)=("r0","r1","r2","r3");
@@ -91,7 +89,7 @@ my @s=@_[12..15];
 
 sub InBasisChange {
 # input in  lsb > [b0, b1, b2, b3, b4, b5, b6, b7] < msb
-# output in lsb > [b6, b5, b0, b3, b7, b1, b4, b2] < msb 
+# output in lsb > [b6, b5, b0, b3, b7, b1, b4, b2] < msb
 my @b=@_[0..7];
 $code.=<<___;
        veor    @b[2], @b[2], @b[1]
@@ -732,7 +730,6 @@ $code.=<<___;
 .arch  armv7-a
 .fpu   neon
 
-.text
 .syntax        unified         @ ARMv7-capable assembler is expected to handle this
 #if defined(__thumb2__) && !defined(__APPLE__)
 .thumb
@@ -741,12 +738,14 @@ $code.=<<___;
 # undef __thumb2__
 #endif
 
+.text
+
 .type  _bsaes_decrypt8,%function
 .align 4
 _bsaes_decrypt8:
-       adr     $const,_bsaes_decrypt8
+       adr     $const,.
        vldmia  $key!, {@XMM[9]}                @ round 0 key
-#ifdef __APPLE__
+#if defined(__thumb2__) || defined(__APPLE__)
        adr     $const,.LM0ISR
 #else
        add     $const,$const,#.LM0ISR-_bsaes_decrypt8
@@ -843,9 +842,9 @@ _bsaes_const:
 .type  _bsaes_encrypt8,%function
 .align 4
 _bsaes_encrypt8:
-       adr     $const,_bsaes_encrypt8
+       adr     $const,.
        vldmia  $key!, {@XMM[9]}                @ round 0 key
-#ifdef __APPLE__
+#if defined(__thumb2__) || defined(__APPLE__)
        adr     $const,.LM0SR
 #else
        sub     $const,$const,#_bsaes_encrypt8-.LM0SR
@@ -951,9 +950,9 @@ $code.=<<___;
 .type  _bsaes_key_convert,%function
 .align 4
 _bsaes_key_convert:
-       adr     $const,_bsaes_key_convert
+       adr     $const,.
        vld1.8  {@XMM[7]},  [$inp]!             @ load round 0 key
-#ifdef __APPLE__
+#if defined(__thumb2__) || defined(__APPLE__)
        adr     $const,.LM0
 #else
        sub     $const,$const,#_bsaes_key_convert-.LM0
@@ -1129,9 +1128,9 @@ bsaes_cbc_encrypt:
 #ifndef        __thumb__
        blo     AES_cbc_encrypt
 #else
-       bhs     1f
+       bhs     .Lcbc_do_bsaes
        b       AES_cbc_encrypt
-1:
+.Lcbc_do_bsaes:
 #endif
 #endif
 
@@ -1365,7 +1364,7 @@ bsaes_cbc_encrypt:
        vmov    @XMM[4],@XMM[15]                @ just in case ensure that IV
        vmov    @XMM[5],@XMM[0]                 @ and input are preserved
        bl      AES_decrypt
-       vld1.8  {@XMM[0]}, [$fp,:64]            @ load result
+       vld1.8  {@XMM[0]}, [$fp]                @ load result
        veor    @XMM[0], @XMM[0], @XMM[4]       @ ^= IV
        vmov    @XMM[15], @XMM[5]               @ @XMM[5] holds input
        vst1.8  {@XMM[0]}, [$rounds]            @ write output
@@ -1839,8 +1838,6 @@ $code.=<<___;
        b               .Lxts_enc_done
 .align 4
 .Lxts_enc_6:
-       vst1.64         {@XMM[14]}, [r0,:128]           @ next round tweak
-
        veor            @XMM[4], @XMM[4], @XMM[12]
 #ifndef        BSAES_ASM_EXTENDED_KEY
        add             r4, sp, #0x90                   @ pass key schedule
@@ -1876,8 +1873,6 @@ $code.=<<___;
 
 .align 5
 .Lxts_enc_5:
-       vst1.64         {@XMM[13]}, [r0,:128]           @ next round tweak
-
        veor            @XMM[3], @XMM[3], @XMM[11]
 #ifndef        BSAES_ASM_EXTENDED_KEY
        add             r4, sp, #0x90                   @ pass key schedule
@@ -1906,8 +1901,6 @@ $code.=<<___;
        b               .Lxts_enc_done
 .align 4
 .Lxts_enc_4:
-       vst1.64         {@XMM[12]}, [r0,:128]           @ next round tweak
-
        veor            @XMM[2], @XMM[2], @XMM[10]
 #ifndef        BSAES_ASM_EXTENDED_KEY
        add             r4, sp, #0x90                   @ pass key schedule
@@ -1933,8 +1926,6 @@ $code.=<<___;
        b               .Lxts_enc_done
 .align 4
 .Lxts_enc_3:
-       vst1.64         {@XMM[11]}, [r0,:128]           @ next round tweak
-
        veor            @XMM[1], @XMM[1], @XMM[9]
 #ifndef        BSAES_ASM_EXTENDED_KEY
        add             r4, sp, #0x90                   @ pass key schedule
@@ -1959,8 +1950,6 @@ $code.=<<___;
        b               .Lxts_enc_done
 .align 4
 .Lxts_enc_2:
-       vst1.64         {@XMM[10]}, [r0,:128]           @ next round tweak
-
        veor            @XMM[0], @XMM[0], @XMM[8]
 #ifndef        BSAES_ASM_EXTENDED_KEY
        add             r4, sp, #0x90                   @ pass key schedule
@@ -1983,7 +1972,7 @@ $code.=<<___;
 .align 4
 .Lxts_enc_1:
        mov             r0, sp
-       veor            @XMM[0], @XMM[8]
+       veor            @XMM[0], @XMM[0], @XMM[8]
        mov             r1, sp
        vst1.8          {@XMM[0]}, [sp,:128]
        mov             r2, $key
@@ -2295,8 +2284,6 @@ $code.=<<___;
        b               .Lxts_dec_done
 .align 4
 .Lxts_dec_5:
-       vst1.64         {@XMM[13]}, [r0,:128]           @ next round tweak
-
        veor            @XMM[3], @XMM[3], @XMM[11]
 #ifndef        BSAES_ASM_EXTENDED_KEY
        add             r4, sp, #0x90                   @ pass key schedule
@@ -2325,8 +2312,6 @@ $code.=<<___;
        b               .Lxts_dec_done
 .align 4
 .Lxts_dec_4:
-       vst1.64         {@XMM[12]}, [r0,:128]           @ next round tweak
-
        veor            @XMM[2], @XMM[2], @XMM[10]
 #ifndef        BSAES_ASM_EXTENDED_KEY
        add             r4, sp, #0x90                   @ pass key schedule
@@ -2352,8 +2337,6 @@ $code.=<<___;
        b               .Lxts_dec_done
 .align 4
 .Lxts_dec_3:
-       vst1.64         {@XMM[11]}, [r0,:128]           @ next round tweak
-
        veor            @XMM[1], @XMM[1], @XMM[9]
 #ifndef        BSAES_ASM_EXTENDED_KEY
        add             r4, sp, #0x90                   @ pass key schedule
@@ -2378,8 +2361,6 @@ $code.=<<___;
        b               .Lxts_dec_done
 .align 4
 .Lxts_dec_2:
-       vst1.64         {@XMM[10]}, [r0,:128]           @ next round tweak
-
        veor            @XMM[0], @XMM[0], @XMM[8]
 #ifndef        BSAES_ASM_EXTENDED_KEY
        add             r4, sp, #0x90                   @ pass key schedule
@@ -2402,7 +2383,7 @@ $code.=<<___;
 .align 4
 .Lxts_dec_1:
        mov             r0, sp
-       veor            @XMM[0], @XMM[8]
+       veor            @XMM[0], @XMM[0], @XMM[8]
        mov             r1, sp
        vst1.8          {@XMM[0]}, [sp,:128]
        mov             r5, $magic                      @ preserve magic
@@ -2510,4 +2491,4 @@ close SELF;
 
 print $code;
 
-close STDOUT;
+close STDOUT or die "error closing STDOUT";